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