1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ring buffer based function tracer 4 * 5 * Copyright (C) 2007-2012 Steven Rostedt <[email protected]> 6 * Copyright (C) 2008 Ingo Molnar <[email protected]> 7 * 8 * Originally taken from the RT patch by: 9 * Arnaldo Carvalho de Melo <[email protected]> 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 min((size_t)trace_seq_used(&iter->seq), 6825 PAGE_SIZE)); 6826 if (ret < 0) { 6827 __free_page(spd.pages[i]); 6828 break; 6829 } 6830 spd.partial[i].offset = 0; 6831 spd.partial[i].len = ret; 6832 6833 trace_seq_init(&iter->seq); 6834 } 6835 6836 trace_access_unlock(iter->cpu_file); 6837 trace_event_read_unlock(); 6838 mutex_unlock(&iter->mutex); 6839 6840 spd.nr_pages = i; 6841 6842 if (i) 6843 ret = splice_to_pipe(pipe, &spd); 6844 else 6845 ret = 0; 6846 out: 6847 splice_shrink_spd(&spd); 6848 return ret; 6849 6850 out_err: 6851 mutex_unlock(&iter->mutex); 6852 goto out; 6853 } 6854 6855 static ssize_t 6856 tracing_entries_read(struct file *filp, char __user *ubuf, 6857 size_t cnt, loff_t *ppos) 6858 { 6859 struct inode *inode = file_inode(filp); 6860 struct trace_array *tr = inode->i_private; 6861 int cpu = tracing_get_cpu(inode); 6862 char buf[64]; 6863 int r = 0; 6864 ssize_t ret; 6865 6866 mutex_lock(&trace_types_lock); 6867 6868 if (cpu == RING_BUFFER_ALL_CPUS) { 6869 int cpu, buf_size_same; 6870 unsigned long size; 6871 6872 size = 0; 6873 buf_size_same = 1; 6874 /* check if all cpu sizes are same */ 6875 for_each_tracing_cpu(cpu) { 6876 /* fill in the size from first enabled cpu */ 6877 if (size == 0) 6878 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries; 6879 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) { 6880 buf_size_same = 0; 6881 break; 6882 } 6883 } 6884 6885 if (buf_size_same) { 6886 if (!tr->ring_buffer_expanded) 6887 r = sprintf(buf, "%lu (expanded: %lu)\n", 6888 size >> 10, 6889 trace_buf_size >> 10); 6890 else 6891 r = sprintf(buf, "%lu\n", size >> 10); 6892 } else 6893 r = sprintf(buf, "X\n"); 6894 } else 6895 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10); 6896 6897 mutex_unlock(&trace_types_lock); 6898 6899 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6900 return ret; 6901 } 6902 6903 static ssize_t 6904 tracing_entries_write(struct file *filp, const char __user *ubuf, 6905 size_t cnt, loff_t *ppos) 6906 { 6907 struct inode *inode = file_inode(filp); 6908 struct trace_array *tr = inode->i_private; 6909 unsigned long val; 6910 int ret; 6911 6912 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 6913 if (ret) 6914 return ret; 6915 6916 /* must have at least 1 entry */ 6917 if (!val) 6918 return -EINVAL; 6919 6920 /* value is in KB */ 6921 val <<= 10; 6922 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode)); 6923 if (ret < 0) 6924 return ret; 6925 6926 *ppos += cnt; 6927 6928 return cnt; 6929 } 6930 6931 static ssize_t 6932 tracing_total_entries_read(struct file *filp, char __user *ubuf, 6933 size_t cnt, loff_t *ppos) 6934 { 6935 struct trace_array *tr = filp->private_data; 6936 char buf[64]; 6937 int r, cpu; 6938 unsigned long size = 0, expanded_size = 0; 6939 6940 mutex_lock(&trace_types_lock); 6941 for_each_tracing_cpu(cpu) { 6942 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10; 6943 if (!tr->ring_buffer_expanded) 6944 expanded_size += trace_buf_size >> 10; 6945 } 6946 if (tr->ring_buffer_expanded) 6947 r = sprintf(buf, "%lu\n", size); 6948 else 6949 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); 6950 mutex_unlock(&trace_types_lock); 6951 6952 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 6953 } 6954 6955 #define LAST_BOOT_HEADER ((void *)1) 6956 6957 static void *l_next(struct seq_file *m, void *v, loff_t *pos) 6958 { 6959 struct trace_array *tr = m->private; 6960 struct trace_scratch *tscratch = tr->scratch; 6961 unsigned int index = *pos; 6962 6963 (*pos)++; 6964 6965 if (*pos == 1) 6966 return LAST_BOOT_HEADER; 6967 6968 /* Only show offsets of the last boot data */ 6969 if (!tscratch || !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) 6970 return NULL; 6971 6972 /* *pos 0 is for the header, 1 is for the first module */ 6973 index--; 6974 6975 if (index >= tscratch->nr_entries) 6976 return NULL; 6977 6978 return &tscratch->entries[index]; 6979 } 6980 6981 static void *l_start(struct seq_file *m, loff_t *pos) 6982 { 6983 mutex_lock(&scratch_mutex); 6984 6985 return l_next(m, NULL, pos); 6986 } 6987 6988 static void l_stop(struct seq_file *m, void *p) 6989 { 6990 mutex_unlock(&scratch_mutex); 6991 } 6992 6993 static void show_last_boot_header(struct seq_file *m, struct trace_array *tr) 6994 { 6995 struct trace_scratch *tscratch = tr->scratch; 6996 6997 /* 6998 * Do not leak KASLR address. This only shows the KASLR address of 6999 * the last boot. When the ring buffer is started, the LAST_BOOT 7000 * flag gets cleared, and this should only report "current". 7001 * Otherwise it shows the KASLR address from the previous boot which 7002 * should not be the same as the current boot. 7003 */ 7004 if (tscratch && (tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) 7005 seq_printf(m, "%lx\t[kernel]\n", tscratch->text_addr); 7006 else 7007 seq_puts(m, "# Current\n"); 7008 } 7009 7010 static int l_show(struct seq_file *m, void *v) 7011 { 7012 struct trace_array *tr = m->private; 7013 struct trace_mod_entry *entry = v; 7014 7015 if (v == LAST_BOOT_HEADER) { 7016 show_last_boot_header(m, tr); 7017 return 0; 7018 } 7019 7020 seq_printf(m, "%lx\t%s\n", entry->mod_addr, entry->mod_name); 7021 return 0; 7022 } 7023 7024 static const struct seq_operations last_boot_seq_ops = { 7025 .start = l_start, 7026 .next = l_next, 7027 .stop = l_stop, 7028 .show = l_show, 7029 }; 7030 7031 static int tracing_last_boot_open(struct inode *inode, struct file *file) 7032 { 7033 struct trace_array *tr = inode->i_private; 7034 struct seq_file *m; 7035 int ret; 7036 7037 ret = tracing_check_open_get_tr(tr); 7038 if (ret) 7039 return ret; 7040 7041 ret = seq_open(file, &last_boot_seq_ops); 7042 if (ret) { 7043 trace_array_put(tr); 7044 return ret; 7045 } 7046 7047 m = file->private_data; 7048 m->private = tr; 7049 7050 return 0; 7051 } 7052 7053 static int tracing_buffer_meta_open(struct inode *inode, struct file *filp) 7054 { 7055 struct trace_array *tr = inode->i_private; 7056 int cpu = tracing_get_cpu(inode); 7057 int ret; 7058 7059 ret = tracing_check_open_get_tr(tr); 7060 if (ret) 7061 return ret; 7062 7063 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu); 7064 if (ret < 0) 7065 __trace_array_put(tr); 7066 return ret; 7067 } 7068 7069 static ssize_t 7070 tracing_free_buffer_write(struct file *filp, const char __user *ubuf, 7071 size_t cnt, loff_t *ppos) 7072 { 7073 /* 7074 * There is no need to read what the user has written, this function 7075 * is just to make sure that there is no error when "echo" is used 7076 */ 7077 7078 *ppos += cnt; 7079 7080 return cnt; 7081 } 7082 7083 static int 7084 tracing_free_buffer_release(struct inode *inode, struct file *filp) 7085 { 7086 struct trace_array *tr = inode->i_private; 7087 7088 /* disable tracing ? */ 7089 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE) 7090 tracer_tracing_off(tr); 7091 /* resize the ring buffer to 0 */ 7092 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS); 7093 7094 trace_array_put(tr); 7095 7096 return 0; 7097 } 7098 7099 #define TRACE_MARKER_MAX_SIZE 4096 7100 7101 static ssize_t 7102 tracing_mark_write(struct file *filp, const char __user *ubuf, 7103 size_t cnt, loff_t *fpos) 7104 { 7105 struct trace_array *tr = filp->private_data; 7106 struct ring_buffer_event *event; 7107 enum event_trigger_type tt = ETT_NONE; 7108 struct trace_buffer *buffer; 7109 struct print_entry *entry; 7110 int meta_size; 7111 ssize_t written; 7112 size_t size; 7113 int len; 7114 7115 /* Used in tracing_mark_raw_write() as well */ 7116 #define FAULTED_STR "<faulted>" 7117 #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */ 7118 7119 if (tracing_disabled) 7120 return -EINVAL; 7121 7122 if (!(tr->trace_flags & TRACE_ITER_MARKERS)) 7123 return -EINVAL; 7124 7125 if ((ssize_t)cnt < 0) 7126 return -EINVAL; 7127 7128 if (cnt > TRACE_MARKER_MAX_SIZE) 7129 cnt = TRACE_MARKER_MAX_SIZE; 7130 7131 meta_size = sizeof(*entry) + 2; /* add '\0' and possible '\n' */ 7132 again: 7133 size = cnt + meta_size; 7134 7135 /* If less than "<faulted>", then make sure we can still add that */ 7136 if (cnt < FAULTED_SIZE) 7137 size += FAULTED_SIZE - cnt; 7138 7139 buffer = tr->array_buffer.buffer; 7140 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 7141 tracing_gen_ctx()); 7142 if (unlikely(!event)) { 7143 /* 7144 * If the size was greater than what was allowed, then 7145 * make it smaller and try again. 7146 */ 7147 if (size > ring_buffer_max_event_size(buffer)) { 7148 /* cnt < FAULTED size should never be bigger than max */ 7149 if (WARN_ON_ONCE(cnt < FAULTED_SIZE)) 7150 return -EBADF; 7151 cnt = ring_buffer_max_event_size(buffer) - meta_size; 7152 /* The above should only happen once */ 7153 if (WARN_ON_ONCE(cnt + meta_size == size)) 7154 return -EBADF; 7155 goto again; 7156 } 7157 7158 /* Ring buffer disabled, return as if not open for write */ 7159 return -EBADF; 7160 } 7161 7162 entry = ring_buffer_event_data(event); 7163 entry->ip = _THIS_IP_; 7164 7165 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt); 7166 if (len) { 7167 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); 7168 cnt = FAULTED_SIZE; 7169 written = -EFAULT; 7170 } else 7171 written = cnt; 7172 7173 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) { 7174 /* do not add \n before testing triggers, but add \0 */ 7175 entry->buf[cnt] = '\0'; 7176 tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event); 7177 } 7178 7179 if (entry->buf[cnt - 1] != '\n') { 7180 entry->buf[cnt] = '\n'; 7181 entry->buf[cnt + 1] = '\0'; 7182 } else 7183 entry->buf[cnt] = '\0'; 7184 7185 if (static_branch_unlikely(&trace_marker_exports_enabled)) 7186 ftrace_exports(event, TRACE_EXPORT_MARKER); 7187 __buffer_unlock_commit(buffer, event); 7188 7189 if (tt) 7190 event_triggers_post_call(tr->trace_marker_file, tt); 7191 7192 return written; 7193 } 7194 7195 static ssize_t 7196 tracing_mark_raw_write(struct file *filp, const char __user *ubuf, 7197 size_t cnt, loff_t *fpos) 7198 { 7199 struct trace_array *tr = filp->private_data; 7200 struct ring_buffer_event *event; 7201 struct trace_buffer *buffer; 7202 struct raw_data_entry *entry; 7203 ssize_t written; 7204 int size; 7205 int len; 7206 7207 #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int)) 7208 7209 if (tracing_disabled) 7210 return -EINVAL; 7211 7212 if (!(tr->trace_flags & TRACE_ITER_MARKERS)) 7213 return -EINVAL; 7214 7215 /* The marker must at least have a tag id */ 7216 if (cnt < sizeof(unsigned int)) 7217 return -EINVAL; 7218 7219 size = sizeof(*entry) + cnt; 7220 if (cnt < FAULT_SIZE_ID) 7221 size += FAULT_SIZE_ID - cnt; 7222 7223 buffer = tr->array_buffer.buffer; 7224 7225 if (size > ring_buffer_max_event_size(buffer)) 7226 return -EINVAL; 7227 7228 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size, 7229 tracing_gen_ctx()); 7230 if (!event) 7231 /* Ring buffer disabled, return as if not open for write */ 7232 return -EBADF; 7233 7234 entry = ring_buffer_event_data(event); 7235 7236 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt); 7237 if (len) { 7238 entry->id = -1; 7239 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE); 7240 written = -EFAULT; 7241 } else 7242 written = cnt; 7243 7244 __buffer_unlock_commit(buffer, event); 7245 7246 return written; 7247 } 7248 7249 static int tracing_clock_show(struct seq_file *m, void *v) 7250 { 7251 struct trace_array *tr = m->private; 7252 int i; 7253 7254 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) 7255 seq_printf(m, 7256 "%s%s%s%s", i ? " " : "", 7257 i == tr->clock_id ? "[" : "", trace_clocks[i].name, 7258 i == tr->clock_id ? "]" : ""); 7259 seq_putc(m, '\n'); 7260 7261 return 0; 7262 } 7263 7264 int tracing_set_clock(struct trace_array *tr, const char *clockstr) 7265 { 7266 int i; 7267 7268 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { 7269 if (strcmp(trace_clocks[i].name, clockstr) == 0) 7270 break; 7271 } 7272 if (i == ARRAY_SIZE(trace_clocks)) 7273 return -EINVAL; 7274 7275 mutex_lock(&trace_types_lock); 7276 7277 tr->clock_id = i; 7278 7279 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func); 7280 7281 /* 7282 * New clock may not be consistent with the previous clock. 7283 * Reset the buffer so that it doesn't have incomparable timestamps. 7284 */ 7285 tracing_reset_online_cpus(&tr->array_buffer); 7286 7287 #ifdef CONFIG_TRACER_MAX_TRACE 7288 if (tr->max_buffer.buffer) 7289 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func); 7290 tracing_reset_online_cpus(&tr->max_buffer); 7291 #endif 7292 7293 mutex_unlock(&trace_types_lock); 7294 7295 return 0; 7296 } 7297 7298 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, 7299 size_t cnt, loff_t *fpos) 7300 { 7301 struct seq_file *m = filp->private_data; 7302 struct trace_array *tr = m->private; 7303 char buf[64]; 7304 const char *clockstr; 7305 int ret; 7306 7307 if (cnt >= sizeof(buf)) 7308 return -EINVAL; 7309 7310 if (copy_from_user(buf, ubuf, cnt)) 7311 return -EFAULT; 7312 7313 buf[cnt] = 0; 7314 7315 clockstr = strstrip(buf); 7316 7317 ret = tracing_set_clock(tr, clockstr); 7318 if (ret) 7319 return ret; 7320 7321 *fpos += cnt; 7322 7323 return cnt; 7324 } 7325 7326 static int tracing_clock_open(struct inode *inode, struct file *file) 7327 { 7328 struct trace_array *tr = inode->i_private; 7329 int ret; 7330 7331 ret = tracing_check_open_get_tr(tr); 7332 if (ret) 7333 return ret; 7334 7335 ret = single_open(file, tracing_clock_show, inode->i_private); 7336 if (ret < 0) 7337 trace_array_put(tr); 7338 7339 return ret; 7340 } 7341 7342 static int tracing_time_stamp_mode_show(struct seq_file *m, void *v) 7343 { 7344 struct trace_array *tr = m->private; 7345 7346 mutex_lock(&trace_types_lock); 7347 7348 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer)) 7349 seq_puts(m, "delta [absolute]\n"); 7350 else 7351 seq_puts(m, "[delta] absolute\n"); 7352 7353 mutex_unlock(&trace_types_lock); 7354 7355 return 0; 7356 } 7357 7358 static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file) 7359 { 7360 struct trace_array *tr = inode->i_private; 7361 int ret; 7362 7363 ret = tracing_check_open_get_tr(tr); 7364 if (ret) 7365 return ret; 7366 7367 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private); 7368 if (ret < 0) 7369 trace_array_put(tr); 7370 7371 return ret; 7372 } 7373 7374 u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe) 7375 { 7376 if (rbe == this_cpu_read(trace_buffered_event)) 7377 return ring_buffer_time_stamp(buffer); 7378 7379 return ring_buffer_event_time_stamp(buffer, rbe); 7380 } 7381 7382 /* 7383 * Set or disable using the per CPU trace_buffer_event when possible. 7384 */ 7385 int tracing_set_filter_buffering(struct trace_array *tr, bool set) 7386 { 7387 guard(mutex)(&trace_types_lock); 7388 7389 if (set && tr->no_filter_buffering_ref++) 7390 return 0; 7391 7392 if (!set) { 7393 if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) 7394 return -EINVAL; 7395 7396 --tr->no_filter_buffering_ref; 7397 } 7398 7399 return 0; 7400 } 7401 7402 struct ftrace_buffer_info { 7403 struct trace_iterator iter; 7404 void *spare; 7405 unsigned int spare_cpu; 7406 unsigned int spare_size; 7407 unsigned int read; 7408 }; 7409 7410 #ifdef CONFIG_TRACER_SNAPSHOT 7411 static int tracing_snapshot_open(struct inode *inode, struct file *file) 7412 { 7413 struct trace_array *tr = inode->i_private; 7414 struct trace_iterator *iter; 7415 struct seq_file *m; 7416 int ret; 7417 7418 ret = tracing_check_open_get_tr(tr); 7419 if (ret) 7420 return ret; 7421 7422 if (file->f_mode & FMODE_READ) { 7423 iter = __tracing_open(inode, file, true); 7424 if (IS_ERR(iter)) 7425 ret = PTR_ERR(iter); 7426 } else { 7427 /* Writes still need the seq_file to hold the private data */ 7428 ret = -ENOMEM; 7429 m = kzalloc(sizeof(*m), GFP_KERNEL); 7430 if (!m) 7431 goto out; 7432 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 7433 if (!iter) { 7434 kfree(m); 7435 goto out; 7436 } 7437 ret = 0; 7438 7439 iter->tr = tr; 7440 iter->array_buffer = &tr->max_buffer; 7441 iter->cpu_file = tracing_get_cpu(inode); 7442 m->private = iter; 7443 file->private_data = m; 7444 } 7445 out: 7446 if (ret < 0) 7447 trace_array_put(tr); 7448 7449 return ret; 7450 } 7451 7452 static void tracing_swap_cpu_buffer(void *tr) 7453 { 7454 update_max_tr_single((struct trace_array *)tr, current, smp_processor_id()); 7455 } 7456 7457 static ssize_t 7458 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, 7459 loff_t *ppos) 7460 { 7461 struct seq_file *m = filp->private_data; 7462 struct trace_iterator *iter = m->private; 7463 struct trace_array *tr = iter->tr; 7464 unsigned long val; 7465 int ret; 7466 7467 ret = tracing_update_buffers(tr); 7468 if (ret < 0) 7469 return ret; 7470 7471 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 7472 if (ret) 7473 return ret; 7474 7475 guard(mutex)(&trace_types_lock); 7476 7477 if (tr->current_trace->use_max_tr) 7478 return -EBUSY; 7479 7480 local_irq_disable(); 7481 arch_spin_lock(&tr->max_lock); 7482 if (tr->cond_snapshot) 7483 ret = -EBUSY; 7484 arch_spin_unlock(&tr->max_lock); 7485 local_irq_enable(); 7486 if (ret) 7487 return ret; 7488 7489 switch (val) { 7490 case 0: 7491 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) 7492 return -EINVAL; 7493 if (tr->allocated_snapshot) 7494 free_snapshot(tr); 7495 break; 7496 case 1: 7497 /* Only allow per-cpu swap if the ring buffer supports it */ 7498 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP 7499 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) 7500 return -EINVAL; 7501 #endif 7502 if (tr->allocated_snapshot) 7503 ret = resize_buffer_duplicate_size(&tr->max_buffer, 7504 &tr->array_buffer, iter->cpu_file); 7505 7506 ret = tracing_arm_snapshot_locked(tr); 7507 if (ret) 7508 return ret; 7509 7510 /* Now, we're going to swap */ 7511 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { 7512 local_irq_disable(); 7513 update_max_tr(tr, current, smp_processor_id(), NULL); 7514 local_irq_enable(); 7515 } else { 7516 smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer, 7517 (void *)tr, 1); 7518 } 7519 tracing_disarm_snapshot(tr); 7520 break; 7521 default: 7522 if (tr->allocated_snapshot) { 7523 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 7524 tracing_reset_online_cpus(&tr->max_buffer); 7525 else 7526 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file); 7527 } 7528 break; 7529 } 7530 7531 if (ret >= 0) { 7532 *ppos += cnt; 7533 ret = cnt; 7534 } 7535 7536 return ret; 7537 } 7538 7539 static int tracing_snapshot_release(struct inode *inode, struct file *file) 7540 { 7541 struct seq_file *m = file->private_data; 7542 int ret; 7543 7544 ret = tracing_release(inode, file); 7545 7546 if (file->f_mode & FMODE_READ) 7547 return ret; 7548 7549 /* If write only, the seq_file is just a stub */ 7550 if (m) 7551 kfree(m->private); 7552 kfree(m); 7553 7554 return 0; 7555 } 7556 7557 static int tracing_buffers_open(struct inode *inode, struct file *filp); 7558 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, 7559 size_t count, loff_t *ppos); 7560 static int tracing_buffers_release(struct inode *inode, struct file *file); 7561 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, 7562 struct pipe_inode_info *pipe, size_t len, unsigned int flags); 7563 7564 static int snapshot_raw_open(struct inode *inode, struct file *filp) 7565 { 7566 struct ftrace_buffer_info *info; 7567 int ret; 7568 7569 /* The following checks for tracefs lockdown */ 7570 ret = tracing_buffers_open(inode, filp); 7571 if (ret < 0) 7572 return ret; 7573 7574 info = filp->private_data; 7575 7576 if (info->iter.trace->use_max_tr) { 7577 tracing_buffers_release(inode, filp); 7578 return -EBUSY; 7579 } 7580 7581 info->iter.snapshot = true; 7582 info->iter.array_buffer = &info->iter.tr->max_buffer; 7583 7584 return ret; 7585 } 7586 7587 #endif /* CONFIG_TRACER_SNAPSHOT */ 7588 7589 7590 static const struct file_operations tracing_thresh_fops = { 7591 .open = tracing_open_generic, 7592 .read = tracing_thresh_read, 7593 .write = tracing_thresh_write, 7594 .llseek = generic_file_llseek, 7595 }; 7596 7597 #ifdef CONFIG_TRACER_MAX_TRACE 7598 static const struct file_operations tracing_max_lat_fops = { 7599 .open = tracing_open_generic_tr, 7600 .read = tracing_max_lat_read, 7601 .write = tracing_max_lat_write, 7602 .llseek = generic_file_llseek, 7603 .release = tracing_release_generic_tr, 7604 }; 7605 #endif 7606 7607 static const struct file_operations set_tracer_fops = { 7608 .open = tracing_open_generic_tr, 7609 .read = tracing_set_trace_read, 7610 .write = tracing_set_trace_write, 7611 .llseek = generic_file_llseek, 7612 .release = tracing_release_generic_tr, 7613 }; 7614 7615 static const struct file_operations tracing_pipe_fops = { 7616 .open = tracing_open_pipe, 7617 .poll = tracing_poll_pipe, 7618 .read = tracing_read_pipe, 7619 .splice_read = tracing_splice_read_pipe, 7620 .release = tracing_release_pipe, 7621 }; 7622 7623 static const struct file_operations tracing_entries_fops = { 7624 .open = tracing_open_generic_tr, 7625 .read = tracing_entries_read, 7626 .write = tracing_entries_write, 7627 .llseek = generic_file_llseek, 7628 .release = tracing_release_generic_tr, 7629 }; 7630 7631 static const struct file_operations tracing_buffer_meta_fops = { 7632 .open = tracing_buffer_meta_open, 7633 .read = seq_read, 7634 .llseek = seq_lseek, 7635 .release = tracing_seq_release, 7636 }; 7637 7638 static const struct file_operations tracing_total_entries_fops = { 7639 .open = tracing_open_generic_tr, 7640 .read = tracing_total_entries_read, 7641 .llseek = generic_file_llseek, 7642 .release = tracing_release_generic_tr, 7643 }; 7644 7645 static const struct file_operations tracing_free_buffer_fops = { 7646 .open = tracing_open_generic_tr, 7647 .write = tracing_free_buffer_write, 7648 .release = tracing_free_buffer_release, 7649 }; 7650 7651 static const struct file_operations tracing_mark_fops = { 7652 .open = tracing_mark_open, 7653 .write = tracing_mark_write, 7654 .release = tracing_release_generic_tr, 7655 }; 7656 7657 static const struct file_operations tracing_mark_raw_fops = { 7658 .open = tracing_mark_open, 7659 .write = tracing_mark_raw_write, 7660 .release = tracing_release_generic_tr, 7661 }; 7662 7663 static const struct file_operations trace_clock_fops = { 7664 .open = tracing_clock_open, 7665 .read = seq_read, 7666 .llseek = seq_lseek, 7667 .release = tracing_single_release_tr, 7668 .write = tracing_clock_write, 7669 }; 7670 7671 static const struct file_operations trace_time_stamp_mode_fops = { 7672 .open = tracing_time_stamp_mode_open, 7673 .read = seq_read, 7674 .llseek = seq_lseek, 7675 .release = tracing_single_release_tr, 7676 }; 7677 7678 static const struct file_operations last_boot_fops = { 7679 .open = tracing_last_boot_open, 7680 .read = seq_read, 7681 .llseek = seq_lseek, 7682 .release = tracing_seq_release, 7683 }; 7684 7685 #ifdef CONFIG_TRACER_SNAPSHOT 7686 static const struct file_operations snapshot_fops = { 7687 .open = tracing_snapshot_open, 7688 .read = seq_read, 7689 .write = tracing_snapshot_write, 7690 .llseek = tracing_lseek, 7691 .release = tracing_snapshot_release, 7692 }; 7693 7694 static const struct file_operations snapshot_raw_fops = { 7695 .open = snapshot_raw_open, 7696 .read = tracing_buffers_read, 7697 .release = tracing_buffers_release, 7698 .splice_read = tracing_buffers_splice_read, 7699 }; 7700 7701 #endif /* CONFIG_TRACER_SNAPSHOT */ 7702 7703 /* 7704 * trace_min_max_write - Write a u64 value to a trace_min_max_param struct 7705 * @filp: The active open file structure 7706 * @ubuf: The userspace provided buffer to read value into 7707 * @cnt: The maximum number of bytes to read 7708 * @ppos: The current "file" position 7709 * 7710 * This function implements the write interface for a struct trace_min_max_param. 7711 * The filp->private_data must point to a trace_min_max_param structure that 7712 * defines where to write the value, the min and the max acceptable values, 7713 * and a lock to protect the write. 7714 */ 7715 static ssize_t 7716 trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos) 7717 { 7718 struct trace_min_max_param *param = filp->private_data; 7719 u64 val; 7720 int err; 7721 7722 if (!param) 7723 return -EFAULT; 7724 7725 err = kstrtoull_from_user(ubuf, cnt, 10, &val); 7726 if (err) 7727 return err; 7728 7729 if (param->lock) 7730 mutex_lock(param->lock); 7731 7732 if (param->min && val < *param->min) 7733 err = -EINVAL; 7734 7735 if (param->max && val > *param->max) 7736 err = -EINVAL; 7737 7738 if (!err) 7739 *param->val = val; 7740 7741 if (param->lock) 7742 mutex_unlock(param->lock); 7743 7744 if (err) 7745 return err; 7746 7747 return cnt; 7748 } 7749 7750 /* 7751 * trace_min_max_read - Read a u64 value from a trace_min_max_param struct 7752 * @filp: The active open file structure 7753 * @ubuf: The userspace provided buffer to read value into 7754 * @cnt: The maximum number of bytes to read 7755 * @ppos: The current "file" position 7756 * 7757 * This function implements the read interface for a struct trace_min_max_param. 7758 * The filp->private_data must point to a trace_min_max_param struct with valid 7759 * data. 7760 */ 7761 static ssize_t 7762 trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 7763 { 7764 struct trace_min_max_param *param = filp->private_data; 7765 char buf[U64_STR_SIZE]; 7766 int len; 7767 u64 val; 7768 7769 if (!param) 7770 return -EFAULT; 7771 7772 val = *param->val; 7773 7774 if (cnt > sizeof(buf)) 7775 cnt = sizeof(buf); 7776 7777 len = snprintf(buf, sizeof(buf), "%llu\n", val); 7778 7779 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len); 7780 } 7781 7782 const struct file_operations trace_min_max_fops = { 7783 .open = tracing_open_generic, 7784 .read = trace_min_max_read, 7785 .write = trace_min_max_write, 7786 }; 7787 7788 #define TRACING_LOG_ERRS_MAX 8 7789 #define TRACING_LOG_LOC_MAX 128 7790 7791 #define CMD_PREFIX " Command: " 7792 7793 struct err_info { 7794 const char **errs; /* ptr to loc-specific array of err strings */ 7795 u8 type; /* index into errs -> specific err string */ 7796 u16 pos; /* caret position */ 7797 u64 ts; 7798 }; 7799 7800 struct tracing_log_err { 7801 struct list_head list; 7802 struct err_info info; 7803 char loc[TRACING_LOG_LOC_MAX]; /* err location */ 7804 char *cmd; /* what caused err */ 7805 }; 7806 7807 static DEFINE_MUTEX(tracing_err_log_lock); 7808 7809 static struct tracing_log_err *alloc_tracing_log_err(int len) 7810 { 7811 struct tracing_log_err *err; 7812 7813 err = kzalloc(sizeof(*err), GFP_KERNEL); 7814 if (!err) 7815 return ERR_PTR(-ENOMEM); 7816 7817 err->cmd = kzalloc(len, GFP_KERNEL); 7818 if (!err->cmd) { 7819 kfree(err); 7820 return ERR_PTR(-ENOMEM); 7821 } 7822 7823 return err; 7824 } 7825 7826 static void free_tracing_log_err(struct tracing_log_err *err) 7827 { 7828 kfree(err->cmd); 7829 kfree(err); 7830 } 7831 7832 static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr, 7833 int len) 7834 { 7835 struct tracing_log_err *err; 7836 char *cmd; 7837 7838 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) { 7839 err = alloc_tracing_log_err(len); 7840 if (PTR_ERR(err) != -ENOMEM) 7841 tr->n_err_log_entries++; 7842 7843 return err; 7844 } 7845 cmd = kzalloc(len, GFP_KERNEL); 7846 if (!cmd) 7847 return ERR_PTR(-ENOMEM); 7848 err = list_first_entry(&tr->err_log, struct tracing_log_err, list); 7849 kfree(err->cmd); 7850 err->cmd = cmd; 7851 list_del(&err->list); 7852 7853 return err; 7854 } 7855 7856 /** 7857 * err_pos - find the position of a string within a command for error careting 7858 * @cmd: The tracing command that caused the error 7859 * @str: The string to position the caret at within @cmd 7860 * 7861 * Finds the position of the first occurrence of @str within @cmd. The 7862 * return value can be passed to tracing_log_err() for caret placement 7863 * within @cmd. 7864 * 7865 * Returns the index within @cmd of the first occurrence of @str or 0 7866 * if @str was not found. 7867 */ 7868 unsigned int err_pos(char *cmd, const char *str) 7869 { 7870 char *found; 7871 7872 if (WARN_ON(!strlen(cmd))) 7873 return 0; 7874 7875 found = strstr(cmd, str); 7876 if (found) 7877 return found - cmd; 7878 7879 return 0; 7880 } 7881 7882 /** 7883 * tracing_log_err - write an error to the tracing error log 7884 * @tr: The associated trace array for the error (NULL for top level array) 7885 * @loc: A string describing where the error occurred 7886 * @cmd: The tracing command that caused the error 7887 * @errs: The array of loc-specific static error strings 7888 * @type: The index into errs[], which produces the specific static err string 7889 * @pos: The position the caret should be placed in the cmd 7890 * 7891 * Writes an error into tracing/error_log of the form: 7892 * 7893 * <loc>: error: <text> 7894 * Command: <cmd> 7895 * ^ 7896 * 7897 * tracing/error_log is a small log file containing the last 7898 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated 7899 * unless there has been a tracing error, and the error log can be 7900 * cleared and have its memory freed by writing the empty string in 7901 * truncation mode to it i.e. echo > tracing/error_log. 7902 * 7903 * NOTE: the @errs array along with the @type param are used to 7904 * produce a static error string - this string is not copied and saved 7905 * when the error is logged - only a pointer to it is saved. See 7906 * existing callers for examples of how static strings are typically 7907 * defined for use with tracing_log_err(). 7908 */ 7909 void tracing_log_err(struct trace_array *tr, 7910 const char *loc, const char *cmd, 7911 const char **errs, u8 type, u16 pos) 7912 { 7913 struct tracing_log_err *err; 7914 int len = 0; 7915 7916 if (!tr) 7917 tr = &global_trace; 7918 7919 len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1; 7920 7921 guard(mutex)(&tracing_err_log_lock); 7922 7923 err = get_tracing_log_err(tr, len); 7924 if (PTR_ERR(err) == -ENOMEM) 7925 return; 7926 7927 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc); 7928 snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd); 7929 7930 err->info.errs = errs; 7931 err->info.type = type; 7932 err->info.pos = pos; 7933 err->info.ts = local_clock(); 7934 7935 list_add_tail(&err->list, &tr->err_log); 7936 } 7937 7938 static void clear_tracing_err_log(struct trace_array *tr) 7939 { 7940 struct tracing_log_err *err, *next; 7941 7942 mutex_lock(&tracing_err_log_lock); 7943 list_for_each_entry_safe(err, next, &tr->err_log, list) { 7944 list_del(&err->list); 7945 free_tracing_log_err(err); 7946 } 7947 7948 tr->n_err_log_entries = 0; 7949 mutex_unlock(&tracing_err_log_lock); 7950 } 7951 7952 static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos) 7953 { 7954 struct trace_array *tr = m->private; 7955 7956 mutex_lock(&tracing_err_log_lock); 7957 7958 return seq_list_start(&tr->err_log, *pos); 7959 } 7960 7961 static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos) 7962 { 7963 struct trace_array *tr = m->private; 7964 7965 return seq_list_next(v, &tr->err_log, pos); 7966 } 7967 7968 static void tracing_err_log_seq_stop(struct seq_file *m, void *v) 7969 { 7970 mutex_unlock(&tracing_err_log_lock); 7971 } 7972 7973 static void tracing_err_log_show_pos(struct seq_file *m, u16 pos) 7974 { 7975 u16 i; 7976 7977 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++) 7978 seq_putc(m, ' '); 7979 for (i = 0; i < pos; i++) 7980 seq_putc(m, ' '); 7981 seq_puts(m, "^\n"); 7982 } 7983 7984 static int tracing_err_log_seq_show(struct seq_file *m, void *v) 7985 { 7986 struct tracing_log_err *err = v; 7987 7988 if (err) { 7989 const char *err_text = err->info.errs[err->info.type]; 7990 u64 sec = err->info.ts; 7991 u32 nsec; 7992 7993 nsec = do_div(sec, NSEC_PER_SEC); 7994 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000, 7995 err->loc, err_text); 7996 seq_printf(m, "%s", err->cmd); 7997 tracing_err_log_show_pos(m, err->info.pos); 7998 } 7999 8000 return 0; 8001 } 8002 8003 static const struct seq_operations tracing_err_log_seq_ops = { 8004 .start = tracing_err_log_seq_start, 8005 .next = tracing_err_log_seq_next, 8006 .stop = tracing_err_log_seq_stop, 8007 .show = tracing_err_log_seq_show 8008 }; 8009 8010 static int tracing_err_log_open(struct inode *inode, struct file *file) 8011 { 8012 struct trace_array *tr = inode->i_private; 8013 int ret = 0; 8014 8015 ret = tracing_check_open_get_tr(tr); 8016 if (ret) 8017 return ret; 8018 8019 /* If this file was opened for write, then erase contents */ 8020 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) 8021 clear_tracing_err_log(tr); 8022 8023 if (file->f_mode & FMODE_READ) { 8024 ret = seq_open(file, &tracing_err_log_seq_ops); 8025 if (!ret) { 8026 struct seq_file *m = file->private_data; 8027 m->private = tr; 8028 } else { 8029 trace_array_put(tr); 8030 } 8031 } 8032 return ret; 8033 } 8034 8035 static ssize_t tracing_err_log_write(struct file *file, 8036 const char __user *buffer, 8037 size_t count, loff_t *ppos) 8038 { 8039 return count; 8040 } 8041 8042 static int tracing_err_log_release(struct inode *inode, struct file *file) 8043 { 8044 struct trace_array *tr = inode->i_private; 8045 8046 trace_array_put(tr); 8047 8048 if (file->f_mode & FMODE_READ) 8049 seq_release(inode, file); 8050 8051 return 0; 8052 } 8053 8054 static const struct file_operations tracing_err_log_fops = { 8055 .open = tracing_err_log_open, 8056 .write = tracing_err_log_write, 8057 .read = seq_read, 8058 .llseek = tracing_lseek, 8059 .release = tracing_err_log_release, 8060 }; 8061 8062 static int tracing_buffers_open(struct inode *inode, struct file *filp) 8063 { 8064 struct trace_array *tr = inode->i_private; 8065 struct ftrace_buffer_info *info; 8066 int ret; 8067 8068 ret = tracing_check_open_get_tr(tr); 8069 if (ret) 8070 return ret; 8071 8072 info = kvzalloc(sizeof(*info), GFP_KERNEL); 8073 if (!info) { 8074 trace_array_put(tr); 8075 return -ENOMEM; 8076 } 8077 8078 mutex_lock(&trace_types_lock); 8079 8080 info->iter.tr = tr; 8081 info->iter.cpu_file = tracing_get_cpu(inode); 8082 info->iter.trace = tr->current_trace; 8083 info->iter.array_buffer = &tr->array_buffer; 8084 info->spare = NULL; 8085 /* Force reading ring buffer for first read */ 8086 info->read = (unsigned int)-1; 8087 8088 filp->private_data = info; 8089 8090 tr->trace_ref++; 8091 8092 mutex_unlock(&trace_types_lock); 8093 8094 ret = nonseekable_open(inode, filp); 8095 if (ret < 0) 8096 trace_array_put(tr); 8097 8098 return ret; 8099 } 8100 8101 static __poll_t 8102 tracing_buffers_poll(struct file *filp, poll_table *poll_table) 8103 { 8104 struct ftrace_buffer_info *info = filp->private_data; 8105 struct trace_iterator *iter = &info->iter; 8106 8107 return trace_poll(iter, filp, poll_table); 8108 } 8109 8110 static ssize_t 8111 tracing_buffers_read(struct file *filp, char __user *ubuf, 8112 size_t count, loff_t *ppos) 8113 { 8114 struct ftrace_buffer_info *info = filp->private_data; 8115 struct trace_iterator *iter = &info->iter; 8116 void *trace_data; 8117 int page_size; 8118 ssize_t ret = 0; 8119 ssize_t size; 8120 8121 if (!count) 8122 return 0; 8123 8124 #ifdef CONFIG_TRACER_MAX_TRACE 8125 if (iter->snapshot && iter->tr->current_trace->use_max_tr) 8126 return -EBUSY; 8127 #endif 8128 8129 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer); 8130 8131 /* Make sure the spare matches the current sub buffer size */ 8132 if (info->spare) { 8133 if (page_size != info->spare_size) { 8134 ring_buffer_free_read_page(iter->array_buffer->buffer, 8135 info->spare_cpu, info->spare); 8136 info->spare = NULL; 8137 } 8138 } 8139 8140 if (!info->spare) { 8141 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer, 8142 iter->cpu_file); 8143 if (IS_ERR(info->spare)) { 8144 ret = PTR_ERR(info->spare); 8145 info->spare = NULL; 8146 } else { 8147 info->spare_cpu = iter->cpu_file; 8148 info->spare_size = page_size; 8149 } 8150 } 8151 if (!info->spare) 8152 return ret; 8153 8154 /* Do we have previous read data to read? */ 8155 if (info->read < page_size) 8156 goto read; 8157 8158 again: 8159 trace_access_lock(iter->cpu_file); 8160 ret = ring_buffer_read_page(iter->array_buffer->buffer, 8161 info->spare, 8162 count, 8163 iter->cpu_file, 0); 8164 trace_access_unlock(iter->cpu_file); 8165 8166 if (ret < 0) { 8167 if (trace_empty(iter) && !iter->closed) { 8168 if ((filp->f_flags & O_NONBLOCK)) 8169 return -EAGAIN; 8170 8171 ret = wait_on_pipe(iter, 0); 8172 if (ret) 8173 return ret; 8174 8175 goto again; 8176 } 8177 return 0; 8178 } 8179 8180 info->read = 0; 8181 read: 8182 size = page_size - info->read; 8183 if (size > count) 8184 size = count; 8185 trace_data = ring_buffer_read_page_data(info->spare); 8186 ret = copy_to_user(ubuf, trace_data + info->read, size); 8187 if (ret == size) 8188 return -EFAULT; 8189 8190 size -= ret; 8191 8192 *ppos += size; 8193 info->read += size; 8194 8195 return size; 8196 } 8197 8198 static int tracing_buffers_flush(struct file *file, fl_owner_t id) 8199 { 8200 struct ftrace_buffer_info *info = file->private_data; 8201 struct trace_iterator *iter = &info->iter; 8202 8203 iter->closed = true; 8204 /* Make sure the waiters see the new wait_index */ 8205 (void)atomic_fetch_inc_release(&iter->wait_index); 8206 8207 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file); 8208 8209 return 0; 8210 } 8211 8212 static int tracing_buffers_release(struct inode *inode, struct file *file) 8213 { 8214 struct ftrace_buffer_info *info = file->private_data; 8215 struct trace_iterator *iter = &info->iter; 8216 8217 mutex_lock(&trace_types_lock); 8218 8219 iter->tr->trace_ref--; 8220 8221 __trace_array_put(iter->tr); 8222 8223 if (info->spare) 8224 ring_buffer_free_read_page(iter->array_buffer->buffer, 8225 info->spare_cpu, info->spare); 8226 kvfree(info); 8227 8228 mutex_unlock(&trace_types_lock); 8229 8230 return 0; 8231 } 8232 8233 struct buffer_ref { 8234 struct trace_buffer *buffer; 8235 void *page; 8236 int cpu; 8237 refcount_t refcount; 8238 }; 8239 8240 static void buffer_ref_release(struct buffer_ref *ref) 8241 { 8242 if (!refcount_dec_and_test(&ref->refcount)) 8243 return; 8244 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); 8245 kfree(ref); 8246 } 8247 8248 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, 8249 struct pipe_buffer *buf) 8250 { 8251 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 8252 8253 buffer_ref_release(ref); 8254 buf->private = 0; 8255 } 8256 8257 static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe, 8258 struct pipe_buffer *buf) 8259 { 8260 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 8261 8262 if (refcount_read(&ref->refcount) > INT_MAX/2) 8263 return false; 8264 8265 refcount_inc(&ref->refcount); 8266 return true; 8267 } 8268 8269 /* Pipe buffer operations for a buffer. */ 8270 static const struct pipe_buf_operations buffer_pipe_buf_ops = { 8271 .release = buffer_pipe_buf_release, 8272 .get = buffer_pipe_buf_get, 8273 }; 8274 8275 /* 8276 * Callback from splice_to_pipe(), if we need to release some pages 8277 * at the end of the spd in case we error'ed out in filling the pipe. 8278 */ 8279 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) 8280 { 8281 struct buffer_ref *ref = 8282 (struct buffer_ref *)spd->partial[i].private; 8283 8284 buffer_ref_release(ref); 8285 spd->partial[i].private = 0; 8286 } 8287 8288 static ssize_t 8289 tracing_buffers_splice_read(struct file *file, loff_t *ppos, 8290 struct pipe_inode_info *pipe, size_t len, 8291 unsigned int flags) 8292 { 8293 struct ftrace_buffer_info *info = file->private_data; 8294 struct trace_iterator *iter = &info->iter; 8295 struct partial_page partial_def[PIPE_DEF_BUFFERS]; 8296 struct page *pages_def[PIPE_DEF_BUFFERS]; 8297 struct splice_pipe_desc spd = { 8298 .pages = pages_def, 8299 .partial = partial_def, 8300 .nr_pages_max = PIPE_DEF_BUFFERS, 8301 .ops = &buffer_pipe_buf_ops, 8302 .spd_release = buffer_spd_release, 8303 }; 8304 struct buffer_ref *ref; 8305 bool woken = false; 8306 int page_size; 8307 int entries, i; 8308 ssize_t ret = 0; 8309 8310 #ifdef CONFIG_TRACER_MAX_TRACE 8311 if (iter->snapshot && iter->tr->current_trace->use_max_tr) 8312 return -EBUSY; 8313 #endif 8314 8315 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer); 8316 if (*ppos & (page_size - 1)) 8317 return -EINVAL; 8318 8319 if (len & (page_size - 1)) { 8320 if (len < page_size) 8321 return -EINVAL; 8322 len &= (~(page_size - 1)); 8323 } 8324 8325 if (splice_grow_spd(pipe, &spd)) 8326 return -ENOMEM; 8327 8328 again: 8329 trace_access_lock(iter->cpu_file); 8330 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); 8331 8332 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= page_size) { 8333 struct page *page; 8334 int r; 8335 8336 ref = kzalloc(sizeof(*ref), GFP_KERNEL); 8337 if (!ref) { 8338 ret = -ENOMEM; 8339 break; 8340 } 8341 8342 refcount_set(&ref->refcount, 1); 8343 ref->buffer = iter->array_buffer->buffer; 8344 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); 8345 if (IS_ERR(ref->page)) { 8346 ret = PTR_ERR(ref->page); 8347 ref->page = NULL; 8348 kfree(ref); 8349 break; 8350 } 8351 ref->cpu = iter->cpu_file; 8352 8353 r = ring_buffer_read_page(ref->buffer, ref->page, 8354 len, iter->cpu_file, 1); 8355 if (r < 0) { 8356 ring_buffer_free_read_page(ref->buffer, ref->cpu, 8357 ref->page); 8358 kfree(ref); 8359 break; 8360 } 8361 8362 page = virt_to_page(ring_buffer_read_page_data(ref->page)); 8363 8364 spd.pages[i] = page; 8365 spd.partial[i].len = page_size; 8366 spd.partial[i].offset = 0; 8367 spd.partial[i].private = (unsigned long)ref; 8368 spd.nr_pages++; 8369 *ppos += page_size; 8370 8371 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file); 8372 } 8373 8374 trace_access_unlock(iter->cpu_file); 8375 spd.nr_pages = i; 8376 8377 /* did we read anything? */ 8378 if (!spd.nr_pages) { 8379 8380 if (ret) 8381 goto out; 8382 8383 if (woken) 8384 goto out; 8385 8386 ret = -EAGAIN; 8387 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) 8388 goto out; 8389 8390 ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent); 8391 if (ret) 8392 goto out; 8393 8394 /* No need to wait after waking up when tracing is off */ 8395 if (!tracer_tracing_is_on(iter->tr)) 8396 goto out; 8397 8398 /* Iterate one more time to collect any new data then exit */ 8399 woken = true; 8400 8401 goto again; 8402 } 8403 8404 ret = splice_to_pipe(pipe, &spd); 8405 out: 8406 splice_shrink_spd(&spd); 8407 8408 return ret; 8409 } 8410 8411 static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 8412 { 8413 struct ftrace_buffer_info *info = file->private_data; 8414 struct trace_iterator *iter = &info->iter; 8415 int err; 8416 8417 if (cmd == TRACE_MMAP_IOCTL_GET_READER) { 8418 if (!(file->f_flags & O_NONBLOCK)) { 8419 err = ring_buffer_wait(iter->array_buffer->buffer, 8420 iter->cpu_file, 8421 iter->tr->buffer_percent, 8422 NULL, NULL); 8423 if (err) 8424 return err; 8425 } 8426 8427 return ring_buffer_map_get_reader(iter->array_buffer->buffer, 8428 iter->cpu_file); 8429 } else if (cmd) { 8430 return -ENOTTY; 8431 } 8432 8433 /* 8434 * An ioctl call with cmd 0 to the ring buffer file will wake up all 8435 * waiters 8436 */ 8437 mutex_lock(&trace_types_lock); 8438 8439 /* Make sure the waiters see the new wait_index */ 8440 (void)atomic_fetch_inc_release(&iter->wait_index); 8441 8442 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file); 8443 8444 mutex_unlock(&trace_types_lock); 8445 return 0; 8446 } 8447 8448 #ifdef CONFIG_TRACER_MAX_TRACE 8449 static int get_snapshot_map(struct trace_array *tr) 8450 { 8451 int err = 0; 8452 8453 /* 8454 * Called with mmap_lock held. lockdep would be unhappy if we would now 8455 * take trace_types_lock. Instead use the specific 8456 * snapshot_trigger_lock. 8457 */ 8458 spin_lock(&tr->snapshot_trigger_lock); 8459 8460 if (tr->snapshot || tr->mapped == UINT_MAX) 8461 err = -EBUSY; 8462 else 8463 tr->mapped++; 8464 8465 spin_unlock(&tr->snapshot_trigger_lock); 8466 8467 /* Wait for update_max_tr() to observe iter->tr->mapped */ 8468 if (tr->mapped == 1) 8469 synchronize_rcu(); 8470 8471 return err; 8472 8473 } 8474 static void put_snapshot_map(struct trace_array *tr) 8475 { 8476 spin_lock(&tr->snapshot_trigger_lock); 8477 if (!WARN_ON(!tr->mapped)) 8478 tr->mapped--; 8479 spin_unlock(&tr->snapshot_trigger_lock); 8480 } 8481 #else 8482 static inline int get_snapshot_map(struct trace_array *tr) { return 0; } 8483 static inline void put_snapshot_map(struct trace_array *tr) { } 8484 #endif 8485 8486 static void tracing_buffers_mmap_close(struct vm_area_struct *vma) 8487 { 8488 struct ftrace_buffer_info *info = vma->vm_file->private_data; 8489 struct trace_iterator *iter = &info->iter; 8490 8491 WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file)); 8492 put_snapshot_map(iter->tr); 8493 } 8494 8495 static const struct vm_operations_struct tracing_buffers_vmops = { 8496 .close = tracing_buffers_mmap_close, 8497 }; 8498 8499 static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma) 8500 { 8501 struct ftrace_buffer_info *info = filp->private_data; 8502 struct trace_iterator *iter = &info->iter; 8503 int ret = 0; 8504 8505 /* A memmap'ed buffer is not supported for user space mmap */ 8506 if (iter->tr->flags & TRACE_ARRAY_FL_MEMMAP) 8507 return -ENODEV; 8508 8509 /* Currently the boot mapped buffer is not supported for mmap */ 8510 if (iter->tr->flags & TRACE_ARRAY_FL_BOOT) 8511 return -ENODEV; 8512 8513 ret = get_snapshot_map(iter->tr); 8514 if (ret) 8515 return ret; 8516 8517 ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma); 8518 if (ret) 8519 put_snapshot_map(iter->tr); 8520 8521 vma->vm_ops = &tracing_buffers_vmops; 8522 8523 return ret; 8524 } 8525 8526 static const struct file_operations tracing_buffers_fops = { 8527 .open = tracing_buffers_open, 8528 .read = tracing_buffers_read, 8529 .poll = tracing_buffers_poll, 8530 .release = tracing_buffers_release, 8531 .flush = tracing_buffers_flush, 8532 .splice_read = tracing_buffers_splice_read, 8533 .unlocked_ioctl = tracing_buffers_ioctl, 8534 .mmap = tracing_buffers_mmap, 8535 }; 8536 8537 static ssize_t 8538 tracing_stats_read(struct file *filp, char __user *ubuf, 8539 size_t count, loff_t *ppos) 8540 { 8541 struct inode *inode = file_inode(filp); 8542 struct trace_array *tr = inode->i_private; 8543 struct array_buffer *trace_buf = &tr->array_buffer; 8544 int cpu = tracing_get_cpu(inode); 8545 struct trace_seq *s; 8546 unsigned long cnt; 8547 unsigned long long t; 8548 unsigned long usec_rem; 8549 8550 s = kmalloc(sizeof(*s), GFP_KERNEL); 8551 if (!s) 8552 return -ENOMEM; 8553 8554 trace_seq_init(s); 8555 8556 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu); 8557 trace_seq_printf(s, "entries: %ld\n", cnt); 8558 8559 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu); 8560 trace_seq_printf(s, "overrun: %ld\n", cnt); 8561 8562 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu); 8563 trace_seq_printf(s, "commit overrun: %ld\n", cnt); 8564 8565 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu); 8566 trace_seq_printf(s, "bytes: %ld\n", cnt); 8567 8568 if (trace_clocks[tr->clock_id].in_ns) { 8569 /* local or global for trace_clock */ 8570 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 8571 usec_rem = do_div(t, USEC_PER_SEC); 8572 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", 8573 t, usec_rem); 8574 8575 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer)); 8576 usec_rem = do_div(t, USEC_PER_SEC); 8577 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); 8578 } else { 8579 /* counter or tsc mode for trace_clock */ 8580 trace_seq_printf(s, "oldest event ts: %llu\n", 8581 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 8582 8583 trace_seq_printf(s, "now ts: %llu\n", 8584 ring_buffer_time_stamp(trace_buf->buffer)); 8585 } 8586 8587 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu); 8588 trace_seq_printf(s, "dropped events: %ld\n", cnt); 8589 8590 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu); 8591 trace_seq_printf(s, "read events: %ld\n", cnt); 8592 8593 count = simple_read_from_buffer(ubuf, count, ppos, 8594 s->buffer, trace_seq_used(s)); 8595 8596 kfree(s); 8597 8598 return count; 8599 } 8600 8601 static const struct file_operations tracing_stats_fops = { 8602 .open = tracing_open_generic_tr, 8603 .read = tracing_stats_read, 8604 .llseek = generic_file_llseek, 8605 .release = tracing_release_generic_tr, 8606 }; 8607 8608 #ifdef CONFIG_DYNAMIC_FTRACE 8609 8610 static ssize_t 8611 tracing_read_dyn_info(struct file *filp, char __user *ubuf, 8612 size_t cnt, loff_t *ppos) 8613 { 8614 ssize_t ret; 8615 char *buf; 8616 int r; 8617 8618 /* 512 should be plenty to hold the amount needed */ 8619 #define DYN_INFO_BUF_SIZE 512 8620 8621 buf = kmalloc(DYN_INFO_BUF_SIZE, GFP_KERNEL); 8622 if (!buf) 8623 return -ENOMEM; 8624 8625 r = scnprintf(buf, DYN_INFO_BUF_SIZE, 8626 "%ld pages:%ld groups: %ld\n" 8627 "ftrace boot update time = %llu (ns)\n" 8628 "ftrace module total update time = %llu (ns)\n", 8629 ftrace_update_tot_cnt, 8630 ftrace_number_of_pages, 8631 ftrace_number_of_groups, 8632 ftrace_update_time, 8633 ftrace_total_mod_time); 8634 8635 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 8636 kfree(buf); 8637 return ret; 8638 } 8639 8640 static const struct file_operations tracing_dyn_info_fops = { 8641 .open = tracing_open_generic, 8642 .read = tracing_read_dyn_info, 8643 .llseek = generic_file_llseek, 8644 }; 8645 #endif /* CONFIG_DYNAMIC_FTRACE */ 8646 8647 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) 8648 static void 8649 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, 8650 struct trace_array *tr, struct ftrace_probe_ops *ops, 8651 void *data) 8652 { 8653 tracing_snapshot_instance(tr); 8654 } 8655 8656 static void 8657 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, 8658 struct trace_array *tr, struct ftrace_probe_ops *ops, 8659 void *data) 8660 { 8661 struct ftrace_func_mapper *mapper = data; 8662 long *count = NULL; 8663 8664 if (mapper) 8665 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 8666 8667 if (count) { 8668 8669 if (*count <= 0) 8670 return; 8671 8672 (*count)--; 8673 } 8674 8675 tracing_snapshot_instance(tr); 8676 } 8677 8678 static int 8679 ftrace_snapshot_print(struct seq_file *m, unsigned long ip, 8680 struct ftrace_probe_ops *ops, void *data) 8681 { 8682 struct ftrace_func_mapper *mapper = data; 8683 long *count = NULL; 8684 8685 seq_printf(m, "%ps:", (void *)ip); 8686 8687 seq_puts(m, "snapshot"); 8688 8689 if (mapper) 8690 count = (long *)ftrace_func_mapper_find_ip(mapper, ip); 8691 8692 if (count) 8693 seq_printf(m, ":count=%ld\n", *count); 8694 else 8695 seq_puts(m, ":unlimited\n"); 8696 8697 return 0; 8698 } 8699 8700 static int 8701 ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr, 8702 unsigned long ip, void *init_data, void **data) 8703 { 8704 struct ftrace_func_mapper *mapper = *data; 8705 8706 if (!mapper) { 8707 mapper = allocate_ftrace_func_mapper(); 8708 if (!mapper) 8709 return -ENOMEM; 8710 *data = mapper; 8711 } 8712 8713 return ftrace_func_mapper_add_ip(mapper, ip, init_data); 8714 } 8715 8716 static void 8717 ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr, 8718 unsigned long ip, void *data) 8719 { 8720 struct ftrace_func_mapper *mapper = data; 8721 8722 if (!ip) { 8723 if (!mapper) 8724 return; 8725 free_ftrace_func_mapper(mapper, NULL); 8726 return; 8727 } 8728 8729 ftrace_func_mapper_remove_ip(mapper, ip); 8730 } 8731 8732 static struct ftrace_probe_ops snapshot_probe_ops = { 8733 .func = ftrace_snapshot, 8734 .print = ftrace_snapshot_print, 8735 }; 8736 8737 static struct ftrace_probe_ops snapshot_count_probe_ops = { 8738 .func = ftrace_count_snapshot, 8739 .print = ftrace_snapshot_print, 8740 .init = ftrace_snapshot_init, 8741 .free = ftrace_snapshot_free, 8742 }; 8743 8744 static int 8745 ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, 8746 char *glob, char *cmd, char *param, int enable) 8747 { 8748 struct ftrace_probe_ops *ops; 8749 void *count = (void *)-1; 8750 char *number; 8751 int ret; 8752 8753 if (!tr) 8754 return -ENODEV; 8755 8756 /* hash funcs only work with set_ftrace_filter */ 8757 if (!enable) 8758 return -EINVAL; 8759 8760 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; 8761 8762 if (glob[0] == '!') { 8763 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops); 8764 if (!ret) 8765 tracing_disarm_snapshot(tr); 8766 8767 return ret; 8768 } 8769 8770 if (!param) 8771 goto out_reg; 8772 8773 number = strsep(¶m, ":"); 8774 8775 if (!strlen(number)) 8776 goto out_reg; 8777 8778 /* 8779 * We use the callback data field (which is a pointer) 8780 * as our counter. 8781 */ 8782 ret = kstrtoul(number, 0, (unsigned long *)&count); 8783 if (ret) 8784 return ret; 8785 8786 out_reg: 8787 ret = tracing_arm_snapshot(tr); 8788 if (ret < 0) 8789 goto out; 8790 8791 ret = register_ftrace_function_probe(glob, tr, ops, count); 8792 if (ret < 0) 8793 tracing_disarm_snapshot(tr); 8794 out: 8795 return ret < 0 ? ret : 0; 8796 } 8797 8798 static struct ftrace_func_command ftrace_snapshot_cmd = { 8799 .name = "snapshot", 8800 .func = ftrace_trace_snapshot_callback, 8801 }; 8802 8803 static __init int register_snapshot_cmd(void) 8804 { 8805 return register_ftrace_command(&ftrace_snapshot_cmd); 8806 } 8807 #else 8808 static inline __init int register_snapshot_cmd(void) { return 0; } 8809 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */ 8810 8811 static struct dentry *tracing_get_dentry(struct trace_array *tr) 8812 { 8813 if (WARN_ON(!tr->dir)) 8814 return ERR_PTR(-ENODEV); 8815 8816 /* Top directory uses NULL as the parent */ 8817 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 8818 return NULL; 8819 8820 /* All sub buffers have a descriptor */ 8821 return tr->dir; 8822 } 8823 8824 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu) 8825 { 8826 struct dentry *d_tracer; 8827 8828 if (tr->percpu_dir) 8829 return tr->percpu_dir; 8830 8831 d_tracer = tracing_get_dentry(tr); 8832 if (IS_ERR(d_tracer)) 8833 return NULL; 8834 8835 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer); 8836 8837 MEM_FAIL(!tr->percpu_dir, 8838 "Could not create tracefs directory 'per_cpu/%d'\n", cpu); 8839 8840 return tr->percpu_dir; 8841 } 8842 8843 static struct dentry * 8844 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent, 8845 void *data, long cpu, const struct file_operations *fops) 8846 { 8847 struct dentry *ret = trace_create_file(name, mode, parent, data, fops); 8848 8849 if (ret) /* See tracing_get_cpu() */ 8850 d_inode(ret)->i_cdev = (void *)(cpu + 1); 8851 return ret; 8852 } 8853 8854 static void 8855 tracing_init_tracefs_percpu(struct trace_array *tr, long cpu) 8856 { 8857 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu); 8858 struct dentry *d_cpu; 8859 char cpu_dir[30]; /* 30 characters should be more than enough */ 8860 8861 if (!d_percpu) 8862 return; 8863 8864 snprintf(cpu_dir, 30, "cpu%ld", cpu); 8865 d_cpu = tracefs_create_dir(cpu_dir, d_percpu); 8866 if (!d_cpu) { 8867 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir); 8868 return; 8869 } 8870 8871 /* per cpu trace_pipe */ 8872 trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu, 8873 tr, cpu, &tracing_pipe_fops); 8874 8875 /* per cpu trace */ 8876 trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu, 8877 tr, cpu, &tracing_fops); 8878 8879 trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu, 8880 tr, cpu, &tracing_buffers_fops); 8881 8882 trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu, 8883 tr, cpu, &tracing_stats_fops); 8884 8885 trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu, 8886 tr, cpu, &tracing_entries_fops); 8887 8888 if (tr->range_addr_start) 8889 trace_create_cpu_file("buffer_meta", TRACE_MODE_READ, d_cpu, 8890 tr, cpu, &tracing_buffer_meta_fops); 8891 #ifdef CONFIG_TRACER_SNAPSHOT 8892 if (!tr->range_addr_start) { 8893 trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu, 8894 tr, cpu, &snapshot_fops); 8895 8896 trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu, 8897 tr, cpu, &snapshot_raw_fops); 8898 } 8899 #endif 8900 } 8901 8902 #ifdef CONFIG_FTRACE_SELFTEST 8903 /* Let selftest have access to static functions in this file */ 8904 #include "trace_selftest.c" 8905 #endif 8906 8907 static ssize_t 8908 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, 8909 loff_t *ppos) 8910 { 8911 struct trace_option_dentry *topt = filp->private_data; 8912 char *buf; 8913 8914 if (topt->flags->val & topt->opt->bit) 8915 buf = "1\n"; 8916 else 8917 buf = "0\n"; 8918 8919 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 8920 } 8921 8922 static ssize_t 8923 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, 8924 loff_t *ppos) 8925 { 8926 struct trace_option_dentry *topt = filp->private_data; 8927 unsigned long val; 8928 int ret; 8929 8930 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 8931 if (ret) 8932 return ret; 8933 8934 if (val != 0 && val != 1) 8935 return -EINVAL; 8936 8937 if (!!(topt->flags->val & topt->opt->bit) != val) { 8938 mutex_lock(&trace_types_lock); 8939 ret = __set_tracer_option(topt->tr, topt->flags, 8940 topt->opt, !val); 8941 mutex_unlock(&trace_types_lock); 8942 if (ret) 8943 return ret; 8944 } 8945 8946 *ppos += cnt; 8947 8948 return cnt; 8949 } 8950 8951 static int tracing_open_options(struct inode *inode, struct file *filp) 8952 { 8953 struct trace_option_dentry *topt = inode->i_private; 8954 int ret; 8955 8956 ret = tracing_check_open_get_tr(topt->tr); 8957 if (ret) 8958 return ret; 8959 8960 filp->private_data = inode->i_private; 8961 return 0; 8962 } 8963 8964 static int tracing_release_options(struct inode *inode, struct file *file) 8965 { 8966 struct trace_option_dentry *topt = file->private_data; 8967 8968 trace_array_put(topt->tr); 8969 return 0; 8970 } 8971 8972 static const struct file_operations trace_options_fops = { 8973 .open = tracing_open_options, 8974 .read = trace_options_read, 8975 .write = trace_options_write, 8976 .llseek = generic_file_llseek, 8977 .release = tracing_release_options, 8978 }; 8979 8980 /* 8981 * In order to pass in both the trace_array descriptor as well as the index 8982 * to the flag that the trace option file represents, the trace_array 8983 * has a character array of trace_flags_index[], which holds the index 8984 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc. 8985 * The address of this character array is passed to the flag option file 8986 * read/write callbacks. 8987 * 8988 * In order to extract both the index and the trace_array descriptor, 8989 * get_tr_index() uses the following algorithm. 8990 * 8991 * idx = *ptr; 8992 * 8993 * As the pointer itself contains the address of the index (remember 8994 * index[1] == 1). 8995 * 8996 * Then to get the trace_array descriptor, by subtracting that index 8997 * from the ptr, we get to the start of the index itself. 8998 * 8999 * ptr - idx == &index[0] 9000 * 9001 * Then a simple container_of() from that pointer gets us to the 9002 * trace_array descriptor. 9003 */ 9004 static void get_tr_index(void *data, struct trace_array **ptr, 9005 unsigned int *pindex) 9006 { 9007 *pindex = *(unsigned char *)data; 9008 9009 *ptr = container_of(data - *pindex, struct trace_array, 9010 trace_flags_index); 9011 } 9012 9013 static ssize_t 9014 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, 9015 loff_t *ppos) 9016 { 9017 void *tr_index = filp->private_data; 9018 struct trace_array *tr; 9019 unsigned int index; 9020 char *buf; 9021 9022 get_tr_index(tr_index, &tr, &index); 9023 9024 if (tr->trace_flags & (1 << index)) 9025 buf = "1\n"; 9026 else 9027 buf = "0\n"; 9028 9029 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 9030 } 9031 9032 static ssize_t 9033 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, 9034 loff_t *ppos) 9035 { 9036 void *tr_index = filp->private_data; 9037 struct trace_array *tr; 9038 unsigned int index; 9039 unsigned long val; 9040 int ret; 9041 9042 get_tr_index(tr_index, &tr, &index); 9043 9044 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 9045 if (ret) 9046 return ret; 9047 9048 if (val != 0 && val != 1) 9049 return -EINVAL; 9050 9051 mutex_lock(&event_mutex); 9052 mutex_lock(&trace_types_lock); 9053 ret = set_tracer_flag(tr, 1 << index, val); 9054 mutex_unlock(&trace_types_lock); 9055 mutex_unlock(&event_mutex); 9056 9057 if (ret < 0) 9058 return ret; 9059 9060 *ppos += cnt; 9061 9062 return cnt; 9063 } 9064 9065 static const struct file_operations trace_options_core_fops = { 9066 .open = tracing_open_generic, 9067 .read = trace_options_core_read, 9068 .write = trace_options_core_write, 9069 .llseek = generic_file_llseek, 9070 }; 9071 9072 struct dentry *trace_create_file(const char *name, 9073 umode_t mode, 9074 struct dentry *parent, 9075 void *data, 9076 const struct file_operations *fops) 9077 { 9078 struct dentry *ret; 9079 9080 ret = tracefs_create_file(name, mode, parent, data, fops); 9081 if (!ret) 9082 pr_warn("Could not create tracefs '%s' entry\n", name); 9083 9084 return ret; 9085 } 9086 9087 9088 static struct dentry *trace_options_init_dentry(struct trace_array *tr) 9089 { 9090 struct dentry *d_tracer; 9091 9092 if (tr->options) 9093 return tr->options; 9094 9095 d_tracer = tracing_get_dentry(tr); 9096 if (IS_ERR(d_tracer)) 9097 return NULL; 9098 9099 tr->options = tracefs_create_dir("options", d_tracer); 9100 if (!tr->options) { 9101 pr_warn("Could not create tracefs directory 'options'\n"); 9102 return NULL; 9103 } 9104 9105 return tr->options; 9106 } 9107 9108 static void 9109 create_trace_option_file(struct trace_array *tr, 9110 struct trace_option_dentry *topt, 9111 struct tracer_flags *flags, 9112 struct tracer_opt *opt) 9113 { 9114 struct dentry *t_options; 9115 9116 t_options = trace_options_init_dentry(tr); 9117 if (!t_options) 9118 return; 9119 9120 topt->flags = flags; 9121 topt->opt = opt; 9122 topt->tr = tr; 9123 9124 topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE, 9125 t_options, topt, &trace_options_fops); 9126 9127 } 9128 9129 static void 9130 create_trace_option_files(struct trace_array *tr, struct tracer *tracer) 9131 { 9132 struct trace_option_dentry *topts; 9133 struct trace_options *tr_topts; 9134 struct tracer_flags *flags; 9135 struct tracer_opt *opts; 9136 int cnt; 9137 int i; 9138 9139 if (!tracer) 9140 return; 9141 9142 flags = tracer->flags; 9143 9144 if (!flags || !flags->opts) 9145 return; 9146 9147 /* 9148 * If this is an instance, only create flags for tracers 9149 * the instance may have. 9150 */ 9151 if (!trace_ok_for_array(tracer, tr)) 9152 return; 9153 9154 for (i = 0; i < tr->nr_topts; i++) { 9155 /* Make sure there's no duplicate flags. */ 9156 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags)) 9157 return; 9158 } 9159 9160 opts = flags->opts; 9161 9162 for (cnt = 0; opts[cnt].name; cnt++) 9163 ; 9164 9165 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); 9166 if (!topts) 9167 return; 9168 9169 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1), 9170 GFP_KERNEL); 9171 if (!tr_topts) { 9172 kfree(topts); 9173 return; 9174 } 9175 9176 tr->topts = tr_topts; 9177 tr->topts[tr->nr_topts].tracer = tracer; 9178 tr->topts[tr->nr_topts].topts = topts; 9179 tr->nr_topts++; 9180 9181 for (cnt = 0; opts[cnt].name; cnt++) { 9182 create_trace_option_file(tr, &topts[cnt], flags, 9183 &opts[cnt]); 9184 MEM_FAIL(topts[cnt].entry == NULL, 9185 "Failed to create trace option: %s", 9186 opts[cnt].name); 9187 } 9188 } 9189 9190 static struct dentry * 9191 create_trace_option_core_file(struct trace_array *tr, 9192 const char *option, long index) 9193 { 9194 struct dentry *t_options; 9195 9196 t_options = trace_options_init_dentry(tr); 9197 if (!t_options) 9198 return NULL; 9199 9200 return trace_create_file(option, TRACE_MODE_WRITE, t_options, 9201 (void *)&tr->trace_flags_index[index], 9202 &trace_options_core_fops); 9203 } 9204 9205 static void create_trace_options_dir(struct trace_array *tr) 9206 { 9207 struct dentry *t_options; 9208 bool top_level = tr == &global_trace; 9209 int i; 9210 9211 t_options = trace_options_init_dentry(tr); 9212 if (!t_options) 9213 return; 9214 9215 for (i = 0; trace_options[i]; i++) { 9216 if (top_level || 9217 !((1 << i) & TOP_LEVEL_TRACE_FLAGS)) 9218 create_trace_option_core_file(tr, trace_options[i], i); 9219 } 9220 } 9221 9222 static ssize_t 9223 rb_simple_read(struct file *filp, char __user *ubuf, 9224 size_t cnt, loff_t *ppos) 9225 { 9226 struct trace_array *tr = filp->private_data; 9227 char buf[64]; 9228 int r; 9229 9230 r = tracer_tracing_is_on(tr); 9231 r = sprintf(buf, "%d\n", r); 9232 9233 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 9234 } 9235 9236 static ssize_t 9237 rb_simple_write(struct file *filp, const char __user *ubuf, 9238 size_t cnt, loff_t *ppos) 9239 { 9240 struct trace_array *tr = filp->private_data; 9241 struct trace_buffer *buffer = tr->array_buffer.buffer; 9242 unsigned long val; 9243 int ret; 9244 9245 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 9246 if (ret) 9247 return ret; 9248 9249 if (buffer) { 9250 mutex_lock(&trace_types_lock); 9251 if (!!val == tracer_tracing_is_on(tr)) { 9252 val = 0; /* do nothing */ 9253 } else if (val) { 9254 tracer_tracing_on(tr); 9255 if (tr->current_trace->start) 9256 tr->current_trace->start(tr); 9257 } else { 9258 tracer_tracing_off(tr); 9259 if (tr->current_trace->stop) 9260 tr->current_trace->stop(tr); 9261 /* Wake up any waiters */ 9262 ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS); 9263 } 9264 mutex_unlock(&trace_types_lock); 9265 } 9266 9267 (*ppos)++; 9268 9269 return cnt; 9270 } 9271 9272 static const struct file_operations rb_simple_fops = { 9273 .open = tracing_open_generic_tr, 9274 .read = rb_simple_read, 9275 .write = rb_simple_write, 9276 .release = tracing_release_generic_tr, 9277 .llseek = default_llseek, 9278 }; 9279 9280 static ssize_t 9281 buffer_percent_read(struct file *filp, char __user *ubuf, 9282 size_t cnt, loff_t *ppos) 9283 { 9284 struct trace_array *tr = filp->private_data; 9285 char buf[64]; 9286 int r; 9287 9288 r = tr->buffer_percent; 9289 r = sprintf(buf, "%d\n", r); 9290 9291 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 9292 } 9293 9294 static ssize_t 9295 buffer_percent_write(struct file *filp, const char __user *ubuf, 9296 size_t cnt, loff_t *ppos) 9297 { 9298 struct trace_array *tr = filp->private_data; 9299 unsigned long val; 9300 int ret; 9301 9302 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 9303 if (ret) 9304 return ret; 9305 9306 if (val > 100) 9307 return -EINVAL; 9308 9309 tr->buffer_percent = val; 9310 9311 (*ppos)++; 9312 9313 return cnt; 9314 } 9315 9316 static const struct file_operations buffer_percent_fops = { 9317 .open = tracing_open_generic_tr, 9318 .read = buffer_percent_read, 9319 .write = buffer_percent_write, 9320 .release = tracing_release_generic_tr, 9321 .llseek = default_llseek, 9322 }; 9323 9324 static ssize_t 9325 buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 9326 { 9327 struct trace_array *tr = filp->private_data; 9328 size_t size; 9329 char buf[64]; 9330 int order; 9331 int r; 9332 9333 order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer); 9334 size = (PAGE_SIZE << order) / 1024; 9335 9336 r = sprintf(buf, "%zd\n", size); 9337 9338 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 9339 } 9340 9341 static ssize_t 9342 buffer_subbuf_size_write(struct file *filp, const char __user *ubuf, 9343 size_t cnt, loff_t *ppos) 9344 { 9345 struct trace_array *tr = filp->private_data; 9346 unsigned long val; 9347 int old_order; 9348 int order; 9349 int pages; 9350 int ret; 9351 9352 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 9353 if (ret) 9354 return ret; 9355 9356 val *= 1024; /* value passed in is in KB */ 9357 9358 pages = DIV_ROUND_UP(val, PAGE_SIZE); 9359 order = fls(pages - 1); 9360 9361 /* limit between 1 and 128 system pages */ 9362 if (order < 0 || order > 7) 9363 return -EINVAL; 9364 9365 /* Do not allow tracing while changing the order of the ring buffer */ 9366 tracing_stop_tr(tr); 9367 9368 old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer); 9369 if (old_order == order) 9370 goto out; 9371 9372 ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order); 9373 if (ret) 9374 goto out; 9375 9376 #ifdef CONFIG_TRACER_MAX_TRACE 9377 9378 if (!tr->allocated_snapshot) 9379 goto out_max; 9380 9381 ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order); 9382 if (ret) { 9383 /* Put back the old order */ 9384 cnt = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, old_order); 9385 if (WARN_ON_ONCE(cnt)) { 9386 /* 9387 * AARGH! We are left with different orders! 9388 * The max buffer is our "snapshot" buffer. 9389 * When a tracer needs a snapshot (one of the 9390 * latency tracers), it swaps the max buffer 9391 * with the saved snap shot. We succeeded to 9392 * update the order of the main buffer, but failed to 9393 * update the order of the max buffer. But when we tried 9394 * to reset the main buffer to the original size, we 9395 * failed there too. This is very unlikely to 9396 * happen, but if it does, warn and kill all 9397 * tracing. 9398 */ 9399 tracing_disabled = 1; 9400 } 9401 goto out; 9402 } 9403 out_max: 9404 #endif 9405 (*ppos)++; 9406 out: 9407 if (ret) 9408 cnt = ret; 9409 tracing_start_tr(tr); 9410 return cnt; 9411 } 9412 9413 static const struct file_operations buffer_subbuf_size_fops = { 9414 .open = tracing_open_generic_tr, 9415 .read = buffer_subbuf_size_read, 9416 .write = buffer_subbuf_size_write, 9417 .release = tracing_release_generic_tr, 9418 .llseek = default_llseek, 9419 }; 9420 9421 static struct dentry *trace_instance_dir; 9422 9423 static void 9424 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer); 9425 9426 #ifdef CONFIG_MODULES 9427 static int make_mod_delta(struct module *mod, void *data) 9428 { 9429 struct trace_module_delta *module_delta; 9430 struct trace_scratch *tscratch; 9431 struct trace_mod_entry *entry; 9432 struct trace_array *tr = data; 9433 int i; 9434 9435 tscratch = tr->scratch; 9436 module_delta = READ_ONCE(tr->module_delta); 9437 for (i = 0; i < tscratch->nr_entries; i++) { 9438 entry = &tscratch->entries[i]; 9439 if (strcmp(mod->name, entry->mod_name)) 9440 continue; 9441 if (mod->state == MODULE_STATE_GOING) 9442 module_delta->delta[i] = 0; 9443 else 9444 module_delta->delta[i] = (unsigned long)mod->mem[MOD_TEXT].base 9445 - entry->mod_addr; 9446 break; 9447 } 9448 return 0; 9449 } 9450 #else 9451 static int make_mod_delta(struct module *mod, void *data) 9452 { 9453 return 0; 9454 } 9455 #endif 9456 9457 static int mod_addr_comp(const void *a, const void *b, const void *data) 9458 { 9459 const struct trace_mod_entry *e1 = a; 9460 const struct trace_mod_entry *e2 = b; 9461 9462 return e1->mod_addr > e2->mod_addr ? 1 : -1; 9463 } 9464 9465 static void setup_trace_scratch(struct trace_array *tr, 9466 struct trace_scratch *tscratch, unsigned int size) 9467 { 9468 struct trace_module_delta *module_delta; 9469 struct trace_mod_entry *entry; 9470 int i, nr_entries; 9471 9472 if (!tscratch) 9473 return; 9474 9475 tr->scratch = tscratch; 9476 tr->scratch_size = size; 9477 9478 if (tscratch->text_addr) 9479 tr->text_delta = (unsigned long)_text - tscratch->text_addr; 9480 9481 if (struct_size(tscratch, entries, tscratch->nr_entries) > size) 9482 goto reset; 9483 9484 /* Check if each module name is a valid string */ 9485 for (i = 0; i < tscratch->nr_entries; i++) { 9486 int n; 9487 9488 entry = &tscratch->entries[i]; 9489 9490 for (n = 0; n < MODULE_NAME_LEN; n++) { 9491 if (entry->mod_name[n] == '\0') 9492 break; 9493 if (!isprint(entry->mod_name[n])) 9494 goto reset; 9495 } 9496 if (n == MODULE_NAME_LEN) 9497 goto reset; 9498 } 9499 9500 /* Sort the entries so that we can find appropriate module from address. */ 9501 nr_entries = tscratch->nr_entries; 9502 sort_r(tscratch->entries, nr_entries, sizeof(struct trace_mod_entry), 9503 mod_addr_comp, NULL, NULL); 9504 9505 if (IS_ENABLED(CONFIG_MODULES)) { 9506 module_delta = kzalloc(struct_size(module_delta, delta, nr_entries), GFP_KERNEL); 9507 if (!module_delta) { 9508 pr_info("module_delta allocation failed. Not able to decode module address."); 9509 goto reset; 9510 } 9511 init_rcu_head(&module_delta->rcu); 9512 } else 9513 module_delta = NULL; 9514 WRITE_ONCE(tr->module_delta, module_delta); 9515 9516 /* Scan modules to make text delta for modules. */ 9517 module_for_each_mod(make_mod_delta, tr); 9518 return; 9519 reset: 9520 /* Invalid trace modules */ 9521 memset(tscratch, 0, size); 9522 } 9523 9524 static int 9525 allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size) 9526 { 9527 enum ring_buffer_flags rb_flags; 9528 struct trace_scratch *tscratch; 9529 unsigned int scratch_size = 0; 9530 9531 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; 9532 9533 buf->tr = tr; 9534 9535 if (tr->range_addr_start && tr->range_addr_size) { 9536 /* Add scratch buffer to handle 128 modules */ 9537 buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0, 9538 tr->range_addr_start, 9539 tr->range_addr_size, 9540 struct_size(tscratch, entries, 128)); 9541 9542 tscratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size); 9543 setup_trace_scratch(tr, tscratch, scratch_size); 9544 9545 /* 9546 * This is basically the same as a mapped buffer, 9547 * with the same restrictions. 9548 */ 9549 tr->mapped++; 9550 } else { 9551 buf->buffer = ring_buffer_alloc(size, rb_flags); 9552 } 9553 if (!buf->buffer) 9554 return -ENOMEM; 9555 9556 buf->data = alloc_percpu(struct trace_array_cpu); 9557 if (!buf->data) { 9558 ring_buffer_free(buf->buffer); 9559 buf->buffer = NULL; 9560 return -ENOMEM; 9561 } 9562 9563 /* Allocate the first page for all buffers */ 9564 set_buffer_entries(&tr->array_buffer, 9565 ring_buffer_size(tr->array_buffer.buffer, 0)); 9566 9567 return 0; 9568 } 9569 9570 static void free_trace_buffer(struct array_buffer *buf) 9571 { 9572 if (buf->buffer) { 9573 ring_buffer_free(buf->buffer); 9574 buf->buffer = NULL; 9575 free_percpu(buf->data); 9576 buf->data = NULL; 9577 } 9578 } 9579 9580 static int allocate_trace_buffers(struct trace_array *tr, int size) 9581 { 9582 int ret; 9583 9584 ret = allocate_trace_buffer(tr, &tr->array_buffer, size); 9585 if (ret) 9586 return ret; 9587 9588 #ifdef CONFIG_TRACER_MAX_TRACE 9589 /* Fix mapped buffer trace arrays do not have snapshot buffers */ 9590 if (tr->range_addr_start) 9591 return 0; 9592 9593 ret = allocate_trace_buffer(tr, &tr->max_buffer, 9594 allocate_snapshot ? size : 1); 9595 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) { 9596 free_trace_buffer(&tr->array_buffer); 9597 return -ENOMEM; 9598 } 9599 tr->allocated_snapshot = allocate_snapshot; 9600 9601 allocate_snapshot = false; 9602 #endif 9603 9604 return 0; 9605 } 9606 9607 static void free_trace_buffers(struct trace_array *tr) 9608 { 9609 if (!tr) 9610 return; 9611 9612 free_trace_buffer(&tr->array_buffer); 9613 kfree(tr->module_delta); 9614 9615 #ifdef CONFIG_TRACER_MAX_TRACE 9616 free_trace_buffer(&tr->max_buffer); 9617 #endif 9618 } 9619 9620 static void init_trace_flags_index(struct trace_array *tr) 9621 { 9622 int i; 9623 9624 /* Used by the trace options files */ 9625 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) 9626 tr->trace_flags_index[i] = i; 9627 } 9628 9629 static void __update_tracer_options(struct trace_array *tr) 9630 { 9631 struct tracer *t; 9632 9633 for (t = trace_types; t; t = t->next) 9634 add_tracer_options(tr, t); 9635 } 9636 9637 static void update_tracer_options(struct trace_array *tr) 9638 { 9639 mutex_lock(&trace_types_lock); 9640 tracer_options_updated = true; 9641 __update_tracer_options(tr); 9642 mutex_unlock(&trace_types_lock); 9643 } 9644 9645 /* Must have trace_types_lock held */ 9646 struct trace_array *trace_array_find(const char *instance) 9647 { 9648 struct trace_array *tr, *found = NULL; 9649 9650 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9651 if (tr->name && strcmp(tr->name, instance) == 0) { 9652 found = tr; 9653 break; 9654 } 9655 } 9656 9657 return found; 9658 } 9659 9660 struct trace_array *trace_array_find_get(const char *instance) 9661 { 9662 struct trace_array *tr; 9663 9664 mutex_lock(&trace_types_lock); 9665 tr = trace_array_find(instance); 9666 if (tr) 9667 tr->ref++; 9668 mutex_unlock(&trace_types_lock); 9669 9670 return tr; 9671 } 9672 9673 static int trace_array_create_dir(struct trace_array *tr) 9674 { 9675 int ret; 9676 9677 tr->dir = tracefs_create_dir(tr->name, trace_instance_dir); 9678 if (!tr->dir) 9679 return -EINVAL; 9680 9681 ret = event_trace_add_tracer(tr->dir, tr); 9682 if (ret) { 9683 tracefs_remove(tr->dir); 9684 return ret; 9685 } 9686 9687 init_tracer_tracefs(tr, tr->dir); 9688 __update_tracer_options(tr); 9689 9690 return ret; 9691 } 9692 9693 static struct trace_array * 9694 trace_array_create_systems(const char *name, const char *systems, 9695 unsigned long range_addr_start, 9696 unsigned long range_addr_size) 9697 { 9698 struct trace_array *tr; 9699 int ret; 9700 9701 ret = -ENOMEM; 9702 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 9703 if (!tr) 9704 return ERR_PTR(ret); 9705 9706 tr->name = kstrdup(name, GFP_KERNEL); 9707 if (!tr->name) 9708 goto out_free_tr; 9709 9710 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL)) 9711 goto out_free_tr; 9712 9713 if (!zalloc_cpumask_var(&tr->pipe_cpumask, GFP_KERNEL)) 9714 goto out_free_tr; 9715 9716 if (systems) { 9717 tr->system_names = kstrdup_const(systems, GFP_KERNEL); 9718 if (!tr->system_names) 9719 goto out_free_tr; 9720 } 9721 9722 /* Only for boot up memory mapped ring buffers */ 9723 tr->range_addr_start = range_addr_start; 9724 tr->range_addr_size = range_addr_size; 9725 9726 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS; 9727 9728 cpumask_copy(tr->tracing_cpumask, cpu_all_mask); 9729 9730 raw_spin_lock_init(&tr->start_lock); 9731 9732 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 9733 #ifdef CONFIG_TRACER_MAX_TRACE 9734 spin_lock_init(&tr->snapshot_trigger_lock); 9735 #endif 9736 tr->current_trace = &nop_trace; 9737 9738 INIT_LIST_HEAD(&tr->systems); 9739 INIT_LIST_HEAD(&tr->events); 9740 INIT_LIST_HEAD(&tr->hist_vars); 9741 INIT_LIST_HEAD(&tr->err_log); 9742 9743 #ifdef CONFIG_MODULES 9744 INIT_LIST_HEAD(&tr->mod_events); 9745 #endif 9746 9747 if (allocate_trace_buffers(tr, trace_buf_size) < 0) 9748 goto out_free_tr; 9749 9750 /* The ring buffer is defaultly expanded */ 9751 trace_set_ring_buffer_expanded(tr); 9752 9753 if (ftrace_allocate_ftrace_ops(tr) < 0) 9754 goto out_free_tr; 9755 9756 ftrace_init_trace_array(tr); 9757 9758 init_trace_flags_index(tr); 9759 9760 if (trace_instance_dir) { 9761 ret = trace_array_create_dir(tr); 9762 if (ret) 9763 goto out_free_tr; 9764 } else 9765 __trace_early_add_events(tr); 9766 9767 list_add(&tr->list, &ftrace_trace_arrays); 9768 9769 tr->ref++; 9770 9771 return tr; 9772 9773 out_free_tr: 9774 ftrace_free_ftrace_ops(tr); 9775 free_trace_buffers(tr); 9776 free_cpumask_var(tr->pipe_cpumask); 9777 free_cpumask_var(tr->tracing_cpumask); 9778 kfree_const(tr->system_names); 9779 kfree(tr->range_name); 9780 kfree(tr->name); 9781 kfree(tr); 9782 9783 return ERR_PTR(ret); 9784 } 9785 9786 static struct trace_array *trace_array_create(const char *name) 9787 { 9788 return trace_array_create_systems(name, NULL, 0, 0); 9789 } 9790 9791 static int instance_mkdir(const char *name) 9792 { 9793 struct trace_array *tr; 9794 int ret; 9795 9796 guard(mutex)(&event_mutex); 9797 guard(mutex)(&trace_types_lock); 9798 9799 ret = -EEXIST; 9800 if (trace_array_find(name)) 9801 return -EEXIST; 9802 9803 tr = trace_array_create(name); 9804 9805 ret = PTR_ERR_OR_ZERO(tr); 9806 9807 return ret; 9808 } 9809 9810 #ifdef CONFIG_MMU 9811 static u64 map_pages(unsigned long start, unsigned long size) 9812 { 9813 unsigned long vmap_start, vmap_end; 9814 struct vm_struct *area; 9815 int ret; 9816 9817 area = get_vm_area(size, VM_IOREMAP); 9818 if (!area) 9819 return 0; 9820 9821 vmap_start = (unsigned long) area->addr; 9822 vmap_end = vmap_start + size; 9823 9824 ret = vmap_page_range(vmap_start, vmap_end, 9825 start, pgprot_nx(PAGE_KERNEL)); 9826 if (ret < 0) { 9827 free_vm_area(area); 9828 return 0; 9829 } 9830 9831 return (u64)vmap_start; 9832 } 9833 #else 9834 static inline u64 map_pages(unsigned long start, unsigned long size) 9835 { 9836 return 0; 9837 } 9838 #endif 9839 9840 /** 9841 * trace_array_get_by_name - Create/Lookup a trace array, given its name. 9842 * @name: The name of the trace array to be looked up/created. 9843 * @systems: A list of systems to create event directories for (NULL for all) 9844 * 9845 * Returns pointer to trace array with given name. 9846 * NULL, if it cannot be created. 9847 * 9848 * NOTE: This function increments the reference counter associated with the 9849 * trace array returned. This makes sure it cannot be freed while in use. 9850 * Use trace_array_put() once the trace array is no longer needed. 9851 * If the trace_array is to be freed, trace_array_destroy() needs to 9852 * be called after the trace_array_put(), or simply let user space delete 9853 * it from the tracefs instances directory. But until the 9854 * trace_array_put() is called, user space can not delete it. 9855 * 9856 */ 9857 struct trace_array *trace_array_get_by_name(const char *name, const char *systems) 9858 { 9859 struct trace_array *tr; 9860 9861 guard(mutex)(&event_mutex); 9862 guard(mutex)(&trace_types_lock); 9863 9864 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9865 if (tr->name && strcmp(tr->name, name) == 0) { 9866 tr->ref++; 9867 return tr; 9868 } 9869 } 9870 9871 tr = trace_array_create_systems(name, systems, 0, 0); 9872 9873 if (IS_ERR(tr)) 9874 tr = NULL; 9875 else 9876 tr->ref++; 9877 9878 return tr; 9879 } 9880 EXPORT_SYMBOL_GPL(trace_array_get_by_name); 9881 9882 static int __remove_instance(struct trace_array *tr) 9883 { 9884 int i; 9885 9886 /* Reference counter for a newly created trace array = 1. */ 9887 if (tr->ref > 1 || (tr->current_trace && tr->trace_ref)) 9888 return -EBUSY; 9889 9890 list_del(&tr->list); 9891 9892 /* Disable all the flags that were enabled coming in */ 9893 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) { 9894 if ((1 << i) & ZEROED_TRACE_FLAGS) 9895 set_tracer_flag(tr, 1 << i, 0); 9896 } 9897 9898 if (printk_trace == tr) 9899 update_printk_trace(&global_trace); 9900 9901 tracing_set_nop(tr); 9902 clear_ftrace_function_probes(tr); 9903 event_trace_del_tracer(tr); 9904 ftrace_clear_pids(tr); 9905 ftrace_destroy_function_files(tr); 9906 tracefs_remove(tr->dir); 9907 free_percpu(tr->last_func_repeats); 9908 free_trace_buffers(tr); 9909 clear_tracing_err_log(tr); 9910 9911 if (tr->range_name) { 9912 reserve_mem_release_by_name(tr->range_name); 9913 kfree(tr->range_name); 9914 } 9915 9916 for (i = 0; i < tr->nr_topts; i++) { 9917 kfree(tr->topts[i].topts); 9918 } 9919 kfree(tr->topts); 9920 9921 free_cpumask_var(tr->pipe_cpumask); 9922 free_cpumask_var(tr->tracing_cpumask); 9923 kfree_const(tr->system_names); 9924 kfree(tr->name); 9925 kfree(tr); 9926 9927 return 0; 9928 } 9929 9930 int trace_array_destroy(struct trace_array *this_tr) 9931 { 9932 struct trace_array *tr; 9933 9934 if (!this_tr) 9935 return -EINVAL; 9936 9937 guard(mutex)(&event_mutex); 9938 guard(mutex)(&trace_types_lock); 9939 9940 9941 /* Making sure trace array exists before destroying it. */ 9942 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9943 if (tr == this_tr) 9944 return __remove_instance(tr); 9945 } 9946 9947 return -ENODEV; 9948 } 9949 EXPORT_SYMBOL_GPL(trace_array_destroy); 9950 9951 static int instance_rmdir(const char *name) 9952 { 9953 struct trace_array *tr; 9954 9955 guard(mutex)(&event_mutex); 9956 guard(mutex)(&trace_types_lock); 9957 9958 tr = trace_array_find(name); 9959 if (!tr) 9960 return -ENODEV; 9961 9962 return __remove_instance(tr); 9963 } 9964 9965 static __init void create_trace_instances(struct dentry *d_tracer) 9966 { 9967 struct trace_array *tr; 9968 9969 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer, 9970 instance_mkdir, 9971 instance_rmdir); 9972 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n")) 9973 return; 9974 9975 guard(mutex)(&event_mutex); 9976 guard(mutex)(&trace_types_lock); 9977 9978 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 9979 if (!tr->name) 9980 continue; 9981 if (MEM_FAIL(trace_array_create_dir(tr) < 0, 9982 "Failed to create instance directory\n")) 9983 return; 9984 } 9985 } 9986 9987 static void 9988 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer) 9989 { 9990 int cpu; 9991 9992 trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer, 9993 tr, &show_traces_fops); 9994 9995 trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer, 9996 tr, &set_tracer_fops); 9997 9998 trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer, 9999 tr, &tracing_cpumask_fops); 10000 10001 trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer, 10002 tr, &tracing_iter_fops); 10003 10004 trace_create_file("trace", TRACE_MODE_WRITE, d_tracer, 10005 tr, &tracing_fops); 10006 10007 trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer, 10008 tr, &tracing_pipe_fops); 10009 10010 trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer, 10011 tr, &tracing_entries_fops); 10012 10013 trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer, 10014 tr, &tracing_total_entries_fops); 10015 10016 trace_create_file("free_buffer", 0200, d_tracer, 10017 tr, &tracing_free_buffer_fops); 10018 10019 trace_create_file("trace_marker", 0220, d_tracer, 10020 tr, &tracing_mark_fops); 10021 10022 tr->trace_marker_file = __find_event_file(tr, "ftrace", "print"); 10023 10024 trace_create_file("trace_marker_raw", 0220, d_tracer, 10025 tr, &tracing_mark_raw_fops); 10026 10027 trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr, 10028 &trace_clock_fops); 10029 10030 trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer, 10031 tr, &rb_simple_fops); 10032 10033 trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr, 10034 &trace_time_stamp_mode_fops); 10035 10036 tr->buffer_percent = 50; 10037 10038 trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer, 10039 tr, &buffer_percent_fops); 10040 10041 trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer, 10042 tr, &buffer_subbuf_size_fops); 10043 10044 create_trace_options_dir(tr); 10045 10046 #ifdef CONFIG_TRACER_MAX_TRACE 10047 trace_create_maxlat_file(tr, d_tracer); 10048 #endif 10049 10050 if (ftrace_create_function_files(tr, d_tracer)) 10051 MEM_FAIL(1, "Could not allocate function filter files"); 10052 10053 if (tr->range_addr_start) { 10054 trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer, 10055 tr, &last_boot_fops); 10056 #ifdef CONFIG_TRACER_SNAPSHOT 10057 } else { 10058 trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer, 10059 tr, &snapshot_fops); 10060 #endif 10061 } 10062 10063 trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer, 10064 tr, &tracing_err_log_fops); 10065 10066 for_each_tracing_cpu(cpu) 10067 tracing_init_tracefs_percpu(tr, cpu); 10068 10069 ftrace_init_tracefs(tr, d_tracer); 10070 } 10071 10072 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore) 10073 { 10074 struct vfsmount *mnt; 10075 struct file_system_type *type; 10076 10077 /* 10078 * To maintain backward compatibility for tools that mount 10079 * debugfs to get to the tracing facility, tracefs is automatically 10080 * mounted to the debugfs/tracing directory. 10081 */ 10082 type = get_fs_type("tracefs"); 10083 if (!type) 10084 return NULL; 10085 mnt = vfs_submount(mntpt, type, "tracefs", NULL); 10086 put_filesystem(type); 10087 if (IS_ERR(mnt)) 10088 return NULL; 10089 mntget(mnt); 10090 10091 return mnt; 10092 } 10093 10094 /** 10095 * tracing_init_dentry - initialize top level trace array 10096 * 10097 * This is called when creating files or directories in the tracing 10098 * directory. It is called via fs_initcall() by any of the boot up code 10099 * and expects to return the dentry of the top level tracing directory. 10100 */ 10101 int tracing_init_dentry(void) 10102 { 10103 struct trace_array *tr = &global_trace; 10104 10105 if (security_locked_down(LOCKDOWN_TRACEFS)) { 10106 pr_warn("Tracing disabled due to lockdown\n"); 10107 return -EPERM; 10108 } 10109 10110 /* The top level trace array uses NULL as parent */ 10111 if (tr->dir) 10112 return 0; 10113 10114 if (WARN_ON(!tracefs_initialized())) 10115 return -ENODEV; 10116 10117 /* 10118 * As there may still be users that expect the tracing 10119 * files to exist in debugfs/tracing, we must automount 10120 * the tracefs file system there, so older tools still 10121 * work with the newer kernel. 10122 */ 10123 tr->dir = debugfs_create_automount("tracing", NULL, 10124 trace_automount, NULL); 10125 10126 return 0; 10127 } 10128 10129 extern struct trace_eval_map *__start_ftrace_eval_maps[]; 10130 extern struct trace_eval_map *__stop_ftrace_eval_maps[]; 10131 10132 static struct workqueue_struct *eval_map_wq __initdata; 10133 static struct work_struct eval_map_work __initdata; 10134 static struct work_struct tracerfs_init_work __initdata; 10135 10136 static void __init eval_map_work_func(struct work_struct *work) 10137 { 10138 int len; 10139 10140 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps; 10141 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len); 10142 } 10143 10144 static int __init trace_eval_init(void) 10145 { 10146 INIT_WORK(&eval_map_work, eval_map_work_func); 10147 10148 eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0); 10149 if (!eval_map_wq) { 10150 pr_err("Unable to allocate eval_map_wq\n"); 10151 /* Do work here */ 10152 eval_map_work_func(&eval_map_work); 10153 return -ENOMEM; 10154 } 10155 10156 queue_work(eval_map_wq, &eval_map_work); 10157 return 0; 10158 } 10159 10160 subsys_initcall(trace_eval_init); 10161 10162 static int __init trace_eval_sync(void) 10163 { 10164 /* Make sure the eval map updates are finished */ 10165 if (eval_map_wq) 10166 destroy_workqueue(eval_map_wq); 10167 return 0; 10168 } 10169 10170 late_initcall_sync(trace_eval_sync); 10171 10172 10173 #ifdef CONFIG_MODULES 10174 10175 bool module_exists(const char *module) 10176 { 10177 /* All modules have the symbol __this_module */ 10178 static const char this_mod[] = "__this_module"; 10179 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2]; 10180 unsigned long val; 10181 int n; 10182 10183 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod); 10184 10185 if (n > sizeof(modname) - 1) 10186 return false; 10187 10188 val = module_kallsyms_lookup_name(modname); 10189 return val != 0; 10190 } 10191 10192 static void trace_module_add_evals(struct module *mod) 10193 { 10194 if (!mod->num_trace_evals) 10195 return; 10196 10197 /* 10198 * Modules with bad taint do not have events created, do 10199 * not bother with enums either. 10200 */ 10201 if (trace_module_has_bad_taint(mod)) 10202 return; 10203 10204 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals); 10205 } 10206 10207 #ifdef CONFIG_TRACE_EVAL_MAP_FILE 10208 static void trace_module_remove_evals(struct module *mod) 10209 { 10210 union trace_eval_map_item *map; 10211 union trace_eval_map_item **last = &trace_eval_maps; 10212 10213 if (!mod->num_trace_evals) 10214 return; 10215 10216 guard(mutex)(&trace_eval_mutex); 10217 10218 map = trace_eval_maps; 10219 10220 while (map) { 10221 if (map->head.mod == mod) 10222 break; 10223 map = trace_eval_jmp_to_tail(map); 10224 last = &map->tail.next; 10225 map = map->tail.next; 10226 } 10227 if (!map) 10228 return; 10229 10230 *last = trace_eval_jmp_to_tail(map)->tail.next; 10231 kfree(map); 10232 } 10233 #else 10234 static inline void trace_module_remove_evals(struct module *mod) { } 10235 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ 10236 10237 static void trace_module_record(struct module *mod, bool add) 10238 { 10239 struct trace_array *tr; 10240 unsigned long flags; 10241 10242 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 10243 flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT); 10244 /* Update any persistent trace array that has already been started */ 10245 if (flags == TRACE_ARRAY_FL_BOOT && add) { 10246 guard(mutex)(&scratch_mutex); 10247 save_mod(mod, tr); 10248 } else if (flags & TRACE_ARRAY_FL_LAST_BOOT) { 10249 /* Update delta if the module loaded in previous boot */ 10250 make_mod_delta(mod, tr); 10251 } 10252 } 10253 } 10254 10255 static int trace_module_notify(struct notifier_block *self, 10256 unsigned long val, void *data) 10257 { 10258 struct module *mod = data; 10259 10260 switch (val) { 10261 case MODULE_STATE_COMING: 10262 trace_module_add_evals(mod); 10263 trace_module_record(mod, true); 10264 break; 10265 case MODULE_STATE_GOING: 10266 trace_module_remove_evals(mod); 10267 trace_module_record(mod, false); 10268 break; 10269 } 10270 10271 return NOTIFY_OK; 10272 } 10273 10274 static struct notifier_block trace_module_nb = { 10275 .notifier_call = trace_module_notify, 10276 .priority = 0, 10277 }; 10278 #endif /* CONFIG_MODULES */ 10279 10280 static __init void tracer_init_tracefs_work_func(struct work_struct *work) 10281 { 10282 10283 event_trace_init(); 10284 10285 init_tracer_tracefs(&global_trace, NULL); 10286 ftrace_init_tracefs_toplevel(&global_trace, NULL); 10287 10288 trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL, 10289 &global_trace, &tracing_thresh_fops); 10290 10291 trace_create_file("README", TRACE_MODE_READ, NULL, 10292 NULL, &tracing_readme_fops); 10293 10294 trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL, 10295 NULL, &tracing_saved_cmdlines_fops); 10296 10297 trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL, 10298 NULL, &tracing_saved_cmdlines_size_fops); 10299 10300 trace_create_file("saved_tgids", TRACE_MODE_READ, NULL, 10301 NULL, &tracing_saved_tgids_fops); 10302 10303 trace_create_eval_file(NULL); 10304 10305 #ifdef CONFIG_MODULES 10306 register_module_notifier(&trace_module_nb); 10307 #endif 10308 10309 #ifdef CONFIG_DYNAMIC_FTRACE 10310 trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL, 10311 NULL, &tracing_dyn_info_fops); 10312 #endif 10313 10314 create_trace_instances(NULL); 10315 10316 update_tracer_options(&global_trace); 10317 } 10318 10319 static __init int tracer_init_tracefs(void) 10320 { 10321 int ret; 10322 10323 trace_access_lock_init(); 10324 10325 ret = tracing_init_dentry(); 10326 if (ret) 10327 return 0; 10328 10329 if (eval_map_wq) { 10330 INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func); 10331 queue_work(eval_map_wq, &tracerfs_init_work); 10332 } else { 10333 tracer_init_tracefs_work_func(NULL); 10334 } 10335 10336 rv_init_interface(); 10337 10338 return 0; 10339 } 10340 10341 fs_initcall(tracer_init_tracefs); 10342 10343 static int trace_die_panic_handler(struct notifier_block *self, 10344 unsigned long ev, void *unused); 10345 10346 static struct notifier_block trace_panic_notifier = { 10347 .notifier_call = trace_die_panic_handler, 10348 .priority = INT_MAX - 1, 10349 }; 10350 10351 static struct notifier_block trace_die_notifier = { 10352 .notifier_call = trace_die_panic_handler, 10353 .priority = INT_MAX - 1, 10354 }; 10355 10356 /* 10357 * The idea is to execute the following die/panic callback early, in order 10358 * to avoid showing irrelevant information in the trace (like other panic 10359 * notifier functions); we are the 2nd to run, after hung_task/rcu_stall 10360 * warnings get disabled (to prevent potential log flooding). 10361 */ 10362 static int trace_die_panic_handler(struct notifier_block *self, 10363 unsigned long ev, void *unused) 10364 { 10365 if (!ftrace_dump_on_oops_enabled()) 10366 return NOTIFY_DONE; 10367 10368 /* The die notifier requires DIE_OOPS to trigger */ 10369 if (self == &trace_die_notifier && ev != DIE_OOPS) 10370 return NOTIFY_DONE; 10371 10372 ftrace_dump(DUMP_PARAM); 10373 10374 return NOTIFY_DONE; 10375 } 10376 10377 /* 10378 * printk is set to max of 1024, we really don't need it that big. 10379 * Nothing should be printing 1000 characters anyway. 10380 */ 10381 #define TRACE_MAX_PRINT 1000 10382 10383 /* 10384 * Define here KERN_TRACE so that we have one place to modify 10385 * it if we decide to change what log level the ftrace dump 10386 * should be at. 10387 */ 10388 #define KERN_TRACE KERN_EMERG 10389 10390 void 10391 trace_printk_seq(struct trace_seq *s) 10392 { 10393 /* Probably should print a warning here. */ 10394 if (s->seq.len >= TRACE_MAX_PRINT) 10395 s->seq.len = TRACE_MAX_PRINT; 10396 10397 /* 10398 * More paranoid code. Although the buffer size is set to 10399 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just 10400 * an extra layer of protection. 10401 */ 10402 if (WARN_ON_ONCE(s->seq.len >= s->seq.size)) 10403 s->seq.len = s->seq.size - 1; 10404 10405 /* should be zero ended, but we are paranoid. */ 10406 s->buffer[s->seq.len] = 0; 10407 10408 printk(KERN_TRACE "%s", s->buffer); 10409 10410 trace_seq_init(s); 10411 } 10412 10413 static void trace_init_iter(struct trace_iterator *iter, struct trace_array *tr) 10414 { 10415 iter->tr = tr; 10416 iter->trace = iter->tr->current_trace; 10417 iter->cpu_file = RING_BUFFER_ALL_CPUS; 10418 iter->array_buffer = &tr->array_buffer; 10419 10420 if (iter->trace && iter->trace->open) 10421 iter->trace->open(iter); 10422 10423 /* Annotate start of buffers if we had overruns */ 10424 if (ring_buffer_overruns(iter->array_buffer->buffer)) 10425 iter->iter_flags |= TRACE_FILE_ANNOTATE; 10426 10427 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 10428 if (trace_clocks[iter->tr->clock_id].in_ns) 10429 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 10430 10431 /* Can not use kmalloc for iter.temp and iter.fmt */ 10432 iter->temp = static_temp_buf; 10433 iter->temp_size = STATIC_TEMP_BUF_SIZE; 10434 iter->fmt = static_fmt_buf; 10435 iter->fmt_size = STATIC_FMT_BUF_SIZE; 10436 } 10437 10438 void trace_init_global_iter(struct trace_iterator *iter) 10439 { 10440 trace_init_iter(iter, &global_trace); 10441 } 10442 10443 static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mode) 10444 { 10445 /* use static because iter can be a bit big for the stack */ 10446 static struct trace_iterator iter; 10447 unsigned int old_userobj; 10448 unsigned long flags; 10449 int cnt = 0, cpu; 10450 10451 /* 10452 * Always turn off tracing when we dump. 10453 * We don't need to show trace output of what happens 10454 * between multiple crashes. 10455 * 10456 * If the user does a sysrq-z, then they can re-enable 10457 * tracing with echo 1 > tracing_on. 10458 */ 10459 tracer_tracing_off(tr); 10460 10461 local_irq_save(flags); 10462 10463 /* Simulate the iterator */ 10464 trace_init_iter(&iter, tr); 10465 10466 for_each_tracing_cpu(cpu) { 10467 atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); 10468 } 10469 10470 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ; 10471 10472 /* don't look at user memory in panic mode */ 10473 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ; 10474 10475 if (dump_mode == DUMP_ORIG) 10476 iter.cpu_file = raw_smp_processor_id(); 10477 else 10478 iter.cpu_file = RING_BUFFER_ALL_CPUS; 10479 10480 if (tr == &global_trace) 10481 printk(KERN_TRACE "Dumping ftrace buffer:\n"); 10482 else 10483 printk(KERN_TRACE "Dumping ftrace instance %s buffer:\n", tr->name); 10484 10485 /* Did function tracer already get disabled? */ 10486 if (ftrace_is_dead()) { 10487 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); 10488 printk("# MAY BE MISSING FUNCTION EVENTS\n"); 10489 } 10490 10491 /* 10492 * We need to stop all tracing on all CPUS to read 10493 * the next buffer. This is a bit expensive, but is 10494 * not done often. We fill all what we can read, 10495 * and then release the locks again. 10496 */ 10497 10498 while (!trace_empty(&iter)) { 10499 10500 if (!cnt) 10501 printk(KERN_TRACE "---------------------------------\n"); 10502 10503 cnt++; 10504 10505 trace_iterator_reset(&iter); 10506 iter.iter_flags |= TRACE_FILE_LAT_FMT; 10507 10508 if (trace_find_next_entry_inc(&iter) != NULL) { 10509 int ret; 10510 10511 ret = print_trace_line(&iter); 10512 if (ret != TRACE_TYPE_NO_CONSUME) 10513 trace_consume(&iter); 10514 } 10515 touch_nmi_watchdog(); 10516 10517 trace_printk_seq(&iter.seq); 10518 } 10519 10520 if (!cnt) 10521 printk(KERN_TRACE " (ftrace buffer empty)\n"); 10522 else 10523 printk(KERN_TRACE "---------------------------------\n"); 10524 10525 tr->trace_flags |= old_userobj; 10526 10527 for_each_tracing_cpu(cpu) { 10528 atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled); 10529 } 10530 local_irq_restore(flags); 10531 } 10532 10533 static void ftrace_dump_by_param(void) 10534 { 10535 bool first_param = true; 10536 char dump_param[MAX_TRACER_SIZE]; 10537 char *buf, *token, *inst_name; 10538 struct trace_array *tr; 10539 10540 strscpy(dump_param, ftrace_dump_on_oops, MAX_TRACER_SIZE); 10541 buf = dump_param; 10542 10543 while ((token = strsep(&buf, ",")) != NULL) { 10544 if (first_param) { 10545 first_param = false; 10546 if (!strcmp("0", token)) 10547 continue; 10548 else if (!strcmp("1", token)) { 10549 ftrace_dump_one(&global_trace, DUMP_ALL); 10550 continue; 10551 } 10552 else if (!strcmp("2", token) || 10553 !strcmp("orig_cpu", token)) { 10554 ftrace_dump_one(&global_trace, DUMP_ORIG); 10555 continue; 10556 } 10557 } 10558 10559 inst_name = strsep(&token, "="); 10560 tr = trace_array_find(inst_name); 10561 if (!tr) { 10562 printk(KERN_TRACE "Instance %s not found\n", inst_name); 10563 continue; 10564 } 10565 10566 if (token && (!strcmp("2", token) || 10567 !strcmp("orig_cpu", token))) 10568 ftrace_dump_one(tr, DUMP_ORIG); 10569 else 10570 ftrace_dump_one(tr, DUMP_ALL); 10571 } 10572 } 10573 10574 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) 10575 { 10576 static atomic_t dump_running; 10577 10578 /* Only allow one dump user at a time. */ 10579 if (atomic_inc_return(&dump_running) != 1) { 10580 atomic_dec(&dump_running); 10581 return; 10582 } 10583 10584 switch (oops_dump_mode) { 10585 case DUMP_ALL: 10586 ftrace_dump_one(&global_trace, DUMP_ALL); 10587 break; 10588 case DUMP_ORIG: 10589 ftrace_dump_one(&global_trace, DUMP_ORIG); 10590 break; 10591 case DUMP_PARAM: 10592 ftrace_dump_by_param(); 10593 break; 10594 case DUMP_NONE: 10595 break; 10596 default: 10597 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); 10598 ftrace_dump_one(&global_trace, DUMP_ALL); 10599 } 10600 10601 atomic_dec(&dump_running); 10602 } 10603 EXPORT_SYMBOL_GPL(ftrace_dump); 10604 10605 #define WRITE_BUFSIZE 4096 10606 10607 ssize_t trace_parse_run_command(struct file *file, const char __user *buffer, 10608 size_t count, loff_t *ppos, 10609 int (*createfn)(const char *)) 10610 { 10611 char *kbuf, *buf, *tmp; 10612 int ret = 0; 10613 size_t done = 0; 10614 size_t size; 10615 10616 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); 10617 if (!kbuf) 10618 return -ENOMEM; 10619 10620 while (done < count) { 10621 size = count - done; 10622 10623 if (size >= WRITE_BUFSIZE) 10624 size = WRITE_BUFSIZE - 1; 10625 10626 if (copy_from_user(kbuf, buffer + done, size)) { 10627 ret = -EFAULT; 10628 goto out; 10629 } 10630 kbuf[size] = '\0'; 10631 buf = kbuf; 10632 do { 10633 tmp = strchr(buf, '\n'); 10634 if (tmp) { 10635 *tmp = '\0'; 10636 size = tmp - buf + 1; 10637 } else { 10638 size = strlen(buf); 10639 if (done + size < count) { 10640 if (buf != kbuf) 10641 break; 10642 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */ 10643 pr_warn("Line length is too long: Should be less than %d\n", 10644 WRITE_BUFSIZE - 2); 10645 ret = -EINVAL; 10646 goto out; 10647 } 10648 } 10649 done += size; 10650 10651 /* Remove comments */ 10652 tmp = strchr(buf, '#'); 10653 10654 if (tmp) 10655 *tmp = '\0'; 10656 10657 ret = createfn(buf); 10658 if (ret) 10659 goto out; 10660 buf += size; 10661 10662 } while (done < count); 10663 } 10664 ret = done; 10665 10666 out: 10667 kfree(kbuf); 10668 10669 return ret; 10670 } 10671 10672 #ifdef CONFIG_TRACER_MAX_TRACE 10673 __init static bool tr_needs_alloc_snapshot(const char *name) 10674 { 10675 char *test; 10676 int len = strlen(name); 10677 bool ret; 10678 10679 if (!boot_snapshot_index) 10680 return false; 10681 10682 if (strncmp(name, boot_snapshot_info, len) == 0 && 10683 boot_snapshot_info[len] == '\t') 10684 return true; 10685 10686 test = kmalloc(strlen(name) + 3, GFP_KERNEL); 10687 if (!test) 10688 return false; 10689 10690 sprintf(test, "\t%s\t", name); 10691 ret = strstr(boot_snapshot_info, test) == NULL; 10692 kfree(test); 10693 return ret; 10694 } 10695 10696 __init static void do_allocate_snapshot(const char *name) 10697 { 10698 if (!tr_needs_alloc_snapshot(name)) 10699 return; 10700 10701 /* 10702 * When allocate_snapshot is set, the next call to 10703 * allocate_trace_buffers() (called by trace_array_get_by_name()) 10704 * will allocate the snapshot buffer. That will alse clear 10705 * this flag. 10706 */ 10707 allocate_snapshot = true; 10708 } 10709 #else 10710 static inline void do_allocate_snapshot(const char *name) { } 10711 #endif 10712 10713 __init static void enable_instances(void) 10714 { 10715 struct trace_array *tr; 10716 bool memmap_area = false; 10717 char *curr_str; 10718 char *name; 10719 char *str; 10720 char *tok; 10721 10722 /* A tab is always appended */ 10723 boot_instance_info[boot_instance_index - 1] = '\0'; 10724 str = boot_instance_info; 10725 10726 while ((curr_str = strsep(&str, "\t"))) { 10727 phys_addr_t start = 0; 10728 phys_addr_t size = 0; 10729 unsigned long addr = 0; 10730 bool traceprintk = false; 10731 bool traceoff = false; 10732 char *flag_delim; 10733 char *addr_delim; 10734 char *rname __free(kfree) = NULL; 10735 10736 tok = strsep(&curr_str, ","); 10737 10738 flag_delim = strchr(tok, '^'); 10739 addr_delim = strchr(tok, '@'); 10740 10741 if (addr_delim) 10742 *addr_delim++ = '\0'; 10743 10744 if (flag_delim) 10745 *flag_delim++ = '\0'; 10746 10747 name = tok; 10748 10749 if (flag_delim) { 10750 char *flag; 10751 10752 while ((flag = strsep(&flag_delim, "^"))) { 10753 if (strcmp(flag, "traceoff") == 0) { 10754 traceoff = true; 10755 } else if ((strcmp(flag, "printk") == 0) || 10756 (strcmp(flag, "traceprintk") == 0) || 10757 (strcmp(flag, "trace_printk") == 0)) { 10758 traceprintk = true; 10759 } else { 10760 pr_info("Tracing: Invalid instance flag '%s' for %s\n", 10761 flag, name); 10762 } 10763 } 10764 } 10765 10766 tok = addr_delim; 10767 if (tok && isdigit(*tok)) { 10768 start = memparse(tok, &tok); 10769 if (!start) { 10770 pr_warn("Tracing: Invalid boot instance address for %s\n", 10771 name); 10772 continue; 10773 } 10774 if (*tok != ':') { 10775 pr_warn("Tracing: No size specified for instance %s\n", name); 10776 continue; 10777 } 10778 tok++; 10779 size = memparse(tok, &tok); 10780 if (!size) { 10781 pr_warn("Tracing: Invalid boot instance size for %s\n", 10782 name); 10783 continue; 10784 } 10785 memmap_area = true; 10786 } else if (tok) { 10787 if (!reserve_mem_find_by_name(tok, &start, &size)) { 10788 start = 0; 10789 pr_warn("Failed to map boot instance %s to %s\n", name, tok); 10790 continue; 10791 } 10792 rname = kstrdup(tok, GFP_KERNEL); 10793 } 10794 10795 if (start) { 10796 /* Start and size must be page aligned */ 10797 if (start & ~PAGE_MASK) { 10798 pr_warn("Tracing: mapping start addr %pa is not page aligned\n", &start); 10799 continue; 10800 } 10801 if (size & ~PAGE_MASK) { 10802 pr_warn("Tracing: mapping size %pa is not page aligned\n", &size); 10803 continue; 10804 } 10805 10806 if (memmap_area) 10807 addr = map_pages(start, size); 10808 else 10809 addr = (unsigned long)phys_to_virt(start); 10810 if (addr) { 10811 pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n", 10812 name, &start, (unsigned long)size); 10813 } else { 10814 pr_warn("Tracing: Failed to map boot instance %s\n", name); 10815 continue; 10816 } 10817 } else { 10818 /* Only non mapped buffers have snapshot buffers */ 10819 if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE)) 10820 do_allocate_snapshot(name); 10821 } 10822 10823 tr = trace_array_create_systems(name, NULL, addr, size); 10824 if (IS_ERR(tr)) { 10825 pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str); 10826 continue; 10827 } 10828 10829 if (traceoff) 10830 tracer_tracing_off(tr); 10831 10832 if (traceprintk) 10833 update_printk_trace(tr); 10834 10835 /* 10836 * memmap'd buffers can not be freed. 10837 */ 10838 if (memmap_area) { 10839 tr->flags |= TRACE_ARRAY_FL_MEMMAP; 10840 tr->ref++; 10841 } 10842 10843 if (start) { 10844 tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT; 10845 tr->range_name = no_free_ptr(rname); 10846 } 10847 10848 while ((tok = strsep(&curr_str, ","))) { 10849 early_enable_events(tr, tok, true); 10850 } 10851 } 10852 } 10853 10854 __init static int tracer_alloc_buffers(void) 10855 { 10856 int ring_buf_size; 10857 int ret = -ENOMEM; 10858 10859 10860 if (security_locked_down(LOCKDOWN_TRACEFS)) { 10861 pr_warn("Tracing disabled due to lockdown\n"); 10862 return -EPERM; 10863 } 10864 10865 /* 10866 * Make sure we don't accidentally add more trace options 10867 * than we have bits for. 10868 */ 10869 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE); 10870 10871 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) 10872 goto out; 10873 10874 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL)) 10875 goto out_free_buffer_mask; 10876 10877 /* Only allocate trace_printk buffers if a trace_printk exists */ 10878 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt) 10879 /* Must be called before global_trace.buffer is allocated */ 10880 trace_printk_init_buffers(); 10881 10882 /* To save memory, keep the ring buffer size to its minimum */ 10883 if (global_trace.ring_buffer_expanded) 10884 ring_buf_size = trace_buf_size; 10885 else 10886 ring_buf_size = 1; 10887 10888 cpumask_copy(tracing_buffer_mask, cpu_possible_mask); 10889 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask); 10890 10891 raw_spin_lock_init(&global_trace.start_lock); 10892 10893 /* 10894 * The prepare callbacks allocates some memory for the ring buffer. We 10895 * don't free the buffer if the CPU goes down. If we were to free 10896 * the buffer, then the user would lose any trace that was in the 10897 * buffer. The memory will be removed once the "instance" is removed. 10898 */ 10899 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE, 10900 "trace/RB:prepare", trace_rb_cpu_prepare, 10901 NULL); 10902 if (ret < 0) 10903 goto out_free_cpumask; 10904 /* Used for event triggers */ 10905 ret = -ENOMEM; 10906 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE); 10907 if (!temp_buffer) 10908 goto out_rm_hp_state; 10909 10910 if (trace_create_savedcmd() < 0) 10911 goto out_free_temp_buffer; 10912 10913 if (!zalloc_cpumask_var(&global_trace.pipe_cpumask, GFP_KERNEL)) 10914 goto out_free_savedcmd; 10915 10916 /* TODO: make the number of buffers hot pluggable with CPUS */ 10917 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) { 10918 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n"); 10919 goto out_free_pipe_cpumask; 10920 } 10921 if (global_trace.buffer_disabled) 10922 tracing_off(); 10923 10924 if (trace_boot_clock) { 10925 ret = tracing_set_clock(&global_trace, trace_boot_clock); 10926 if (ret < 0) 10927 pr_warn("Trace clock %s not defined, going back to default\n", 10928 trace_boot_clock); 10929 } 10930 10931 /* 10932 * register_tracer() might reference current_trace, so it 10933 * needs to be set before we register anything. This is 10934 * just a bootstrap of current_trace anyway. 10935 */ 10936 global_trace.current_trace = &nop_trace; 10937 10938 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 10939 #ifdef CONFIG_TRACER_MAX_TRACE 10940 spin_lock_init(&global_trace.snapshot_trigger_lock); 10941 #endif 10942 ftrace_init_global_array_ops(&global_trace); 10943 10944 #ifdef CONFIG_MODULES 10945 INIT_LIST_HEAD(&global_trace.mod_events); 10946 #endif 10947 10948 init_trace_flags_index(&global_trace); 10949 10950 register_tracer(&nop_trace); 10951 10952 /* Function tracing may start here (via kernel command line) */ 10953 init_function_trace(); 10954 10955 /* All seems OK, enable tracing */ 10956 tracing_disabled = 0; 10957 10958 atomic_notifier_chain_register(&panic_notifier_list, 10959 &trace_panic_notifier); 10960 10961 register_die_notifier(&trace_die_notifier); 10962 10963 global_trace.flags = TRACE_ARRAY_FL_GLOBAL; 10964 10965 INIT_LIST_HEAD(&global_trace.systems); 10966 INIT_LIST_HEAD(&global_trace.events); 10967 INIT_LIST_HEAD(&global_trace.hist_vars); 10968 INIT_LIST_HEAD(&global_trace.err_log); 10969 list_add(&global_trace.list, &ftrace_trace_arrays); 10970 10971 apply_trace_boot_options(); 10972 10973 register_snapshot_cmd(); 10974 10975 return 0; 10976 10977 out_free_pipe_cpumask: 10978 free_cpumask_var(global_trace.pipe_cpumask); 10979 out_free_savedcmd: 10980 trace_free_saved_cmdlines_buffer(); 10981 out_free_temp_buffer: 10982 ring_buffer_free(temp_buffer); 10983 out_rm_hp_state: 10984 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE); 10985 out_free_cpumask: 10986 free_cpumask_var(global_trace.tracing_cpumask); 10987 out_free_buffer_mask: 10988 free_cpumask_var(tracing_buffer_mask); 10989 out: 10990 return ret; 10991 } 10992 10993 #ifdef CONFIG_FUNCTION_TRACER 10994 /* Used to set module cached ftrace filtering at boot up */ 10995 __init struct trace_array *trace_get_global_array(void) 10996 { 10997 return &global_trace; 10998 } 10999 #endif 11000 11001 void __init ftrace_boot_snapshot(void) 11002 { 11003 #ifdef CONFIG_TRACER_MAX_TRACE 11004 struct trace_array *tr; 11005 11006 if (!snapshot_at_boot) 11007 return; 11008 11009 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 11010 if (!tr->allocated_snapshot) 11011 continue; 11012 11013 tracing_snapshot_instance(tr); 11014 trace_array_puts(tr, "** Boot snapshot taken **\n"); 11015 } 11016 #endif 11017 } 11018 11019 void __init early_trace_init(void) 11020 { 11021 if (tracepoint_printk) { 11022 tracepoint_print_iter = 11023 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL); 11024 if (MEM_FAIL(!tracepoint_print_iter, 11025 "Failed to allocate trace iterator\n")) 11026 tracepoint_printk = 0; 11027 else 11028 static_key_enable(&tracepoint_printk_key.key); 11029 } 11030 tracer_alloc_buffers(); 11031 11032 init_events(); 11033 } 11034 11035 void __init trace_init(void) 11036 { 11037 trace_event_init(); 11038 11039 if (boot_instance_index) 11040 enable_instances(); 11041 } 11042 11043 __init static void clear_boot_tracer(void) 11044 { 11045 /* 11046 * The default tracer at boot buffer is an init section. 11047 * This function is called in lateinit. If we did not 11048 * find the boot tracer, then clear it out, to prevent 11049 * later registration from accessing the buffer that is 11050 * about to be freed. 11051 */ 11052 if (!default_bootup_tracer) 11053 return; 11054 11055 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", 11056 default_bootup_tracer); 11057 default_bootup_tracer = NULL; 11058 } 11059 11060 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK 11061 __init static void tracing_set_default_clock(void) 11062 { 11063 /* sched_clock_stable() is determined in late_initcall */ 11064 if (!trace_boot_clock && !sched_clock_stable()) { 11065 if (security_locked_down(LOCKDOWN_TRACEFS)) { 11066 pr_warn("Can not set tracing clock due to lockdown\n"); 11067 return; 11068 } 11069 11070 printk(KERN_WARNING 11071 "Unstable clock detected, switching default tracing clock to \"global\"\n" 11072 "If you want to keep using the local clock, then add:\n" 11073 " \"trace_clock=local\"\n" 11074 "on the kernel command line\n"); 11075 tracing_set_clock(&global_trace, "global"); 11076 } 11077 } 11078 #else 11079 static inline void tracing_set_default_clock(void) { } 11080 #endif 11081 11082 __init static int late_trace_init(void) 11083 { 11084 if (tracepoint_printk && tracepoint_printk_stop_on_boot) { 11085 static_key_disable(&tracepoint_printk_key.key); 11086 tracepoint_printk = 0; 11087 } 11088 11089 if (traceoff_after_boot) 11090 tracing_off(); 11091 11092 tracing_set_default_clock(); 11093 clear_boot_tracer(); 11094 return 0; 11095 } 11096 11097 late_initcall_sync(late_trace_init); 11098