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