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