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