1 /* 2 * ring buffer based function tracer 3 * 4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com> 5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 6 * 7 * Originally taken from the RT patch by: 8 * Arnaldo Carvalho de Melo <acme@redhat.com> 9 * 10 * Based on code from the latency_tracer, that is: 11 * Copyright (C) 2004-2006 Ingo Molnar 12 * Copyright (C) 2004 Nadia Yvette Chambers 13 */ 14 #include <linux/ring_buffer.h> 15 #include <generated/utsrelease.h> 16 #include <linux/stacktrace.h> 17 #include <linux/writeback.h> 18 #include <linux/kallsyms.h> 19 #include <linux/seq_file.h> 20 #include <linux/notifier.h> 21 #include <linux/irqflags.h> 22 #include <linux/debugfs.h> 23 #include <linux/pagemap.h> 24 #include <linux/hardirq.h> 25 #include <linux/linkage.h> 26 #include <linux/uaccess.h> 27 #include <linux/kprobes.h> 28 #include <linux/ftrace.h> 29 #include <linux/module.h> 30 #include <linux/percpu.h> 31 #include <linux/splice.h> 32 #include <linux/kdebug.h> 33 #include <linux/string.h> 34 #include <linux/rwsem.h> 35 #include <linux/slab.h> 36 #include <linux/ctype.h> 37 #include <linux/init.h> 38 #include <linux/poll.h> 39 #include <linux/nmi.h> 40 #include <linux/fs.h> 41 #include <linux/sched/rt.h> 42 43 #include "trace.h" 44 #include "trace_output.h" 45 46 /* 47 * On boot up, the ring buffer is set to the minimum size, so that 48 * we do not waste memory on systems that are not using tracing. 49 */ 50 bool ring_buffer_expanded; 51 52 /* 53 * We need to change this state when a selftest is running. 54 * A selftest will lurk into the ring-buffer to count the 55 * entries inserted during the selftest although some concurrent 56 * insertions into the ring-buffer such as trace_printk could occurred 57 * at the same time, giving false positive or negative results. 58 */ 59 static bool __read_mostly tracing_selftest_running; 60 61 /* 62 * If a tracer is running, we do not want to run SELFTEST. 63 */ 64 bool __read_mostly tracing_selftest_disabled; 65 66 /* For tracers that don't implement custom flags */ 67 static struct tracer_opt dummy_tracer_opt[] = { 68 { } 69 }; 70 71 static struct tracer_flags dummy_tracer_flags = { 72 .val = 0, 73 .opts = dummy_tracer_opt 74 }; 75 76 static int dummy_set_flag(u32 old_flags, u32 bit, int set) 77 { 78 return 0; 79 } 80 81 /* 82 * To prevent the comm cache from being overwritten when no 83 * tracing is active, only save the comm when a trace event 84 * occurred. 85 */ 86 static DEFINE_PER_CPU(bool, trace_cmdline_save); 87 88 /* 89 * Kill all tracing for good (never come back). 90 * It is initialized to 1 but will turn to zero if the initialization 91 * of the tracer is successful. But that is the only place that sets 92 * this back to zero. 93 */ 94 static int tracing_disabled = 1; 95 96 DEFINE_PER_CPU(int, ftrace_cpu_disabled); 97 98 cpumask_var_t __read_mostly tracing_buffer_mask; 99 100 /* 101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops 102 * 103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops 104 * is set, then ftrace_dump is called. This will output the contents 105 * of the ftrace buffers to the console. This is very useful for 106 * capturing traces that lead to crashes and outputing it to a 107 * serial console. 108 * 109 * It is default off, but you can enable it with either specifying 110 * "ftrace_dump_on_oops" in the kernel command line, or setting 111 * /proc/sys/kernel/ftrace_dump_on_oops 112 * Set 1 if you want to dump buffers of all CPUs 113 * Set 2 if you want to dump the buffer of the CPU that triggered oops 114 */ 115 116 enum ftrace_dump_mode ftrace_dump_on_oops; 117 118 static int tracing_set_tracer(const char *buf); 119 120 #define MAX_TRACER_SIZE 100 121 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; 122 static char *default_bootup_tracer; 123 124 static bool allocate_snapshot; 125 126 static int __init set_cmdline_ftrace(char *str) 127 { 128 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); 129 default_bootup_tracer = bootup_tracer_buf; 130 /* We are using ftrace early, expand it */ 131 ring_buffer_expanded = true; 132 return 1; 133 } 134 __setup("ftrace=", set_cmdline_ftrace); 135 136 static int __init set_ftrace_dump_on_oops(char *str) 137 { 138 if (*str++ != '=' || !*str) { 139 ftrace_dump_on_oops = DUMP_ALL; 140 return 1; 141 } 142 143 if (!strcmp("orig_cpu", str)) { 144 ftrace_dump_on_oops = DUMP_ORIG; 145 return 1; 146 } 147 148 return 0; 149 } 150 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); 151 152 static int __init boot_alloc_snapshot(char *str) 153 { 154 allocate_snapshot = true; 155 /* We also need the main ring buffer expanded */ 156 ring_buffer_expanded = true; 157 return 1; 158 } 159 __setup("alloc_snapshot", boot_alloc_snapshot); 160 161 162 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; 163 static char *trace_boot_options __initdata; 164 165 static int __init set_trace_boot_options(char *str) 166 { 167 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE); 168 trace_boot_options = trace_boot_options_buf; 169 return 0; 170 } 171 __setup("trace_options=", set_trace_boot_options); 172 173 unsigned long long ns2usecs(cycle_t nsec) 174 { 175 nsec += 500; 176 do_div(nsec, 1000); 177 return nsec; 178 } 179 180 /* 181 * The global_trace is the descriptor that holds the tracing 182 * buffers for the live tracing. For each CPU, it contains 183 * a link list of pages that will store trace entries. The 184 * page descriptor of the pages in the memory is used to hold 185 * the link list by linking the lru item in the page descriptor 186 * to each of the pages in the buffer per CPU. 187 * 188 * For each active CPU there is a data field that holds the 189 * pages for the buffer for that CPU. Each CPU has the same number 190 * of pages allocated for its buffer. 191 */ 192 static struct trace_array global_trace; 193 194 LIST_HEAD(ftrace_trace_arrays); 195 196 int filter_current_check_discard(struct ring_buffer *buffer, 197 struct ftrace_event_call *call, void *rec, 198 struct ring_buffer_event *event) 199 { 200 return filter_check_discard(call, rec, buffer, event); 201 } 202 EXPORT_SYMBOL_GPL(filter_current_check_discard); 203 204 cycle_t ftrace_now(int cpu) 205 { 206 u64 ts; 207 208 /* Early boot up does not have a buffer yet */ 209 if (!global_trace.trace_buffer.buffer) 210 return trace_clock_local(); 211 212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu); 213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts); 214 215 return ts; 216 } 217 218 int tracing_is_enabled(void) 219 { 220 return tracing_is_on(); 221 } 222 223 /* 224 * trace_buf_size is the size in bytes that is allocated 225 * for a buffer. Note, the number of bytes is always rounded 226 * to page size. 227 * 228 * This number is purposely set to a low number of 16384. 229 * If the dump on oops happens, it will be much appreciated 230 * to not have to wait for all that output. Anyway this can be 231 * boot time and run time configurable. 232 */ 233 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ 234 235 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; 236 237 /* trace_types holds a link list of available tracers. */ 238 static struct tracer *trace_types __read_mostly; 239 240 /* 241 * trace_types_lock is used to protect the trace_types list. 242 */ 243 static DEFINE_MUTEX(trace_types_lock); 244 245 /* 246 * serialize the access of the ring buffer 247 * 248 * ring buffer serializes readers, but it is low level protection. 249 * The validity of the events (which returns by ring_buffer_peek() ..etc) 250 * are not protected by ring buffer. 251 * 252 * The content of events may become garbage if we allow other process consumes 253 * these events concurrently: 254 * A) the page of the consumed events may become a normal page 255 * (not reader page) in ring buffer, and this page will be rewrited 256 * by events producer. 257 * B) The page of the consumed events may become a page for splice_read, 258 * and this page will be returned to system. 259 * 260 * These primitives allow multi process access to different cpu ring buffer 261 * concurrently. 262 * 263 * These primitives don't distinguish read-only and read-consume access. 264 * Multi read-only access are also serialized. 265 */ 266 267 #ifdef CONFIG_SMP 268 static DECLARE_RWSEM(all_cpu_access_lock); 269 static DEFINE_PER_CPU(struct mutex, cpu_access_lock); 270 271 static inline void trace_access_lock(int cpu) 272 { 273 if (cpu == RING_BUFFER_ALL_CPUS) { 274 /* gain it for accessing the whole ring buffer. */ 275 down_write(&all_cpu_access_lock); 276 } else { 277 /* gain it for accessing a cpu ring buffer. */ 278 279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */ 280 down_read(&all_cpu_access_lock); 281 282 /* Secondly block other access to this @cpu ring buffer. */ 283 mutex_lock(&per_cpu(cpu_access_lock, cpu)); 284 } 285 } 286 287 static inline void trace_access_unlock(int cpu) 288 { 289 if (cpu == RING_BUFFER_ALL_CPUS) { 290 up_write(&all_cpu_access_lock); 291 } else { 292 mutex_unlock(&per_cpu(cpu_access_lock, cpu)); 293 up_read(&all_cpu_access_lock); 294 } 295 } 296 297 static inline void trace_access_lock_init(void) 298 { 299 int cpu; 300 301 for_each_possible_cpu(cpu) 302 mutex_init(&per_cpu(cpu_access_lock, cpu)); 303 } 304 305 #else 306 307 static DEFINE_MUTEX(access_lock); 308 309 static inline void trace_access_lock(int cpu) 310 { 311 (void)cpu; 312 mutex_lock(&access_lock); 313 } 314 315 static inline void trace_access_unlock(int cpu) 316 { 317 (void)cpu; 318 mutex_unlock(&access_lock); 319 } 320 321 static inline void trace_access_lock_init(void) 322 { 323 } 324 325 #endif 326 327 /* trace_flags holds trace_options default values */ 328 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | 329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME | 330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | 331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION; 332 333 /** 334 * tracing_on - enable tracing buffers 335 * 336 * This function enables tracing buffers that may have been 337 * disabled with tracing_off. 338 */ 339 void tracing_on(void) 340 { 341 if (global_trace.trace_buffer.buffer) 342 ring_buffer_record_on(global_trace.trace_buffer.buffer); 343 /* 344 * This flag is only looked at when buffers haven't been 345 * allocated yet. We don't really care about the race 346 * between setting this flag and actually turning 347 * on the buffer. 348 */ 349 global_trace.buffer_disabled = 0; 350 } 351 EXPORT_SYMBOL_GPL(tracing_on); 352 353 /** 354 * __trace_puts - write a constant string into the trace buffer. 355 * @ip: The address of the caller 356 * @str: The constant string to write 357 * @size: The size of the string. 358 */ 359 int __trace_puts(unsigned long ip, const char *str, int size) 360 { 361 struct ring_buffer_event *event; 362 struct ring_buffer *buffer; 363 struct print_entry *entry; 364 unsigned long irq_flags; 365 int alloc; 366 367 alloc = sizeof(*entry) + size + 2; /* possible \n added */ 368 369 local_save_flags(irq_flags); 370 buffer = global_trace.trace_buffer.buffer; 371 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 372 irq_flags, preempt_count()); 373 if (!event) 374 return 0; 375 376 entry = ring_buffer_event_data(event); 377 entry->ip = ip; 378 379 memcpy(&entry->buf, str, size); 380 381 /* Add a newline if necessary */ 382 if (entry->buf[size - 1] != '\n') { 383 entry->buf[size] = '\n'; 384 entry->buf[size + 1] = '\0'; 385 } else 386 entry->buf[size] = '\0'; 387 388 __buffer_unlock_commit(buffer, event); 389 390 return size; 391 } 392 EXPORT_SYMBOL_GPL(__trace_puts); 393 394 /** 395 * __trace_bputs - write the pointer to a constant string into trace buffer 396 * @ip: The address of the caller 397 * @str: The constant string to write to the buffer to 398 */ 399 int __trace_bputs(unsigned long ip, const char *str) 400 { 401 struct ring_buffer_event *event; 402 struct ring_buffer *buffer; 403 struct bputs_entry *entry; 404 unsigned long irq_flags; 405 int size = sizeof(struct bputs_entry); 406 407 local_save_flags(irq_flags); 408 buffer = global_trace.trace_buffer.buffer; 409 event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, 410 irq_flags, preempt_count()); 411 if (!event) 412 return 0; 413 414 entry = ring_buffer_event_data(event); 415 entry->ip = ip; 416 entry->str = str; 417 418 __buffer_unlock_commit(buffer, event); 419 420 return 1; 421 } 422 EXPORT_SYMBOL_GPL(__trace_bputs); 423 424 #ifdef CONFIG_TRACER_SNAPSHOT 425 /** 426 * trace_snapshot - take a snapshot of the current buffer. 427 * 428 * This causes a swap between the snapshot buffer and the current live 429 * tracing buffer. You can use this to take snapshots of the live 430 * trace when some condition is triggered, but continue to trace. 431 * 432 * Note, make sure to allocate the snapshot with either 433 * a tracing_snapshot_alloc(), or by doing it manually 434 * with: echo 1 > /sys/kernel/debug/tracing/snapshot 435 * 436 * If the snapshot buffer is not allocated, it will stop tracing. 437 * Basically making a permanent snapshot. 438 */ 439 void tracing_snapshot(void) 440 { 441 struct trace_array *tr = &global_trace; 442 struct tracer *tracer = tr->current_trace; 443 unsigned long flags; 444 445 if (in_nmi()) { 446 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); 447 internal_trace_puts("*** snapshot is being ignored ***\n"); 448 return; 449 } 450 451 if (!tr->allocated_snapshot) { 452 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n"); 453 internal_trace_puts("*** stopping trace here! ***\n"); 454 tracing_off(); 455 return; 456 } 457 458 /* Note, snapshot can not be used when the tracer uses it */ 459 if (tracer->use_max_tr) { 460 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n"); 461 internal_trace_puts("*** Can not use snapshot (sorry) ***\n"); 462 return; 463 } 464 465 local_irq_save(flags); 466 update_max_tr(tr, current, smp_processor_id()); 467 local_irq_restore(flags); 468 } 469 EXPORT_SYMBOL_GPL(tracing_snapshot); 470 471 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf, 472 struct trace_buffer *size_buf, int cpu_id); 473 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val); 474 475 static int alloc_snapshot(struct trace_array *tr) 476 { 477 int ret; 478 479 if (!tr->allocated_snapshot) { 480 481 /* allocate spare buffer */ 482 ret = resize_buffer_duplicate_size(&tr->max_buffer, 483 &tr->trace_buffer, RING_BUFFER_ALL_CPUS); 484 if (ret < 0) 485 return ret; 486 487 tr->allocated_snapshot = true; 488 } 489 490 return 0; 491 } 492 493 void free_snapshot(struct trace_array *tr) 494 { 495 /* 496 * We don't free the ring buffer. instead, resize it because 497 * The max_tr ring buffer has some state (e.g. ring->clock) and 498 * we want preserve it. 499 */ 500 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); 501 set_buffer_entries(&tr->max_buffer, 1); 502 tracing_reset_online_cpus(&tr->max_buffer); 503 tr->allocated_snapshot = false; 504 } 505 506 /** 507 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer. 508 * 509 * This is similar to trace_snapshot(), but it will allocate the 510 * snapshot buffer if it isn't already allocated. Use this only 511 * where it is safe to sleep, as the allocation may sleep. 512 * 513 * This causes a swap between the snapshot buffer and the current live 514 * tracing buffer. You can use this to take snapshots of the live 515 * trace when some condition is triggered, but continue to trace. 516 */ 517 void tracing_snapshot_alloc(void) 518 { 519 struct trace_array *tr = &global_trace; 520 int ret; 521 522 ret = alloc_snapshot(tr); 523 if (WARN_ON(ret < 0)) 524 return; 525 526 tracing_snapshot(); 527 } 528 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); 529 #else 530 void tracing_snapshot(void) 531 { 532 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); 533 } 534 EXPORT_SYMBOL_GPL(tracing_snapshot); 535 void tracing_snapshot_alloc(void) 536 { 537 /* Give warning */ 538 tracing_snapshot(); 539 } 540 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); 541 #endif /* CONFIG_TRACER_SNAPSHOT */ 542 543 /** 544 * tracing_off - turn off tracing buffers 545 * 546 * This function stops the tracing buffers from recording data. 547 * It does not disable any overhead the tracers themselves may 548 * be causing. This function simply causes all recording to 549 * the ring buffers to fail. 550 */ 551 void tracing_off(void) 552 { 553 if (global_trace.trace_buffer.buffer) 554 ring_buffer_record_off(global_trace.trace_buffer.buffer); 555 /* 556 * This flag is only looked at when buffers haven't been 557 * allocated yet. We don't really care about the race 558 * between setting this flag and actually turning 559 * on the buffer. 560 */ 561 global_trace.buffer_disabled = 1; 562 } 563 EXPORT_SYMBOL_GPL(tracing_off); 564 565 /** 566 * tracing_is_on - show state of ring buffers enabled 567 */ 568 int tracing_is_on(void) 569 { 570 if (global_trace.trace_buffer.buffer) 571 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer); 572 return !global_trace.buffer_disabled; 573 } 574 EXPORT_SYMBOL_GPL(tracing_is_on); 575 576 static int __init set_buf_size(char *str) 577 { 578 unsigned long buf_size; 579 580 if (!str) 581 return 0; 582 buf_size = memparse(str, &str); 583 /* nr_entries can not be zero */ 584 if (buf_size == 0) 585 return 0; 586 trace_buf_size = buf_size; 587 return 1; 588 } 589 __setup("trace_buf_size=", set_buf_size); 590 591 static int __init set_tracing_thresh(char *str) 592 { 593 unsigned long threshold; 594 int ret; 595 596 if (!str) 597 return 0; 598 ret = kstrtoul(str, 0, &threshold); 599 if (ret < 0) 600 return 0; 601 tracing_thresh = threshold * 1000; 602 return 1; 603 } 604 __setup("tracing_thresh=", set_tracing_thresh); 605 606 unsigned long nsecs_to_usecs(unsigned long nsecs) 607 { 608 return nsecs / 1000; 609 } 610 611 /* These must match the bit postions in trace_iterator_flags */ 612 static const char *trace_options[] = { 613 "print-parent", 614 "sym-offset", 615 "sym-addr", 616 "verbose", 617 "raw", 618 "hex", 619 "bin", 620 "block", 621 "stacktrace", 622 "trace_printk", 623 "ftrace_preempt", 624 "branch", 625 "annotate", 626 "userstacktrace", 627 "sym-userobj", 628 "printk-msg-only", 629 "context-info", 630 "latency-format", 631 "sleep-time", 632 "graph-time", 633 "record-cmd", 634 "overwrite", 635 "disable_on_free", 636 "irq-info", 637 "markers", 638 "function-trace", 639 NULL 640 }; 641 642 static struct { 643 u64 (*func)(void); 644 const char *name; 645 int in_ns; /* is this clock in nanoseconds? */ 646 } trace_clocks[] = { 647 { trace_clock_local, "local", 1 }, 648 { trace_clock_global, "global", 1 }, 649 { trace_clock_counter, "counter", 0 }, 650 { trace_clock_jiffies, "uptime", 1 }, 651 { trace_clock, "perf", 1 }, 652 ARCH_TRACE_CLOCKS 653 }; 654 655 int trace_clock_id; 656 657 /* 658 * trace_parser_get_init - gets the buffer for trace parser 659 */ 660 int trace_parser_get_init(struct trace_parser *parser, int size) 661 { 662 memset(parser, 0, sizeof(*parser)); 663 664 parser->buffer = kmalloc(size, GFP_KERNEL); 665 if (!parser->buffer) 666 return 1; 667 668 parser->size = size; 669 return 0; 670 } 671 672 /* 673 * trace_parser_put - frees the buffer for trace parser 674 */ 675 void trace_parser_put(struct trace_parser *parser) 676 { 677 kfree(parser->buffer); 678 } 679 680 /* 681 * trace_get_user - reads the user input string separated by space 682 * (matched by isspace(ch)) 683 * 684 * For each string found the 'struct trace_parser' is updated, 685 * and the function returns. 686 * 687 * Returns number of bytes read. 688 * 689 * See kernel/trace/trace.h for 'struct trace_parser' details. 690 */ 691 int trace_get_user(struct trace_parser *parser, const char __user *ubuf, 692 size_t cnt, loff_t *ppos) 693 { 694 char ch; 695 size_t read = 0; 696 ssize_t ret; 697 698 if (!*ppos) 699 trace_parser_clear(parser); 700 701 ret = get_user(ch, ubuf++); 702 if (ret) 703 goto out; 704 705 read++; 706 cnt--; 707 708 /* 709 * The parser is not finished with the last write, 710 * continue reading the user input without skipping spaces. 711 */ 712 if (!parser->cont) { 713 /* skip white space */ 714 while (cnt && isspace(ch)) { 715 ret = get_user(ch, ubuf++); 716 if (ret) 717 goto out; 718 read++; 719 cnt--; 720 } 721 722 /* only spaces were written */ 723 if (isspace(ch)) { 724 *ppos += read; 725 ret = read; 726 goto out; 727 } 728 729 parser->idx = 0; 730 } 731 732 /* read the non-space input */ 733 while (cnt && !isspace(ch)) { 734 if (parser->idx < parser->size - 1) 735 parser->buffer[parser->idx++] = ch; 736 else { 737 ret = -EINVAL; 738 goto out; 739 } 740 ret = get_user(ch, ubuf++); 741 if (ret) 742 goto out; 743 read++; 744 cnt--; 745 } 746 747 /* We either got finished input or we have to wait for another call. */ 748 if (isspace(ch)) { 749 parser->buffer[parser->idx] = 0; 750 parser->cont = false; 751 } else { 752 parser->cont = true; 753 parser->buffer[parser->idx++] = ch; 754 } 755 756 *ppos += read; 757 ret = read; 758 759 out: 760 return ret; 761 } 762 763 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt) 764 { 765 int len; 766 int ret; 767 768 if (!cnt) 769 return 0; 770 771 if (s->len <= s->readpos) 772 return -EBUSY; 773 774 len = s->len - s->readpos; 775 if (cnt > len) 776 cnt = len; 777 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); 778 if (ret == cnt) 779 return -EFAULT; 780 781 cnt -= ret; 782 783 s->readpos += cnt; 784 return cnt; 785 } 786 787 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) 788 { 789 int len; 790 791 if (s->len <= s->readpos) 792 return -EBUSY; 793 794 len = s->len - s->readpos; 795 if (cnt > len) 796 cnt = len; 797 memcpy(buf, s->buffer + s->readpos, cnt); 798 799 s->readpos += cnt; 800 return cnt; 801 } 802 803 /* 804 * ftrace_max_lock is used to protect the swapping of buffers 805 * when taking a max snapshot. The buffers themselves are 806 * protected by per_cpu spinlocks. But the action of the swap 807 * needs its own lock. 808 * 809 * This is defined as a arch_spinlock_t in order to help 810 * with performance when lockdep debugging is enabled. 811 * 812 * It is also used in other places outside the update_max_tr 813 * so it needs to be defined outside of the 814 * CONFIG_TRACER_MAX_TRACE. 815 */ 816 static arch_spinlock_t ftrace_max_lock = 817 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 818 819 unsigned long __read_mostly tracing_thresh; 820 821 #ifdef CONFIG_TRACER_MAX_TRACE 822 unsigned long __read_mostly tracing_max_latency; 823 824 /* 825 * Copy the new maximum trace into the separate maximum-trace 826 * structure. (this way the maximum trace is permanently saved, 827 * for later retrieval via /sys/kernel/debug/tracing/latency_trace) 828 */ 829 static void 830 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) 831 { 832 struct trace_buffer *trace_buf = &tr->trace_buffer; 833 struct trace_buffer *max_buf = &tr->max_buffer; 834 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu); 835 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu); 836 837 max_buf->cpu = cpu; 838 max_buf->time_start = data->preempt_timestamp; 839 840 max_data->saved_latency = tracing_max_latency; 841 max_data->critical_start = data->critical_start; 842 max_data->critical_end = data->critical_end; 843 844 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN); 845 max_data->pid = tsk->pid; 846 /* 847 * If tsk == current, then use current_uid(), as that does not use 848 * RCU. The irq tracer can be called out of RCU scope. 849 */ 850 if (tsk == current) 851 max_data->uid = current_uid(); 852 else 853 max_data->uid = task_uid(tsk); 854 855 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; 856 max_data->policy = tsk->policy; 857 max_data->rt_priority = tsk->rt_priority; 858 859 /* record this tasks comm */ 860 tracing_record_cmdline(tsk); 861 } 862 863 /** 864 * update_max_tr - snapshot all trace buffers from global_trace to max_tr 865 * @tr: tracer 866 * @tsk: the task with the latency 867 * @cpu: The cpu that initiated the trace. 868 * 869 * Flip the buffers between the @tr and the max_tr and record information 870 * about which task was the cause of this latency. 871 */ 872 void 873 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) 874 { 875 struct ring_buffer *buf; 876 877 if (tr->stop_count) 878 return; 879 880 WARN_ON_ONCE(!irqs_disabled()); 881 882 if (!tr->allocated_snapshot) { 883 /* Only the nop tracer should hit this when disabling */ 884 WARN_ON_ONCE(tr->current_trace != &nop_trace); 885 return; 886 } 887 888 arch_spin_lock(&ftrace_max_lock); 889 890 buf = tr->trace_buffer.buffer; 891 tr->trace_buffer.buffer = tr->max_buffer.buffer; 892 tr->max_buffer.buffer = buf; 893 894 __update_max_tr(tr, tsk, cpu); 895 arch_spin_unlock(&ftrace_max_lock); 896 } 897 898 /** 899 * update_max_tr_single - only copy one trace over, and reset the rest 900 * @tr - tracer 901 * @tsk - task with the latency 902 * @cpu - the cpu of the buffer to copy. 903 * 904 * Flip the trace of a single CPU buffer between the @tr and the max_tr. 905 */ 906 void 907 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) 908 { 909 int ret; 910 911 if (tr->stop_count) 912 return; 913 914 WARN_ON_ONCE(!irqs_disabled()); 915 if (!tr->allocated_snapshot) { 916 /* Only the nop tracer should hit this when disabling */ 917 WARN_ON_ONCE(tr->current_trace != &nop_trace); 918 return; 919 } 920 921 arch_spin_lock(&ftrace_max_lock); 922 923 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu); 924 925 if (ret == -EBUSY) { 926 /* 927 * We failed to swap the buffer due to a commit taking 928 * place on this CPU. We fail to record, but we reset 929 * the max trace buffer (no one writes directly to it) 930 * and flag that it failed. 931 */ 932 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_, 933 "Failed to swap buffers due to commit in progress\n"); 934 } 935 936 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); 937 938 __update_max_tr(tr, tsk, cpu); 939 arch_spin_unlock(&ftrace_max_lock); 940 } 941 #endif /* CONFIG_TRACER_MAX_TRACE */ 942 943 static void default_wait_pipe(struct trace_iterator *iter) 944 { 945 /* Iterators are static, they should be filled or empty */ 946 if (trace_buffer_iter(iter, iter->cpu_file)) 947 return; 948 949 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file); 950 } 951 952 #ifdef CONFIG_FTRACE_STARTUP_TEST 953 static int run_tracer_selftest(struct tracer *type) 954 { 955 struct trace_array *tr = &global_trace; 956 struct tracer *saved_tracer = tr->current_trace; 957 int ret; 958 959 if (!type->selftest || tracing_selftest_disabled) 960 return 0; 961 962 /* 963 * Run a selftest on this tracer. 964 * Here we reset the trace buffer, and set the current 965 * tracer to be this tracer. The tracer can then run some 966 * internal tracing to verify that everything is in order. 967 * If we fail, we do not register this tracer. 968 */ 969 tracing_reset_online_cpus(&tr->trace_buffer); 970 971 tr->current_trace = type; 972 973 #ifdef CONFIG_TRACER_MAX_TRACE 974 if (type->use_max_tr) { 975 /* If we expanded the buffers, make sure the max is expanded too */ 976 if (ring_buffer_expanded) 977 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size, 978 RING_BUFFER_ALL_CPUS); 979 tr->allocated_snapshot = true; 980 } 981 #endif 982 983 /* the test is responsible for initializing and enabling */ 984 pr_info("Testing tracer %s: ", type->name); 985 ret = type->selftest(type, tr); 986 /* the test is responsible for resetting too */ 987 tr->current_trace = saved_tracer; 988 if (ret) { 989 printk(KERN_CONT "FAILED!\n"); 990 /* Add the warning after printing 'FAILED' */ 991 WARN_ON(1); 992 return -1; 993 } 994 /* Only reset on passing, to avoid touching corrupted buffers */ 995 tracing_reset_online_cpus(&tr->trace_buffer); 996 997 #ifdef CONFIG_TRACER_MAX_TRACE 998 if (type->use_max_tr) { 999 tr->allocated_snapshot = false; 1000 1001 /* Shrink the max buffer again */ 1002 if (ring_buffer_expanded) 1003 ring_buffer_resize(tr->max_buffer.buffer, 1, 1004 RING_BUFFER_ALL_CPUS); 1005 } 1006 #endif 1007 1008 printk(KERN_CONT "PASSED\n"); 1009 return 0; 1010 } 1011 #else 1012 static inline int run_tracer_selftest(struct tracer *type) 1013 { 1014 return 0; 1015 } 1016 #endif /* CONFIG_FTRACE_STARTUP_TEST */ 1017 1018 /** 1019 * register_tracer - register a tracer with the ftrace system. 1020 * @type - the plugin for the tracer 1021 * 1022 * Register a new plugin tracer. 1023 */ 1024 int register_tracer(struct tracer *type) 1025 { 1026 struct tracer *t; 1027 int ret = 0; 1028 1029 if (!type->name) { 1030 pr_info("Tracer must have a name\n"); 1031 return -1; 1032 } 1033 1034 if (strlen(type->name) >= MAX_TRACER_SIZE) { 1035 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); 1036 return -1; 1037 } 1038 1039 mutex_lock(&trace_types_lock); 1040 1041 tracing_selftest_running = true; 1042 1043 for (t = trace_types; t; t = t->next) { 1044 if (strcmp(type->name, t->name) == 0) { 1045 /* already found */ 1046 pr_info("Tracer %s already registered\n", 1047 type->name); 1048 ret = -1; 1049 goto out; 1050 } 1051 } 1052 1053 if (!type->set_flag) 1054 type->set_flag = &dummy_set_flag; 1055 if (!type->flags) 1056 type->flags = &dummy_tracer_flags; 1057 else 1058 if (!type->flags->opts) 1059 type->flags->opts = dummy_tracer_opt; 1060 if (!type->wait_pipe) 1061 type->wait_pipe = default_wait_pipe; 1062 1063 ret = run_tracer_selftest(type); 1064 if (ret < 0) 1065 goto out; 1066 1067 type->next = trace_types; 1068 trace_types = type; 1069 1070 out: 1071 tracing_selftest_running = false; 1072 mutex_unlock(&trace_types_lock); 1073 1074 if (ret || !default_bootup_tracer) 1075 goto out_unlock; 1076 1077 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) 1078 goto out_unlock; 1079 1080 printk(KERN_INFO "Starting tracer '%s'\n", type->name); 1081 /* Do we want this tracer to start on bootup? */ 1082 tracing_set_tracer(type->name); 1083 default_bootup_tracer = NULL; 1084 /* disable other selftests, since this will break it. */ 1085 tracing_selftest_disabled = true; 1086 #ifdef CONFIG_FTRACE_STARTUP_TEST 1087 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n", 1088 type->name); 1089 #endif 1090 1091 out_unlock: 1092 return ret; 1093 } 1094 1095 void tracing_reset(struct trace_buffer *buf, int cpu) 1096 { 1097 struct ring_buffer *buffer = buf->buffer; 1098 1099 if (!buffer) 1100 return; 1101 1102 ring_buffer_record_disable(buffer); 1103 1104 /* Make sure all commits have finished */ 1105 synchronize_sched(); 1106 ring_buffer_reset_cpu(buffer, cpu); 1107 1108 ring_buffer_record_enable(buffer); 1109 } 1110 1111 void tracing_reset_online_cpus(struct trace_buffer *buf) 1112 { 1113 struct ring_buffer *buffer = buf->buffer; 1114 int cpu; 1115 1116 if (!buffer) 1117 return; 1118 1119 ring_buffer_record_disable(buffer); 1120 1121 /* Make sure all commits have finished */ 1122 synchronize_sched(); 1123 1124 buf->time_start = ftrace_now(buf->cpu); 1125 1126 for_each_online_cpu(cpu) 1127 ring_buffer_reset_cpu(buffer, cpu); 1128 1129 ring_buffer_record_enable(buffer); 1130 } 1131 1132 void tracing_reset_current(int cpu) 1133 { 1134 tracing_reset(&global_trace.trace_buffer, cpu); 1135 } 1136 1137 void tracing_reset_all_online_cpus(void) 1138 { 1139 struct trace_array *tr; 1140 1141 mutex_lock(&trace_types_lock); 1142 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 1143 tracing_reset_online_cpus(&tr->trace_buffer); 1144 #ifdef CONFIG_TRACER_MAX_TRACE 1145 tracing_reset_online_cpus(&tr->max_buffer); 1146 #endif 1147 } 1148 mutex_unlock(&trace_types_lock); 1149 } 1150 1151 #define SAVED_CMDLINES 128 1152 #define NO_CMDLINE_MAP UINT_MAX 1153 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; 1154 static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; 1155 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; 1156 static int cmdline_idx; 1157 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; 1158 1159 /* temporary disable recording */ 1160 static atomic_t trace_record_cmdline_disabled __read_mostly; 1161 1162 static void trace_init_cmdlines(void) 1163 { 1164 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline)); 1165 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid)); 1166 cmdline_idx = 0; 1167 } 1168 1169 int is_tracing_stopped(void) 1170 { 1171 return global_trace.stop_count; 1172 } 1173 1174 /** 1175 * ftrace_off_permanent - disable all ftrace code permanently 1176 * 1177 * This should only be called when a serious anomally has 1178 * been detected. This will turn off the function tracing, 1179 * ring buffers, and other tracing utilites. It takes no 1180 * locks and can be called from any context. 1181 */ 1182 void ftrace_off_permanent(void) 1183 { 1184 tracing_disabled = 1; 1185 ftrace_stop(); 1186 tracing_off_permanent(); 1187 } 1188 1189 /** 1190 * tracing_start - quick start of the tracer 1191 * 1192 * If tracing is enabled but was stopped by tracing_stop, 1193 * this will start the tracer back up. 1194 */ 1195 void tracing_start(void) 1196 { 1197 struct ring_buffer *buffer; 1198 unsigned long flags; 1199 1200 if (tracing_disabled) 1201 return; 1202 1203 raw_spin_lock_irqsave(&global_trace.start_lock, flags); 1204 if (--global_trace.stop_count) { 1205 if (global_trace.stop_count < 0) { 1206 /* Someone screwed up their debugging */ 1207 WARN_ON_ONCE(1); 1208 global_trace.stop_count = 0; 1209 } 1210 goto out; 1211 } 1212 1213 /* Prevent the buffers from switching */ 1214 arch_spin_lock(&ftrace_max_lock); 1215 1216 buffer = global_trace.trace_buffer.buffer; 1217 if (buffer) 1218 ring_buffer_record_enable(buffer); 1219 1220 #ifdef CONFIG_TRACER_MAX_TRACE 1221 buffer = global_trace.max_buffer.buffer; 1222 if (buffer) 1223 ring_buffer_record_enable(buffer); 1224 #endif 1225 1226 arch_spin_unlock(&ftrace_max_lock); 1227 1228 ftrace_start(); 1229 out: 1230 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); 1231 } 1232 1233 static void tracing_start_tr(struct trace_array *tr) 1234 { 1235 struct ring_buffer *buffer; 1236 unsigned long flags; 1237 1238 if (tracing_disabled) 1239 return; 1240 1241 /* If global, we need to also start the max tracer */ 1242 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 1243 return tracing_start(); 1244 1245 raw_spin_lock_irqsave(&tr->start_lock, flags); 1246 1247 if (--tr->stop_count) { 1248 if (tr->stop_count < 0) { 1249 /* Someone screwed up their debugging */ 1250 WARN_ON_ONCE(1); 1251 tr->stop_count = 0; 1252 } 1253 goto out; 1254 } 1255 1256 buffer = tr->trace_buffer.buffer; 1257 if (buffer) 1258 ring_buffer_record_enable(buffer); 1259 1260 out: 1261 raw_spin_unlock_irqrestore(&tr->start_lock, flags); 1262 } 1263 1264 /** 1265 * tracing_stop - quick stop of the tracer 1266 * 1267 * Light weight way to stop tracing. Use in conjunction with 1268 * tracing_start. 1269 */ 1270 void tracing_stop(void) 1271 { 1272 struct ring_buffer *buffer; 1273 unsigned long flags; 1274 1275 ftrace_stop(); 1276 raw_spin_lock_irqsave(&global_trace.start_lock, flags); 1277 if (global_trace.stop_count++) 1278 goto out; 1279 1280 /* Prevent the buffers from switching */ 1281 arch_spin_lock(&ftrace_max_lock); 1282 1283 buffer = global_trace.trace_buffer.buffer; 1284 if (buffer) 1285 ring_buffer_record_disable(buffer); 1286 1287 #ifdef CONFIG_TRACER_MAX_TRACE 1288 buffer = global_trace.max_buffer.buffer; 1289 if (buffer) 1290 ring_buffer_record_disable(buffer); 1291 #endif 1292 1293 arch_spin_unlock(&ftrace_max_lock); 1294 1295 out: 1296 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); 1297 } 1298 1299 static void tracing_stop_tr(struct trace_array *tr) 1300 { 1301 struct ring_buffer *buffer; 1302 unsigned long flags; 1303 1304 /* If global, we need to also stop the max tracer */ 1305 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 1306 return tracing_stop(); 1307 1308 raw_spin_lock_irqsave(&tr->start_lock, flags); 1309 if (tr->stop_count++) 1310 goto out; 1311 1312 buffer = tr->trace_buffer.buffer; 1313 if (buffer) 1314 ring_buffer_record_disable(buffer); 1315 1316 out: 1317 raw_spin_unlock_irqrestore(&tr->start_lock, flags); 1318 } 1319 1320 void trace_stop_cmdline_recording(void); 1321 1322 static void trace_save_cmdline(struct task_struct *tsk) 1323 { 1324 unsigned pid, idx; 1325 1326 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) 1327 return; 1328 1329 /* 1330 * It's not the end of the world if we don't get 1331 * the lock, but we also don't want to spin 1332 * nor do we want to disable interrupts, 1333 * so if we miss here, then better luck next time. 1334 */ 1335 if (!arch_spin_trylock(&trace_cmdline_lock)) 1336 return; 1337 1338 idx = map_pid_to_cmdline[tsk->pid]; 1339 if (idx == NO_CMDLINE_MAP) { 1340 idx = (cmdline_idx + 1) % SAVED_CMDLINES; 1341 1342 /* 1343 * Check whether the cmdline buffer at idx has a pid 1344 * mapped. We are going to overwrite that entry so we 1345 * need to clear the map_pid_to_cmdline. Otherwise we 1346 * would read the new comm for the old pid. 1347 */ 1348 pid = map_cmdline_to_pid[idx]; 1349 if (pid != NO_CMDLINE_MAP) 1350 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; 1351 1352 map_cmdline_to_pid[idx] = tsk->pid; 1353 map_pid_to_cmdline[tsk->pid] = idx; 1354 1355 cmdline_idx = idx; 1356 } 1357 1358 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); 1359 1360 arch_spin_unlock(&trace_cmdline_lock); 1361 } 1362 1363 void trace_find_cmdline(int pid, char comm[]) 1364 { 1365 unsigned map; 1366 1367 if (!pid) { 1368 strcpy(comm, "<idle>"); 1369 return; 1370 } 1371 1372 if (WARN_ON_ONCE(pid < 0)) { 1373 strcpy(comm, "<XXX>"); 1374 return; 1375 } 1376 1377 if (pid > PID_MAX_DEFAULT) { 1378 strcpy(comm, "<...>"); 1379 return; 1380 } 1381 1382 preempt_disable(); 1383 arch_spin_lock(&trace_cmdline_lock); 1384 map = map_pid_to_cmdline[pid]; 1385 if (map != NO_CMDLINE_MAP) 1386 strcpy(comm, saved_cmdlines[map]); 1387 else 1388 strcpy(comm, "<...>"); 1389 1390 arch_spin_unlock(&trace_cmdline_lock); 1391 preempt_enable(); 1392 } 1393 1394 void tracing_record_cmdline(struct task_struct *tsk) 1395 { 1396 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on()) 1397 return; 1398 1399 if (!__this_cpu_read(trace_cmdline_save)) 1400 return; 1401 1402 __this_cpu_write(trace_cmdline_save, false); 1403 1404 trace_save_cmdline(tsk); 1405 } 1406 1407 void 1408 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, 1409 int pc) 1410 { 1411 struct task_struct *tsk = current; 1412 1413 entry->preempt_count = pc & 0xff; 1414 entry->pid = (tsk) ? tsk->pid : 0; 1415 entry->flags = 1416 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT 1417 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | 1418 #else 1419 TRACE_FLAG_IRQS_NOSUPPORT | 1420 #endif 1421 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | 1422 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | 1423 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); 1424 } 1425 EXPORT_SYMBOL_GPL(tracing_generic_entry_update); 1426 1427 struct ring_buffer_event * 1428 trace_buffer_lock_reserve(struct ring_buffer *buffer, 1429 int type, 1430 unsigned long len, 1431 unsigned long flags, int pc) 1432 { 1433 struct ring_buffer_event *event; 1434 1435 event = ring_buffer_lock_reserve(buffer, len); 1436 if (event != NULL) { 1437 struct trace_entry *ent = ring_buffer_event_data(event); 1438 1439 tracing_generic_entry_update(ent, flags, pc); 1440 ent->type = type; 1441 } 1442 1443 return event; 1444 } 1445 1446 void 1447 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event) 1448 { 1449 __this_cpu_write(trace_cmdline_save, true); 1450 ring_buffer_unlock_commit(buffer, event); 1451 } 1452 1453 static inline void 1454 __trace_buffer_unlock_commit(struct ring_buffer *buffer, 1455 struct ring_buffer_event *event, 1456 unsigned long flags, int pc) 1457 { 1458 __buffer_unlock_commit(buffer, event); 1459 1460 ftrace_trace_stack(buffer, flags, 6, pc); 1461 ftrace_trace_userstack(buffer, flags, pc); 1462 } 1463 1464 void trace_buffer_unlock_commit(struct ring_buffer *buffer, 1465 struct ring_buffer_event *event, 1466 unsigned long flags, int pc) 1467 { 1468 __trace_buffer_unlock_commit(buffer, event, flags, pc); 1469 } 1470 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit); 1471 1472 struct ring_buffer_event * 1473 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb, 1474 struct ftrace_event_file *ftrace_file, 1475 int type, unsigned long len, 1476 unsigned long flags, int pc) 1477 { 1478 *current_rb = ftrace_file->tr->trace_buffer.buffer; 1479 return trace_buffer_lock_reserve(*current_rb, 1480 type, len, flags, pc); 1481 } 1482 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve); 1483 1484 struct ring_buffer_event * 1485 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb, 1486 int type, unsigned long len, 1487 unsigned long flags, int pc) 1488 { 1489 *current_rb = global_trace.trace_buffer.buffer; 1490 return trace_buffer_lock_reserve(*current_rb, 1491 type, len, flags, pc); 1492 } 1493 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve); 1494 1495 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer, 1496 struct ring_buffer_event *event, 1497 unsigned long flags, int pc) 1498 { 1499 __trace_buffer_unlock_commit(buffer, event, flags, pc); 1500 } 1501 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit); 1502 1503 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer, 1504 struct ring_buffer_event *event, 1505 unsigned long flags, int pc, 1506 struct pt_regs *regs) 1507 { 1508 __buffer_unlock_commit(buffer, event); 1509 1510 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs); 1511 ftrace_trace_userstack(buffer, flags, pc); 1512 } 1513 EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs); 1514 1515 void trace_current_buffer_discard_commit(struct ring_buffer *buffer, 1516 struct ring_buffer_event *event) 1517 { 1518 ring_buffer_discard_commit(buffer, event); 1519 } 1520 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit); 1521 1522 void 1523 trace_function(struct trace_array *tr, 1524 unsigned long ip, unsigned long parent_ip, unsigned long flags, 1525 int pc) 1526 { 1527 struct ftrace_event_call *call = &event_function; 1528 struct ring_buffer *buffer = tr->trace_buffer.buffer; 1529 struct ring_buffer_event *event; 1530 struct ftrace_entry *entry; 1531 1532 /* If we are reading the ring buffer, don't trace */ 1533 if (unlikely(__this_cpu_read(ftrace_cpu_disabled))) 1534 return; 1535 1536 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), 1537 flags, pc); 1538 if (!event) 1539 return; 1540 entry = ring_buffer_event_data(event); 1541 entry->ip = ip; 1542 entry->parent_ip = parent_ip; 1543 1544 if (!filter_check_discard(call, entry, buffer, event)) 1545 __buffer_unlock_commit(buffer, event); 1546 } 1547 1548 void 1549 ftrace(struct trace_array *tr, struct trace_array_cpu *data, 1550 unsigned long ip, unsigned long parent_ip, unsigned long flags, 1551 int pc) 1552 { 1553 if (likely(!atomic_read(&data->disabled))) 1554 trace_function(tr, ip, parent_ip, flags, pc); 1555 } 1556 1557 #ifdef CONFIG_STACKTRACE 1558 1559 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) 1560 struct ftrace_stack { 1561 unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; 1562 }; 1563 1564 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); 1565 static DEFINE_PER_CPU(int, ftrace_stack_reserve); 1566 1567 static void __ftrace_trace_stack(struct ring_buffer *buffer, 1568 unsigned long flags, 1569 int skip, int pc, struct pt_regs *regs) 1570 { 1571 struct ftrace_event_call *call = &event_kernel_stack; 1572 struct ring_buffer_event *event; 1573 struct stack_entry *entry; 1574 struct stack_trace trace; 1575 int use_stack; 1576 int size = FTRACE_STACK_ENTRIES; 1577 1578 trace.nr_entries = 0; 1579 trace.skip = skip; 1580 1581 /* 1582 * Since events can happen in NMIs there's no safe way to 1583 * use the per cpu ftrace_stacks. We reserve it and if an interrupt 1584 * or NMI comes in, it will just have to use the default 1585 * FTRACE_STACK_SIZE. 1586 */ 1587 preempt_disable_notrace(); 1588 1589 use_stack = __this_cpu_inc_return(ftrace_stack_reserve); 1590 /* 1591 * We don't need any atomic variables, just a barrier. 1592 * If an interrupt comes in, we don't care, because it would 1593 * have exited and put the counter back to what we want. 1594 * We just need a barrier to keep gcc from moving things 1595 * around. 1596 */ 1597 barrier(); 1598 if (use_stack == 1) { 1599 trace.entries = &__get_cpu_var(ftrace_stack).calls[0]; 1600 trace.max_entries = FTRACE_STACK_MAX_ENTRIES; 1601 1602 if (regs) 1603 save_stack_trace_regs(regs, &trace); 1604 else 1605 save_stack_trace(&trace); 1606 1607 if (trace.nr_entries > size) 1608 size = trace.nr_entries; 1609 } else 1610 /* From now on, use_stack is a boolean */ 1611 use_stack = 0; 1612 1613 size *= sizeof(unsigned long); 1614 1615 event = trace_buffer_lock_reserve(buffer, TRACE_STACK, 1616 sizeof(*entry) + size, flags, pc); 1617 if (!event) 1618 goto out; 1619 entry = ring_buffer_event_data(event); 1620 1621 memset(&entry->caller, 0, size); 1622 1623 if (use_stack) 1624 memcpy(&entry->caller, trace.entries, 1625 trace.nr_entries * sizeof(unsigned long)); 1626 else { 1627 trace.max_entries = FTRACE_STACK_ENTRIES; 1628 trace.entries = entry->caller; 1629 if (regs) 1630 save_stack_trace_regs(regs, &trace); 1631 else 1632 save_stack_trace(&trace); 1633 } 1634 1635 entry->size = trace.nr_entries; 1636 1637 if (!filter_check_discard(call, entry, buffer, event)) 1638 __buffer_unlock_commit(buffer, event); 1639 1640 out: 1641 /* Again, don't let gcc optimize things here */ 1642 barrier(); 1643 __this_cpu_dec(ftrace_stack_reserve); 1644 preempt_enable_notrace(); 1645 1646 } 1647 1648 void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags, 1649 int skip, int pc, struct pt_regs *regs) 1650 { 1651 if (!(trace_flags & TRACE_ITER_STACKTRACE)) 1652 return; 1653 1654 __ftrace_trace_stack(buffer, flags, skip, pc, regs); 1655 } 1656 1657 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags, 1658 int skip, int pc) 1659 { 1660 if (!(trace_flags & TRACE_ITER_STACKTRACE)) 1661 return; 1662 1663 __ftrace_trace_stack(buffer, flags, skip, pc, NULL); 1664 } 1665 1666 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip, 1667 int pc) 1668 { 1669 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL); 1670 } 1671 1672 /** 1673 * trace_dump_stack - record a stack back trace in the trace buffer 1674 * @skip: Number of functions to skip (helper handlers) 1675 */ 1676 void trace_dump_stack(int skip) 1677 { 1678 unsigned long flags; 1679 1680 if (tracing_disabled || tracing_selftest_running) 1681 return; 1682 1683 local_save_flags(flags); 1684 1685 /* 1686 * Skip 3 more, seems to get us at the caller of 1687 * this function. 1688 */ 1689 skip += 3; 1690 __ftrace_trace_stack(global_trace.trace_buffer.buffer, 1691 flags, skip, preempt_count(), NULL); 1692 } 1693 1694 static DEFINE_PER_CPU(int, user_stack_count); 1695 1696 void 1697 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) 1698 { 1699 struct ftrace_event_call *call = &event_user_stack; 1700 struct ring_buffer_event *event; 1701 struct userstack_entry *entry; 1702 struct stack_trace trace; 1703 1704 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE)) 1705 return; 1706 1707 /* 1708 * NMIs can not handle page faults, even with fix ups. 1709 * The save user stack can (and often does) fault. 1710 */ 1711 if (unlikely(in_nmi())) 1712 return; 1713 1714 /* 1715 * prevent recursion, since the user stack tracing may 1716 * trigger other kernel events. 1717 */ 1718 preempt_disable(); 1719 if (__this_cpu_read(user_stack_count)) 1720 goto out; 1721 1722 __this_cpu_inc(user_stack_count); 1723 1724 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, 1725 sizeof(*entry), flags, pc); 1726 if (!event) 1727 goto out_drop_count; 1728 entry = ring_buffer_event_data(event); 1729 1730 entry->tgid = current->tgid; 1731 memset(&entry->caller, 0, sizeof(entry->caller)); 1732 1733 trace.nr_entries = 0; 1734 trace.max_entries = FTRACE_STACK_ENTRIES; 1735 trace.skip = 0; 1736 trace.entries = entry->caller; 1737 1738 save_stack_trace_user(&trace); 1739 if (!filter_check_discard(call, entry, buffer, event)) 1740 __buffer_unlock_commit(buffer, event); 1741 1742 out_drop_count: 1743 __this_cpu_dec(user_stack_count); 1744 out: 1745 preempt_enable(); 1746 } 1747 1748 #ifdef UNUSED 1749 static void __trace_userstack(struct trace_array *tr, unsigned long flags) 1750 { 1751 ftrace_trace_userstack(tr, flags, preempt_count()); 1752 } 1753 #endif /* UNUSED */ 1754 1755 #endif /* CONFIG_STACKTRACE */ 1756 1757 /* created for use with alloc_percpu */ 1758 struct trace_buffer_struct { 1759 char buffer[TRACE_BUF_SIZE]; 1760 }; 1761 1762 static struct trace_buffer_struct *trace_percpu_buffer; 1763 static struct trace_buffer_struct *trace_percpu_sirq_buffer; 1764 static struct trace_buffer_struct *trace_percpu_irq_buffer; 1765 static struct trace_buffer_struct *trace_percpu_nmi_buffer; 1766 1767 /* 1768 * The buffer used is dependent on the context. There is a per cpu 1769 * buffer for normal context, softirq contex, hard irq context and 1770 * for NMI context. Thise allows for lockless recording. 1771 * 1772 * Note, if the buffers failed to be allocated, then this returns NULL 1773 */ 1774 static char *get_trace_buf(void) 1775 { 1776 struct trace_buffer_struct *percpu_buffer; 1777 1778 /* 1779 * If we have allocated per cpu buffers, then we do not 1780 * need to do any locking. 1781 */ 1782 if (in_nmi()) 1783 percpu_buffer = trace_percpu_nmi_buffer; 1784 else if (in_irq()) 1785 percpu_buffer = trace_percpu_irq_buffer; 1786 else if (in_softirq()) 1787 percpu_buffer = trace_percpu_sirq_buffer; 1788 else 1789 percpu_buffer = trace_percpu_buffer; 1790 1791 if (!percpu_buffer) 1792 return NULL; 1793 1794 return this_cpu_ptr(&percpu_buffer->buffer[0]); 1795 } 1796 1797 static int alloc_percpu_trace_buffer(void) 1798 { 1799 struct trace_buffer_struct *buffers; 1800 struct trace_buffer_struct *sirq_buffers; 1801 struct trace_buffer_struct *irq_buffers; 1802 struct trace_buffer_struct *nmi_buffers; 1803 1804 buffers = alloc_percpu(struct trace_buffer_struct); 1805 if (!buffers) 1806 goto err_warn; 1807 1808 sirq_buffers = alloc_percpu(struct trace_buffer_struct); 1809 if (!sirq_buffers) 1810 goto err_sirq; 1811 1812 irq_buffers = alloc_percpu(struct trace_buffer_struct); 1813 if (!irq_buffers) 1814 goto err_irq; 1815 1816 nmi_buffers = alloc_percpu(struct trace_buffer_struct); 1817 if (!nmi_buffers) 1818 goto err_nmi; 1819 1820 trace_percpu_buffer = buffers; 1821 trace_percpu_sirq_buffer = sirq_buffers; 1822 trace_percpu_irq_buffer = irq_buffers; 1823 trace_percpu_nmi_buffer = nmi_buffers; 1824 1825 return 0; 1826 1827 err_nmi: 1828 free_percpu(irq_buffers); 1829 err_irq: 1830 free_percpu(sirq_buffers); 1831 err_sirq: 1832 free_percpu(buffers); 1833 err_warn: 1834 WARN(1, "Could not allocate percpu trace_printk buffer"); 1835 return -ENOMEM; 1836 } 1837 1838 static int buffers_allocated; 1839 1840 void trace_printk_init_buffers(void) 1841 { 1842 if (buffers_allocated) 1843 return; 1844 1845 if (alloc_percpu_trace_buffer()) 1846 return; 1847 1848 pr_info("ftrace: Allocated trace_printk buffers\n"); 1849 1850 /* Expand the buffers to set size */ 1851 tracing_update_buffers(); 1852 1853 buffers_allocated = 1; 1854 1855 /* 1856 * trace_printk_init_buffers() can be called by modules. 1857 * If that happens, then we need to start cmdline recording 1858 * directly here. If the global_trace.buffer is already 1859 * allocated here, then this was called by module code. 1860 */ 1861 if (global_trace.trace_buffer.buffer) 1862 tracing_start_cmdline_record(); 1863 } 1864 1865 void trace_printk_start_comm(void) 1866 { 1867 /* Start tracing comms if trace printk is set */ 1868 if (!buffers_allocated) 1869 return; 1870 tracing_start_cmdline_record(); 1871 } 1872 1873 static void trace_printk_start_stop_comm(int enabled) 1874 { 1875 if (!buffers_allocated) 1876 return; 1877 1878 if (enabled) 1879 tracing_start_cmdline_record(); 1880 else 1881 tracing_stop_cmdline_record(); 1882 } 1883 1884 /** 1885 * trace_vbprintk - write binary msg to tracing buffer 1886 * 1887 */ 1888 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) 1889 { 1890 struct ftrace_event_call *call = &event_bprint; 1891 struct ring_buffer_event *event; 1892 struct ring_buffer *buffer; 1893 struct trace_array *tr = &global_trace; 1894 struct bprint_entry *entry; 1895 unsigned long flags; 1896 char *tbuffer; 1897 int len = 0, size, pc; 1898 1899 if (unlikely(tracing_selftest_running || tracing_disabled)) 1900 return 0; 1901 1902 /* Don't pollute graph traces with trace_vprintk internals */ 1903 pause_graph_tracing(); 1904 1905 pc = preempt_count(); 1906 preempt_disable_notrace(); 1907 1908 tbuffer = get_trace_buf(); 1909 if (!tbuffer) { 1910 len = 0; 1911 goto out; 1912 } 1913 1914 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); 1915 1916 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) 1917 goto out; 1918 1919 local_save_flags(flags); 1920 size = sizeof(*entry) + sizeof(u32) * len; 1921 buffer = tr->trace_buffer.buffer; 1922 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, 1923 flags, pc); 1924 if (!event) 1925 goto out; 1926 entry = ring_buffer_event_data(event); 1927 entry->ip = ip; 1928 entry->fmt = fmt; 1929 1930 memcpy(entry->buf, tbuffer, sizeof(u32) * len); 1931 if (!filter_check_discard(call, entry, buffer, event)) { 1932 __buffer_unlock_commit(buffer, event); 1933 ftrace_trace_stack(buffer, flags, 6, pc); 1934 } 1935 1936 out: 1937 preempt_enable_notrace(); 1938 unpause_graph_tracing(); 1939 1940 return len; 1941 } 1942 EXPORT_SYMBOL_GPL(trace_vbprintk); 1943 1944 static int 1945 __trace_array_vprintk(struct ring_buffer *buffer, 1946 unsigned long ip, const char *fmt, va_list args) 1947 { 1948 struct ftrace_event_call *call = &event_print; 1949 struct ring_buffer_event *event; 1950 int len = 0, size, pc; 1951 struct print_entry *entry; 1952 unsigned long flags; 1953 char *tbuffer; 1954 1955 if (tracing_disabled || tracing_selftest_running) 1956 return 0; 1957 1958 /* Don't pollute graph traces with trace_vprintk internals */ 1959 pause_graph_tracing(); 1960 1961 pc = preempt_count(); 1962 preempt_disable_notrace(); 1963 1964 1965 tbuffer = get_trace_buf(); 1966 if (!tbuffer) { 1967 len = 0; 1968 goto out; 1969 } 1970 1971 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); 1972 if (len > TRACE_BUF_SIZE) 1973 goto out; 1974 1975 local_save_flags(flags); 1976 size = sizeof(*entry) + len + 1; 1977 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 1978 flags, pc); 1979 if (!event) 1980 goto out; 1981 entry = ring_buffer_event_data(event); 1982 entry->ip = ip; 1983 1984 memcpy(&entry->buf, tbuffer, len); 1985 entry->buf[len] = '\0'; 1986 if (!filter_check_discard(call, entry, buffer, event)) { 1987 __buffer_unlock_commit(buffer, event); 1988 ftrace_trace_stack(buffer, flags, 6, pc); 1989 } 1990 out: 1991 preempt_enable_notrace(); 1992 unpause_graph_tracing(); 1993 1994 return len; 1995 } 1996 1997 int trace_array_vprintk(struct trace_array *tr, 1998 unsigned long ip, const char *fmt, va_list args) 1999 { 2000 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args); 2001 } 2002 2003 int trace_array_printk(struct trace_array *tr, 2004 unsigned long ip, const char *fmt, ...) 2005 { 2006 int ret; 2007 va_list ap; 2008 2009 if (!(trace_flags & TRACE_ITER_PRINTK)) 2010 return 0; 2011 2012 va_start(ap, fmt); 2013 ret = trace_array_vprintk(tr, ip, fmt, ap); 2014 va_end(ap); 2015 return ret; 2016 } 2017 2018 int trace_array_printk_buf(struct ring_buffer *buffer, 2019 unsigned long ip, const char *fmt, ...) 2020 { 2021 int ret; 2022 va_list ap; 2023 2024 if (!(trace_flags & TRACE_ITER_PRINTK)) 2025 return 0; 2026 2027 va_start(ap, fmt); 2028 ret = __trace_array_vprintk(buffer, ip, fmt, ap); 2029 va_end(ap); 2030 return ret; 2031 } 2032 2033 int trace_vprintk(unsigned long ip, const char *fmt, va_list args) 2034 { 2035 return trace_array_vprintk(&global_trace, ip, fmt, args); 2036 } 2037 EXPORT_SYMBOL_GPL(trace_vprintk); 2038 2039 static void trace_iterator_increment(struct trace_iterator *iter) 2040 { 2041 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu); 2042 2043 iter->idx++; 2044 if (buf_iter) 2045 ring_buffer_read(buf_iter, NULL); 2046 } 2047 2048 static struct trace_entry * 2049 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, 2050 unsigned long *lost_events) 2051 { 2052 struct ring_buffer_event *event; 2053 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu); 2054 2055 if (buf_iter) 2056 event = ring_buffer_iter_peek(buf_iter, ts); 2057 else 2058 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts, 2059 lost_events); 2060 2061 if (event) { 2062 iter->ent_size = ring_buffer_event_length(event); 2063 return ring_buffer_event_data(event); 2064 } 2065 iter->ent_size = 0; 2066 return NULL; 2067 } 2068 2069 static struct trace_entry * 2070 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, 2071 unsigned long *missing_events, u64 *ent_ts) 2072 { 2073 struct ring_buffer *buffer = iter->trace_buffer->buffer; 2074 struct trace_entry *ent, *next = NULL; 2075 unsigned long lost_events = 0, next_lost = 0; 2076 int cpu_file = iter->cpu_file; 2077 u64 next_ts = 0, ts; 2078 int next_cpu = -1; 2079 int next_size = 0; 2080 int cpu; 2081 2082 /* 2083 * If we are in a per_cpu trace file, don't bother by iterating over 2084 * all cpu and peek directly. 2085 */ 2086 if (cpu_file > RING_BUFFER_ALL_CPUS) { 2087 if (ring_buffer_empty_cpu(buffer, cpu_file)) 2088 return NULL; 2089 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); 2090 if (ent_cpu) 2091 *ent_cpu = cpu_file; 2092 2093 return ent; 2094 } 2095 2096 for_each_tracing_cpu(cpu) { 2097 2098 if (ring_buffer_empty_cpu(buffer, cpu)) 2099 continue; 2100 2101 ent = peek_next_entry(iter, cpu, &ts, &lost_events); 2102 2103 /* 2104 * Pick the entry with the smallest timestamp: 2105 */ 2106 if (ent && (!next || ts < next_ts)) { 2107 next = ent; 2108 next_cpu = cpu; 2109 next_ts = ts; 2110 next_lost = lost_events; 2111 next_size = iter->ent_size; 2112 } 2113 } 2114 2115 iter->ent_size = next_size; 2116 2117 if (ent_cpu) 2118 *ent_cpu = next_cpu; 2119 2120 if (ent_ts) 2121 *ent_ts = next_ts; 2122 2123 if (missing_events) 2124 *missing_events = next_lost; 2125 2126 return next; 2127 } 2128 2129 /* Find the next real entry, without updating the iterator itself */ 2130 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, 2131 int *ent_cpu, u64 *ent_ts) 2132 { 2133 return __find_next_entry(iter, ent_cpu, NULL, ent_ts); 2134 } 2135 2136 /* Find the next real entry, and increment the iterator to the next entry */ 2137 void *trace_find_next_entry_inc(struct trace_iterator *iter) 2138 { 2139 iter->ent = __find_next_entry(iter, &iter->cpu, 2140 &iter->lost_events, &iter->ts); 2141 2142 if (iter->ent) 2143 trace_iterator_increment(iter); 2144 2145 return iter->ent ? iter : NULL; 2146 } 2147 2148 static void trace_consume(struct trace_iterator *iter) 2149 { 2150 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts, 2151 &iter->lost_events); 2152 } 2153 2154 static void *s_next(struct seq_file *m, void *v, loff_t *pos) 2155 { 2156 struct trace_iterator *iter = m->private; 2157 int i = (int)*pos; 2158 void *ent; 2159 2160 WARN_ON_ONCE(iter->leftover); 2161 2162 (*pos)++; 2163 2164 /* can't go backwards */ 2165 if (iter->idx > i) 2166 return NULL; 2167 2168 if (iter->idx < 0) 2169 ent = trace_find_next_entry_inc(iter); 2170 else 2171 ent = iter; 2172 2173 while (ent && iter->idx < i) 2174 ent = trace_find_next_entry_inc(iter); 2175 2176 iter->pos = *pos; 2177 2178 return ent; 2179 } 2180 2181 void tracing_iter_reset(struct trace_iterator *iter, int cpu) 2182 { 2183 struct ring_buffer_event *event; 2184 struct ring_buffer_iter *buf_iter; 2185 unsigned long entries = 0; 2186 u64 ts; 2187 2188 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0; 2189 2190 buf_iter = trace_buffer_iter(iter, cpu); 2191 if (!buf_iter) 2192 return; 2193 2194 ring_buffer_iter_reset(buf_iter); 2195 2196 /* 2197 * We could have the case with the max latency tracers 2198 * that a reset never took place on a cpu. This is evident 2199 * by the timestamp being before the start of the buffer. 2200 */ 2201 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) { 2202 if (ts >= iter->trace_buffer->time_start) 2203 break; 2204 entries++; 2205 ring_buffer_read(buf_iter, NULL); 2206 } 2207 2208 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries; 2209 } 2210 2211 /* 2212 * The current tracer is copied to avoid a global locking 2213 * all around. 2214 */ 2215 static void *s_start(struct seq_file *m, loff_t *pos) 2216 { 2217 struct trace_iterator *iter = m->private; 2218 struct trace_array *tr = iter->tr; 2219 int cpu_file = iter->cpu_file; 2220 void *p = NULL; 2221 loff_t l = 0; 2222 int cpu; 2223 2224 /* 2225 * copy the tracer to avoid using a global lock all around. 2226 * iter->trace is a copy of current_trace, the pointer to the 2227 * name may be used instead of a strcmp(), as iter->trace->name 2228 * will point to the same string as current_trace->name. 2229 */ 2230 mutex_lock(&trace_types_lock); 2231 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) 2232 *iter->trace = *tr->current_trace; 2233 mutex_unlock(&trace_types_lock); 2234 2235 #ifdef CONFIG_TRACER_MAX_TRACE 2236 if (iter->snapshot && iter->trace->use_max_tr) 2237 return ERR_PTR(-EBUSY); 2238 #endif 2239 2240 if (!iter->snapshot) 2241 atomic_inc(&trace_record_cmdline_disabled); 2242 2243 if (*pos != iter->pos) { 2244 iter->ent = NULL; 2245 iter->cpu = 0; 2246 iter->idx = -1; 2247 2248 if (cpu_file == RING_BUFFER_ALL_CPUS) { 2249 for_each_tracing_cpu(cpu) 2250 tracing_iter_reset(iter, cpu); 2251 } else 2252 tracing_iter_reset(iter, cpu_file); 2253 2254 iter->leftover = 0; 2255 for (p = iter; p && l < *pos; p = s_next(m, p, &l)) 2256 ; 2257 2258 } else { 2259 /* 2260 * If we overflowed the seq_file before, then we want 2261 * to just reuse the trace_seq buffer again. 2262 */ 2263 if (iter->leftover) 2264 p = iter; 2265 else { 2266 l = *pos - 1; 2267 p = s_next(m, p, &l); 2268 } 2269 } 2270 2271 trace_event_read_lock(); 2272 trace_access_lock(cpu_file); 2273 return p; 2274 } 2275 2276 static void s_stop(struct seq_file *m, void *p) 2277 { 2278 struct trace_iterator *iter = m->private; 2279 2280 #ifdef CONFIG_TRACER_MAX_TRACE 2281 if (iter->snapshot && iter->trace->use_max_tr) 2282 return; 2283 #endif 2284 2285 if (!iter->snapshot) 2286 atomic_dec(&trace_record_cmdline_disabled); 2287 2288 trace_access_unlock(iter->cpu_file); 2289 trace_event_read_unlock(); 2290 } 2291 2292 static void 2293 get_total_entries(struct trace_buffer *buf, 2294 unsigned long *total, unsigned long *entries) 2295 { 2296 unsigned long count; 2297 int cpu; 2298 2299 *total = 0; 2300 *entries = 0; 2301 2302 for_each_tracing_cpu(cpu) { 2303 count = ring_buffer_entries_cpu(buf->buffer, cpu); 2304 /* 2305 * If this buffer has skipped entries, then we hold all 2306 * entries for the trace and we need to ignore the 2307 * ones before the time stamp. 2308 */ 2309 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) { 2310 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries; 2311 /* total is the same as the entries */ 2312 *total += count; 2313 } else 2314 *total += count + 2315 ring_buffer_overrun_cpu(buf->buffer, cpu); 2316 *entries += count; 2317 } 2318 } 2319 2320 static void print_lat_help_header(struct seq_file *m) 2321 { 2322 seq_puts(m, "# _------=> CPU# \n"); 2323 seq_puts(m, "# / _-----=> irqs-off \n"); 2324 seq_puts(m, "# | / _----=> need-resched \n"); 2325 seq_puts(m, "# || / _---=> hardirq/softirq \n"); 2326 seq_puts(m, "# ||| / _--=> preempt-depth \n"); 2327 seq_puts(m, "# |||| / delay \n"); 2328 seq_puts(m, "# cmd pid ||||| time | caller \n"); 2329 seq_puts(m, "# \\ / ||||| \\ | / \n"); 2330 } 2331 2332 static void print_event_info(struct trace_buffer *buf, struct seq_file *m) 2333 { 2334 unsigned long total; 2335 unsigned long entries; 2336 2337 get_total_entries(buf, &total, &entries); 2338 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", 2339 entries, total, num_online_cpus()); 2340 seq_puts(m, "#\n"); 2341 } 2342 2343 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m) 2344 { 2345 print_event_info(buf, m); 2346 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); 2347 seq_puts(m, "# | | | | |\n"); 2348 } 2349 2350 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m) 2351 { 2352 print_event_info(buf, m); 2353 seq_puts(m, "# _-----=> irqs-off\n"); 2354 seq_puts(m, "# / _----=> need-resched\n"); 2355 seq_puts(m, "# | / _---=> hardirq/softirq\n"); 2356 seq_puts(m, "# || / _--=> preempt-depth\n"); 2357 seq_puts(m, "# ||| / delay\n"); 2358 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"); 2359 seq_puts(m, "# | | | |||| | |\n"); 2360 } 2361 2362 void 2363 print_trace_header(struct seq_file *m, struct trace_iterator *iter) 2364 { 2365 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); 2366 struct trace_buffer *buf = iter->trace_buffer; 2367 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu); 2368 struct tracer *type = iter->trace; 2369 unsigned long entries; 2370 unsigned long total; 2371 const char *name = "preemption"; 2372 2373 name = type->name; 2374 2375 get_total_entries(buf, &total, &entries); 2376 2377 seq_printf(m, "# %s latency trace v1.1.5 on %s\n", 2378 name, UTS_RELEASE); 2379 seq_puts(m, "# -----------------------------------" 2380 "---------------------------------\n"); 2381 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |" 2382 " (M:%s VP:%d, KP:%d, SP:%d HP:%d", 2383 nsecs_to_usecs(data->saved_latency), 2384 entries, 2385 total, 2386 buf->cpu, 2387 #if defined(CONFIG_PREEMPT_NONE) 2388 "server", 2389 #elif defined(CONFIG_PREEMPT_VOLUNTARY) 2390 "desktop", 2391 #elif defined(CONFIG_PREEMPT) 2392 "preempt", 2393 #else 2394 "unknown", 2395 #endif 2396 /* These are reserved for later use */ 2397 0, 0, 0, 0); 2398 #ifdef CONFIG_SMP 2399 seq_printf(m, " #P:%d)\n", num_online_cpus()); 2400 #else 2401 seq_puts(m, ")\n"); 2402 #endif 2403 seq_puts(m, "# -----------------\n"); 2404 seq_printf(m, "# | task: %.16s-%d " 2405 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", 2406 data->comm, data->pid, 2407 from_kuid_munged(seq_user_ns(m), data->uid), data->nice, 2408 data->policy, data->rt_priority); 2409 seq_puts(m, "# -----------------\n"); 2410 2411 if (data->critical_start) { 2412 seq_puts(m, "# => started at: "); 2413 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); 2414 trace_print_seq(m, &iter->seq); 2415 seq_puts(m, "\n# => ended at: "); 2416 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); 2417 trace_print_seq(m, &iter->seq); 2418 seq_puts(m, "\n#\n"); 2419 } 2420 2421 seq_puts(m, "#\n"); 2422 } 2423 2424 static void test_cpu_buff_start(struct trace_iterator *iter) 2425 { 2426 struct trace_seq *s = &iter->seq; 2427 2428 if (!(trace_flags & TRACE_ITER_ANNOTATE)) 2429 return; 2430 2431 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) 2432 return; 2433 2434 if (cpumask_test_cpu(iter->cpu, iter->started)) 2435 return; 2436 2437 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries) 2438 return; 2439 2440 cpumask_set_cpu(iter->cpu, iter->started); 2441 2442 /* Don't print started cpu buffer for the first entry of the trace */ 2443 if (iter->idx > 1) 2444 trace_seq_printf(s, "##### CPU %u buffer started ####\n", 2445 iter->cpu); 2446 } 2447 2448 static enum print_line_t print_trace_fmt(struct trace_iterator *iter) 2449 { 2450 struct trace_seq *s = &iter->seq; 2451 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); 2452 struct trace_entry *entry; 2453 struct trace_event *event; 2454 2455 entry = iter->ent; 2456 2457 test_cpu_buff_start(iter); 2458 2459 event = ftrace_find_event(entry->type); 2460 2461 if (trace_flags & TRACE_ITER_CONTEXT_INFO) { 2462 if (iter->iter_flags & TRACE_FILE_LAT_FMT) { 2463 if (!trace_print_lat_context(iter)) 2464 goto partial; 2465 } else { 2466 if (!trace_print_context(iter)) 2467 goto partial; 2468 } 2469 } 2470 2471 if (event) 2472 return event->funcs->trace(iter, sym_flags, event); 2473 2474 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type)) 2475 goto partial; 2476 2477 return TRACE_TYPE_HANDLED; 2478 partial: 2479 return TRACE_TYPE_PARTIAL_LINE; 2480 } 2481 2482 static enum print_line_t print_raw_fmt(struct trace_iterator *iter) 2483 { 2484 struct trace_seq *s = &iter->seq; 2485 struct trace_entry *entry; 2486 struct trace_event *event; 2487 2488 entry = iter->ent; 2489 2490 if (trace_flags & TRACE_ITER_CONTEXT_INFO) { 2491 if (!trace_seq_printf(s, "%d %d %llu ", 2492 entry->pid, iter->cpu, iter->ts)) 2493 goto partial; 2494 } 2495 2496 event = ftrace_find_event(entry->type); 2497 if (event) 2498 return event->funcs->raw(iter, 0, event); 2499 2500 if (!trace_seq_printf(s, "%d ?\n", entry->type)) 2501 goto partial; 2502 2503 return TRACE_TYPE_HANDLED; 2504 partial: 2505 return TRACE_TYPE_PARTIAL_LINE; 2506 } 2507 2508 static enum print_line_t print_hex_fmt(struct trace_iterator *iter) 2509 { 2510 struct trace_seq *s = &iter->seq; 2511 unsigned char newline = '\n'; 2512 struct trace_entry *entry; 2513 struct trace_event *event; 2514 2515 entry = iter->ent; 2516 2517 if (trace_flags & TRACE_ITER_CONTEXT_INFO) { 2518 SEQ_PUT_HEX_FIELD_RET(s, entry->pid); 2519 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); 2520 SEQ_PUT_HEX_FIELD_RET(s, iter->ts); 2521 } 2522 2523 event = ftrace_find_event(entry->type); 2524 if (event) { 2525 enum print_line_t ret = event->funcs->hex(iter, 0, event); 2526 if (ret != TRACE_TYPE_HANDLED) 2527 return ret; 2528 } 2529 2530 SEQ_PUT_FIELD_RET(s, newline); 2531 2532 return TRACE_TYPE_HANDLED; 2533 } 2534 2535 static enum print_line_t print_bin_fmt(struct trace_iterator *iter) 2536 { 2537 struct trace_seq *s = &iter->seq; 2538 struct trace_entry *entry; 2539 struct trace_event *event; 2540 2541 entry = iter->ent; 2542 2543 if (trace_flags & TRACE_ITER_CONTEXT_INFO) { 2544 SEQ_PUT_FIELD_RET(s, entry->pid); 2545 SEQ_PUT_FIELD_RET(s, iter->cpu); 2546 SEQ_PUT_FIELD_RET(s, iter->ts); 2547 } 2548 2549 event = ftrace_find_event(entry->type); 2550 return event ? event->funcs->binary(iter, 0, event) : 2551 TRACE_TYPE_HANDLED; 2552 } 2553 2554 int trace_empty(struct trace_iterator *iter) 2555 { 2556 struct ring_buffer_iter *buf_iter; 2557 int cpu; 2558 2559 /* If we are looking at one CPU buffer, only check that one */ 2560 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 2561 cpu = iter->cpu_file; 2562 buf_iter = trace_buffer_iter(iter, cpu); 2563 if (buf_iter) { 2564 if (!ring_buffer_iter_empty(buf_iter)) 2565 return 0; 2566 } else { 2567 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu)) 2568 return 0; 2569 } 2570 return 1; 2571 } 2572 2573 for_each_tracing_cpu(cpu) { 2574 buf_iter = trace_buffer_iter(iter, cpu); 2575 if (buf_iter) { 2576 if (!ring_buffer_iter_empty(buf_iter)) 2577 return 0; 2578 } else { 2579 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu)) 2580 return 0; 2581 } 2582 } 2583 2584 return 1; 2585 } 2586 2587 /* Called with trace_event_read_lock() held. */ 2588 enum print_line_t print_trace_line(struct trace_iterator *iter) 2589 { 2590 enum print_line_t ret; 2591 2592 if (iter->lost_events && 2593 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n", 2594 iter->cpu, iter->lost_events)) 2595 return TRACE_TYPE_PARTIAL_LINE; 2596 2597 if (iter->trace && iter->trace->print_line) { 2598 ret = iter->trace->print_line(iter); 2599 if (ret != TRACE_TYPE_UNHANDLED) 2600 return ret; 2601 } 2602 2603 if (iter->ent->type == TRACE_BPUTS && 2604 trace_flags & TRACE_ITER_PRINTK && 2605 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 2606 return trace_print_bputs_msg_only(iter); 2607 2608 if (iter->ent->type == TRACE_BPRINT && 2609 trace_flags & TRACE_ITER_PRINTK && 2610 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 2611 return trace_print_bprintk_msg_only(iter); 2612 2613 if (iter->ent->type == TRACE_PRINT && 2614 trace_flags & TRACE_ITER_PRINTK && 2615 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 2616 return trace_print_printk_msg_only(iter); 2617 2618 if (trace_flags & TRACE_ITER_BIN) 2619 return print_bin_fmt(iter); 2620 2621 if (trace_flags & TRACE_ITER_HEX) 2622 return print_hex_fmt(iter); 2623 2624 if (trace_flags & TRACE_ITER_RAW) 2625 return print_raw_fmt(iter); 2626 2627 return print_trace_fmt(iter); 2628 } 2629 2630 void trace_latency_header(struct seq_file *m) 2631 { 2632 struct trace_iterator *iter = m->private; 2633 2634 /* print nothing if the buffers are empty */ 2635 if (trace_empty(iter)) 2636 return; 2637 2638 if (iter->iter_flags & TRACE_FILE_LAT_FMT) 2639 print_trace_header(m, iter); 2640 2641 if (!(trace_flags & TRACE_ITER_VERBOSE)) 2642 print_lat_help_header(m); 2643 } 2644 2645 void trace_default_header(struct seq_file *m) 2646 { 2647 struct trace_iterator *iter = m->private; 2648 2649 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO)) 2650 return; 2651 2652 if (iter->iter_flags & TRACE_FILE_LAT_FMT) { 2653 /* print nothing if the buffers are empty */ 2654 if (trace_empty(iter)) 2655 return; 2656 print_trace_header(m, iter); 2657 if (!(trace_flags & TRACE_ITER_VERBOSE)) 2658 print_lat_help_header(m); 2659 } else { 2660 if (!(trace_flags & TRACE_ITER_VERBOSE)) { 2661 if (trace_flags & TRACE_ITER_IRQ_INFO) 2662 print_func_help_header_irq(iter->trace_buffer, m); 2663 else 2664 print_func_help_header(iter->trace_buffer, m); 2665 } 2666 } 2667 } 2668 2669 static void test_ftrace_alive(struct seq_file *m) 2670 { 2671 if (!ftrace_is_dead()) 2672 return; 2673 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"); 2674 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n"); 2675 } 2676 2677 #ifdef CONFIG_TRACER_MAX_TRACE 2678 static void show_snapshot_main_help(struct seq_file *m) 2679 { 2680 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"); 2681 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"); 2682 seq_printf(m, "# Takes a snapshot of the main buffer.\n"); 2683 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n"); 2684 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n"); 2685 seq_printf(m, "# is not a '0' or '1')\n"); 2686 } 2687 2688 static void show_snapshot_percpu_help(struct seq_file *m) 2689 { 2690 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); 2691 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP 2692 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"); 2693 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n"); 2694 #else 2695 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n"); 2696 seq_printf(m, "# Must use main snapshot file to allocate.\n"); 2697 #endif 2698 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"); 2699 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n"); 2700 seq_printf(m, "# is not a '0' or '1')\n"); 2701 } 2702 2703 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) 2704 { 2705 if (iter->tr->allocated_snapshot) 2706 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n"); 2707 else 2708 seq_printf(m, "#\n# * Snapshot is freed *\n#\n"); 2709 2710 seq_printf(m, "# Snapshot commands:\n"); 2711 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 2712 show_snapshot_main_help(m); 2713 else 2714 show_snapshot_percpu_help(m); 2715 } 2716 #else 2717 /* Should never be called */ 2718 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { } 2719 #endif 2720 2721 static int s_show(struct seq_file *m, void *v) 2722 { 2723 struct trace_iterator *iter = v; 2724 int ret; 2725 2726 if (iter->ent == NULL) { 2727 if (iter->tr) { 2728 seq_printf(m, "# tracer: %s\n", iter->trace->name); 2729 seq_puts(m, "#\n"); 2730 test_ftrace_alive(m); 2731 } 2732 if (iter->snapshot && trace_empty(iter)) 2733 print_snapshot_help(m, iter); 2734 else if (iter->trace && iter->trace->print_header) 2735 iter->trace->print_header(m); 2736 else 2737 trace_default_header(m); 2738 2739 } else if (iter->leftover) { 2740 /* 2741 * If we filled the seq_file buffer earlier, we 2742 * want to just show it now. 2743 */ 2744 ret = trace_print_seq(m, &iter->seq); 2745 2746 /* ret should this time be zero, but you never know */ 2747 iter->leftover = ret; 2748 2749 } else { 2750 print_trace_line(iter); 2751 ret = trace_print_seq(m, &iter->seq); 2752 /* 2753 * If we overflow the seq_file buffer, then it will 2754 * ask us for this data again at start up. 2755 * Use that instead. 2756 * ret is 0 if seq_file write succeeded. 2757 * -1 otherwise. 2758 */ 2759 iter->leftover = ret; 2760 } 2761 2762 return 0; 2763 } 2764 2765 static const struct seq_operations tracer_seq_ops = { 2766 .start = s_start, 2767 .next = s_next, 2768 .stop = s_stop, 2769 .show = s_show, 2770 }; 2771 2772 static struct trace_iterator * 2773 __tracing_open(struct inode *inode, struct file *file, bool snapshot) 2774 { 2775 struct trace_cpu *tc = inode->i_private; 2776 struct trace_array *tr = tc->tr; 2777 struct trace_iterator *iter; 2778 int cpu; 2779 2780 if (tracing_disabled) 2781 return ERR_PTR(-ENODEV); 2782 2783 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter)); 2784 if (!iter) 2785 return ERR_PTR(-ENOMEM); 2786 2787 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(), 2788 GFP_KERNEL); 2789 if (!iter->buffer_iter) 2790 goto release; 2791 2792 /* 2793 * We make a copy of the current tracer to avoid concurrent 2794 * changes on it while we are reading. 2795 */ 2796 mutex_lock(&trace_types_lock); 2797 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL); 2798 if (!iter->trace) 2799 goto fail; 2800 2801 *iter->trace = *tr->current_trace; 2802 2803 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL)) 2804 goto fail; 2805 2806 iter->tr = tr; 2807 2808 #ifdef CONFIG_TRACER_MAX_TRACE 2809 /* Currently only the top directory has a snapshot */ 2810 if (tr->current_trace->print_max || snapshot) 2811 iter->trace_buffer = &tr->max_buffer; 2812 else 2813 #endif 2814 iter->trace_buffer = &tr->trace_buffer; 2815 iter->snapshot = snapshot; 2816 iter->pos = -1; 2817 mutex_init(&iter->mutex); 2818 iter->cpu_file = tc->cpu; 2819 2820 /* Notify the tracer early; before we stop tracing. */ 2821 if (iter->trace && iter->trace->open) 2822 iter->trace->open(iter); 2823 2824 /* Annotate start of buffers if we had overruns */ 2825 if (ring_buffer_overruns(iter->trace_buffer->buffer)) 2826 iter->iter_flags |= TRACE_FILE_ANNOTATE; 2827 2828 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 2829 if (trace_clocks[trace_clock_id].in_ns) 2830 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 2831 2832 /* stop the trace while dumping if we are not opening "snapshot" */ 2833 if (!iter->snapshot) 2834 tracing_stop_tr(tr); 2835 2836 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { 2837 for_each_tracing_cpu(cpu) { 2838 iter->buffer_iter[cpu] = 2839 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu); 2840 } 2841 ring_buffer_read_prepare_sync(); 2842 for_each_tracing_cpu(cpu) { 2843 ring_buffer_read_start(iter->buffer_iter[cpu]); 2844 tracing_iter_reset(iter, cpu); 2845 } 2846 } else { 2847 cpu = iter->cpu_file; 2848 iter->buffer_iter[cpu] = 2849 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu); 2850 ring_buffer_read_prepare_sync(); 2851 ring_buffer_read_start(iter->buffer_iter[cpu]); 2852 tracing_iter_reset(iter, cpu); 2853 } 2854 2855 tr->ref++; 2856 2857 mutex_unlock(&trace_types_lock); 2858 2859 return iter; 2860 2861 fail: 2862 mutex_unlock(&trace_types_lock); 2863 kfree(iter->trace); 2864 kfree(iter->buffer_iter); 2865 release: 2866 seq_release_private(inode, file); 2867 return ERR_PTR(-ENOMEM); 2868 } 2869 2870 int tracing_open_generic(struct inode *inode, struct file *filp) 2871 { 2872 if (tracing_disabled) 2873 return -ENODEV; 2874 2875 filp->private_data = inode->i_private; 2876 return 0; 2877 } 2878 2879 static int tracing_release(struct inode *inode, struct file *file) 2880 { 2881 struct seq_file *m = file->private_data; 2882 struct trace_iterator *iter; 2883 struct trace_array *tr; 2884 int cpu; 2885 2886 if (!(file->f_mode & FMODE_READ)) 2887 return 0; 2888 2889 iter = m->private; 2890 tr = iter->tr; 2891 2892 mutex_lock(&trace_types_lock); 2893 2894 WARN_ON(!tr->ref); 2895 tr->ref--; 2896 2897 for_each_tracing_cpu(cpu) { 2898 if (iter->buffer_iter[cpu]) 2899 ring_buffer_read_finish(iter->buffer_iter[cpu]); 2900 } 2901 2902 if (iter->trace && iter->trace->close) 2903 iter->trace->close(iter); 2904 2905 if (!iter->snapshot) 2906 /* reenable tracing if it was previously enabled */ 2907 tracing_start_tr(tr); 2908 mutex_unlock(&trace_types_lock); 2909 2910 mutex_destroy(&iter->mutex); 2911 free_cpumask_var(iter->started); 2912 kfree(iter->trace); 2913 kfree(iter->buffer_iter); 2914 seq_release_private(inode, file); 2915 return 0; 2916 } 2917 2918 static int tracing_open(struct inode *inode, struct file *file) 2919 { 2920 struct trace_iterator *iter; 2921 int ret = 0; 2922 2923 /* If this file was open for write, then erase contents */ 2924 if ((file->f_mode & FMODE_WRITE) && 2925 (file->f_flags & O_TRUNC)) { 2926 struct trace_cpu *tc = inode->i_private; 2927 struct trace_array *tr = tc->tr; 2928 2929 if (tc->cpu == RING_BUFFER_ALL_CPUS) 2930 tracing_reset_online_cpus(&tr->trace_buffer); 2931 else 2932 tracing_reset(&tr->trace_buffer, tc->cpu); 2933 } 2934 2935 if (file->f_mode & FMODE_READ) { 2936 iter = __tracing_open(inode, file, false); 2937 if (IS_ERR(iter)) 2938 ret = PTR_ERR(iter); 2939 else if (trace_flags & TRACE_ITER_LATENCY_FMT) 2940 iter->iter_flags |= TRACE_FILE_LAT_FMT; 2941 } 2942 return ret; 2943 } 2944 2945 static void * 2946 t_next(struct seq_file *m, void *v, loff_t *pos) 2947 { 2948 struct tracer *t = v; 2949 2950 (*pos)++; 2951 2952 if (t) 2953 t = t->next; 2954 2955 return t; 2956 } 2957 2958 static void *t_start(struct seq_file *m, loff_t *pos) 2959 { 2960 struct tracer *t; 2961 loff_t l = 0; 2962 2963 mutex_lock(&trace_types_lock); 2964 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l)) 2965 ; 2966 2967 return t; 2968 } 2969 2970 static void t_stop(struct seq_file *m, void *p) 2971 { 2972 mutex_unlock(&trace_types_lock); 2973 } 2974 2975 static int t_show(struct seq_file *m, void *v) 2976 { 2977 struct tracer *t = v; 2978 2979 if (!t) 2980 return 0; 2981 2982 seq_printf(m, "%s", t->name); 2983 if (t->next) 2984 seq_putc(m, ' '); 2985 else 2986 seq_putc(m, '\n'); 2987 2988 return 0; 2989 } 2990 2991 static const struct seq_operations show_traces_seq_ops = { 2992 .start = t_start, 2993 .next = t_next, 2994 .stop = t_stop, 2995 .show = t_show, 2996 }; 2997 2998 static int show_traces_open(struct inode *inode, struct file *file) 2999 { 3000 if (tracing_disabled) 3001 return -ENODEV; 3002 3003 return seq_open(file, &show_traces_seq_ops); 3004 } 3005 3006 static ssize_t 3007 tracing_write_stub(struct file *filp, const char __user *ubuf, 3008 size_t count, loff_t *ppos) 3009 { 3010 return count; 3011 } 3012 3013 static loff_t tracing_seek(struct file *file, loff_t offset, int origin) 3014 { 3015 if (file->f_mode & FMODE_READ) 3016 return seq_lseek(file, offset, origin); 3017 else 3018 return 0; 3019 } 3020 3021 static const struct file_operations tracing_fops = { 3022 .open = tracing_open, 3023 .read = seq_read, 3024 .write = tracing_write_stub, 3025 .llseek = tracing_seek, 3026 .release = tracing_release, 3027 }; 3028 3029 static const struct file_operations show_traces_fops = { 3030 .open = show_traces_open, 3031 .read = seq_read, 3032 .release = seq_release, 3033 .llseek = seq_lseek, 3034 }; 3035 3036 /* 3037 * Only trace on a CPU if the bitmask is set: 3038 */ 3039 static cpumask_var_t tracing_cpumask; 3040 3041 /* 3042 * The tracer itself will not take this lock, but still we want 3043 * to provide a consistent cpumask to user-space: 3044 */ 3045 static DEFINE_MUTEX(tracing_cpumask_update_lock); 3046 3047 /* 3048 * Temporary storage for the character representation of the 3049 * CPU bitmask (and one more byte for the newline): 3050 */ 3051 static char mask_str[NR_CPUS + 1]; 3052 3053 static ssize_t 3054 tracing_cpumask_read(struct file *filp, char __user *ubuf, 3055 size_t count, loff_t *ppos) 3056 { 3057 int len; 3058 3059 mutex_lock(&tracing_cpumask_update_lock); 3060 3061 len = cpumask_scnprintf(mask_str, count, tracing_cpumask); 3062 if (count - len < 2) { 3063 count = -EINVAL; 3064 goto out_err; 3065 } 3066 len += sprintf(mask_str + len, "\n"); 3067 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); 3068 3069 out_err: 3070 mutex_unlock(&tracing_cpumask_update_lock); 3071 3072 return count; 3073 } 3074 3075 static ssize_t 3076 tracing_cpumask_write(struct file *filp, const char __user *ubuf, 3077 size_t count, loff_t *ppos) 3078 { 3079 struct trace_array *tr = filp->private_data; 3080 cpumask_var_t tracing_cpumask_new; 3081 int err, cpu; 3082 3083 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) 3084 return -ENOMEM; 3085 3086 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); 3087 if (err) 3088 goto err_unlock; 3089 3090 mutex_lock(&tracing_cpumask_update_lock); 3091 3092 local_irq_disable(); 3093 arch_spin_lock(&ftrace_max_lock); 3094 for_each_tracing_cpu(cpu) { 3095 /* 3096 * Increase/decrease the disabled counter if we are 3097 * about to flip a bit in the cpumask: 3098 */ 3099 if (cpumask_test_cpu(cpu, tracing_cpumask) && 3100 !cpumask_test_cpu(cpu, tracing_cpumask_new)) { 3101 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled); 3102 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu); 3103 } 3104 if (!cpumask_test_cpu(cpu, tracing_cpumask) && 3105 cpumask_test_cpu(cpu, tracing_cpumask_new)) { 3106 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled); 3107 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu); 3108 } 3109 } 3110 arch_spin_unlock(&ftrace_max_lock); 3111 local_irq_enable(); 3112 3113 cpumask_copy(tracing_cpumask, tracing_cpumask_new); 3114 3115 mutex_unlock(&tracing_cpumask_update_lock); 3116 free_cpumask_var(tracing_cpumask_new); 3117 3118 return count; 3119 3120 err_unlock: 3121 free_cpumask_var(tracing_cpumask_new); 3122 3123 return err; 3124 } 3125 3126 static const struct file_operations tracing_cpumask_fops = { 3127 .open = tracing_open_generic, 3128 .read = tracing_cpumask_read, 3129 .write = tracing_cpumask_write, 3130 .llseek = generic_file_llseek, 3131 }; 3132 3133 static int tracing_trace_options_show(struct seq_file *m, void *v) 3134 { 3135 struct tracer_opt *trace_opts; 3136 struct trace_array *tr = m->private; 3137 u32 tracer_flags; 3138 int i; 3139 3140 mutex_lock(&trace_types_lock); 3141 tracer_flags = tr->current_trace->flags->val; 3142 trace_opts = tr->current_trace->flags->opts; 3143 3144 for (i = 0; trace_options[i]; i++) { 3145 if (trace_flags & (1 << i)) 3146 seq_printf(m, "%s\n", trace_options[i]); 3147 else 3148 seq_printf(m, "no%s\n", trace_options[i]); 3149 } 3150 3151 for (i = 0; trace_opts[i].name; i++) { 3152 if (tracer_flags & trace_opts[i].bit) 3153 seq_printf(m, "%s\n", trace_opts[i].name); 3154 else 3155 seq_printf(m, "no%s\n", trace_opts[i].name); 3156 } 3157 mutex_unlock(&trace_types_lock); 3158 3159 return 0; 3160 } 3161 3162 static int __set_tracer_option(struct tracer *trace, 3163 struct tracer_flags *tracer_flags, 3164 struct tracer_opt *opts, int neg) 3165 { 3166 int ret; 3167 3168 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg); 3169 if (ret) 3170 return ret; 3171 3172 if (neg) 3173 tracer_flags->val &= ~opts->bit; 3174 else 3175 tracer_flags->val |= opts->bit; 3176 return 0; 3177 } 3178 3179 /* Try to assign a tracer specific option */ 3180 static int set_tracer_option(struct tracer *trace, char *cmp, int neg) 3181 { 3182 struct tracer_flags *tracer_flags = trace->flags; 3183 struct tracer_opt *opts = NULL; 3184 int i; 3185 3186 for (i = 0; tracer_flags->opts[i].name; i++) { 3187 opts = &tracer_flags->opts[i]; 3188 3189 if (strcmp(cmp, opts->name) == 0) 3190 return __set_tracer_option(trace, trace->flags, 3191 opts, neg); 3192 } 3193 3194 return -EINVAL; 3195 } 3196 3197 /* Some tracers require overwrite to stay enabled */ 3198 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) 3199 { 3200 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set) 3201 return -1; 3202 3203 return 0; 3204 } 3205 3206 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) 3207 { 3208 /* do nothing if flag is already set */ 3209 if (!!(trace_flags & mask) == !!enabled) 3210 return 0; 3211 3212 /* Give the tracer a chance to approve the change */ 3213 if (tr->current_trace->flag_changed) 3214 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled)) 3215 return -EINVAL; 3216 3217 if (enabled) 3218 trace_flags |= mask; 3219 else 3220 trace_flags &= ~mask; 3221 3222 if (mask == TRACE_ITER_RECORD_CMD) 3223 trace_event_enable_cmd_record(enabled); 3224 3225 if (mask == TRACE_ITER_OVERWRITE) { 3226 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled); 3227 #ifdef CONFIG_TRACER_MAX_TRACE 3228 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled); 3229 #endif 3230 } 3231 3232 if (mask == TRACE_ITER_PRINTK) 3233 trace_printk_start_stop_comm(enabled); 3234 3235 return 0; 3236 } 3237 3238 static int trace_set_options(struct trace_array *tr, char *option) 3239 { 3240 char *cmp; 3241 int neg = 0; 3242 int ret = -ENODEV; 3243 int i; 3244 3245 cmp = strstrip(option); 3246 3247 if (strncmp(cmp, "no", 2) == 0) { 3248 neg = 1; 3249 cmp += 2; 3250 } 3251 3252 mutex_lock(&trace_types_lock); 3253 3254 for (i = 0; trace_options[i]; i++) { 3255 if (strcmp(cmp, trace_options[i]) == 0) { 3256 ret = set_tracer_flag(tr, 1 << i, !neg); 3257 break; 3258 } 3259 } 3260 3261 /* If no option could be set, test the specific tracer options */ 3262 if (!trace_options[i]) 3263 ret = set_tracer_option(tr->current_trace, cmp, neg); 3264 3265 mutex_unlock(&trace_types_lock); 3266 3267 return ret; 3268 } 3269 3270 static ssize_t 3271 tracing_trace_options_write(struct file *filp, const char __user *ubuf, 3272 size_t cnt, loff_t *ppos) 3273 { 3274 struct seq_file *m = filp->private_data; 3275 struct trace_array *tr = m->private; 3276 char buf[64]; 3277 int ret; 3278 3279 if (cnt >= sizeof(buf)) 3280 return -EINVAL; 3281 3282 if (copy_from_user(&buf, ubuf, cnt)) 3283 return -EFAULT; 3284 3285 buf[cnt] = 0; 3286 3287 ret = trace_set_options(tr, buf); 3288 if (ret < 0) 3289 return ret; 3290 3291 *ppos += cnt; 3292 3293 return cnt; 3294 } 3295 3296 static int tracing_trace_options_open(struct inode *inode, struct file *file) 3297 { 3298 if (tracing_disabled) 3299 return -ENODEV; 3300 3301 return single_open(file, tracing_trace_options_show, inode->i_private); 3302 } 3303 3304 static const struct file_operations tracing_iter_fops = { 3305 .open = tracing_trace_options_open, 3306 .read = seq_read, 3307 .llseek = seq_lseek, 3308 .release = single_release, 3309 .write = tracing_trace_options_write, 3310 }; 3311 3312 static const char readme_msg[] = 3313 "tracing mini-HOWTO:\n\n" 3314 "# echo 0 > tracing_on : quick way to disable tracing\n" 3315 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n" 3316 " Important files:\n" 3317 " trace\t\t\t- The static contents of the buffer\n" 3318 "\t\t\t To clear the buffer write into this file: echo > trace\n" 3319 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n" 3320 " current_tracer\t- function and latency tracers\n" 3321 " available_tracers\t- list of configured tracers for current_tracer\n" 3322 " buffer_size_kb\t- view and modify size of per cpu buffer\n" 3323 " buffer_total_size_kb - view total size of all cpu buffers\n\n" 3324 " trace_clock\t\t-change the clock used to order events\n" 3325 " local: Per cpu clock but may not be synced across CPUs\n" 3326 " global: Synced across CPUs but slows tracing down.\n" 3327 " counter: Not a clock, but just an increment\n" 3328 " uptime: Jiffy counter from time of boot\n" 3329 " perf: Same clock that perf events use\n" 3330 #ifdef CONFIG_X86_64 3331 " x86-tsc: TSC cycle counter\n" 3332 #endif 3333 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n" 3334 " tracing_cpumask\t- Limit which CPUs to trace\n" 3335 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n" 3336 "\t\t\t Remove sub-buffer with rmdir\n" 3337 " trace_options\t\t- Set format or modify how tracing happens\n" 3338 "\t\t\t Disable an option by adding a suffix 'no' to the option name\n" 3339 #ifdef CONFIG_DYNAMIC_FTRACE 3340 "\n available_filter_functions - list of functions that can be filtered on\n" 3341 " set_ftrace_filter\t- echo function name in here to only trace these functions\n" 3342 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n" 3343 " modules: Can select a group via module\n" 3344 " Format: :mod:<module-name>\n" 3345 " example: echo :mod:ext3 > set_ftrace_filter\n" 3346 " triggers: a command to perform when function is hit\n" 3347 " Format: <function>:<trigger>[:count]\n" 3348 " trigger: traceon, traceoff\n" 3349 " enable_event:<system>:<event>\n" 3350 " disable_event:<system>:<event>\n" 3351 #ifdef CONFIG_STACKTRACE 3352 " stacktrace\n" 3353 #endif 3354 #ifdef CONFIG_TRACER_SNAPSHOT 3355 " snapshot\n" 3356 #endif 3357 " example: echo do_fault:traceoff > set_ftrace_filter\n" 3358 " echo do_trap:traceoff:3 > set_ftrace_filter\n" 3359 " The first one will disable tracing every time do_fault is hit\n" 3360 " The second will disable tracing at most 3 times when do_trap is hit\n" 3361 " The first time do trap is hit and it disables tracing, the counter\n" 3362 " will decrement to 2. If tracing is already disabled, the counter\n" 3363 " will not decrement. It only decrements when the trigger did work\n" 3364 " To remove trigger without count:\n" 3365 " echo '!<function>:<trigger> > set_ftrace_filter\n" 3366 " To remove trigger with a count:\n" 3367 " echo '!<function>:<trigger>:0 > set_ftrace_filter\n" 3368 " set_ftrace_notrace\t- echo function name in here to never trace.\n" 3369 " accepts: func_full_name, *func_end, func_begin*, *func_middle*\n" 3370 " modules: Can select a group via module command :mod:\n" 3371 " Does not accept triggers\n" 3372 #endif /* CONFIG_DYNAMIC_FTRACE */ 3373 #ifdef CONFIG_FUNCTION_TRACER 3374 " set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n" 3375 #endif 3376 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 3377 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n" 3378 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n" 3379 #endif 3380 #ifdef CONFIG_TRACER_SNAPSHOT 3381 "\n snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n" 3382 "\t\t\t Read the contents for more information\n" 3383 #endif 3384 #ifdef CONFIG_STACKTRACE 3385 " stack_trace\t\t- Shows the max stack trace when active\n" 3386 " stack_max_size\t- Shows current max stack size that was traced\n" 3387 "\t\t\t Write into this file to reset the max size (trigger a new trace)\n" 3388 #ifdef CONFIG_DYNAMIC_FTRACE 3389 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n" 3390 #endif 3391 #endif /* CONFIG_STACKTRACE */ 3392 ; 3393 3394 static ssize_t 3395 tracing_readme_read(struct file *filp, char __user *ubuf, 3396 size_t cnt, loff_t *ppos) 3397 { 3398 return simple_read_from_buffer(ubuf, cnt, ppos, 3399 readme_msg, strlen(readme_msg)); 3400 } 3401 3402 static const struct file_operations tracing_readme_fops = { 3403 .open = tracing_open_generic, 3404 .read = tracing_readme_read, 3405 .llseek = generic_file_llseek, 3406 }; 3407 3408 static ssize_t 3409 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf, 3410 size_t cnt, loff_t *ppos) 3411 { 3412 char *buf_comm; 3413 char *file_buf; 3414 char *buf; 3415 int len = 0; 3416 int pid; 3417 int i; 3418 3419 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL); 3420 if (!file_buf) 3421 return -ENOMEM; 3422 3423 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL); 3424 if (!buf_comm) { 3425 kfree(file_buf); 3426 return -ENOMEM; 3427 } 3428 3429 buf = file_buf; 3430 3431 for (i = 0; i < SAVED_CMDLINES; i++) { 3432 int r; 3433 3434 pid = map_cmdline_to_pid[i]; 3435 if (pid == -1 || pid == NO_CMDLINE_MAP) 3436 continue; 3437 3438 trace_find_cmdline(pid, buf_comm); 3439 r = sprintf(buf, "%d %s\n", pid, buf_comm); 3440 buf += r; 3441 len += r; 3442 } 3443 3444 len = simple_read_from_buffer(ubuf, cnt, ppos, 3445 file_buf, len); 3446 3447 kfree(file_buf); 3448 kfree(buf_comm); 3449 3450 return len; 3451 } 3452 3453 static const struct file_operations tracing_saved_cmdlines_fops = { 3454 .open = tracing_open_generic, 3455 .read = tracing_saved_cmdlines_read, 3456 .llseek = generic_file_llseek, 3457 }; 3458 3459 static ssize_t 3460 tracing_set_trace_read(struct file *filp, char __user *ubuf, 3461 size_t cnt, loff_t *ppos) 3462 { 3463 struct trace_array *tr = filp->private_data; 3464 char buf[MAX_TRACER_SIZE+2]; 3465 int r; 3466 3467 mutex_lock(&trace_types_lock); 3468 r = sprintf(buf, "%s\n", tr->current_trace->name); 3469 mutex_unlock(&trace_types_lock); 3470 3471 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 3472 } 3473 3474 int tracer_init(struct tracer *t, struct trace_array *tr) 3475 { 3476 tracing_reset_online_cpus(&tr->trace_buffer); 3477 return t->init(tr); 3478 } 3479 3480 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val) 3481 { 3482 int cpu; 3483 3484 for_each_tracing_cpu(cpu) 3485 per_cpu_ptr(buf->data, cpu)->entries = val; 3486 } 3487 3488 #ifdef CONFIG_TRACER_MAX_TRACE 3489 /* resize @tr's buffer to the size of @size_tr's entries */ 3490 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf, 3491 struct trace_buffer *size_buf, int cpu_id) 3492 { 3493 int cpu, ret = 0; 3494 3495 if (cpu_id == RING_BUFFER_ALL_CPUS) { 3496 for_each_tracing_cpu(cpu) { 3497 ret = ring_buffer_resize(trace_buf->buffer, 3498 per_cpu_ptr(size_buf->data, cpu)->entries, cpu); 3499 if (ret < 0) 3500 break; 3501 per_cpu_ptr(trace_buf->data, cpu)->entries = 3502 per_cpu_ptr(size_buf->data, cpu)->entries; 3503 } 3504 } else { 3505 ret = ring_buffer_resize(trace_buf->buffer, 3506 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); 3507 if (ret == 0) 3508 per_cpu_ptr(trace_buf->data, cpu_id)->entries = 3509 per_cpu_ptr(size_buf->data, cpu_id)->entries; 3510 } 3511 3512 return ret; 3513 } 3514 #endif /* CONFIG_TRACER_MAX_TRACE */ 3515 3516 static int __tracing_resize_ring_buffer(struct trace_array *tr, 3517 unsigned long size, int cpu) 3518 { 3519 int ret; 3520 3521 /* 3522 * If kernel or user changes the size of the ring buffer 3523 * we use the size that was given, and we can forget about 3524 * expanding it later. 3525 */ 3526 ring_buffer_expanded = true; 3527 3528 /* May be called before buffers are initialized */ 3529 if (!tr->trace_buffer.buffer) 3530 return 0; 3531 3532 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu); 3533 if (ret < 0) 3534 return ret; 3535 3536 #ifdef CONFIG_TRACER_MAX_TRACE 3537 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) || 3538 !tr->current_trace->use_max_tr) 3539 goto out; 3540 3541 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu); 3542 if (ret < 0) { 3543 int r = resize_buffer_duplicate_size(&tr->trace_buffer, 3544 &tr->trace_buffer, cpu); 3545 if (r < 0) { 3546 /* 3547 * AARGH! We are left with different 3548 * size max buffer!!!! 3549 * The max buffer is our "snapshot" buffer. 3550 * When a tracer needs a snapshot (one of the 3551 * latency tracers), it swaps the max buffer 3552 * with the saved snap shot. We succeeded to 3553 * update the size of the main buffer, but failed to 3554 * update the size of the max buffer. But when we tried 3555 * to reset the main buffer to the original size, we 3556 * failed there too. This is very unlikely to 3557 * happen, but if it does, warn and kill all 3558 * tracing. 3559 */ 3560 WARN_ON(1); 3561 tracing_disabled = 1; 3562 } 3563 return ret; 3564 } 3565 3566 if (cpu == RING_BUFFER_ALL_CPUS) 3567 set_buffer_entries(&tr->max_buffer, size); 3568 else 3569 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size; 3570 3571 out: 3572 #endif /* CONFIG_TRACER_MAX_TRACE */ 3573 3574 if (cpu == RING_BUFFER_ALL_CPUS) 3575 set_buffer_entries(&tr->trace_buffer, size); 3576 else 3577 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size; 3578 3579 return ret; 3580 } 3581 3582 static ssize_t tracing_resize_ring_buffer(struct trace_array *tr, 3583 unsigned long size, int cpu_id) 3584 { 3585 int ret = size; 3586 3587 mutex_lock(&trace_types_lock); 3588 3589 if (cpu_id != RING_BUFFER_ALL_CPUS) { 3590 /* make sure, this cpu is enabled in the mask */ 3591 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) { 3592 ret = -EINVAL; 3593 goto out; 3594 } 3595 } 3596 3597 ret = __tracing_resize_ring_buffer(tr, size, cpu_id); 3598 if (ret < 0) 3599 ret = -ENOMEM; 3600 3601 out: 3602 mutex_unlock(&trace_types_lock); 3603 3604 return ret; 3605 } 3606 3607 3608 /** 3609 * tracing_update_buffers - used by tracing facility to expand ring buffers 3610 * 3611 * To save on memory when the tracing is never used on a system with it 3612 * configured in. The ring buffers are set to a minimum size. But once 3613 * a user starts to use the tracing facility, then they need to grow 3614 * to their default size. 3615 * 3616 * This function is to be called when a tracer is about to be used. 3617 */ 3618 int tracing_update_buffers(void) 3619 { 3620 int ret = 0; 3621 3622 mutex_lock(&trace_types_lock); 3623 if (!ring_buffer_expanded) 3624 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size, 3625 RING_BUFFER_ALL_CPUS); 3626 mutex_unlock(&trace_types_lock); 3627 3628 return ret; 3629 } 3630 3631 struct trace_option_dentry; 3632 3633 static struct trace_option_dentry * 3634 create_trace_option_files(struct trace_array *tr, struct tracer *tracer); 3635 3636 static void 3637 destroy_trace_option_files(struct trace_option_dentry *topts); 3638 3639 static int tracing_set_tracer(const char *buf) 3640 { 3641 static struct trace_option_dentry *topts; 3642 struct trace_array *tr = &global_trace; 3643 struct tracer *t; 3644 #ifdef CONFIG_TRACER_MAX_TRACE 3645 bool had_max_tr; 3646 #endif 3647 int ret = 0; 3648 3649 mutex_lock(&trace_types_lock); 3650 3651 if (!ring_buffer_expanded) { 3652 ret = __tracing_resize_ring_buffer(tr, trace_buf_size, 3653 RING_BUFFER_ALL_CPUS); 3654 if (ret < 0) 3655 goto out; 3656 ret = 0; 3657 } 3658 3659 for (t = trace_types; t; t = t->next) { 3660 if (strcmp(t->name, buf) == 0) 3661 break; 3662 } 3663 if (!t) { 3664 ret = -EINVAL; 3665 goto out; 3666 } 3667 if (t == tr->current_trace) 3668 goto out; 3669 3670 trace_branch_disable(); 3671 3672 tr->current_trace->enabled = false; 3673 3674 if (tr->current_trace->reset) 3675 tr->current_trace->reset(tr); 3676 3677 /* Current trace needs to be nop_trace before synchronize_sched */ 3678 tr->current_trace = &nop_trace; 3679 3680 #ifdef CONFIG_TRACER_MAX_TRACE 3681 had_max_tr = tr->allocated_snapshot; 3682 3683 if (had_max_tr && !t->use_max_tr) { 3684 /* 3685 * We need to make sure that the update_max_tr sees that 3686 * current_trace changed to nop_trace to keep it from 3687 * swapping the buffers after we resize it. 3688 * The update_max_tr is called from interrupts disabled 3689 * so a synchronized_sched() is sufficient. 3690 */ 3691 synchronize_sched(); 3692 free_snapshot(tr); 3693 } 3694 #endif 3695 destroy_trace_option_files(topts); 3696 3697 topts = create_trace_option_files(tr, t); 3698 3699 #ifdef CONFIG_TRACER_MAX_TRACE 3700 if (t->use_max_tr && !had_max_tr) { 3701 ret = alloc_snapshot(tr); 3702 if (ret < 0) 3703 goto out; 3704 } 3705 #endif 3706 3707 if (t->init) { 3708 ret = tracer_init(t, tr); 3709 if (ret) 3710 goto out; 3711 } 3712 3713 tr->current_trace = t; 3714 tr->current_trace->enabled = true; 3715 trace_branch_enable(tr); 3716 out: 3717 mutex_unlock(&trace_types_lock); 3718 3719 return ret; 3720 } 3721 3722 static ssize_t 3723 tracing_set_trace_write(struct file *filp, const char __user *ubuf, 3724 size_t cnt, loff_t *ppos) 3725 { 3726 char buf[MAX_TRACER_SIZE+1]; 3727 int i; 3728 size_t ret; 3729 int err; 3730 3731 ret = cnt; 3732 3733 if (cnt > MAX_TRACER_SIZE) 3734 cnt = MAX_TRACER_SIZE; 3735 3736 if (copy_from_user(&buf, ubuf, cnt)) 3737 return -EFAULT; 3738 3739 buf[cnt] = 0; 3740 3741 /* strip ending whitespace. */ 3742 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) 3743 buf[i] = 0; 3744 3745 err = tracing_set_tracer(buf); 3746 if (err) 3747 return err; 3748 3749 *ppos += ret; 3750 3751 return ret; 3752 } 3753 3754 static ssize_t 3755 tracing_max_lat_read(struct file *filp, char __user *ubuf, 3756 size_t cnt, loff_t *ppos) 3757 { 3758 unsigned long *ptr = filp->private_data; 3759 char buf[64]; 3760 int r; 3761 3762 r = snprintf(buf, sizeof(buf), "%ld\n", 3763 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); 3764 if (r > sizeof(buf)) 3765 r = sizeof(buf); 3766 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 3767 } 3768 3769 static ssize_t 3770 tracing_max_lat_write(struct file *filp, const char __user *ubuf, 3771 size_t cnt, loff_t *ppos) 3772 { 3773 unsigned long *ptr = filp->private_data; 3774 unsigned long val; 3775 int ret; 3776 3777 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 3778 if (ret) 3779 return ret; 3780 3781 *ptr = val * 1000; 3782 3783 return cnt; 3784 } 3785 3786 static int tracing_open_pipe(struct inode *inode, struct file *filp) 3787 { 3788 struct trace_cpu *tc = inode->i_private; 3789 struct trace_array *tr = tc->tr; 3790 struct trace_iterator *iter; 3791 int ret = 0; 3792 3793 if (tracing_disabled) 3794 return -ENODEV; 3795 3796 mutex_lock(&trace_types_lock); 3797 3798 /* create a buffer to store the information to pass to userspace */ 3799 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 3800 if (!iter) { 3801 ret = -ENOMEM; 3802 goto out; 3803 } 3804 3805 /* 3806 * We make a copy of the current tracer to avoid concurrent 3807 * changes on it while we are reading. 3808 */ 3809 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL); 3810 if (!iter->trace) { 3811 ret = -ENOMEM; 3812 goto fail; 3813 } 3814 *iter->trace = *tr->current_trace; 3815 3816 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { 3817 ret = -ENOMEM; 3818 goto fail; 3819 } 3820 3821 /* trace pipe does not show start of buffer */ 3822 cpumask_setall(iter->started); 3823 3824 if (trace_flags & TRACE_ITER_LATENCY_FMT) 3825 iter->iter_flags |= TRACE_FILE_LAT_FMT; 3826 3827 /* Output in nanoseconds only if we are using a clock in nanoseconds. */ 3828 if (trace_clocks[trace_clock_id].in_ns) 3829 iter->iter_flags |= TRACE_FILE_TIME_IN_NS; 3830 3831 iter->cpu_file = tc->cpu; 3832 iter->tr = tc->tr; 3833 iter->trace_buffer = &tc->tr->trace_buffer; 3834 mutex_init(&iter->mutex); 3835 filp->private_data = iter; 3836 3837 if (iter->trace->pipe_open) 3838 iter->trace->pipe_open(iter); 3839 3840 nonseekable_open(inode, filp); 3841 out: 3842 mutex_unlock(&trace_types_lock); 3843 return ret; 3844 3845 fail: 3846 kfree(iter->trace); 3847 kfree(iter); 3848 mutex_unlock(&trace_types_lock); 3849 return ret; 3850 } 3851 3852 static int tracing_release_pipe(struct inode *inode, struct file *file) 3853 { 3854 struct trace_iterator *iter = file->private_data; 3855 3856 mutex_lock(&trace_types_lock); 3857 3858 if (iter->trace->pipe_close) 3859 iter->trace->pipe_close(iter); 3860 3861 mutex_unlock(&trace_types_lock); 3862 3863 free_cpumask_var(iter->started); 3864 mutex_destroy(&iter->mutex); 3865 kfree(iter->trace); 3866 kfree(iter); 3867 3868 return 0; 3869 } 3870 3871 static unsigned int 3872 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table) 3873 { 3874 /* Iterators are static, they should be filled or empty */ 3875 if (trace_buffer_iter(iter, iter->cpu_file)) 3876 return POLLIN | POLLRDNORM; 3877 3878 if (trace_flags & TRACE_ITER_BLOCK) 3879 /* 3880 * Always select as readable when in blocking mode 3881 */ 3882 return POLLIN | POLLRDNORM; 3883 else 3884 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file, 3885 filp, poll_table); 3886 } 3887 3888 static unsigned int 3889 tracing_poll_pipe(struct file *filp, poll_table *poll_table) 3890 { 3891 struct trace_iterator *iter = filp->private_data; 3892 3893 return trace_poll(iter, filp, poll_table); 3894 } 3895 3896 /* 3897 * This is a make-shift waitqueue. 3898 * A tracer might use this callback on some rare cases: 3899 * 3900 * 1) the current tracer might hold the runqueue lock when it wakes up 3901 * a reader, hence a deadlock (sched, function, and function graph tracers) 3902 * 2) the function tracers, trace all functions, we don't want 3903 * the overhead of calling wake_up and friends 3904 * (and tracing them too) 3905 * 3906 * Anyway, this is really very primitive wakeup. 3907 */ 3908 void poll_wait_pipe(struct trace_iterator *iter) 3909 { 3910 set_current_state(TASK_INTERRUPTIBLE); 3911 /* sleep for 100 msecs, and try again. */ 3912 schedule_timeout(HZ / 10); 3913 } 3914 3915 /* Must be called with trace_types_lock mutex held. */ 3916 static int tracing_wait_pipe(struct file *filp) 3917 { 3918 struct trace_iterator *iter = filp->private_data; 3919 3920 while (trace_empty(iter)) { 3921 3922 if ((filp->f_flags & O_NONBLOCK)) { 3923 return -EAGAIN; 3924 } 3925 3926 mutex_unlock(&iter->mutex); 3927 3928 iter->trace->wait_pipe(iter); 3929 3930 mutex_lock(&iter->mutex); 3931 3932 if (signal_pending(current)) 3933 return -EINTR; 3934 3935 /* 3936 * We block until we read something and tracing is disabled. 3937 * We still block if tracing is disabled, but we have never 3938 * read anything. This allows a user to cat this file, and 3939 * then enable tracing. But after we have read something, 3940 * we give an EOF when tracing is again disabled. 3941 * 3942 * iter->pos will be 0 if we haven't read anything. 3943 */ 3944 if (!tracing_is_enabled() && iter->pos) 3945 break; 3946 } 3947 3948 return 1; 3949 } 3950 3951 /* 3952 * Consumer reader. 3953 */ 3954 static ssize_t 3955 tracing_read_pipe(struct file *filp, char __user *ubuf, 3956 size_t cnt, loff_t *ppos) 3957 { 3958 struct trace_iterator *iter = filp->private_data; 3959 struct trace_array *tr = iter->tr; 3960 ssize_t sret; 3961 3962 /* return any leftover data */ 3963 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 3964 if (sret != -EBUSY) 3965 return sret; 3966 3967 trace_seq_init(&iter->seq); 3968 3969 /* copy the tracer to avoid using a global lock all around */ 3970 mutex_lock(&trace_types_lock); 3971 if (unlikely(iter->trace->name != tr->current_trace->name)) 3972 *iter->trace = *tr->current_trace; 3973 mutex_unlock(&trace_types_lock); 3974 3975 /* 3976 * Avoid more than one consumer on a single file descriptor 3977 * This is just a matter of traces coherency, the ring buffer itself 3978 * is protected. 3979 */ 3980 mutex_lock(&iter->mutex); 3981 if (iter->trace->read) { 3982 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); 3983 if (sret) 3984 goto out; 3985 } 3986 3987 waitagain: 3988 sret = tracing_wait_pipe(filp); 3989 if (sret <= 0) 3990 goto out; 3991 3992 /* stop when tracing is finished */ 3993 if (trace_empty(iter)) { 3994 sret = 0; 3995 goto out; 3996 } 3997 3998 if (cnt >= PAGE_SIZE) 3999 cnt = PAGE_SIZE - 1; 4000 4001 /* reset all but tr, trace, and overruns */ 4002 memset(&iter->seq, 0, 4003 sizeof(struct trace_iterator) - 4004 offsetof(struct trace_iterator, seq)); 4005 iter->pos = -1; 4006 4007 trace_event_read_lock(); 4008 trace_access_lock(iter->cpu_file); 4009 while (trace_find_next_entry_inc(iter) != NULL) { 4010 enum print_line_t ret; 4011 int len = iter->seq.len; 4012 4013 ret = print_trace_line(iter); 4014 if (ret == TRACE_TYPE_PARTIAL_LINE) { 4015 /* don't print partial lines */ 4016 iter->seq.len = len; 4017 break; 4018 } 4019 if (ret != TRACE_TYPE_NO_CONSUME) 4020 trace_consume(iter); 4021 4022 if (iter->seq.len >= cnt) 4023 break; 4024 4025 /* 4026 * Setting the full flag means we reached the trace_seq buffer 4027 * size and we should leave by partial output condition above. 4028 * One of the trace_seq_* functions is not used properly. 4029 */ 4030 WARN_ONCE(iter->seq.full, "full flag set for trace type %d", 4031 iter->ent->type); 4032 } 4033 trace_access_unlock(iter->cpu_file); 4034 trace_event_read_unlock(); 4035 4036 /* Now copy what we have to the user */ 4037 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 4038 if (iter->seq.readpos >= iter->seq.len) 4039 trace_seq_init(&iter->seq); 4040 4041 /* 4042 * If there was nothing to send to user, in spite of consuming trace 4043 * entries, go back to wait for more entries. 4044 */ 4045 if (sret == -EBUSY) 4046 goto waitagain; 4047 4048 out: 4049 mutex_unlock(&iter->mutex); 4050 4051 return sret; 4052 } 4053 4054 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe, 4055 struct pipe_buffer *buf) 4056 { 4057 __free_page(buf->page); 4058 } 4059 4060 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd, 4061 unsigned int idx) 4062 { 4063 __free_page(spd->pages[idx]); 4064 } 4065 4066 static const struct pipe_buf_operations tracing_pipe_buf_ops = { 4067 .can_merge = 0, 4068 .map = generic_pipe_buf_map, 4069 .unmap = generic_pipe_buf_unmap, 4070 .confirm = generic_pipe_buf_confirm, 4071 .release = tracing_pipe_buf_release, 4072 .steal = generic_pipe_buf_steal, 4073 .get = generic_pipe_buf_get, 4074 }; 4075 4076 static size_t 4077 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter) 4078 { 4079 size_t count; 4080 int ret; 4081 4082 /* Seq buffer is page-sized, exactly what we need. */ 4083 for (;;) { 4084 count = iter->seq.len; 4085 ret = print_trace_line(iter); 4086 count = iter->seq.len - count; 4087 if (rem < count) { 4088 rem = 0; 4089 iter->seq.len -= count; 4090 break; 4091 } 4092 if (ret == TRACE_TYPE_PARTIAL_LINE) { 4093 iter->seq.len -= count; 4094 break; 4095 } 4096 4097 if (ret != TRACE_TYPE_NO_CONSUME) 4098 trace_consume(iter); 4099 rem -= count; 4100 if (!trace_find_next_entry_inc(iter)) { 4101 rem = 0; 4102 iter->ent = NULL; 4103 break; 4104 } 4105 } 4106 4107 return rem; 4108 } 4109 4110 static ssize_t tracing_splice_read_pipe(struct file *filp, 4111 loff_t *ppos, 4112 struct pipe_inode_info *pipe, 4113 size_t len, 4114 unsigned int flags) 4115 { 4116 struct page *pages_def[PIPE_DEF_BUFFERS]; 4117 struct partial_page partial_def[PIPE_DEF_BUFFERS]; 4118 struct trace_iterator *iter = filp->private_data; 4119 struct splice_pipe_desc spd = { 4120 .pages = pages_def, 4121 .partial = partial_def, 4122 .nr_pages = 0, /* This gets updated below. */ 4123 .nr_pages_max = PIPE_DEF_BUFFERS, 4124 .flags = flags, 4125 .ops = &tracing_pipe_buf_ops, 4126 .spd_release = tracing_spd_release_pipe, 4127 }; 4128 struct trace_array *tr = iter->tr; 4129 ssize_t ret; 4130 size_t rem; 4131 unsigned int i; 4132 4133 if (splice_grow_spd(pipe, &spd)) 4134 return -ENOMEM; 4135 4136 /* copy the tracer to avoid using a global lock all around */ 4137 mutex_lock(&trace_types_lock); 4138 if (unlikely(iter->trace->name != tr->current_trace->name)) 4139 *iter->trace = *tr->current_trace; 4140 mutex_unlock(&trace_types_lock); 4141 4142 mutex_lock(&iter->mutex); 4143 4144 if (iter->trace->splice_read) { 4145 ret = iter->trace->splice_read(iter, filp, 4146 ppos, pipe, len, flags); 4147 if (ret) 4148 goto out_err; 4149 } 4150 4151 ret = tracing_wait_pipe(filp); 4152 if (ret <= 0) 4153 goto out_err; 4154 4155 if (!iter->ent && !trace_find_next_entry_inc(iter)) { 4156 ret = -EFAULT; 4157 goto out_err; 4158 } 4159 4160 trace_event_read_lock(); 4161 trace_access_lock(iter->cpu_file); 4162 4163 /* Fill as many pages as possible. */ 4164 for (i = 0, rem = len; i < pipe->buffers && rem; i++) { 4165 spd.pages[i] = alloc_page(GFP_KERNEL); 4166 if (!spd.pages[i]) 4167 break; 4168 4169 rem = tracing_fill_pipe_page(rem, iter); 4170 4171 /* Copy the data into the page, so we can start over. */ 4172 ret = trace_seq_to_buffer(&iter->seq, 4173 page_address(spd.pages[i]), 4174 iter->seq.len); 4175 if (ret < 0) { 4176 __free_page(spd.pages[i]); 4177 break; 4178 } 4179 spd.partial[i].offset = 0; 4180 spd.partial[i].len = iter->seq.len; 4181 4182 trace_seq_init(&iter->seq); 4183 } 4184 4185 trace_access_unlock(iter->cpu_file); 4186 trace_event_read_unlock(); 4187 mutex_unlock(&iter->mutex); 4188 4189 spd.nr_pages = i; 4190 4191 ret = splice_to_pipe(pipe, &spd); 4192 out: 4193 splice_shrink_spd(&spd); 4194 return ret; 4195 4196 out_err: 4197 mutex_unlock(&iter->mutex); 4198 goto out; 4199 } 4200 4201 static ssize_t 4202 tracing_entries_read(struct file *filp, char __user *ubuf, 4203 size_t cnt, loff_t *ppos) 4204 { 4205 struct trace_cpu *tc = filp->private_data; 4206 struct trace_array *tr = tc->tr; 4207 char buf[64]; 4208 int r = 0; 4209 ssize_t ret; 4210 4211 mutex_lock(&trace_types_lock); 4212 4213 if (tc->cpu == RING_BUFFER_ALL_CPUS) { 4214 int cpu, buf_size_same; 4215 unsigned long size; 4216 4217 size = 0; 4218 buf_size_same = 1; 4219 /* check if all cpu sizes are same */ 4220 for_each_tracing_cpu(cpu) { 4221 /* fill in the size from first enabled cpu */ 4222 if (size == 0) 4223 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries; 4224 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) { 4225 buf_size_same = 0; 4226 break; 4227 } 4228 } 4229 4230 if (buf_size_same) { 4231 if (!ring_buffer_expanded) 4232 r = sprintf(buf, "%lu (expanded: %lu)\n", 4233 size >> 10, 4234 trace_buf_size >> 10); 4235 else 4236 r = sprintf(buf, "%lu\n", size >> 10); 4237 } else 4238 r = sprintf(buf, "X\n"); 4239 } else 4240 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10); 4241 4242 mutex_unlock(&trace_types_lock); 4243 4244 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 4245 return ret; 4246 } 4247 4248 static ssize_t 4249 tracing_entries_write(struct file *filp, const char __user *ubuf, 4250 size_t cnt, loff_t *ppos) 4251 { 4252 struct trace_cpu *tc = filp->private_data; 4253 unsigned long val; 4254 int ret; 4255 4256 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 4257 if (ret) 4258 return ret; 4259 4260 /* must have at least 1 entry */ 4261 if (!val) 4262 return -EINVAL; 4263 4264 /* value is in KB */ 4265 val <<= 10; 4266 4267 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu); 4268 if (ret < 0) 4269 return ret; 4270 4271 *ppos += cnt; 4272 4273 return cnt; 4274 } 4275 4276 static ssize_t 4277 tracing_total_entries_read(struct file *filp, char __user *ubuf, 4278 size_t cnt, loff_t *ppos) 4279 { 4280 struct trace_array *tr = filp->private_data; 4281 char buf[64]; 4282 int r, cpu; 4283 unsigned long size = 0, expanded_size = 0; 4284 4285 mutex_lock(&trace_types_lock); 4286 for_each_tracing_cpu(cpu) { 4287 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10; 4288 if (!ring_buffer_expanded) 4289 expanded_size += trace_buf_size >> 10; 4290 } 4291 if (ring_buffer_expanded) 4292 r = sprintf(buf, "%lu\n", size); 4293 else 4294 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size); 4295 mutex_unlock(&trace_types_lock); 4296 4297 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 4298 } 4299 4300 static ssize_t 4301 tracing_free_buffer_write(struct file *filp, const char __user *ubuf, 4302 size_t cnt, loff_t *ppos) 4303 { 4304 /* 4305 * There is no need to read what the user has written, this function 4306 * is just to make sure that there is no error when "echo" is used 4307 */ 4308 4309 *ppos += cnt; 4310 4311 return cnt; 4312 } 4313 4314 static int 4315 tracing_free_buffer_release(struct inode *inode, struct file *filp) 4316 { 4317 struct trace_array *tr = inode->i_private; 4318 4319 /* disable tracing ? */ 4320 if (trace_flags & TRACE_ITER_STOP_ON_FREE) 4321 tracing_off(); 4322 /* resize the ring buffer to 0 */ 4323 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS); 4324 4325 return 0; 4326 } 4327 4328 static ssize_t 4329 tracing_mark_write(struct file *filp, const char __user *ubuf, 4330 size_t cnt, loff_t *fpos) 4331 { 4332 unsigned long addr = (unsigned long)ubuf; 4333 struct ring_buffer_event *event; 4334 struct ring_buffer *buffer; 4335 struct print_entry *entry; 4336 unsigned long irq_flags; 4337 struct page *pages[2]; 4338 void *map_page[2]; 4339 int nr_pages = 1; 4340 ssize_t written; 4341 int offset; 4342 int size; 4343 int len; 4344 int ret; 4345 int i; 4346 4347 if (tracing_disabled) 4348 return -EINVAL; 4349 4350 if (!(trace_flags & TRACE_ITER_MARKERS)) 4351 return -EINVAL; 4352 4353 if (cnt > TRACE_BUF_SIZE) 4354 cnt = TRACE_BUF_SIZE; 4355 4356 /* 4357 * Userspace is injecting traces into the kernel trace buffer. 4358 * We want to be as non intrusive as possible. 4359 * To do so, we do not want to allocate any special buffers 4360 * or take any locks, but instead write the userspace data 4361 * straight into the ring buffer. 4362 * 4363 * First we need to pin the userspace buffer into memory, 4364 * which, most likely it is, because it just referenced it. 4365 * But there's no guarantee that it is. By using get_user_pages_fast() 4366 * and kmap_atomic/kunmap_atomic() we can get access to the 4367 * pages directly. We then write the data directly into the 4368 * ring buffer. 4369 */ 4370 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE); 4371 4372 /* check if we cross pages */ 4373 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK)) 4374 nr_pages = 2; 4375 4376 offset = addr & (PAGE_SIZE - 1); 4377 addr &= PAGE_MASK; 4378 4379 ret = get_user_pages_fast(addr, nr_pages, 0, pages); 4380 if (ret < nr_pages) { 4381 while (--ret >= 0) 4382 put_page(pages[ret]); 4383 written = -EFAULT; 4384 goto out; 4385 } 4386 4387 for (i = 0; i < nr_pages; i++) 4388 map_page[i] = kmap_atomic(pages[i]); 4389 4390 local_save_flags(irq_flags); 4391 size = sizeof(*entry) + cnt + 2; /* possible \n added */ 4392 buffer = global_trace.trace_buffer.buffer; 4393 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, 4394 irq_flags, preempt_count()); 4395 if (!event) { 4396 /* Ring buffer disabled, return as if not open for write */ 4397 written = -EBADF; 4398 goto out_unlock; 4399 } 4400 4401 entry = ring_buffer_event_data(event); 4402 entry->ip = _THIS_IP_; 4403 4404 if (nr_pages == 2) { 4405 len = PAGE_SIZE - offset; 4406 memcpy(&entry->buf, map_page[0] + offset, len); 4407 memcpy(&entry->buf[len], map_page[1], cnt - len); 4408 } else 4409 memcpy(&entry->buf, map_page[0] + offset, cnt); 4410 4411 if (entry->buf[cnt - 1] != '\n') { 4412 entry->buf[cnt] = '\n'; 4413 entry->buf[cnt + 1] = '\0'; 4414 } else 4415 entry->buf[cnt] = '\0'; 4416 4417 __buffer_unlock_commit(buffer, event); 4418 4419 written = cnt; 4420 4421 *fpos += written; 4422 4423 out_unlock: 4424 for (i = 0; i < nr_pages; i++){ 4425 kunmap_atomic(map_page[i]); 4426 put_page(pages[i]); 4427 } 4428 out: 4429 return written; 4430 } 4431 4432 static int tracing_clock_show(struct seq_file *m, void *v) 4433 { 4434 struct trace_array *tr = m->private; 4435 int i; 4436 4437 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) 4438 seq_printf(m, 4439 "%s%s%s%s", i ? " " : "", 4440 i == tr->clock_id ? "[" : "", trace_clocks[i].name, 4441 i == tr->clock_id ? "]" : ""); 4442 seq_putc(m, '\n'); 4443 4444 return 0; 4445 } 4446 4447 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf, 4448 size_t cnt, loff_t *fpos) 4449 { 4450 struct seq_file *m = filp->private_data; 4451 struct trace_array *tr = m->private; 4452 char buf[64]; 4453 const char *clockstr; 4454 int i; 4455 4456 if (cnt >= sizeof(buf)) 4457 return -EINVAL; 4458 4459 if (copy_from_user(&buf, ubuf, cnt)) 4460 return -EFAULT; 4461 4462 buf[cnt] = 0; 4463 4464 clockstr = strstrip(buf); 4465 4466 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) { 4467 if (strcmp(trace_clocks[i].name, clockstr) == 0) 4468 break; 4469 } 4470 if (i == ARRAY_SIZE(trace_clocks)) 4471 return -EINVAL; 4472 4473 mutex_lock(&trace_types_lock); 4474 4475 tr->clock_id = i; 4476 4477 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func); 4478 4479 /* 4480 * New clock may not be consistent with the previous clock. 4481 * Reset the buffer so that it doesn't have incomparable timestamps. 4482 */ 4483 tracing_reset_online_cpus(&global_trace.trace_buffer); 4484 4485 #ifdef CONFIG_TRACER_MAX_TRACE 4486 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer) 4487 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func); 4488 tracing_reset_online_cpus(&global_trace.max_buffer); 4489 #endif 4490 4491 mutex_unlock(&trace_types_lock); 4492 4493 *fpos += cnt; 4494 4495 return cnt; 4496 } 4497 4498 static int tracing_clock_open(struct inode *inode, struct file *file) 4499 { 4500 if (tracing_disabled) 4501 return -ENODEV; 4502 4503 return single_open(file, tracing_clock_show, inode->i_private); 4504 } 4505 4506 struct ftrace_buffer_info { 4507 struct trace_iterator iter; 4508 void *spare; 4509 unsigned int read; 4510 }; 4511 4512 #ifdef CONFIG_TRACER_SNAPSHOT 4513 static int tracing_snapshot_open(struct inode *inode, struct file *file) 4514 { 4515 struct trace_cpu *tc = inode->i_private; 4516 struct trace_iterator *iter; 4517 struct seq_file *m; 4518 int ret = 0; 4519 4520 if (file->f_mode & FMODE_READ) { 4521 iter = __tracing_open(inode, file, true); 4522 if (IS_ERR(iter)) 4523 ret = PTR_ERR(iter); 4524 } else { 4525 /* Writes still need the seq_file to hold the private data */ 4526 m = kzalloc(sizeof(*m), GFP_KERNEL); 4527 if (!m) 4528 return -ENOMEM; 4529 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 4530 if (!iter) { 4531 kfree(m); 4532 return -ENOMEM; 4533 } 4534 iter->tr = tc->tr; 4535 iter->trace_buffer = &tc->tr->max_buffer; 4536 iter->cpu_file = tc->cpu; 4537 m->private = iter; 4538 file->private_data = m; 4539 } 4540 4541 return ret; 4542 } 4543 4544 static ssize_t 4545 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, 4546 loff_t *ppos) 4547 { 4548 struct seq_file *m = filp->private_data; 4549 struct trace_iterator *iter = m->private; 4550 struct trace_array *tr = iter->tr; 4551 unsigned long val; 4552 int ret; 4553 4554 ret = tracing_update_buffers(); 4555 if (ret < 0) 4556 return ret; 4557 4558 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 4559 if (ret) 4560 return ret; 4561 4562 mutex_lock(&trace_types_lock); 4563 4564 if (tr->current_trace->use_max_tr) { 4565 ret = -EBUSY; 4566 goto out; 4567 } 4568 4569 switch (val) { 4570 case 0: 4571 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 4572 ret = -EINVAL; 4573 break; 4574 } 4575 if (tr->allocated_snapshot) 4576 free_snapshot(tr); 4577 break; 4578 case 1: 4579 /* Only allow per-cpu swap if the ring buffer supports it */ 4580 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP 4581 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) { 4582 ret = -EINVAL; 4583 break; 4584 } 4585 #endif 4586 if (!tr->allocated_snapshot) { 4587 ret = alloc_snapshot(tr); 4588 if (ret < 0) 4589 break; 4590 } 4591 local_irq_disable(); 4592 /* Now, we're going to swap */ 4593 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 4594 update_max_tr(tr, current, smp_processor_id()); 4595 else 4596 update_max_tr_single(tr, current, iter->cpu_file); 4597 local_irq_enable(); 4598 break; 4599 default: 4600 if (tr->allocated_snapshot) { 4601 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) 4602 tracing_reset_online_cpus(&tr->max_buffer); 4603 else 4604 tracing_reset(&tr->max_buffer, iter->cpu_file); 4605 } 4606 break; 4607 } 4608 4609 if (ret >= 0) { 4610 *ppos += cnt; 4611 ret = cnt; 4612 } 4613 out: 4614 mutex_unlock(&trace_types_lock); 4615 return ret; 4616 } 4617 4618 static int tracing_snapshot_release(struct inode *inode, struct file *file) 4619 { 4620 struct seq_file *m = file->private_data; 4621 4622 if (file->f_mode & FMODE_READ) 4623 return tracing_release(inode, file); 4624 4625 /* If write only, the seq_file is just a stub */ 4626 if (m) 4627 kfree(m->private); 4628 kfree(m); 4629 4630 return 0; 4631 } 4632 4633 static int tracing_buffers_open(struct inode *inode, struct file *filp); 4634 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, 4635 size_t count, loff_t *ppos); 4636 static int tracing_buffers_release(struct inode *inode, struct file *file); 4637 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, 4638 struct pipe_inode_info *pipe, size_t len, unsigned int flags); 4639 4640 static int snapshot_raw_open(struct inode *inode, struct file *filp) 4641 { 4642 struct ftrace_buffer_info *info; 4643 int ret; 4644 4645 ret = tracing_buffers_open(inode, filp); 4646 if (ret < 0) 4647 return ret; 4648 4649 info = filp->private_data; 4650 4651 if (info->iter.trace->use_max_tr) { 4652 tracing_buffers_release(inode, filp); 4653 return -EBUSY; 4654 } 4655 4656 info->iter.snapshot = true; 4657 info->iter.trace_buffer = &info->iter.tr->max_buffer; 4658 4659 return ret; 4660 } 4661 4662 #endif /* CONFIG_TRACER_SNAPSHOT */ 4663 4664 4665 static const struct file_operations tracing_max_lat_fops = { 4666 .open = tracing_open_generic, 4667 .read = tracing_max_lat_read, 4668 .write = tracing_max_lat_write, 4669 .llseek = generic_file_llseek, 4670 }; 4671 4672 static const struct file_operations set_tracer_fops = { 4673 .open = tracing_open_generic, 4674 .read = tracing_set_trace_read, 4675 .write = tracing_set_trace_write, 4676 .llseek = generic_file_llseek, 4677 }; 4678 4679 static const struct file_operations tracing_pipe_fops = { 4680 .open = tracing_open_pipe, 4681 .poll = tracing_poll_pipe, 4682 .read = tracing_read_pipe, 4683 .splice_read = tracing_splice_read_pipe, 4684 .release = tracing_release_pipe, 4685 .llseek = no_llseek, 4686 }; 4687 4688 static const struct file_operations tracing_entries_fops = { 4689 .open = tracing_open_generic, 4690 .read = tracing_entries_read, 4691 .write = tracing_entries_write, 4692 .llseek = generic_file_llseek, 4693 }; 4694 4695 static const struct file_operations tracing_total_entries_fops = { 4696 .open = tracing_open_generic, 4697 .read = tracing_total_entries_read, 4698 .llseek = generic_file_llseek, 4699 }; 4700 4701 static const struct file_operations tracing_free_buffer_fops = { 4702 .write = tracing_free_buffer_write, 4703 .release = tracing_free_buffer_release, 4704 }; 4705 4706 static const struct file_operations tracing_mark_fops = { 4707 .open = tracing_open_generic, 4708 .write = tracing_mark_write, 4709 .llseek = generic_file_llseek, 4710 }; 4711 4712 static const struct file_operations trace_clock_fops = { 4713 .open = tracing_clock_open, 4714 .read = seq_read, 4715 .llseek = seq_lseek, 4716 .release = single_release, 4717 .write = tracing_clock_write, 4718 }; 4719 4720 #ifdef CONFIG_TRACER_SNAPSHOT 4721 static const struct file_operations snapshot_fops = { 4722 .open = tracing_snapshot_open, 4723 .read = seq_read, 4724 .write = tracing_snapshot_write, 4725 .llseek = tracing_seek, 4726 .release = tracing_snapshot_release, 4727 }; 4728 4729 static const struct file_operations snapshot_raw_fops = { 4730 .open = snapshot_raw_open, 4731 .read = tracing_buffers_read, 4732 .release = tracing_buffers_release, 4733 .splice_read = tracing_buffers_splice_read, 4734 .llseek = no_llseek, 4735 }; 4736 4737 #endif /* CONFIG_TRACER_SNAPSHOT */ 4738 4739 static int tracing_buffers_open(struct inode *inode, struct file *filp) 4740 { 4741 struct trace_cpu *tc = inode->i_private; 4742 struct trace_array *tr = tc->tr; 4743 struct ftrace_buffer_info *info; 4744 4745 if (tracing_disabled) 4746 return -ENODEV; 4747 4748 info = kzalloc(sizeof(*info), GFP_KERNEL); 4749 if (!info) 4750 return -ENOMEM; 4751 4752 mutex_lock(&trace_types_lock); 4753 4754 tr->ref++; 4755 4756 info->iter.tr = tr; 4757 info->iter.cpu_file = tc->cpu; 4758 info->iter.trace = tr->current_trace; 4759 info->iter.trace_buffer = &tr->trace_buffer; 4760 info->spare = NULL; 4761 /* Force reading ring buffer for first read */ 4762 info->read = (unsigned int)-1; 4763 4764 filp->private_data = info; 4765 4766 mutex_unlock(&trace_types_lock); 4767 4768 return nonseekable_open(inode, filp); 4769 } 4770 4771 static unsigned int 4772 tracing_buffers_poll(struct file *filp, poll_table *poll_table) 4773 { 4774 struct ftrace_buffer_info *info = filp->private_data; 4775 struct trace_iterator *iter = &info->iter; 4776 4777 return trace_poll(iter, filp, poll_table); 4778 } 4779 4780 static ssize_t 4781 tracing_buffers_read(struct file *filp, char __user *ubuf, 4782 size_t count, loff_t *ppos) 4783 { 4784 struct ftrace_buffer_info *info = filp->private_data; 4785 struct trace_iterator *iter = &info->iter; 4786 ssize_t ret; 4787 ssize_t size; 4788 4789 if (!count) 4790 return 0; 4791 4792 mutex_lock(&trace_types_lock); 4793 4794 #ifdef CONFIG_TRACER_MAX_TRACE 4795 if (iter->snapshot && iter->tr->current_trace->use_max_tr) { 4796 size = -EBUSY; 4797 goto out_unlock; 4798 } 4799 #endif 4800 4801 if (!info->spare) 4802 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer, 4803 iter->cpu_file); 4804 size = -ENOMEM; 4805 if (!info->spare) 4806 goto out_unlock; 4807 4808 /* Do we have previous read data to read? */ 4809 if (info->read < PAGE_SIZE) 4810 goto read; 4811 4812 again: 4813 trace_access_lock(iter->cpu_file); 4814 ret = ring_buffer_read_page(iter->trace_buffer->buffer, 4815 &info->spare, 4816 count, 4817 iter->cpu_file, 0); 4818 trace_access_unlock(iter->cpu_file); 4819 4820 if (ret < 0) { 4821 if (trace_empty(iter)) { 4822 if ((filp->f_flags & O_NONBLOCK)) { 4823 size = -EAGAIN; 4824 goto out_unlock; 4825 } 4826 mutex_unlock(&trace_types_lock); 4827 iter->trace->wait_pipe(iter); 4828 mutex_lock(&trace_types_lock); 4829 if (signal_pending(current)) { 4830 size = -EINTR; 4831 goto out_unlock; 4832 } 4833 goto again; 4834 } 4835 size = 0; 4836 goto out_unlock; 4837 } 4838 4839 info->read = 0; 4840 read: 4841 size = PAGE_SIZE - info->read; 4842 if (size > count) 4843 size = count; 4844 4845 ret = copy_to_user(ubuf, info->spare + info->read, size); 4846 if (ret == size) { 4847 size = -EFAULT; 4848 goto out_unlock; 4849 } 4850 size -= ret; 4851 4852 *ppos += size; 4853 info->read += size; 4854 4855 out_unlock: 4856 mutex_unlock(&trace_types_lock); 4857 4858 return size; 4859 } 4860 4861 static int tracing_buffers_release(struct inode *inode, struct file *file) 4862 { 4863 struct ftrace_buffer_info *info = file->private_data; 4864 struct trace_iterator *iter = &info->iter; 4865 4866 mutex_lock(&trace_types_lock); 4867 4868 WARN_ON(!iter->tr->ref); 4869 iter->tr->ref--; 4870 4871 if (info->spare) 4872 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare); 4873 kfree(info); 4874 4875 mutex_unlock(&trace_types_lock); 4876 4877 return 0; 4878 } 4879 4880 struct buffer_ref { 4881 struct ring_buffer *buffer; 4882 void *page; 4883 int ref; 4884 }; 4885 4886 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, 4887 struct pipe_buffer *buf) 4888 { 4889 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 4890 4891 if (--ref->ref) 4892 return; 4893 4894 ring_buffer_free_read_page(ref->buffer, ref->page); 4895 kfree(ref); 4896 buf->private = 0; 4897 } 4898 4899 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe, 4900 struct pipe_buffer *buf) 4901 { 4902 struct buffer_ref *ref = (struct buffer_ref *)buf->private; 4903 4904 ref->ref++; 4905 } 4906 4907 /* Pipe buffer operations for a buffer. */ 4908 static const struct pipe_buf_operations buffer_pipe_buf_ops = { 4909 .can_merge = 0, 4910 .map = generic_pipe_buf_map, 4911 .unmap = generic_pipe_buf_unmap, 4912 .confirm = generic_pipe_buf_confirm, 4913 .release = buffer_pipe_buf_release, 4914 .steal = generic_pipe_buf_steal, 4915 .get = buffer_pipe_buf_get, 4916 }; 4917 4918 /* 4919 * Callback from splice_to_pipe(), if we need to release some pages 4920 * at the end of the spd in case we error'ed out in filling the pipe. 4921 */ 4922 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) 4923 { 4924 struct buffer_ref *ref = 4925 (struct buffer_ref *)spd->partial[i].private; 4926 4927 if (--ref->ref) 4928 return; 4929 4930 ring_buffer_free_read_page(ref->buffer, ref->page); 4931 kfree(ref); 4932 spd->partial[i].private = 0; 4933 } 4934 4935 static ssize_t 4936 tracing_buffers_splice_read(struct file *file, loff_t *ppos, 4937 struct pipe_inode_info *pipe, size_t len, 4938 unsigned int flags) 4939 { 4940 struct ftrace_buffer_info *info = file->private_data; 4941 struct trace_iterator *iter = &info->iter; 4942 struct partial_page partial_def[PIPE_DEF_BUFFERS]; 4943 struct page *pages_def[PIPE_DEF_BUFFERS]; 4944 struct splice_pipe_desc spd = { 4945 .pages = pages_def, 4946 .partial = partial_def, 4947 .nr_pages_max = PIPE_DEF_BUFFERS, 4948 .flags = flags, 4949 .ops = &buffer_pipe_buf_ops, 4950 .spd_release = buffer_spd_release, 4951 }; 4952 struct buffer_ref *ref; 4953 int entries, size, i; 4954 ssize_t ret; 4955 4956 mutex_lock(&trace_types_lock); 4957 4958 #ifdef CONFIG_TRACER_MAX_TRACE 4959 if (iter->snapshot && iter->tr->current_trace->use_max_tr) { 4960 ret = -EBUSY; 4961 goto out; 4962 } 4963 #endif 4964 4965 if (splice_grow_spd(pipe, &spd)) { 4966 ret = -ENOMEM; 4967 goto out; 4968 } 4969 4970 if (*ppos & (PAGE_SIZE - 1)) { 4971 ret = -EINVAL; 4972 goto out; 4973 } 4974 4975 if (len & (PAGE_SIZE - 1)) { 4976 if (len < PAGE_SIZE) { 4977 ret = -EINVAL; 4978 goto out; 4979 } 4980 len &= PAGE_MASK; 4981 } 4982 4983 again: 4984 trace_access_lock(iter->cpu_file); 4985 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file); 4986 4987 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) { 4988 struct page *page; 4989 int r; 4990 4991 ref = kzalloc(sizeof(*ref), GFP_KERNEL); 4992 if (!ref) 4993 break; 4994 4995 ref->ref = 1; 4996 ref->buffer = iter->trace_buffer->buffer; 4997 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); 4998 if (!ref->page) { 4999 kfree(ref); 5000 break; 5001 } 5002 5003 r = ring_buffer_read_page(ref->buffer, &ref->page, 5004 len, iter->cpu_file, 1); 5005 if (r < 0) { 5006 ring_buffer_free_read_page(ref->buffer, ref->page); 5007 kfree(ref); 5008 break; 5009 } 5010 5011 /* 5012 * zero out any left over data, this is going to 5013 * user land. 5014 */ 5015 size = ring_buffer_page_len(ref->page); 5016 if (size < PAGE_SIZE) 5017 memset(ref->page + size, 0, PAGE_SIZE - size); 5018 5019 page = virt_to_page(ref->page); 5020 5021 spd.pages[i] = page; 5022 spd.partial[i].len = PAGE_SIZE; 5023 spd.partial[i].offset = 0; 5024 spd.partial[i].private = (unsigned long)ref; 5025 spd.nr_pages++; 5026 *ppos += PAGE_SIZE; 5027 5028 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file); 5029 } 5030 5031 trace_access_unlock(iter->cpu_file); 5032 spd.nr_pages = i; 5033 5034 /* did we read anything? */ 5035 if (!spd.nr_pages) { 5036 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) { 5037 ret = -EAGAIN; 5038 goto out; 5039 } 5040 mutex_unlock(&trace_types_lock); 5041 iter->trace->wait_pipe(iter); 5042 mutex_lock(&trace_types_lock); 5043 if (signal_pending(current)) { 5044 ret = -EINTR; 5045 goto out; 5046 } 5047 goto again; 5048 } 5049 5050 ret = splice_to_pipe(pipe, &spd); 5051 splice_shrink_spd(&spd); 5052 out: 5053 mutex_unlock(&trace_types_lock); 5054 5055 return ret; 5056 } 5057 5058 static const struct file_operations tracing_buffers_fops = { 5059 .open = tracing_buffers_open, 5060 .read = tracing_buffers_read, 5061 .poll = tracing_buffers_poll, 5062 .release = tracing_buffers_release, 5063 .splice_read = tracing_buffers_splice_read, 5064 .llseek = no_llseek, 5065 }; 5066 5067 static ssize_t 5068 tracing_stats_read(struct file *filp, char __user *ubuf, 5069 size_t count, loff_t *ppos) 5070 { 5071 struct trace_cpu *tc = filp->private_data; 5072 struct trace_array *tr = tc->tr; 5073 struct trace_buffer *trace_buf = &tr->trace_buffer; 5074 struct trace_seq *s; 5075 unsigned long cnt; 5076 unsigned long long t; 5077 unsigned long usec_rem; 5078 int cpu = tc->cpu; 5079 5080 s = kmalloc(sizeof(*s), GFP_KERNEL); 5081 if (!s) 5082 return -ENOMEM; 5083 5084 trace_seq_init(s); 5085 5086 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu); 5087 trace_seq_printf(s, "entries: %ld\n", cnt); 5088 5089 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu); 5090 trace_seq_printf(s, "overrun: %ld\n", cnt); 5091 5092 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu); 5093 trace_seq_printf(s, "commit overrun: %ld\n", cnt); 5094 5095 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu); 5096 trace_seq_printf(s, "bytes: %ld\n", cnt); 5097 5098 if (trace_clocks[trace_clock_id].in_ns) { 5099 /* local or global for trace_clock */ 5100 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 5101 usec_rem = do_div(t, USEC_PER_SEC); 5102 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", 5103 t, usec_rem); 5104 5105 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu)); 5106 usec_rem = do_div(t, USEC_PER_SEC); 5107 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem); 5108 } else { 5109 /* counter or tsc mode for trace_clock */ 5110 trace_seq_printf(s, "oldest event ts: %llu\n", 5111 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu)); 5112 5113 trace_seq_printf(s, "now ts: %llu\n", 5114 ring_buffer_time_stamp(trace_buf->buffer, cpu)); 5115 } 5116 5117 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu); 5118 trace_seq_printf(s, "dropped events: %ld\n", cnt); 5119 5120 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu); 5121 trace_seq_printf(s, "read events: %ld\n", cnt); 5122 5123 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len); 5124 5125 kfree(s); 5126 5127 return count; 5128 } 5129 5130 static const struct file_operations tracing_stats_fops = { 5131 .open = tracing_open_generic, 5132 .read = tracing_stats_read, 5133 .llseek = generic_file_llseek, 5134 }; 5135 5136 #ifdef CONFIG_DYNAMIC_FTRACE 5137 5138 int __weak ftrace_arch_read_dyn_info(char *buf, int size) 5139 { 5140 return 0; 5141 } 5142 5143 static ssize_t 5144 tracing_read_dyn_info(struct file *filp, char __user *ubuf, 5145 size_t cnt, loff_t *ppos) 5146 { 5147 static char ftrace_dyn_info_buffer[1024]; 5148 static DEFINE_MUTEX(dyn_info_mutex); 5149 unsigned long *p = filp->private_data; 5150 char *buf = ftrace_dyn_info_buffer; 5151 int size = ARRAY_SIZE(ftrace_dyn_info_buffer); 5152 int r; 5153 5154 mutex_lock(&dyn_info_mutex); 5155 r = sprintf(buf, "%ld ", *p); 5156 5157 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r); 5158 buf[r++] = '\n'; 5159 5160 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 5161 5162 mutex_unlock(&dyn_info_mutex); 5163 5164 return r; 5165 } 5166 5167 static const struct file_operations tracing_dyn_info_fops = { 5168 .open = tracing_open_generic, 5169 .read = tracing_read_dyn_info, 5170 .llseek = generic_file_llseek, 5171 }; 5172 #endif /* CONFIG_DYNAMIC_FTRACE */ 5173 5174 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) 5175 static void 5176 ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data) 5177 { 5178 tracing_snapshot(); 5179 } 5180 5181 static void 5182 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data) 5183 { 5184 unsigned long *count = (long *)data; 5185 5186 if (!*count) 5187 return; 5188 5189 if (*count != -1) 5190 (*count)--; 5191 5192 tracing_snapshot(); 5193 } 5194 5195 static int 5196 ftrace_snapshot_print(struct seq_file *m, unsigned long ip, 5197 struct ftrace_probe_ops *ops, void *data) 5198 { 5199 long count = (long)data; 5200 5201 seq_printf(m, "%ps:", (void *)ip); 5202 5203 seq_printf(m, "snapshot"); 5204 5205 if (count == -1) 5206 seq_printf(m, ":unlimited\n"); 5207 else 5208 seq_printf(m, ":count=%ld\n", count); 5209 5210 return 0; 5211 } 5212 5213 static struct ftrace_probe_ops snapshot_probe_ops = { 5214 .func = ftrace_snapshot, 5215 .print = ftrace_snapshot_print, 5216 }; 5217 5218 static struct ftrace_probe_ops snapshot_count_probe_ops = { 5219 .func = ftrace_count_snapshot, 5220 .print = ftrace_snapshot_print, 5221 }; 5222 5223 static int 5224 ftrace_trace_snapshot_callback(struct ftrace_hash *hash, 5225 char *glob, char *cmd, char *param, int enable) 5226 { 5227 struct ftrace_probe_ops *ops; 5228 void *count = (void *)-1; 5229 char *number; 5230 int ret; 5231 5232 /* hash funcs only work with set_ftrace_filter */ 5233 if (!enable) 5234 return -EINVAL; 5235 5236 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; 5237 5238 if (glob[0] == '!') { 5239 unregister_ftrace_function_probe_func(glob+1, ops); 5240 return 0; 5241 } 5242 5243 if (!param) 5244 goto out_reg; 5245 5246 number = strsep(¶m, ":"); 5247 5248 if (!strlen(number)) 5249 goto out_reg; 5250 5251 /* 5252 * We use the callback data field (which is a pointer) 5253 * as our counter. 5254 */ 5255 ret = kstrtoul(number, 0, (unsigned long *)&count); 5256 if (ret) 5257 return ret; 5258 5259 out_reg: 5260 ret = register_ftrace_function_probe(glob, ops, count); 5261 5262 if (ret >= 0) 5263 alloc_snapshot(&global_trace); 5264 5265 return ret < 0 ? ret : 0; 5266 } 5267 5268 static struct ftrace_func_command ftrace_snapshot_cmd = { 5269 .name = "snapshot", 5270 .func = ftrace_trace_snapshot_callback, 5271 }; 5272 5273 static int register_snapshot_cmd(void) 5274 { 5275 return register_ftrace_command(&ftrace_snapshot_cmd); 5276 } 5277 #else 5278 static inline int register_snapshot_cmd(void) { return 0; } 5279 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */ 5280 5281 struct dentry *tracing_init_dentry_tr(struct trace_array *tr) 5282 { 5283 if (tr->dir) 5284 return tr->dir; 5285 5286 if (!debugfs_initialized()) 5287 return NULL; 5288 5289 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) 5290 tr->dir = debugfs_create_dir("tracing", NULL); 5291 5292 if (!tr->dir) 5293 pr_warn_once("Could not create debugfs directory 'tracing'\n"); 5294 5295 return tr->dir; 5296 } 5297 5298 struct dentry *tracing_init_dentry(void) 5299 { 5300 return tracing_init_dentry_tr(&global_trace); 5301 } 5302 5303 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu) 5304 { 5305 struct dentry *d_tracer; 5306 5307 if (tr->percpu_dir) 5308 return tr->percpu_dir; 5309 5310 d_tracer = tracing_init_dentry_tr(tr); 5311 if (!d_tracer) 5312 return NULL; 5313 5314 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer); 5315 5316 WARN_ONCE(!tr->percpu_dir, 5317 "Could not create debugfs directory 'per_cpu/%d'\n", cpu); 5318 5319 return tr->percpu_dir; 5320 } 5321 5322 static void 5323 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu) 5324 { 5325 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu); 5326 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu); 5327 struct dentry *d_cpu; 5328 char cpu_dir[30]; /* 30 characters should be more than enough */ 5329 5330 if (!d_percpu) 5331 return; 5332 5333 snprintf(cpu_dir, 30, "cpu%ld", cpu); 5334 d_cpu = debugfs_create_dir(cpu_dir, d_percpu); 5335 if (!d_cpu) { 5336 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir); 5337 return; 5338 } 5339 5340 /* per cpu trace_pipe */ 5341 trace_create_file("trace_pipe", 0444, d_cpu, 5342 (void *)&data->trace_cpu, &tracing_pipe_fops); 5343 5344 /* per cpu trace */ 5345 trace_create_file("trace", 0644, d_cpu, 5346 (void *)&data->trace_cpu, &tracing_fops); 5347 5348 trace_create_file("trace_pipe_raw", 0444, d_cpu, 5349 (void *)&data->trace_cpu, &tracing_buffers_fops); 5350 5351 trace_create_file("stats", 0444, d_cpu, 5352 (void *)&data->trace_cpu, &tracing_stats_fops); 5353 5354 trace_create_file("buffer_size_kb", 0444, d_cpu, 5355 (void *)&data->trace_cpu, &tracing_entries_fops); 5356 5357 #ifdef CONFIG_TRACER_SNAPSHOT 5358 trace_create_file("snapshot", 0644, d_cpu, 5359 (void *)&data->trace_cpu, &snapshot_fops); 5360 5361 trace_create_file("snapshot_raw", 0444, d_cpu, 5362 (void *)&data->trace_cpu, &snapshot_raw_fops); 5363 #endif 5364 } 5365 5366 #ifdef CONFIG_FTRACE_SELFTEST 5367 /* Let selftest have access to static functions in this file */ 5368 #include "trace_selftest.c" 5369 #endif 5370 5371 struct trace_option_dentry { 5372 struct tracer_opt *opt; 5373 struct tracer_flags *flags; 5374 struct trace_array *tr; 5375 struct dentry *entry; 5376 }; 5377 5378 static ssize_t 5379 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt, 5380 loff_t *ppos) 5381 { 5382 struct trace_option_dentry *topt = filp->private_data; 5383 char *buf; 5384 5385 if (topt->flags->val & topt->opt->bit) 5386 buf = "1\n"; 5387 else 5388 buf = "0\n"; 5389 5390 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 5391 } 5392 5393 static ssize_t 5394 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt, 5395 loff_t *ppos) 5396 { 5397 struct trace_option_dentry *topt = filp->private_data; 5398 unsigned long val; 5399 int ret; 5400 5401 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 5402 if (ret) 5403 return ret; 5404 5405 if (val != 0 && val != 1) 5406 return -EINVAL; 5407 5408 if (!!(topt->flags->val & topt->opt->bit) != val) { 5409 mutex_lock(&trace_types_lock); 5410 ret = __set_tracer_option(topt->tr->current_trace, topt->flags, 5411 topt->opt, !val); 5412 mutex_unlock(&trace_types_lock); 5413 if (ret) 5414 return ret; 5415 } 5416 5417 *ppos += cnt; 5418 5419 return cnt; 5420 } 5421 5422 5423 static const struct file_operations trace_options_fops = { 5424 .open = tracing_open_generic, 5425 .read = trace_options_read, 5426 .write = trace_options_write, 5427 .llseek = generic_file_llseek, 5428 }; 5429 5430 static ssize_t 5431 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt, 5432 loff_t *ppos) 5433 { 5434 long index = (long)filp->private_data; 5435 char *buf; 5436 5437 if (trace_flags & (1 << index)) 5438 buf = "1\n"; 5439 else 5440 buf = "0\n"; 5441 5442 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); 5443 } 5444 5445 static ssize_t 5446 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt, 5447 loff_t *ppos) 5448 { 5449 struct trace_array *tr = &global_trace; 5450 long index = (long)filp->private_data; 5451 unsigned long val; 5452 int ret; 5453 5454 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 5455 if (ret) 5456 return ret; 5457 5458 if (val != 0 && val != 1) 5459 return -EINVAL; 5460 5461 mutex_lock(&trace_types_lock); 5462 ret = set_tracer_flag(tr, 1 << index, val); 5463 mutex_unlock(&trace_types_lock); 5464 5465 if (ret < 0) 5466 return ret; 5467 5468 *ppos += cnt; 5469 5470 return cnt; 5471 } 5472 5473 static const struct file_operations trace_options_core_fops = { 5474 .open = tracing_open_generic, 5475 .read = trace_options_core_read, 5476 .write = trace_options_core_write, 5477 .llseek = generic_file_llseek, 5478 }; 5479 5480 struct dentry *trace_create_file(const char *name, 5481 umode_t mode, 5482 struct dentry *parent, 5483 void *data, 5484 const struct file_operations *fops) 5485 { 5486 struct dentry *ret; 5487 5488 ret = debugfs_create_file(name, mode, parent, data, fops); 5489 if (!ret) 5490 pr_warning("Could not create debugfs '%s' entry\n", name); 5491 5492 return ret; 5493 } 5494 5495 5496 static struct dentry *trace_options_init_dentry(struct trace_array *tr) 5497 { 5498 struct dentry *d_tracer; 5499 5500 if (tr->options) 5501 return tr->options; 5502 5503 d_tracer = tracing_init_dentry_tr(tr); 5504 if (!d_tracer) 5505 return NULL; 5506 5507 tr->options = debugfs_create_dir("options", d_tracer); 5508 if (!tr->options) { 5509 pr_warning("Could not create debugfs directory 'options'\n"); 5510 return NULL; 5511 } 5512 5513 return tr->options; 5514 } 5515 5516 static void 5517 create_trace_option_file(struct trace_array *tr, 5518 struct trace_option_dentry *topt, 5519 struct tracer_flags *flags, 5520 struct tracer_opt *opt) 5521 { 5522 struct dentry *t_options; 5523 5524 t_options = trace_options_init_dentry(tr); 5525 if (!t_options) 5526 return; 5527 5528 topt->flags = flags; 5529 topt->opt = opt; 5530 topt->tr = tr; 5531 5532 topt->entry = trace_create_file(opt->name, 0644, t_options, topt, 5533 &trace_options_fops); 5534 5535 } 5536 5537 static struct trace_option_dentry * 5538 create_trace_option_files(struct trace_array *tr, struct tracer *tracer) 5539 { 5540 struct trace_option_dentry *topts; 5541 struct tracer_flags *flags; 5542 struct tracer_opt *opts; 5543 int cnt; 5544 5545 if (!tracer) 5546 return NULL; 5547 5548 flags = tracer->flags; 5549 5550 if (!flags || !flags->opts) 5551 return NULL; 5552 5553 opts = flags->opts; 5554 5555 for (cnt = 0; opts[cnt].name; cnt++) 5556 ; 5557 5558 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL); 5559 if (!topts) 5560 return NULL; 5561 5562 for (cnt = 0; opts[cnt].name; cnt++) 5563 create_trace_option_file(tr, &topts[cnt], flags, 5564 &opts[cnt]); 5565 5566 return topts; 5567 } 5568 5569 static void 5570 destroy_trace_option_files(struct trace_option_dentry *topts) 5571 { 5572 int cnt; 5573 5574 if (!topts) 5575 return; 5576 5577 for (cnt = 0; topts[cnt].opt; cnt++) { 5578 if (topts[cnt].entry) 5579 debugfs_remove(topts[cnt].entry); 5580 } 5581 5582 kfree(topts); 5583 } 5584 5585 static struct dentry * 5586 create_trace_option_core_file(struct trace_array *tr, 5587 const char *option, long index) 5588 { 5589 struct dentry *t_options; 5590 5591 t_options = trace_options_init_dentry(tr); 5592 if (!t_options) 5593 return NULL; 5594 5595 return trace_create_file(option, 0644, t_options, (void *)index, 5596 &trace_options_core_fops); 5597 } 5598 5599 static __init void create_trace_options_dir(struct trace_array *tr) 5600 { 5601 struct dentry *t_options; 5602 int i; 5603 5604 t_options = trace_options_init_dentry(tr); 5605 if (!t_options) 5606 return; 5607 5608 for (i = 0; trace_options[i]; i++) 5609 create_trace_option_core_file(tr, trace_options[i], i); 5610 } 5611 5612 static ssize_t 5613 rb_simple_read(struct file *filp, char __user *ubuf, 5614 size_t cnt, loff_t *ppos) 5615 { 5616 struct trace_array *tr = filp->private_data; 5617 struct ring_buffer *buffer = tr->trace_buffer.buffer; 5618 char buf[64]; 5619 int r; 5620 5621 if (buffer) 5622 r = ring_buffer_record_is_on(buffer); 5623 else 5624 r = 0; 5625 5626 r = sprintf(buf, "%d\n", r); 5627 5628 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 5629 } 5630 5631 static ssize_t 5632 rb_simple_write(struct file *filp, const char __user *ubuf, 5633 size_t cnt, loff_t *ppos) 5634 { 5635 struct trace_array *tr = filp->private_data; 5636 struct ring_buffer *buffer = tr->trace_buffer.buffer; 5637 unsigned long val; 5638 int ret; 5639 5640 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 5641 if (ret) 5642 return ret; 5643 5644 if (buffer) { 5645 mutex_lock(&trace_types_lock); 5646 if (val) { 5647 ring_buffer_record_on(buffer); 5648 if (tr->current_trace->start) 5649 tr->current_trace->start(tr); 5650 } else { 5651 ring_buffer_record_off(buffer); 5652 if (tr->current_trace->stop) 5653 tr->current_trace->stop(tr); 5654 } 5655 mutex_unlock(&trace_types_lock); 5656 } 5657 5658 (*ppos)++; 5659 5660 return cnt; 5661 } 5662 5663 static const struct file_operations rb_simple_fops = { 5664 .open = tracing_open_generic, 5665 .read = rb_simple_read, 5666 .write = rb_simple_write, 5667 .llseek = default_llseek, 5668 }; 5669 5670 struct dentry *trace_instance_dir; 5671 5672 static void 5673 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer); 5674 5675 static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf) 5676 { 5677 int cpu; 5678 5679 for_each_tracing_cpu(cpu) { 5680 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu)); 5681 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu; 5682 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr; 5683 } 5684 } 5685 5686 static int 5687 allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size) 5688 { 5689 enum ring_buffer_flags rb_flags; 5690 5691 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0; 5692 5693 buf->buffer = ring_buffer_alloc(size, rb_flags); 5694 if (!buf->buffer) 5695 return -ENOMEM; 5696 5697 buf->data = alloc_percpu(struct trace_array_cpu); 5698 if (!buf->data) { 5699 ring_buffer_free(buf->buffer); 5700 return -ENOMEM; 5701 } 5702 5703 init_trace_buffers(tr, buf); 5704 5705 /* Allocate the first page for all buffers */ 5706 set_buffer_entries(&tr->trace_buffer, 5707 ring_buffer_size(tr->trace_buffer.buffer, 0)); 5708 5709 return 0; 5710 } 5711 5712 static int allocate_trace_buffers(struct trace_array *tr, int size) 5713 { 5714 int ret; 5715 5716 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size); 5717 if (ret) 5718 return ret; 5719 5720 #ifdef CONFIG_TRACER_MAX_TRACE 5721 ret = allocate_trace_buffer(tr, &tr->max_buffer, 5722 allocate_snapshot ? size : 1); 5723 if (WARN_ON(ret)) { 5724 ring_buffer_free(tr->trace_buffer.buffer); 5725 free_percpu(tr->trace_buffer.data); 5726 return -ENOMEM; 5727 } 5728 tr->allocated_snapshot = allocate_snapshot; 5729 5730 /* 5731 * Only the top level trace array gets its snapshot allocated 5732 * from the kernel command line. 5733 */ 5734 allocate_snapshot = false; 5735 #endif 5736 return 0; 5737 } 5738 5739 static int new_instance_create(const char *name) 5740 { 5741 struct trace_array *tr; 5742 int ret; 5743 5744 mutex_lock(&trace_types_lock); 5745 5746 ret = -EEXIST; 5747 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 5748 if (tr->name && strcmp(tr->name, name) == 0) 5749 goto out_unlock; 5750 } 5751 5752 ret = -ENOMEM; 5753 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 5754 if (!tr) 5755 goto out_unlock; 5756 5757 tr->name = kstrdup(name, GFP_KERNEL); 5758 if (!tr->name) 5759 goto out_free_tr; 5760 5761 raw_spin_lock_init(&tr->start_lock); 5762 5763 tr->current_trace = &nop_trace; 5764 5765 INIT_LIST_HEAD(&tr->systems); 5766 INIT_LIST_HEAD(&tr->events); 5767 5768 if (allocate_trace_buffers(tr, trace_buf_size) < 0) 5769 goto out_free_tr; 5770 5771 /* Holder for file callbacks */ 5772 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS; 5773 tr->trace_cpu.tr = tr; 5774 5775 tr->dir = debugfs_create_dir(name, trace_instance_dir); 5776 if (!tr->dir) 5777 goto out_free_tr; 5778 5779 ret = event_trace_add_tracer(tr->dir, tr); 5780 if (ret) 5781 goto out_free_tr; 5782 5783 init_tracer_debugfs(tr, tr->dir); 5784 5785 list_add(&tr->list, &ftrace_trace_arrays); 5786 5787 mutex_unlock(&trace_types_lock); 5788 5789 return 0; 5790 5791 out_free_tr: 5792 if (tr->trace_buffer.buffer) 5793 ring_buffer_free(tr->trace_buffer.buffer); 5794 kfree(tr->name); 5795 kfree(tr); 5796 5797 out_unlock: 5798 mutex_unlock(&trace_types_lock); 5799 5800 return ret; 5801 5802 } 5803 5804 static int instance_delete(const char *name) 5805 { 5806 struct trace_array *tr; 5807 int found = 0; 5808 int ret; 5809 5810 mutex_lock(&trace_types_lock); 5811 5812 ret = -ENODEV; 5813 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 5814 if (tr->name && strcmp(tr->name, name) == 0) { 5815 found = 1; 5816 break; 5817 } 5818 } 5819 if (!found) 5820 goto out_unlock; 5821 5822 ret = -EBUSY; 5823 if (tr->ref) 5824 goto out_unlock; 5825 5826 list_del(&tr->list); 5827 5828 event_trace_del_tracer(tr); 5829 debugfs_remove_recursive(tr->dir); 5830 free_percpu(tr->trace_buffer.data); 5831 ring_buffer_free(tr->trace_buffer.buffer); 5832 5833 kfree(tr->name); 5834 kfree(tr); 5835 5836 ret = 0; 5837 5838 out_unlock: 5839 mutex_unlock(&trace_types_lock); 5840 5841 return ret; 5842 } 5843 5844 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode) 5845 { 5846 struct dentry *parent; 5847 int ret; 5848 5849 /* Paranoid: Make sure the parent is the "instances" directory */ 5850 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias); 5851 if (WARN_ON_ONCE(parent != trace_instance_dir)) 5852 return -ENOENT; 5853 5854 /* 5855 * The inode mutex is locked, but debugfs_create_dir() will also 5856 * take the mutex. As the instances directory can not be destroyed 5857 * or changed in any other way, it is safe to unlock it, and 5858 * let the dentry try. If two users try to make the same dir at 5859 * the same time, then the new_instance_create() will determine the 5860 * winner. 5861 */ 5862 mutex_unlock(&inode->i_mutex); 5863 5864 ret = new_instance_create(dentry->d_iname); 5865 5866 mutex_lock(&inode->i_mutex); 5867 5868 return ret; 5869 } 5870 5871 static int instance_rmdir(struct inode *inode, struct dentry *dentry) 5872 { 5873 struct dentry *parent; 5874 int ret; 5875 5876 /* Paranoid: Make sure the parent is the "instances" directory */ 5877 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias); 5878 if (WARN_ON_ONCE(parent != trace_instance_dir)) 5879 return -ENOENT; 5880 5881 /* The caller did a dget() on dentry */ 5882 mutex_unlock(&dentry->d_inode->i_mutex); 5883 5884 /* 5885 * The inode mutex is locked, but debugfs_create_dir() will also 5886 * take the mutex. As the instances directory can not be destroyed 5887 * or changed in any other way, it is safe to unlock it, and 5888 * let the dentry try. If two users try to make the same dir at 5889 * the same time, then the instance_delete() will determine the 5890 * winner. 5891 */ 5892 mutex_unlock(&inode->i_mutex); 5893 5894 ret = instance_delete(dentry->d_iname); 5895 5896 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 5897 mutex_lock(&dentry->d_inode->i_mutex); 5898 5899 return ret; 5900 } 5901 5902 static const struct inode_operations instance_dir_inode_operations = { 5903 .lookup = simple_lookup, 5904 .mkdir = instance_mkdir, 5905 .rmdir = instance_rmdir, 5906 }; 5907 5908 static __init void create_trace_instances(struct dentry *d_tracer) 5909 { 5910 trace_instance_dir = debugfs_create_dir("instances", d_tracer); 5911 if (WARN_ON(!trace_instance_dir)) 5912 return; 5913 5914 /* Hijack the dir inode operations, to allow mkdir */ 5915 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations; 5916 } 5917 5918 static void 5919 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer) 5920 { 5921 int cpu; 5922 5923 trace_create_file("trace_options", 0644, d_tracer, 5924 tr, &tracing_iter_fops); 5925 5926 trace_create_file("trace", 0644, d_tracer, 5927 (void *)&tr->trace_cpu, &tracing_fops); 5928 5929 trace_create_file("trace_pipe", 0444, d_tracer, 5930 (void *)&tr->trace_cpu, &tracing_pipe_fops); 5931 5932 trace_create_file("buffer_size_kb", 0644, d_tracer, 5933 (void *)&tr->trace_cpu, &tracing_entries_fops); 5934 5935 trace_create_file("buffer_total_size_kb", 0444, d_tracer, 5936 tr, &tracing_total_entries_fops); 5937 5938 trace_create_file("free_buffer", 0644, d_tracer, 5939 tr, &tracing_free_buffer_fops); 5940 5941 trace_create_file("trace_marker", 0220, d_tracer, 5942 tr, &tracing_mark_fops); 5943 5944 trace_create_file("trace_clock", 0644, d_tracer, tr, 5945 &trace_clock_fops); 5946 5947 trace_create_file("tracing_on", 0644, d_tracer, 5948 tr, &rb_simple_fops); 5949 5950 #ifdef CONFIG_TRACER_SNAPSHOT 5951 trace_create_file("snapshot", 0644, d_tracer, 5952 (void *)&tr->trace_cpu, &snapshot_fops); 5953 #endif 5954 5955 for_each_tracing_cpu(cpu) 5956 tracing_init_debugfs_percpu(tr, cpu); 5957 5958 } 5959 5960 static __init int tracer_init_debugfs(void) 5961 { 5962 struct dentry *d_tracer; 5963 5964 trace_access_lock_init(); 5965 5966 d_tracer = tracing_init_dentry(); 5967 if (!d_tracer) 5968 return 0; 5969 5970 init_tracer_debugfs(&global_trace, d_tracer); 5971 5972 trace_create_file("tracing_cpumask", 0644, d_tracer, 5973 &global_trace, &tracing_cpumask_fops); 5974 5975 trace_create_file("available_tracers", 0444, d_tracer, 5976 &global_trace, &show_traces_fops); 5977 5978 trace_create_file("current_tracer", 0644, d_tracer, 5979 &global_trace, &set_tracer_fops); 5980 5981 #ifdef CONFIG_TRACER_MAX_TRACE 5982 trace_create_file("tracing_max_latency", 0644, d_tracer, 5983 &tracing_max_latency, &tracing_max_lat_fops); 5984 #endif 5985 5986 trace_create_file("tracing_thresh", 0644, d_tracer, 5987 &tracing_thresh, &tracing_max_lat_fops); 5988 5989 trace_create_file("README", 0444, d_tracer, 5990 NULL, &tracing_readme_fops); 5991 5992 trace_create_file("saved_cmdlines", 0444, d_tracer, 5993 NULL, &tracing_saved_cmdlines_fops); 5994 5995 #ifdef CONFIG_DYNAMIC_FTRACE 5996 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer, 5997 &ftrace_update_tot_cnt, &tracing_dyn_info_fops); 5998 #endif 5999 6000 create_trace_instances(d_tracer); 6001 6002 create_trace_options_dir(&global_trace); 6003 6004 return 0; 6005 } 6006 6007 static int trace_panic_handler(struct notifier_block *this, 6008 unsigned long event, void *unused) 6009 { 6010 if (ftrace_dump_on_oops) 6011 ftrace_dump(ftrace_dump_on_oops); 6012 return NOTIFY_OK; 6013 } 6014 6015 static struct notifier_block trace_panic_notifier = { 6016 .notifier_call = trace_panic_handler, 6017 .next = NULL, 6018 .priority = 150 /* priority: INT_MAX >= x >= 0 */ 6019 }; 6020 6021 static int trace_die_handler(struct notifier_block *self, 6022 unsigned long val, 6023 void *data) 6024 { 6025 switch (val) { 6026 case DIE_OOPS: 6027 if (ftrace_dump_on_oops) 6028 ftrace_dump(ftrace_dump_on_oops); 6029 break; 6030 default: 6031 break; 6032 } 6033 return NOTIFY_OK; 6034 } 6035 6036 static struct notifier_block trace_die_notifier = { 6037 .notifier_call = trace_die_handler, 6038 .priority = 200 6039 }; 6040 6041 /* 6042 * printk is set to max of 1024, we really don't need it that big. 6043 * Nothing should be printing 1000 characters anyway. 6044 */ 6045 #define TRACE_MAX_PRINT 1000 6046 6047 /* 6048 * Define here KERN_TRACE so that we have one place to modify 6049 * it if we decide to change what log level the ftrace dump 6050 * should be at. 6051 */ 6052 #define KERN_TRACE KERN_EMERG 6053 6054 void 6055 trace_printk_seq(struct trace_seq *s) 6056 { 6057 /* Probably should print a warning here. */ 6058 if (s->len >= TRACE_MAX_PRINT) 6059 s->len = TRACE_MAX_PRINT; 6060 6061 /* should be zero ended, but we are paranoid. */ 6062 s->buffer[s->len] = 0; 6063 6064 printk(KERN_TRACE "%s", s->buffer); 6065 6066 trace_seq_init(s); 6067 } 6068 6069 void trace_init_global_iter(struct trace_iterator *iter) 6070 { 6071 iter->tr = &global_trace; 6072 iter->trace = iter->tr->current_trace; 6073 iter->cpu_file = RING_BUFFER_ALL_CPUS; 6074 iter->trace_buffer = &global_trace.trace_buffer; 6075 } 6076 6077 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) 6078 { 6079 /* use static because iter can be a bit big for the stack */ 6080 static struct trace_iterator iter; 6081 static atomic_t dump_running; 6082 unsigned int old_userobj; 6083 unsigned long flags; 6084 int cnt = 0, cpu; 6085 6086 /* Only allow one dump user at a time. */ 6087 if (atomic_inc_return(&dump_running) != 1) { 6088 atomic_dec(&dump_running); 6089 return; 6090 } 6091 6092 /* 6093 * Always turn off tracing when we dump. 6094 * We don't need to show trace output of what happens 6095 * between multiple crashes. 6096 * 6097 * If the user does a sysrq-z, then they can re-enable 6098 * tracing with echo 1 > tracing_on. 6099 */ 6100 tracing_off(); 6101 6102 local_irq_save(flags); 6103 6104 /* Simulate the iterator */ 6105 trace_init_global_iter(&iter); 6106 6107 for_each_tracing_cpu(cpu) { 6108 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled); 6109 } 6110 6111 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ; 6112 6113 /* don't look at user memory in panic mode */ 6114 trace_flags &= ~TRACE_ITER_SYM_USEROBJ; 6115 6116 switch (oops_dump_mode) { 6117 case DUMP_ALL: 6118 iter.cpu_file = RING_BUFFER_ALL_CPUS; 6119 break; 6120 case DUMP_ORIG: 6121 iter.cpu_file = raw_smp_processor_id(); 6122 break; 6123 case DUMP_NONE: 6124 goto out_enable; 6125 default: 6126 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n"); 6127 iter.cpu_file = RING_BUFFER_ALL_CPUS; 6128 } 6129 6130 printk(KERN_TRACE "Dumping ftrace buffer:\n"); 6131 6132 /* Did function tracer already get disabled? */ 6133 if (ftrace_is_dead()) { 6134 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n"); 6135 printk("# MAY BE MISSING FUNCTION EVENTS\n"); 6136 } 6137 6138 /* 6139 * We need to stop all tracing on all CPUS to read the 6140 * the next buffer. This is a bit expensive, but is 6141 * not done often. We fill all what we can read, 6142 * and then release the locks again. 6143 */ 6144 6145 while (!trace_empty(&iter)) { 6146 6147 if (!cnt) 6148 printk(KERN_TRACE "---------------------------------\n"); 6149 6150 cnt++; 6151 6152 /* reset all but tr, trace, and overruns */ 6153 memset(&iter.seq, 0, 6154 sizeof(struct trace_iterator) - 6155 offsetof(struct trace_iterator, seq)); 6156 iter.iter_flags |= TRACE_FILE_LAT_FMT; 6157 iter.pos = -1; 6158 6159 if (trace_find_next_entry_inc(&iter) != NULL) { 6160 int ret; 6161 6162 ret = print_trace_line(&iter); 6163 if (ret != TRACE_TYPE_NO_CONSUME) 6164 trace_consume(&iter); 6165 } 6166 touch_nmi_watchdog(); 6167 6168 trace_printk_seq(&iter.seq); 6169 } 6170 6171 if (!cnt) 6172 printk(KERN_TRACE " (ftrace buffer empty)\n"); 6173 else 6174 printk(KERN_TRACE "---------------------------------\n"); 6175 6176 out_enable: 6177 trace_flags |= old_userobj; 6178 6179 for_each_tracing_cpu(cpu) { 6180 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled); 6181 } 6182 atomic_dec(&dump_running); 6183 local_irq_restore(flags); 6184 } 6185 EXPORT_SYMBOL_GPL(ftrace_dump); 6186 6187 __init static int tracer_alloc_buffers(void) 6188 { 6189 int ring_buf_size; 6190 int ret = -ENOMEM; 6191 6192 6193 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) 6194 goto out; 6195 6196 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL)) 6197 goto out_free_buffer_mask; 6198 6199 /* Only allocate trace_printk buffers if a trace_printk exists */ 6200 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt) 6201 /* Must be called before global_trace.buffer is allocated */ 6202 trace_printk_init_buffers(); 6203 6204 /* To save memory, keep the ring buffer size to its minimum */ 6205 if (ring_buffer_expanded) 6206 ring_buf_size = trace_buf_size; 6207 else 6208 ring_buf_size = 1; 6209 6210 cpumask_copy(tracing_buffer_mask, cpu_possible_mask); 6211 cpumask_copy(tracing_cpumask, cpu_all_mask); 6212 6213 raw_spin_lock_init(&global_trace.start_lock); 6214 6215 /* TODO: make the number of buffers hot pluggable with CPUS */ 6216 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) { 6217 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); 6218 WARN_ON(1); 6219 goto out_free_cpumask; 6220 } 6221 6222 if (global_trace.buffer_disabled) 6223 tracing_off(); 6224 6225 trace_init_cmdlines(); 6226 6227 /* 6228 * register_tracer() might reference current_trace, so it 6229 * needs to be set before we register anything. This is 6230 * just a bootstrap of current_trace anyway. 6231 */ 6232 global_trace.current_trace = &nop_trace; 6233 6234 register_tracer(&nop_trace); 6235 6236 /* All seems OK, enable tracing */ 6237 tracing_disabled = 0; 6238 6239 atomic_notifier_chain_register(&panic_notifier_list, 6240 &trace_panic_notifier); 6241 6242 register_die_notifier(&trace_die_notifier); 6243 6244 global_trace.flags = TRACE_ARRAY_FL_GLOBAL; 6245 6246 /* Holder for file callbacks */ 6247 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS; 6248 global_trace.trace_cpu.tr = &global_trace; 6249 6250 INIT_LIST_HEAD(&global_trace.systems); 6251 INIT_LIST_HEAD(&global_trace.events); 6252 list_add(&global_trace.list, &ftrace_trace_arrays); 6253 6254 while (trace_boot_options) { 6255 char *option; 6256 6257 option = strsep(&trace_boot_options, ","); 6258 trace_set_options(&global_trace, option); 6259 } 6260 6261 register_snapshot_cmd(); 6262 6263 return 0; 6264 6265 out_free_cpumask: 6266 free_percpu(global_trace.trace_buffer.data); 6267 #ifdef CONFIG_TRACER_MAX_TRACE 6268 free_percpu(global_trace.max_buffer.data); 6269 #endif 6270 free_cpumask_var(tracing_cpumask); 6271 out_free_buffer_mask: 6272 free_cpumask_var(tracing_buffer_mask); 6273 out: 6274 return ret; 6275 } 6276 6277 __init static int clear_boot_tracer(void) 6278 { 6279 /* 6280 * The default tracer at boot buffer is an init section. 6281 * This function is called in lateinit. If we did not 6282 * find the boot tracer, then clear it out, to prevent 6283 * later registration from accessing the buffer that is 6284 * about to be freed. 6285 */ 6286 if (!default_bootup_tracer) 6287 return 0; 6288 6289 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n", 6290 default_bootup_tracer); 6291 default_bootup_tracer = NULL; 6292 6293 return 0; 6294 } 6295 6296 early_initcall(tracer_alloc_buffers); 6297 fs_initcall(tracer_init_debugfs); 6298 late_initcall(clear_boot_tracer); 6299