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