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