Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 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 William Lee Irwin III
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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/kprobes.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/rwsem.h>
36 #include <linux/slab.h>
37 #include <linux/ctype.h>
38 #include <linux/init.h>
39 #include <linux/poll.h>
40 #include <linux/fs.h>
41
42 #include "trace.h"
43 #include "trace_output.h"
44
45 #define TRACE_BUFFER_FLAGS      (RB_FL_OVERWRITE)
46
47 /*
48  * On boot up, the ring buffer is set to the minimum size, so that
49  * we do not waste memory on systems that are not using tracing.
50  */
51 int ring_buffer_expanded;
52
53 /*
54  * We need to change this state when a selftest is running.
55  * A selftest will lurk into the ring-buffer to count the
56  * entries inserted during the selftest although some concurrent
57  * insertions into the ring-buffer such as trace_printk could occurred
58  * at the same time, giving false positive or negative results.
59  */
60 static bool __read_mostly tracing_selftest_running;
61
62 /*
63  * If a tracer is running, we do not want to run SELFTEST.
64  */
65 bool __read_mostly tracing_selftest_disabled;
66
67 /* For tracers that don't implement custom flags */
68 static struct tracer_opt dummy_tracer_opt[] = {
69         { }
70 };
71
72 static struct tracer_flags dummy_tracer_flags = {
73         .val = 0,
74         .opts = dummy_tracer_opt
75 };
76
77 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78 {
79         return 0;
80 }
81
82 /*
83  * Kill all tracing for good (never come back).
84  * It is initialized to 1 but will turn to zero if the initialization
85  * of the tracer is successful. But that is the only place that sets
86  * this back to zero.
87  */
88 static int tracing_disabled = 1;
89
90 DEFINE_PER_CPU(int, ftrace_cpu_disabled);
91
92 static inline void ftrace_disable_cpu(void)
93 {
94         preempt_disable();
95         __this_cpu_inc(ftrace_cpu_disabled);
96 }
97
98 static inline void ftrace_enable_cpu(void)
99 {
100         __this_cpu_dec(ftrace_cpu_disabled);
101         preempt_enable();
102 }
103
104 static cpumask_var_t __read_mostly      tracing_buffer_mask;
105
106 #define for_each_tracing_cpu(cpu)       \
107         for_each_cpu(cpu, tracing_buffer_mask)
108
109 /*
110  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
111  *
112  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113  * is set, then ftrace_dump is called. This will output the contents
114  * of the ftrace buffers to the console.  This is very useful for
115  * capturing traces that lead to crashes and outputing it to a
116  * serial console.
117  *
118  * It is default off, but you can enable it with either specifying
119  * "ftrace_dump_on_oops" in the kernel command line, or setting
120  * /proc/sys/kernel/ftrace_dump_on_oops
121  * Set 1 if you want to dump buffers of all CPUs
122  * Set 2 if you want to dump the buffer of the CPU that triggered oops
123  */
124
125 enum ftrace_dump_mode ftrace_dump_on_oops;
126
127 static int tracing_set_tracer(const char *buf);
128
129 #define MAX_TRACER_SIZE         100
130 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
131 static char *default_bootup_tracer;
132
133 static int __init set_cmdline_ftrace(char *str)
134 {
135         strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
136         default_bootup_tracer = bootup_tracer_buf;
137         /* We are using ftrace early, expand it */
138         ring_buffer_expanded = 1;
139         return 1;
140 }
141 __setup("ftrace=", set_cmdline_ftrace);
142
143 static int __init set_ftrace_dump_on_oops(char *str)
144 {
145         if (*str++ != '=' || !*str) {
146                 ftrace_dump_on_oops = DUMP_ALL;
147                 return 1;
148         }
149
150         if (!strcmp("orig_cpu", str)) {
151                 ftrace_dump_on_oops = DUMP_ORIG;
152                 return 1;
153         }
154
155         return 0;
156 }
157 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
158
159 unsigned long long ns2usecs(cycle_t nsec)
160 {
161         nsec += 500;
162         do_div(nsec, 1000);
163         return nsec;
164 }
165
166 /*
167  * The global_trace is the descriptor that holds the tracing
168  * buffers for the live tracing. For each CPU, it contains
169  * a link list of pages that will store trace entries. The
170  * page descriptor of the pages in the memory is used to hold
171  * the link list by linking the lru item in the page descriptor
172  * to each of the pages in the buffer per CPU.
173  *
174  * For each active CPU there is a data field that holds the
175  * pages for the buffer for that CPU. Each CPU has the same number
176  * of pages allocated for its buffer.
177  */
178 static struct trace_array       global_trace;
179
180 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
181
182 int filter_current_check_discard(struct ring_buffer *buffer,
183                                  struct ftrace_event_call *call, void *rec,
184                                  struct ring_buffer_event *event)
185 {
186         return filter_check_discard(call, rec, buffer, event);
187 }
188 EXPORT_SYMBOL_GPL(filter_current_check_discard);
189
190 cycle_t ftrace_now(int cpu)
191 {
192         u64 ts;
193
194         /* Early boot up does not have a buffer yet */
195         if (!global_trace.buffer)
196                 return trace_clock_local();
197
198         ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
199         ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
200
201         return ts;
202 }
203
204 /*
205  * The max_tr is used to snapshot the global_trace when a maximum
206  * latency is reached. Some tracers will use this to store a maximum
207  * trace while it continues examining live traces.
208  *
209  * The buffers for the max_tr are set up the same as the global_trace.
210  * When a snapshot is taken, the link list of the max_tr is swapped
211  * with the link list of the global_trace and the buffers are reset for
212  * the global_trace so the tracing can continue.
213  */
214 static struct trace_array       max_tr;
215
216 static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
217
218 /* tracer_enabled is used to toggle activation of a tracer */
219 static int                      tracer_enabled = 1;
220
221 /**
222  * tracing_is_enabled - return tracer_enabled status
223  *
224  * This function is used by other tracers to know the status
225  * of the tracer_enabled flag.  Tracers may use this function
226  * to know if it should enable their features when starting
227  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
228  */
229 int tracing_is_enabled(void)
230 {
231         return tracer_enabled;
232 }
233
234 /*
235  * trace_buf_size is the size in bytes that is allocated
236  * for a buffer. Note, the number of bytes is always rounded
237  * to page size.
238  *
239  * This number is purposely set to a low number of 16384.
240  * If the dump on oops happens, it will be much appreciated
241  * to not have to wait for all that output. Anyway this can be
242  * boot time and run time configurable.
243  */
244 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
245
246 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
247
248 /* trace_types holds a link list of available tracers. */
249 static struct tracer            *trace_types __read_mostly;
250
251 /* current_trace points to the tracer that is currently active */
252 static struct tracer            *current_trace __read_mostly;
253
254 /*
255  * trace_types_lock is used to protect the trace_types list.
256  */
257 static DEFINE_MUTEX(trace_types_lock);
258
259 /*
260  * serialize the access of the ring buffer
261  *
262  * ring buffer serializes readers, but it is low level protection.
263  * The validity of the events (which returns by ring_buffer_peek() ..etc)
264  * are not protected by ring buffer.
265  *
266  * The content of events may become garbage if we allow other process consumes
267  * these events concurrently:
268  *   A) the page of the consumed events may become a normal page
269  *      (not reader page) in ring buffer, and this page will be rewrited
270  *      by events producer.
271  *   B) The page of the consumed events may become a page for splice_read,
272  *      and this page will be returned to system.
273  *
274  * These primitives allow multi process access to different cpu ring buffer
275  * concurrently.
276  *
277  * These primitives don't distinguish read-only and read-consume access.
278  * Multi read-only access are also serialized.
279  */
280
281 #ifdef CONFIG_SMP
282 static DECLARE_RWSEM(all_cpu_access_lock);
283 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
284
285 static inline void trace_access_lock(int cpu)
286 {
287         if (cpu == TRACE_PIPE_ALL_CPU) {
288                 /* gain it for accessing the whole ring buffer. */
289                 down_write(&all_cpu_access_lock);
290         } else {
291                 /* gain it for accessing a cpu ring buffer. */
292
293                 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
294                 down_read(&all_cpu_access_lock);
295
296                 /* Secondly block other access to this @cpu ring buffer. */
297                 mutex_lock(&per_cpu(cpu_access_lock, cpu));
298         }
299 }
300
301 static inline void trace_access_unlock(int cpu)
302 {
303         if (cpu == TRACE_PIPE_ALL_CPU) {
304                 up_write(&all_cpu_access_lock);
305         } else {
306                 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
307                 up_read(&all_cpu_access_lock);
308         }
309 }
310
311 static inline void trace_access_lock_init(void)
312 {
313         int cpu;
314
315         for_each_possible_cpu(cpu)
316                 mutex_init(&per_cpu(cpu_access_lock, cpu));
317 }
318
319 #else
320
321 static DEFINE_MUTEX(access_lock);
322
323 static inline void trace_access_lock(int cpu)
324 {
325         (void)cpu;
326         mutex_lock(&access_lock);
327 }
328
329 static inline void trace_access_unlock(int cpu)
330 {
331         (void)cpu;
332         mutex_unlock(&access_lock);
333 }
334
335 static inline void trace_access_lock_init(void)
336 {
337 }
338
339 #endif
340
341 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
342 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
343
344 /* trace_flags holds trace_options default values */
345 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
346         TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
347         TRACE_ITER_GRAPH_TIME;
348
349 static int trace_stop_count;
350 static DEFINE_SPINLOCK(tracing_start_lock);
351
352 /**
353  * trace_wake_up - wake up tasks waiting for trace input
354  *
355  * Simply wakes up any task that is blocked on the trace_wait
356  * queue. These is used with trace_poll for tasks polling the trace.
357  */
358 void trace_wake_up(void)
359 {
360         int cpu;
361
362         if (trace_flags & TRACE_ITER_BLOCK)
363                 return;
364         /*
365          * The runqueue_is_locked() can fail, but this is the best we
366          * have for now:
367          */
368         cpu = get_cpu();
369         if (!runqueue_is_locked(cpu))
370                 wake_up(&trace_wait);
371         put_cpu();
372 }
373
374 static int __init set_buf_size(char *str)
375 {
376         unsigned long buf_size;
377
378         if (!str)
379                 return 0;
380         buf_size = memparse(str, &str);
381         /* nr_entries can not be zero */
382         if (buf_size == 0)
383                 return 0;
384         trace_buf_size = buf_size;
385         return 1;
386 }
387 __setup("trace_buf_size=", set_buf_size);
388
389 static int __init set_tracing_thresh(char *str)
390 {
391         unsigned long threshhold;
392         int ret;
393
394         if (!str)
395                 return 0;
396         ret = strict_strtoul(str, 0, &threshhold);
397         if (ret < 0)
398                 return 0;
399         tracing_thresh = threshhold * 1000;
400         return 1;
401 }
402 __setup("tracing_thresh=", set_tracing_thresh);
403
404 unsigned long nsecs_to_usecs(unsigned long nsecs)
405 {
406         return nsecs / 1000;
407 }
408
409 /* These must match the bit postions in trace_iterator_flags */
410 static const char *trace_options[] = {
411         "print-parent",
412         "sym-offset",
413         "sym-addr",
414         "verbose",
415         "raw",
416         "hex",
417         "bin",
418         "block",
419         "stacktrace",
420         "trace_printk",
421         "ftrace_preempt",
422         "branch",
423         "annotate",
424         "userstacktrace",
425         "sym-userobj",
426         "printk-msg-only",
427         "context-info",
428         "latency-format",
429         "sleep-time",
430         "graph-time",
431         NULL
432 };
433
434 static struct {
435         u64 (*func)(void);
436         const char *name;
437 } trace_clocks[] = {
438         { trace_clock_local,    "local" },
439         { trace_clock_global,   "global" },
440 };
441
442 int trace_clock_id;
443
444 /*
445  * trace_parser_get_init - gets the buffer for trace parser
446  */
447 int trace_parser_get_init(struct trace_parser *parser, int size)
448 {
449         memset(parser, 0, sizeof(*parser));
450
451         parser->buffer = kmalloc(size, GFP_KERNEL);
452         if (!parser->buffer)
453                 return 1;
454
455         parser->size = size;
456         return 0;
457 }
458
459 /*
460  * trace_parser_put - frees the buffer for trace parser
461  */
462 void trace_parser_put(struct trace_parser *parser)
463 {
464         kfree(parser->buffer);
465 }
466
467 /*
468  * trace_get_user - reads the user input string separated by  space
469  * (matched by isspace(ch))
470  *
471  * For each string found the 'struct trace_parser' is updated,
472  * and the function returns.
473  *
474  * Returns number of bytes read.
475  *
476  * See kernel/trace/trace.h for 'struct trace_parser' details.
477  */
478 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
479         size_t cnt, loff_t *ppos)
480 {
481         char ch;
482         size_t read = 0;
483         ssize_t ret;
484
485         if (!*ppos)
486                 trace_parser_clear(parser);
487
488         ret = get_user(ch, ubuf++);
489         if (ret)
490                 goto out;
491
492         read++;
493         cnt--;
494
495         /*
496          * The parser is not finished with the last write,
497          * continue reading the user input without skipping spaces.
498          */
499         if (!parser->cont) {
500                 /* skip white space */
501                 while (cnt && isspace(ch)) {
502                         ret = get_user(ch, ubuf++);
503                         if (ret)
504                                 goto out;
505                         read++;
506                         cnt--;
507                 }
508
509                 /* only spaces were written */
510                 if (isspace(ch)) {
511                         *ppos += read;
512                         ret = read;
513                         goto out;
514                 }
515
516                 parser->idx = 0;
517         }
518
519         /* read the non-space input */
520         while (cnt && !isspace(ch)) {
521                 if (parser->idx < parser->size - 1)
522                         parser->buffer[parser->idx++] = ch;
523                 else {
524                         ret = -EINVAL;
525                         goto out;
526                 }
527                 ret = get_user(ch, ubuf++);
528                 if (ret)
529                         goto out;
530                 read++;
531                 cnt--;
532         }
533
534         /* We either got finished input or we have to wait for another call. */
535         if (isspace(ch)) {
536                 parser->buffer[parser->idx] = 0;
537                 parser->cont = false;
538         } else {
539                 parser->cont = true;
540                 parser->buffer[parser->idx++] = ch;
541         }
542
543         *ppos += read;
544         ret = read;
545
546 out:
547         return ret;
548 }
549
550 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
551 {
552         int len;
553         int ret;
554
555         if (!cnt)
556                 return 0;
557
558         if (s->len <= s->readpos)
559                 return -EBUSY;
560
561         len = s->len - s->readpos;
562         if (cnt > len)
563                 cnt = len;
564         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
565         if (ret == cnt)
566                 return -EFAULT;
567
568         cnt -= ret;
569
570         s->readpos += cnt;
571         return cnt;
572 }
573
574 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
575 {
576         int len;
577         void *ret;
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 = memcpy(buf, s->buffer + s->readpos, cnt);
586         if (!ret)
587                 return -EFAULT;
588
589         s->readpos += cnt;
590         return cnt;
591 }
592
593 /*
594  * ftrace_max_lock is used to protect the swapping of buffers
595  * when taking a max snapshot. The buffers themselves are
596  * protected by per_cpu spinlocks. But the action of the swap
597  * needs its own lock.
598  *
599  * This is defined as a arch_spinlock_t in order to help
600  * with performance when lockdep debugging is enabled.
601  *
602  * It is also used in other places outside the update_max_tr
603  * so it needs to be defined outside of the
604  * CONFIG_TRACER_MAX_TRACE.
605  */
606 static arch_spinlock_t ftrace_max_lock =
607         (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
608
609 unsigned long __read_mostly     tracing_thresh;
610
611 #ifdef CONFIG_TRACER_MAX_TRACE
612 unsigned long __read_mostly     tracing_max_latency;
613
614 /*
615  * Copy the new maximum trace into the separate maximum-trace
616  * structure. (this way the maximum trace is permanently saved,
617  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
618  */
619 static void
620 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
621 {
622         struct trace_array_cpu *data = tr->data[cpu];
623         struct trace_array_cpu *max_data;
624
625         max_tr.cpu = cpu;
626         max_tr.time_start = data->preempt_timestamp;
627
628         max_data = max_tr.data[cpu];
629         max_data->saved_latency = tracing_max_latency;
630         max_data->critical_start = data->critical_start;
631         max_data->critical_end = data->critical_end;
632
633         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
634         max_data->pid = tsk->pid;
635         max_data->uid = task_uid(tsk);
636         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
637         max_data->policy = tsk->policy;
638         max_data->rt_priority = tsk->rt_priority;
639
640         /* record this tasks comm */
641         tracing_record_cmdline(tsk);
642 }
643
644 /**
645  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
646  * @tr: tracer
647  * @tsk: the task with the latency
648  * @cpu: The cpu that initiated the trace.
649  *
650  * Flip the buffers between the @tr and the max_tr and record information
651  * about which task was the cause of this latency.
652  */
653 void
654 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
655 {
656         struct ring_buffer *buf = tr->buffer;
657
658         if (trace_stop_count)
659                 return;
660
661         WARN_ON_ONCE(!irqs_disabled());
662         arch_spin_lock(&ftrace_max_lock);
663
664         tr->buffer = max_tr.buffer;
665         max_tr.buffer = buf;
666
667         __update_max_tr(tr, tsk, cpu);
668         arch_spin_unlock(&ftrace_max_lock);
669 }
670
671 /**
672  * update_max_tr_single - only copy one trace over, and reset the rest
673  * @tr - tracer
674  * @tsk - task with the latency
675  * @cpu - the cpu of the buffer to copy.
676  *
677  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
678  */
679 void
680 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
681 {
682         int ret;
683
684         if (trace_stop_count)
685                 return;
686
687         WARN_ON_ONCE(!irqs_disabled());
688         arch_spin_lock(&ftrace_max_lock);
689
690         ftrace_disable_cpu();
691
692         ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
693
694         if (ret == -EBUSY) {
695                 /*
696                  * We failed to swap the buffer due to a commit taking
697                  * place on this CPU. We fail to record, but we reset
698                  * the max trace buffer (no one writes directly to it)
699                  * and flag that it failed.
700                  */
701                 trace_array_printk(&max_tr, _THIS_IP_,
702                         "Failed to swap buffers due to commit in progress\n");
703         }
704
705         ftrace_enable_cpu();
706
707         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
708
709         __update_max_tr(tr, tsk, cpu);
710         arch_spin_unlock(&ftrace_max_lock);
711 }
712 #endif /* CONFIG_TRACER_MAX_TRACE */
713
714 /**
715  * register_tracer - register a tracer with the ftrace system.
716  * @type - the plugin for the tracer
717  *
718  * Register a new plugin tracer.
719  */
720 int register_tracer(struct tracer *type)
721 __releases(kernel_lock)
722 __acquires(kernel_lock)
723 {
724         struct tracer *t;
725         int ret = 0;
726
727         if (!type->name) {
728                 pr_info("Tracer must have a name\n");
729                 return -1;
730         }
731
732         if (strlen(type->name) > MAX_TRACER_SIZE) {
733                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
734                 return -1;
735         }
736
737         /*
738          * When this gets called we hold the BKL which means that
739          * preemption is disabled. Various trace selftests however
740          * need to disable and enable preemption for successful tests.
741          * So we drop the BKL here and grab it after the tests again.
742          */
743         unlock_kernel();
744         mutex_lock(&trace_types_lock);
745
746         tracing_selftest_running = true;
747
748         for (t = trace_types; t; t = t->next) {
749                 if (strcmp(type->name, t->name) == 0) {
750                         /* already found */
751                         pr_info("Tracer %s already registered\n",
752                                 type->name);
753                         ret = -1;
754                         goto out;
755                 }
756         }
757
758         if (!type->set_flag)
759                 type->set_flag = &dummy_set_flag;
760         if (!type->flags)
761                 type->flags = &dummy_tracer_flags;
762         else
763                 if (!type->flags->opts)
764                         type->flags->opts = dummy_tracer_opt;
765         if (!type->wait_pipe)
766                 type->wait_pipe = default_wait_pipe;
767
768
769 #ifdef CONFIG_FTRACE_STARTUP_TEST
770         if (type->selftest && !tracing_selftest_disabled) {
771                 struct tracer *saved_tracer = current_trace;
772                 struct trace_array *tr = &global_trace;
773
774                 /*
775                  * Run a selftest on this tracer.
776                  * Here we reset the trace buffer, and set the current
777                  * tracer to be this tracer. The tracer can then run some
778                  * internal tracing to verify that everything is in order.
779                  * If we fail, we do not register this tracer.
780                  */
781                 tracing_reset_online_cpus(tr);
782
783                 current_trace = type;
784                 /* the test is responsible for initializing and enabling */
785                 pr_info("Testing tracer %s: ", type->name);
786                 ret = type->selftest(type, tr);
787                 /* the test is responsible for resetting too */
788                 current_trace = saved_tracer;
789                 if (ret) {
790                         printk(KERN_CONT "FAILED!\n");
791                         goto out;
792                 }
793                 /* Only reset on passing, to avoid touching corrupted buffers */
794                 tracing_reset_online_cpus(tr);
795
796                 printk(KERN_CONT "PASSED\n");
797         }
798 #endif
799
800         type->next = trace_types;
801         trace_types = type;
802
803  out:
804         tracing_selftest_running = false;
805         mutex_unlock(&trace_types_lock);
806
807         if (ret || !default_bootup_tracer)
808                 goto out_unlock;
809
810         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
811                 goto out_unlock;
812
813         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
814         /* Do we want this tracer to start on bootup? */
815         tracing_set_tracer(type->name);
816         default_bootup_tracer = NULL;
817         /* disable other selftests, since this will break it. */
818         tracing_selftest_disabled = 1;
819 #ifdef CONFIG_FTRACE_STARTUP_TEST
820         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
821                type->name);
822 #endif
823
824  out_unlock:
825         lock_kernel();
826         return ret;
827 }
828
829 void unregister_tracer(struct tracer *type)
830 {
831         struct tracer **t;
832
833         mutex_lock(&trace_types_lock);
834         for (t = &trace_types; *t; t = &(*t)->next) {
835                 if (*t == type)
836                         goto found;
837         }
838         pr_info("Tracer %s not registered\n", type->name);
839         goto out;
840
841  found:
842         *t = (*t)->next;
843
844         if (type == current_trace && tracer_enabled) {
845                 tracer_enabled = 0;
846                 tracing_stop();
847                 if (current_trace->stop)
848                         current_trace->stop(&global_trace);
849                 current_trace = &nop_trace;
850         }
851 out:
852         mutex_unlock(&trace_types_lock);
853 }
854
855 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
856 {
857         ftrace_disable_cpu();
858         ring_buffer_reset_cpu(buffer, cpu);
859         ftrace_enable_cpu();
860 }
861
862 void tracing_reset(struct trace_array *tr, int cpu)
863 {
864         struct ring_buffer *buffer = tr->buffer;
865
866         ring_buffer_record_disable(buffer);
867
868         /* Make sure all commits have finished */
869         synchronize_sched();
870         __tracing_reset(buffer, cpu);
871
872         ring_buffer_record_enable(buffer);
873 }
874
875 void tracing_reset_online_cpus(struct trace_array *tr)
876 {
877         struct ring_buffer *buffer = tr->buffer;
878         int cpu;
879
880         ring_buffer_record_disable(buffer);
881
882         /* Make sure all commits have finished */
883         synchronize_sched();
884
885         tr->time_start = ftrace_now(tr->cpu);
886
887         for_each_online_cpu(cpu)
888                 __tracing_reset(buffer, cpu);
889
890         ring_buffer_record_enable(buffer);
891 }
892
893 void tracing_reset_current(int cpu)
894 {
895         tracing_reset(&global_trace, cpu);
896 }
897
898 void tracing_reset_current_online_cpus(void)
899 {
900         tracing_reset_online_cpus(&global_trace);
901 }
902
903 #define SAVED_CMDLINES 128
904 #define NO_CMDLINE_MAP UINT_MAX
905 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
906 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
907 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
908 static int cmdline_idx;
909 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
910
911 /* temporary disable recording */
912 static atomic_t trace_record_cmdline_disabled __read_mostly;
913
914 static void trace_init_cmdlines(void)
915 {
916         memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
917         memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
918         cmdline_idx = 0;
919 }
920
921 int is_tracing_stopped(void)
922 {
923         return trace_stop_count;
924 }
925
926 /**
927  * ftrace_off_permanent - disable all ftrace code permanently
928  *
929  * This should only be called when a serious anomally has
930  * been detected.  This will turn off the function tracing,
931  * ring buffers, and other tracing utilites. It takes no
932  * locks and can be called from any context.
933  */
934 void ftrace_off_permanent(void)
935 {
936         tracing_disabled = 1;
937         ftrace_stop();
938         tracing_off_permanent();
939 }
940
941 /**
942  * tracing_start - quick start of the tracer
943  *
944  * If tracing is enabled but was stopped by tracing_stop,
945  * this will start the tracer back up.
946  */
947 void tracing_start(void)
948 {
949         struct ring_buffer *buffer;
950         unsigned long flags;
951
952         if (tracing_disabled)
953                 return;
954
955         spin_lock_irqsave(&tracing_start_lock, flags);
956         if (--trace_stop_count) {
957                 if (trace_stop_count < 0) {
958                         /* Someone screwed up their debugging */
959                         WARN_ON_ONCE(1);
960                         trace_stop_count = 0;
961                 }
962                 goto out;
963         }
964
965         /* Prevent the buffers from switching */
966         arch_spin_lock(&ftrace_max_lock);
967
968         buffer = global_trace.buffer;
969         if (buffer)
970                 ring_buffer_record_enable(buffer);
971
972         buffer = max_tr.buffer;
973         if (buffer)
974                 ring_buffer_record_enable(buffer);
975
976         arch_spin_unlock(&ftrace_max_lock);
977
978         ftrace_start();
979  out:
980         spin_unlock_irqrestore(&tracing_start_lock, flags);
981 }
982
983 /**
984  * tracing_stop - quick stop of the tracer
985  *
986  * Light weight way to stop tracing. Use in conjunction with
987  * tracing_start.
988  */
989 void tracing_stop(void)
990 {
991         struct ring_buffer *buffer;
992         unsigned long flags;
993
994         ftrace_stop();
995         spin_lock_irqsave(&tracing_start_lock, flags);
996         if (trace_stop_count++)
997                 goto out;
998
999         /* Prevent the buffers from switching */
1000         arch_spin_lock(&ftrace_max_lock);
1001
1002         buffer = global_trace.buffer;
1003         if (buffer)
1004                 ring_buffer_record_disable(buffer);
1005
1006         buffer = max_tr.buffer;
1007         if (buffer)
1008                 ring_buffer_record_disable(buffer);
1009
1010         arch_spin_unlock(&ftrace_max_lock);
1011
1012  out:
1013         spin_unlock_irqrestore(&tracing_start_lock, flags);
1014 }
1015
1016 void trace_stop_cmdline_recording(void);
1017
1018 static void trace_save_cmdline(struct task_struct *tsk)
1019 {
1020         unsigned pid, idx;
1021
1022         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1023                 return;
1024
1025         /*
1026          * It's not the end of the world if we don't get
1027          * the lock, but we also don't want to spin
1028          * nor do we want to disable interrupts,
1029          * so if we miss here, then better luck next time.
1030          */
1031         if (!arch_spin_trylock(&trace_cmdline_lock))
1032                 return;
1033
1034         idx = map_pid_to_cmdline[tsk->pid];
1035         if (idx == NO_CMDLINE_MAP) {
1036                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1037
1038                 /*
1039                  * Check whether the cmdline buffer at idx has a pid
1040                  * mapped. We are going to overwrite that entry so we
1041                  * need to clear the map_pid_to_cmdline. Otherwise we
1042                  * would read the new comm for the old pid.
1043                  */
1044                 pid = map_cmdline_to_pid[idx];
1045                 if (pid != NO_CMDLINE_MAP)
1046                         map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1047
1048                 map_cmdline_to_pid[idx] = tsk->pid;
1049                 map_pid_to_cmdline[tsk->pid] = idx;
1050
1051                 cmdline_idx = idx;
1052         }
1053
1054         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1055
1056         arch_spin_unlock(&trace_cmdline_lock);
1057 }
1058
1059 void trace_find_cmdline(int pid, char comm[])
1060 {
1061         unsigned map;
1062
1063         if (!pid) {
1064                 strcpy(comm, "<idle>");
1065                 return;
1066         }
1067
1068         if (WARN_ON_ONCE(pid < 0)) {
1069                 strcpy(comm, "<XXX>");
1070                 return;
1071         }
1072
1073         if (pid > PID_MAX_DEFAULT) {
1074                 strcpy(comm, "<...>");
1075                 return;
1076         }
1077
1078         preempt_disable();
1079         arch_spin_lock(&trace_cmdline_lock);
1080         map = map_pid_to_cmdline[pid];
1081         if (map != NO_CMDLINE_MAP)
1082                 strcpy(comm, saved_cmdlines[map]);
1083         else
1084                 strcpy(comm, "<...>");
1085
1086         arch_spin_unlock(&trace_cmdline_lock);
1087         preempt_enable();
1088 }
1089
1090 void tracing_record_cmdline(struct task_struct *tsk)
1091 {
1092         if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1093             !tracing_is_on())
1094                 return;
1095
1096         trace_save_cmdline(tsk);
1097 }
1098
1099 void
1100 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1101                              int pc)
1102 {
1103         struct task_struct *tsk = current;
1104
1105         entry->preempt_count            = pc & 0xff;
1106         entry->pid                      = (tsk) ? tsk->pid : 0;
1107         entry->lock_depth               = (tsk) ? tsk->lock_depth : 0;
1108         entry->flags =
1109 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1110                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1111 #else
1112                 TRACE_FLAG_IRQS_NOSUPPORT |
1113 #endif
1114                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1115                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1116                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1117 }
1118 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1119
1120 struct ring_buffer_event *
1121 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1122                           int type,
1123                           unsigned long len,
1124                           unsigned long flags, int pc)
1125 {
1126         struct ring_buffer_event *event;
1127
1128         event = ring_buffer_lock_reserve(buffer, len);
1129         if (event != NULL) {
1130                 struct trace_entry *ent = ring_buffer_event_data(event);
1131
1132                 tracing_generic_entry_update(ent, flags, pc);
1133                 ent->type = type;
1134         }
1135
1136         return event;
1137 }
1138
1139 static inline void
1140 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1141                              struct ring_buffer_event *event,
1142                              unsigned long flags, int pc,
1143                              int wake)
1144 {
1145         ring_buffer_unlock_commit(buffer, event);
1146
1147         ftrace_trace_stack(buffer, flags, 6, pc);
1148         ftrace_trace_userstack(buffer, flags, pc);
1149
1150         if (wake)
1151                 trace_wake_up();
1152 }
1153
1154 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1155                                 struct ring_buffer_event *event,
1156                                 unsigned long flags, int pc)
1157 {
1158         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1159 }
1160
1161 struct ring_buffer_event *
1162 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1163                                   int type, unsigned long len,
1164                                   unsigned long flags, int pc)
1165 {
1166         *current_rb = global_trace.buffer;
1167         return trace_buffer_lock_reserve(*current_rb,
1168                                          type, len, flags, pc);
1169 }
1170 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1171
1172 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1173                                         struct ring_buffer_event *event,
1174                                         unsigned long flags, int pc)
1175 {
1176         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1177 }
1178 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1179
1180 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1181                                        struct ring_buffer_event *event,
1182                                        unsigned long flags, int pc)
1183 {
1184         __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1185 }
1186 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1187
1188 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1189                                          struct ring_buffer_event *event)
1190 {
1191         ring_buffer_discard_commit(buffer, event);
1192 }
1193 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1194
1195 void
1196 trace_function(struct trace_array *tr,
1197                unsigned long ip, unsigned long parent_ip, unsigned long flags,
1198                int pc)
1199 {
1200         struct ftrace_event_call *call = &event_function;
1201         struct ring_buffer *buffer = tr->buffer;
1202         struct ring_buffer_event *event;
1203         struct ftrace_entry *entry;
1204
1205         /* If we are reading the ring buffer, don't trace */
1206         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1207                 return;
1208
1209         event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1210                                           flags, pc);
1211         if (!event)
1212                 return;
1213         entry   = ring_buffer_event_data(event);
1214         entry->ip                       = ip;
1215         entry->parent_ip                = parent_ip;
1216
1217         if (!filter_check_discard(call, entry, buffer, event))
1218                 ring_buffer_unlock_commit(buffer, event);
1219 }
1220
1221 void
1222 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1223        unsigned long ip, unsigned long parent_ip, unsigned long flags,
1224        int pc)
1225 {
1226         if (likely(!atomic_read(&data->disabled)))
1227                 trace_function(tr, ip, parent_ip, flags, pc);
1228 }
1229
1230 #ifdef CONFIG_STACKTRACE
1231 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1232                                  unsigned long flags,
1233                                  int skip, int pc)
1234 {
1235         struct ftrace_event_call *call = &event_kernel_stack;
1236         struct ring_buffer_event *event;
1237         struct stack_entry *entry;
1238         struct stack_trace trace;
1239
1240         event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1241                                           sizeof(*entry), flags, pc);
1242         if (!event)
1243                 return;
1244         entry   = ring_buffer_event_data(event);
1245         memset(&entry->caller, 0, sizeof(entry->caller));
1246
1247         trace.nr_entries        = 0;
1248         trace.max_entries       = FTRACE_STACK_ENTRIES;
1249         trace.skip              = skip;
1250         trace.entries           = entry->caller;
1251
1252         save_stack_trace(&trace);
1253         if (!filter_check_discard(call, entry, buffer, event))
1254                 ring_buffer_unlock_commit(buffer, event);
1255 }
1256
1257 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1258                         int skip, int pc)
1259 {
1260         if (!(trace_flags & TRACE_ITER_STACKTRACE))
1261                 return;
1262
1263         __ftrace_trace_stack(buffer, flags, skip, pc);
1264 }
1265
1266 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1267                    int pc)
1268 {
1269         __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1270 }
1271
1272 /**
1273  * trace_dump_stack - record a stack back trace in the trace buffer
1274  */
1275 void trace_dump_stack(void)
1276 {
1277         unsigned long flags;
1278
1279         if (tracing_disabled || tracing_selftest_running)
1280                 return;
1281
1282         local_save_flags(flags);
1283
1284         /* skipping 3 traces, seems to get us at the caller of this function */
1285         __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1286 }
1287
1288 void
1289 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1290 {
1291         struct ftrace_event_call *call = &event_user_stack;
1292         struct ring_buffer_event *event;
1293         struct userstack_entry *entry;
1294         struct stack_trace trace;
1295
1296         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1297                 return;
1298
1299         /*
1300          * NMIs can not handle page faults, even with fix ups.
1301          * The save user stack can (and often does) fault.
1302          */
1303         if (unlikely(in_nmi()))
1304                 return;
1305
1306         event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1307                                           sizeof(*entry), flags, pc);
1308         if (!event)
1309                 return;
1310         entry   = ring_buffer_event_data(event);
1311
1312         entry->tgid             = current->tgid;
1313         memset(&entry->caller, 0, sizeof(entry->caller));
1314
1315         trace.nr_entries        = 0;
1316         trace.max_entries       = FTRACE_STACK_ENTRIES;
1317         trace.skip              = 0;
1318         trace.entries           = entry->caller;
1319
1320         save_stack_trace_user(&trace);
1321         if (!filter_check_discard(call, entry, buffer, event))
1322                 ring_buffer_unlock_commit(buffer, event);
1323 }
1324
1325 #ifdef UNUSED
1326 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1327 {
1328         ftrace_trace_userstack(tr, flags, preempt_count());
1329 }
1330 #endif /* UNUSED */
1331
1332 #endif /* CONFIG_STACKTRACE */
1333
1334 static void
1335 ftrace_trace_special(void *__tr,
1336                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
1337                      int pc)
1338 {
1339         struct ftrace_event_call *call = &event_special;
1340         struct ring_buffer_event *event;
1341         struct trace_array *tr = __tr;
1342         struct ring_buffer *buffer = tr->buffer;
1343         struct special_entry *entry;
1344
1345         event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1346                                           sizeof(*entry), 0, pc);
1347         if (!event)
1348                 return;
1349         entry   = ring_buffer_event_data(event);
1350         entry->arg1                     = arg1;
1351         entry->arg2                     = arg2;
1352         entry->arg3                     = arg3;
1353
1354         if (!filter_check_discard(call, entry, buffer, event))
1355                 trace_buffer_unlock_commit(buffer, event, 0, pc);
1356 }
1357
1358 void
1359 __trace_special(void *__tr, void *__data,
1360                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1361 {
1362         ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1363 }
1364
1365 void
1366 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1367 {
1368         struct trace_array *tr = &global_trace;
1369         struct trace_array_cpu *data;
1370         unsigned long flags;
1371         int cpu;
1372         int pc;
1373
1374         if (tracing_disabled)
1375                 return;
1376
1377         pc = preempt_count();
1378         local_irq_save(flags);
1379         cpu = raw_smp_processor_id();
1380         data = tr->data[cpu];
1381
1382         if (likely(atomic_inc_return(&data->disabled) == 1))
1383                 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1384
1385         atomic_dec(&data->disabled);
1386         local_irq_restore(flags);
1387 }
1388
1389 /**
1390  * trace_vbprintk - write binary msg to tracing buffer
1391  *
1392  */
1393 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1394 {
1395         static arch_spinlock_t trace_buf_lock =
1396                 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1397         static u32 trace_buf[TRACE_BUF_SIZE];
1398
1399         struct ftrace_event_call *call = &event_bprint;
1400         struct ring_buffer_event *event;
1401         struct ring_buffer *buffer;
1402         struct trace_array *tr = &global_trace;
1403         struct trace_array_cpu *data;
1404         struct bprint_entry *entry;
1405         unsigned long flags;
1406         int disable;
1407         int resched;
1408         int cpu, len = 0, size, pc;
1409
1410         if (unlikely(tracing_selftest_running || tracing_disabled))
1411                 return 0;
1412
1413         /* Don't pollute graph traces with trace_vprintk internals */
1414         pause_graph_tracing();
1415
1416         pc = preempt_count();
1417         resched = ftrace_preempt_disable();
1418         cpu = raw_smp_processor_id();
1419         data = tr->data[cpu];
1420
1421         disable = atomic_inc_return(&data->disabled);
1422         if (unlikely(disable != 1))
1423                 goto out;
1424
1425         /* Lockdep uses trace_printk for lock tracing */
1426         local_irq_save(flags);
1427         arch_spin_lock(&trace_buf_lock);
1428         len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1429
1430         if (len > TRACE_BUF_SIZE || len < 0)
1431                 goto out_unlock;
1432
1433         size = sizeof(*entry) + sizeof(u32) * len;
1434         buffer = tr->buffer;
1435         event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1436                                           flags, pc);
1437         if (!event)
1438                 goto out_unlock;
1439         entry = ring_buffer_event_data(event);
1440         entry->ip                       = ip;
1441         entry->fmt                      = fmt;
1442
1443         memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1444         if (!filter_check_discard(call, entry, buffer, event)) {
1445                 ring_buffer_unlock_commit(buffer, event);
1446                 ftrace_trace_stack(buffer, flags, 6, pc);
1447         }
1448
1449 out_unlock:
1450         arch_spin_unlock(&trace_buf_lock);
1451         local_irq_restore(flags);
1452
1453 out:
1454         atomic_dec_return(&data->disabled);
1455         ftrace_preempt_enable(resched);
1456         unpause_graph_tracing();
1457
1458         return len;
1459 }
1460 EXPORT_SYMBOL_GPL(trace_vbprintk);
1461
1462 int trace_array_printk(struct trace_array *tr,
1463                        unsigned long ip, const char *fmt, ...)
1464 {
1465         int ret;
1466         va_list ap;
1467
1468         if (!(trace_flags & TRACE_ITER_PRINTK))
1469                 return 0;
1470
1471         va_start(ap, fmt);
1472         ret = trace_array_vprintk(tr, ip, fmt, ap);
1473         va_end(ap);
1474         return ret;
1475 }
1476
1477 int trace_array_vprintk(struct trace_array *tr,
1478                         unsigned long ip, const char *fmt, va_list args)
1479 {
1480         static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1481         static char trace_buf[TRACE_BUF_SIZE];
1482
1483         struct ftrace_event_call *call = &event_print;
1484         struct ring_buffer_event *event;
1485         struct ring_buffer *buffer;
1486         struct trace_array_cpu *data;
1487         int cpu, len = 0, size, pc;
1488         struct print_entry *entry;
1489         unsigned long irq_flags;
1490         int disable;
1491
1492         if (tracing_disabled || tracing_selftest_running)
1493                 return 0;
1494
1495         pc = preempt_count();
1496         preempt_disable_notrace();
1497         cpu = raw_smp_processor_id();
1498         data = tr->data[cpu];
1499
1500         disable = atomic_inc_return(&data->disabled);
1501         if (unlikely(disable != 1))
1502                 goto out;
1503
1504         pause_graph_tracing();
1505         raw_local_irq_save(irq_flags);
1506         arch_spin_lock(&trace_buf_lock);
1507         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1508
1509         size = sizeof(*entry) + len + 1;
1510         buffer = tr->buffer;
1511         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1512                                           irq_flags, pc);
1513         if (!event)
1514                 goto out_unlock;
1515         entry = ring_buffer_event_data(event);
1516         entry->ip = ip;
1517
1518         memcpy(&entry->buf, trace_buf, len);
1519         entry->buf[len] = '\0';
1520         if (!filter_check_discard(call, entry, buffer, event)) {
1521                 ring_buffer_unlock_commit(buffer, event);
1522                 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1523         }
1524
1525  out_unlock:
1526         arch_spin_unlock(&trace_buf_lock);
1527         raw_local_irq_restore(irq_flags);
1528         unpause_graph_tracing();
1529  out:
1530         atomic_dec_return(&data->disabled);
1531         preempt_enable_notrace();
1532
1533         return len;
1534 }
1535
1536 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1537 {
1538         return trace_array_vprintk(&global_trace, ip, fmt, args);
1539 }
1540 EXPORT_SYMBOL_GPL(trace_vprintk);
1541
1542 enum trace_file_type {
1543         TRACE_FILE_LAT_FMT      = 1,
1544         TRACE_FILE_ANNOTATE     = 2,
1545 };
1546
1547 static void trace_iterator_increment(struct trace_iterator *iter)
1548 {
1549         /* Don't allow ftrace to trace into the ring buffers */
1550         ftrace_disable_cpu();
1551
1552         iter->idx++;
1553         if (iter->buffer_iter[iter->cpu])
1554                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1555
1556         ftrace_enable_cpu();
1557 }
1558
1559 static struct trace_entry *
1560 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1561                 unsigned long *lost_events)
1562 {
1563         struct ring_buffer_event *event;
1564         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1565
1566         /* Don't allow ftrace to trace into the ring buffers */
1567         ftrace_disable_cpu();
1568
1569         if (buf_iter)
1570                 event = ring_buffer_iter_peek(buf_iter, ts);
1571         else
1572                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1573                                          lost_events);
1574
1575         ftrace_enable_cpu();
1576
1577         return event ? ring_buffer_event_data(event) : NULL;
1578 }
1579
1580 static struct trace_entry *
1581 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1582                   unsigned long *missing_events, u64 *ent_ts)
1583 {
1584         struct ring_buffer *buffer = iter->tr->buffer;
1585         struct trace_entry *ent, *next = NULL;
1586         unsigned long lost_events = 0, next_lost = 0;
1587         int cpu_file = iter->cpu_file;
1588         u64 next_ts = 0, ts;
1589         int next_cpu = -1;
1590         int cpu;
1591
1592         /*
1593          * If we are in a per_cpu trace file, don't bother by iterating over
1594          * all cpu and peek directly.
1595          */
1596         if (cpu_file > TRACE_PIPE_ALL_CPU) {
1597                 if (ring_buffer_empty_cpu(buffer, cpu_file))
1598                         return NULL;
1599                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1600                 if (ent_cpu)
1601                         *ent_cpu = cpu_file;
1602
1603                 return ent;
1604         }
1605
1606         for_each_tracing_cpu(cpu) {
1607
1608                 if (ring_buffer_empty_cpu(buffer, cpu))
1609                         continue;
1610
1611                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1612
1613                 /*
1614                  * Pick the entry with the smallest timestamp:
1615                  */
1616                 if (ent && (!next || ts < next_ts)) {
1617                         next = ent;
1618                         next_cpu = cpu;
1619                         next_ts = ts;
1620                         next_lost = lost_events;
1621                 }
1622         }
1623
1624         if (ent_cpu)
1625                 *ent_cpu = next_cpu;
1626
1627         if (ent_ts)
1628                 *ent_ts = next_ts;
1629
1630         if (missing_events)
1631                 *missing_events = next_lost;
1632
1633         return next;
1634 }
1635
1636 /* Find the next real entry, without updating the iterator itself */
1637 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1638                                           int *ent_cpu, u64 *ent_ts)
1639 {
1640         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1641 }
1642
1643 /* Find the next real entry, and increment the iterator to the next entry */
1644 static void *find_next_entry_inc(struct trace_iterator *iter)
1645 {
1646         iter->ent = __find_next_entry(iter, &iter->cpu,
1647                                       &iter->lost_events, &iter->ts);
1648
1649         if (iter->ent)
1650                 trace_iterator_increment(iter);
1651
1652         return iter->ent ? iter : NULL;
1653 }
1654
1655 static void trace_consume(struct trace_iterator *iter)
1656 {
1657         /* Don't allow ftrace to trace into the ring buffers */
1658         ftrace_disable_cpu();
1659         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1660                             &iter->lost_events);
1661         ftrace_enable_cpu();
1662 }
1663
1664 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1665 {
1666         struct trace_iterator *iter = m->private;
1667         int i = (int)*pos;
1668         void *ent;
1669
1670         WARN_ON_ONCE(iter->leftover);
1671
1672         (*pos)++;
1673
1674         /* can't go backwards */
1675         if (iter->idx > i)
1676                 return NULL;
1677
1678         if (iter->idx < 0)
1679                 ent = find_next_entry_inc(iter);
1680         else
1681                 ent = iter;
1682
1683         while (ent && iter->idx < i)
1684                 ent = find_next_entry_inc(iter);
1685
1686         iter->pos = *pos;
1687
1688         return ent;
1689 }
1690
1691 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1692 {
1693         struct trace_array *tr = iter->tr;
1694         struct ring_buffer_event *event;
1695         struct ring_buffer_iter *buf_iter;
1696         unsigned long entries = 0;
1697         u64 ts;
1698
1699         tr->data[cpu]->skipped_entries = 0;
1700
1701         if (!iter->buffer_iter[cpu])
1702                 return;
1703
1704         buf_iter = iter->buffer_iter[cpu];
1705         ring_buffer_iter_reset(buf_iter);
1706
1707         /*
1708          * We could have the case with the max latency tracers
1709          * that a reset never took place on a cpu. This is evident
1710          * by the timestamp being before the start of the buffer.
1711          */
1712         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1713                 if (ts >= iter->tr->time_start)
1714                         break;
1715                 entries++;
1716                 ring_buffer_read(buf_iter, NULL);
1717         }
1718
1719         tr->data[cpu]->skipped_entries = entries;
1720 }
1721
1722 /*
1723  * The current tracer is copied to avoid a global locking
1724  * all around.
1725  */
1726 static void *s_start(struct seq_file *m, loff_t *pos)
1727 {
1728         struct trace_iterator *iter = m->private;
1729         static struct tracer *old_tracer;
1730         int cpu_file = iter->cpu_file;
1731         void *p = NULL;
1732         loff_t l = 0;
1733         int cpu;
1734
1735         /* copy the tracer to avoid using a global lock all around */
1736         mutex_lock(&trace_types_lock);
1737         if (unlikely(old_tracer != current_trace && current_trace)) {
1738                 old_tracer = current_trace;
1739                 *iter->trace = *current_trace;
1740         }
1741         mutex_unlock(&trace_types_lock);
1742
1743         atomic_inc(&trace_record_cmdline_disabled);
1744
1745         if (*pos != iter->pos) {
1746                 iter->ent = NULL;
1747                 iter->cpu = 0;
1748                 iter->idx = -1;
1749
1750                 ftrace_disable_cpu();
1751
1752                 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1753                         for_each_tracing_cpu(cpu)
1754                                 tracing_iter_reset(iter, cpu);
1755                 } else
1756                         tracing_iter_reset(iter, cpu_file);
1757
1758                 ftrace_enable_cpu();
1759
1760                 iter->leftover = 0;
1761                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1762                         ;
1763
1764         } else {
1765                 /*
1766                  * If we overflowed the seq_file before, then we want
1767                  * to just reuse the trace_seq buffer again.
1768                  */
1769                 if (iter->leftover)
1770                         p = iter;
1771                 else {
1772                         l = *pos - 1;
1773                         p = s_next(m, p, &l);
1774                 }
1775         }
1776
1777         trace_event_read_lock();
1778         trace_access_lock(cpu_file);
1779         return p;
1780 }
1781
1782 static void s_stop(struct seq_file *m, void *p)
1783 {
1784         struct trace_iterator *iter = m->private;
1785
1786         atomic_dec(&trace_record_cmdline_disabled);
1787         trace_access_unlock(iter->cpu_file);
1788         trace_event_read_unlock();
1789 }
1790
1791 static void print_lat_help_header(struct seq_file *m)
1792 {
1793         seq_puts(m, "#                  _------=> CPU#            \n");
1794         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1795         seq_puts(m, "#                | / _----=> need-resched    \n");
1796         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1797         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1798         seq_puts(m, "#                |||| /_--=> lock-depth       \n");
1799         seq_puts(m, "#                |||||/     delay             \n");
1800         seq_puts(m, "#  cmd     pid   |||||| time  |   caller      \n");
1801         seq_puts(m, "#     \\   /      ||||||   \\   |   /           \n");
1802 }
1803
1804 static void print_func_help_header(struct seq_file *m)
1805 {
1806         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1807         seq_puts(m, "#              | |       |          |         |\n");
1808 }
1809
1810
1811 void
1812 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1813 {
1814         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1815         struct trace_array *tr = iter->tr;
1816         struct trace_array_cpu *data = tr->data[tr->cpu];
1817         struct tracer *type = current_trace;
1818         unsigned long entries = 0;
1819         unsigned long total = 0;
1820         unsigned long count;
1821         const char *name = "preemption";
1822         int cpu;
1823
1824         if (type)
1825                 name = type->name;
1826
1827
1828         for_each_tracing_cpu(cpu) {
1829                 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1830                 /*
1831                  * If this buffer has skipped entries, then we hold all
1832                  * entries for the trace and we need to ignore the
1833                  * ones before the time stamp.
1834                  */
1835                 if (tr->data[cpu]->skipped_entries) {
1836                         count -= tr->data[cpu]->skipped_entries;
1837                         /* total is the same as the entries */
1838                         total += count;
1839                 } else
1840                         total += count +
1841                                 ring_buffer_overrun_cpu(tr->buffer, cpu);
1842                 entries += count;
1843         }
1844
1845         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1846                    name, UTS_RELEASE);
1847         seq_puts(m, "# -----------------------------------"
1848                  "---------------------------------\n");
1849         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1850                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1851                    nsecs_to_usecs(data->saved_latency),
1852                    entries,
1853                    total,
1854                    tr->cpu,
1855 #if defined(CONFIG_PREEMPT_NONE)
1856                    "server",
1857 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1858                    "desktop",
1859 #elif defined(CONFIG_PREEMPT)
1860                    "preempt",
1861 #else
1862                    "unknown",
1863 #endif
1864                    /* These are reserved for later use */
1865                    0, 0, 0, 0);
1866 #ifdef CONFIG_SMP
1867         seq_printf(m, " #P:%d)\n", num_online_cpus());
1868 #else
1869         seq_puts(m, ")\n");
1870 #endif
1871         seq_puts(m, "#    -----------------\n");
1872         seq_printf(m, "#    | task: %.16s-%d "
1873                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1874                    data->comm, data->pid, data->uid, data->nice,
1875                    data->policy, data->rt_priority);
1876         seq_puts(m, "#    -----------------\n");
1877
1878         if (data->critical_start) {
1879                 seq_puts(m, "#  => started at: ");
1880                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1881                 trace_print_seq(m, &iter->seq);
1882                 seq_puts(m, "\n#  => ended at:   ");
1883                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1884                 trace_print_seq(m, &iter->seq);
1885                 seq_puts(m, "\n#\n");
1886         }
1887
1888         seq_puts(m, "#\n");
1889 }
1890
1891 static void test_cpu_buff_start(struct trace_iterator *iter)
1892 {
1893         struct trace_seq *s = &iter->seq;
1894
1895         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1896                 return;
1897
1898         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1899                 return;
1900
1901         if (cpumask_test_cpu(iter->cpu, iter->started))
1902                 return;
1903
1904         if (iter->tr->data[iter->cpu]->skipped_entries)
1905                 return;
1906
1907         cpumask_set_cpu(iter->cpu, iter->started);
1908
1909         /* Don't print started cpu buffer for the first entry of the trace */
1910         if (iter->idx > 1)
1911                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1912                                 iter->cpu);
1913 }
1914
1915 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1916 {
1917         struct trace_seq *s = &iter->seq;
1918         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1919         struct trace_entry *entry;
1920         struct trace_event *event;
1921
1922         entry = iter->ent;
1923
1924         test_cpu_buff_start(iter);
1925
1926         event = ftrace_find_event(entry->type);
1927
1928         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1929                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1930                         if (!trace_print_lat_context(iter))
1931                                 goto partial;
1932                 } else {
1933                         if (!trace_print_context(iter))
1934                                 goto partial;
1935                 }
1936         }
1937
1938         if (event)
1939                 return event->funcs->trace(iter, sym_flags, event);
1940
1941         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1942                 goto partial;
1943
1944         return TRACE_TYPE_HANDLED;
1945 partial:
1946         return TRACE_TYPE_PARTIAL_LINE;
1947 }
1948
1949 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1950 {
1951         struct trace_seq *s = &iter->seq;
1952         struct trace_entry *entry;
1953         struct trace_event *event;
1954
1955         entry = iter->ent;
1956
1957         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1958                 if (!trace_seq_printf(s, "%d %d %llu ",
1959                                       entry->pid, iter->cpu, iter->ts))
1960                         goto partial;
1961         }
1962
1963         event = ftrace_find_event(entry->type);
1964         if (event)
1965                 return event->funcs->raw(iter, 0, event);
1966
1967         if (!trace_seq_printf(s, "%d ?\n", entry->type))
1968                 goto partial;
1969
1970         return TRACE_TYPE_HANDLED;
1971 partial:
1972         return TRACE_TYPE_PARTIAL_LINE;
1973 }
1974
1975 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1976 {
1977         struct trace_seq *s = &iter->seq;
1978         unsigned char newline = '\n';
1979         struct trace_entry *entry;
1980         struct trace_event *event;
1981
1982         entry = iter->ent;
1983
1984         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1985                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1986                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1987                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1988         }
1989
1990         event = ftrace_find_event(entry->type);
1991         if (event) {
1992                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
1993                 if (ret != TRACE_TYPE_HANDLED)
1994                         return ret;
1995         }
1996
1997         SEQ_PUT_FIELD_RET(s, newline);
1998
1999         return TRACE_TYPE_HANDLED;
2000 }
2001
2002 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2003 {
2004         struct trace_seq *s = &iter->seq;
2005         struct trace_entry *entry;
2006         struct trace_event *event;
2007
2008         entry = iter->ent;
2009
2010         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2011                 SEQ_PUT_FIELD_RET(s, entry->pid);
2012                 SEQ_PUT_FIELD_RET(s, iter->cpu);
2013                 SEQ_PUT_FIELD_RET(s, iter->ts);
2014         }
2015
2016         event = ftrace_find_event(entry->type);
2017         return event ? event->funcs->binary(iter, 0, event) :
2018                 TRACE_TYPE_HANDLED;
2019 }
2020
2021 int trace_empty(struct trace_iterator *iter)
2022 {
2023         int cpu;
2024
2025         /* If we are looking at one CPU buffer, only check that one */
2026         if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2027                 cpu = iter->cpu_file;
2028                 if (iter->buffer_iter[cpu]) {
2029                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2030                                 return 0;
2031                 } else {
2032                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2033                                 return 0;
2034                 }
2035                 return 1;
2036         }
2037
2038         for_each_tracing_cpu(cpu) {
2039                 if (iter->buffer_iter[cpu]) {
2040                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2041                                 return 0;
2042                 } else {
2043                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2044                                 return 0;
2045                 }
2046         }
2047
2048         return 1;
2049 }
2050
2051 /*  Called with trace_event_read_lock() held. */
2052 static enum print_line_t print_trace_line(struct trace_iterator *iter)
2053 {
2054         enum print_line_t ret;
2055
2056         if (iter->lost_events)
2057                 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2058                                  iter->cpu, iter->lost_events);
2059
2060         if (iter->trace && iter->trace->print_line) {
2061                 ret = iter->trace->print_line(iter);
2062                 if (ret != TRACE_TYPE_UNHANDLED)
2063                         return ret;
2064         }
2065
2066         if (iter->ent->type == TRACE_BPRINT &&
2067                         trace_flags & TRACE_ITER_PRINTK &&
2068                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2069                 return trace_print_bprintk_msg_only(iter);
2070
2071         if (iter->ent->type == TRACE_PRINT &&
2072                         trace_flags & TRACE_ITER_PRINTK &&
2073                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2074                 return trace_print_printk_msg_only(iter);
2075
2076         if (trace_flags & TRACE_ITER_BIN)
2077                 return print_bin_fmt(iter);
2078
2079         if (trace_flags & TRACE_ITER_HEX)
2080                 return print_hex_fmt(iter);
2081
2082         if (trace_flags & TRACE_ITER_RAW)
2083                 return print_raw_fmt(iter);
2084
2085         return print_trace_fmt(iter);
2086 }
2087
2088 void trace_default_header(struct seq_file *m)
2089 {
2090         struct trace_iterator *iter = m->private;
2091
2092         if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2093                 /* print nothing if the buffers are empty */
2094                 if (trace_empty(iter))
2095                         return;
2096                 print_trace_header(m, iter);
2097                 if (!(trace_flags & TRACE_ITER_VERBOSE))
2098                         print_lat_help_header(m);
2099         } else {
2100                 if (!(trace_flags & TRACE_ITER_VERBOSE))
2101                         print_func_help_header(m);
2102         }
2103 }
2104
2105 static int s_show(struct seq_file *m, void *v)
2106 {
2107         struct trace_iterator *iter = v;
2108         int ret;
2109
2110         if (iter->ent == NULL) {
2111                 if (iter->tr) {
2112                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
2113                         seq_puts(m, "#\n");
2114                 }
2115                 if (iter->trace && iter->trace->print_header)
2116                         iter->trace->print_header(m);
2117                 else
2118                         trace_default_header(m);
2119
2120         } else if (iter->leftover) {
2121                 /*
2122                  * If we filled the seq_file buffer earlier, we
2123                  * want to just show it now.
2124                  */
2125                 ret = trace_print_seq(m, &iter->seq);
2126
2127                 /* ret should this time be zero, but you never know */
2128                 iter->leftover = ret;
2129
2130         } else {
2131                 print_trace_line(iter);
2132                 ret = trace_print_seq(m, &iter->seq);
2133                 /*
2134                  * If we overflow the seq_file buffer, then it will
2135                  * ask us for this data again at start up.
2136                  * Use that instead.
2137                  *  ret is 0 if seq_file write succeeded.
2138                  *        -1 otherwise.
2139                  */
2140                 iter->leftover = ret;
2141         }
2142
2143         return 0;
2144 }
2145
2146 static const struct seq_operations tracer_seq_ops = {
2147         .start          = s_start,
2148         .next           = s_next,
2149         .stop           = s_stop,
2150         .show           = s_show,
2151 };
2152
2153 static struct trace_iterator *
2154 __tracing_open(struct inode *inode, struct file *file)
2155 {
2156         long cpu_file = (long) inode->i_private;
2157         void *fail_ret = ERR_PTR(-ENOMEM);
2158         struct trace_iterator *iter;
2159         struct seq_file *m;
2160         int cpu, ret;
2161
2162         if (tracing_disabled)
2163                 return ERR_PTR(-ENODEV);
2164
2165         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2166         if (!iter)
2167                 return ERR_PTR(-ENOMEM);
2168
2169         /*
2170          * We make a copy of the current tracer to avoid concurrent
2171          * changes on it while we are reading.
2172          */
2173         mutex_lock(&trace_types_lock);
2174         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2175         if (!iter->trace)
2176                 goto fail;
2177
2178         if (current_trace)
2179                 *iter->trace = *current_trace;
2180
2181         if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2182                 goto fail;
2183
2184         if (current_trace && current_trace->print_max)
2185                 iter->tr = &max_tr;
2186         else
2187                 iter->tr = &global_trace;
2188         iter->pos = -1;
2189         mutex_init(&iter->mutex);
2190         iter->cpu_file = cpu_file;
2191
2192         /* Notify the tracer early; before we stop tracing. */
2193         if (iter->trace && iter->trace->open)
2194                 iter->trace->open(iter);
2195
2196         /* Annotate start of buffers if we had overruns */
2197         if (ring_buffer_overruns(iter->tr->buffer))
2198                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2199
2200         /* stop the trace while dumping */
2201         tracing_stop();
2202
2203         if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2204                 for_each_tracing_cpu(cpu) {
2205                         iter->buffer_iter[cpu] =
2206                                 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2207                 }
2208                 ring_buffer_read_prepare_sync();
2209                 for_each_tracing_cpu(cpu) {
2210                         ring_buffer_read_start(iter->buffer_iter[cpu]);
2211                         tracing_iter_reset(iter, cpu);
2212                 }
2213         } else {
2214                 cpu = iter->cpu_file;
2215                 iter->buffer_iter[cpu] =
2216                         ring_buffer_read_prepare(iter->tr->buffer, cpu);
2217                 ring_buffer_read_prepare_sync();
2218                 ring_buffer_read_start(iter->buffer_iter[cpu]);
2219                 tracing_iter_reset(iter, cpu);
2220         }
2221
2222         ret = seq_open(file, &tracer_seq_ops);
2223         if (ret < 0) {
2224                 fail_ret = ERR_PTR(ret);
2225                 goto fail_buffer;
2226         }
2227
2228         m = file->private_data;
2229         m->private = iter;
2230
2231         mutex_unlock(&trace_types_lock);
2232
2233         return iter;
2234
2235  fail_buffer:
2236         for_each_tracing_cpu(cpu) {
2237                 if (iter->buffer_iter[cpu])
2238                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2239         }
2240         free_cpumask_var(iter->started);
2241         tracing_start();
2242  fail:
2243         mutex_unlock(&trace_types_lock);
2244         kfree(iter->trace);
2245         kfree(iter);
2246
2247         return fail_ret;
2248 }
2249
2250 int tracing_open_generic(struct inode *inode, struct file *filp)
2251 {
2252         if (tracing_disabled)
2253                 return -ENODEV;
2254
2255         filp->private_data = inode->i_private;
2256         return 0;
2257 }
2258
2259 static int tracing_release(struct inode *inode, struct file *file)
2260 {
2261         struct seq_file *m = (struct seq_file *)file->private_data;
2262         struct trace_iterator *iter;
2263         int cpu;
2264
2265         if (!(file->f_mode & FMODE_READ))
2266                 return 0;
2267
2268         iter = m->private;
2269
2270         mutex_lock(&trace_types_lock);
2271         for_each_tracing_cpu(cpu) {
2272                 if (iter->buffer_iter[cpu])
2273                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2274         }
2275
2276         if (iter->trace && iter->trace->close)
2277                 iter->trace->close(iter);
2278
2279         /* reenable tracing if it was previously enabled */
2280         tracing_start();
2281         mutex_unlock(&trace_types_lock);
2282
2283         seq_release(inode, file);
2284         mutex_destroy(&iter->mutex);
2285         free_cpumask_var(iter->started);
2286         kfree(iter->trace);
2287         kfree(iter);
2288         return 0;
2289 }
2290
2291 static int tracing_open(struct inode *inode, struct file *file)
2292 {
2293         struct trace_iterator *iter;
2294         int ret = 0;
2295
2296         /* If this file was open for write, then erase contents */
2297         if ((file->f_mode & FMODE_WRITE) &&
2298             (file->f_flags & O_TRUNC)) {
2299                 long cpu = (long) inode->i_private;
2300
2301                 if (cpu == TRACE_PIPE_ALL_CPU)
2302                         tracing_reset_online_cpus(&global_trace);
2303                 else
2304                         tracing_reset(&global_trace, cpu);
2305         }
2306
2307         if (file->f_mode & FMODE_READ) {
2308                 iter = __tracing_open(inode, file);
2309                 if (IS_ERR(iter))
2310                         ret = PTR_ERR(iter);
2311                 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2312                         iter->iter_flags |= TRACE_FILE_LAT_FMT;
2313         }
2314         return ret;
2315 }
2316
2317 static void *
2318 t_next(struct seq_file *m, void *v, loff_t *pos)
2319 {
2320         struct tracer *t = v;
2321
2322         (*pos)++;
2323
2324         if (t)
2325                 t = t->next;
2326
2327         return t;
2328 }
2329
2330 static void *t_start(struct seq_file *m, loff_t *pos)
2331 {
2332         struct tracer *t;
2333         loff_t l = 0;
2334
2335         mutex_lock(&trace_types_lock);
2336         for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2337                 ;
2338
2339         return t;
2340 }
2341
2342 static void t_stop(struct seq_file *m, void *p)
2343 {
2344         mutex_unlock(&trace_types_lock);
2345 }
2346
2347 static int t_show(struct seq_file *m, void *v)
2348 {
2349         struct tracer *t = v;
2350
2351         if (!t)
2352                 return 0;
2353
2354         seq_printf(m, "%s", t->name);
2355         if (t->next)
2356                 seq_putc(m, ' ');
2357         else
2358                 seq_putc(m, '\n');
2359
2360         return 0;
2361 }
2362
2363 static const struct seq_operations show_traces_seq_ops = {
2364         .start          = t_start,
2365         .next           = t_next,
2366         .stop           = t_stop,
2367         .show           = t_show,
2368 };
2369
2370 static int show_traces_open(struct inode *inode, struct file *file)
2371 {
2372         if (tracing_disabled)
2373                 return -ENODEV;
2374
2375         return seq_open(file, &show_traces_seq_ops);
2376 }
2377
2378 static ssize_t
2379 tracing_write_stub(struct file *filp, const char __user *ubuf,
2380                    size_t count, loff_t *ppos)
2381 {
2382         return count;
2383 }
2384
2385 static const struct file_operations tracing_fops = {
2386         .open           = tracing_open,
2387         .read           = seq_read,
2388         .write          = tracing_write_stub,
2389         .llseek         = seq_lseek,
2390         .release        = tracing_release,
2391 };
2392
2393 static const struct file_operations show_traces_fops = {
2394         .open           = show_traces_open,
2395         .read           = seq_read,
2396         .release        = seq_release,
2397 };
2398
2399 /*
2400  * Only trace on a CPU if the bitmask is set:
2401  */
2402 static cpumask_var_t tracing_cpumask;
2403
2404 /*
2405  * The tracer itself will not take this lock, but still we want
2406  * to provide a consistent cpumask to user-space:
2407  */
2408 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2409
2410 /*
2411  * Temporary storage for the character representation of the
2412  * CPU bitmask (and one more byte for the newline):
2413  */
2414 static char mask_str[NR_CPUS + 1];
2415
2416 static ssize_t
2417 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2418                      size_t count, loff_t *ppos)
2419 {
2420         int len;
2421
2422         mutex_lock(&tracing_cpumask_update_lock);
2423
2424         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2425         if (count - len < 2) {
2426                 count = -EINVAL;
2427                 goto out_err;
2428         }
2429         len += sprintf(mask_str + len, "\n");
2430         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2431
2432 out_err:
2433         mutex_unlock(&tracing_cpumask_update_lock);
2434
2435         return count;
2436 }
2437
2438 static ssize_t
2439 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2440                       size_t count, loff_t *ppos)
2441 {
2442         int err, cpu;
2443         cpumask_var_t tracing_cpumask_new;
2444
2445         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2446                 return -ENOMEM;
2447
2448         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2449         if (err)
2450                 goto err_unlock;
2451
2452         mutex_lock(&tracing_cpumask_update_lock);
2453
2454         local_irq_disable();
2455         arch_spin_lock(&ftrace_max_lock);
2456         for_each_tracing_cpu(cpu) {
2457                 /*
2458                  * Increase/decrease the disabled counter if we are
2459                  * about to flip a bit in the cpumask:
2460                  */
2461                 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2462                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2463                         atomic_inc(&global_trace.data[cpu]->disabled);
2464                 }
2465                 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2466                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2467                         atomic_dec(&global_trace.data[cpu]->disabled);
2468                 }
2469         }
2470         arch_spin_unlock(&ftrace_max_lock);
2471         local_irq_enable();
2472
2473         cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2474
2475         mutex_unlock(&tracing_cpumask_update_lock);
2476         free_cpumask_var(tracing_cpumask_new);
2477
2478         return count;
2479
2480 err_unlock:
2481         free_cpumask_var(tracing_cpumask_new);
2482
2483         return err;
2484 }
2485
2486 static const struct file_operations tracing_cpumask_fops = {
2487         .open           = tracing_open_generic,
2488         .read           = tracing_cpumask_read,
2489         .write          = tracing_cpumask_write,
2490 };
2491
2492 static int tracing_trace_options_show(struct seq_file *m, void *v)
2493 {
2494         struct tracer_opt *trace_opts;
2495         u32 tracer_flags;
2496         int i;
2497
2498         mutex_lock(&trace_types_lock);
2499         tracer_flags = current_trace->flags->val;
2500         trace_opts = current_trace->flags->opts;
2501
2502         for (i = 0; trace_options[i]; i++) {
2503                 if (trace_flags & (1 << i))
2504                         seq_printf(m, "%s\n", trace_options[i]);
2505                 else
2506                         seq_printf(m, "no%s\n", trace_options[i]);
2507         }
2508
2509         for (i = 0; trace_opts[i].name; i++) {
2510                 if (tracer_flags & trace_opts[i].bit)
2511                         seq_printf(m, "%s\n", trace_opts[i].name);
2512                 else
2513                         seq_printf(m, "no%s\n", trace_opts[i].name);
2514         }
2515         mutex_unlock(&trace_types_lock);
2516
2517         return 0;
2518 }
2519
2520 static int __set_tracer_option(struct tracer *trace,
2521                                struct tracer_flags *tracer_flags,
2522                                struct tracer_opt *opts, int neg)
2523 {
2524         int ret;
2525
2526         ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2527         if (ret)
2528                 return ret;
2529
2530         if (neg)
2531                 tracer_flags->val &= ~opts->bit;
2532         else
2533                 tracer_flags->val |= opts->bit;
2534         return 0;
2535 }
2536
2537 /* Try to assign a tracer specific option */
2538 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2539 {
2540         struct tracer_flags *tracer_flags = trace->flags;
2541         struct tracer_opt *opts = NULL;
2542         int i;
2543
2544         for (i = 0; tracer_flags->opts[i].name; i++) {
2545                 opts = &tracer_flags->opts[i];
2546
2547                 if (strcmp(cmp, opts->name) == 0)
2548                         return __set_tracer_option(trace, trace->flags,
2549                                                    opts, neg);
2550         }
2551
2552         return -EINVAL;
2553 }
2554
2555 static void set_tracer_flags(unsigned int mask, int enabled)
2556 {
2557         /* do nothing if flag is already set */
2558         if (!!(trace_flags & mask) == !!enabled)
2559                 return;
2560
2561         if (enabled)
2562                 trace_flags |= mask;
2563         else
2564                 trace_flags &= ~mask;
2565 }
2566
2567 static ssize_t
2568 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2569                         size_t cnt, loff_t *ppos)
2570 {
2571         char buf[64];
2572         char *cmp;
2573         int neg = 0;
2574         int ret;
2575         int i;
2576
2577         if (cnt >= sizeof(buf))
2578                 return -EINVAL;
2579
2580         if (copy_from_user(&buf, ubuf, cnt))
2581                 return -EFAULT;
2582
2583         buf[cnt] = 0;
2584         cmp = strstrip(buf);
2585
2586         if (strncmp(cmp, "no", 2) == 0) {
2587                 neg = 1;
2588                 cmp += 2;
2589         }
2590
2591         for (i = 0; trace_options[i]; i++) {
2592                 if (strcmp(cmp, trace_options[i]) == 0) {
2593                         set_tracer_flags(1 << i, !neg);
2594                         break;
2595                 }
2596         }
2597
2598         /* If no option could be set, test the specific tracer options */
2599         if (!trace_options[i]) {
2600                 mutex_lock(&trace_types_lock);
2601                 ret = set_tracer_option(current_trace, cmp, neg);
2602                 mutex_unlock(&trace_types_lock);
2603                 if (ret)
2604                         return ret;
2605         }
2606
2607         *ppos += cnt;
2608
2609         return cnt;
2610 }
2611
2612 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2613 {
2614         if (tracing_disabled)
2615                 return -ENODEV;
2616         return single_open(file, tracing_trace_options_show, NULL);
2617 }
2618
2619 static const struct file_operations tracing_iter_fops = {
2620         .open           = tracing_trace_options_open,
2621         .read           = seq_read,
2622         .llseek         = seq_lseek,
2623         .release        = single_release,
2624         .write          = tracing_trace_options_write,
2625 };
2626
2627 static const char readme_msg[] =
2628         "tracing mini-HOWTO:\n\n"
2629         "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2630         "# cat /sys/kernel/debug/tracing/available_tracers\n"
2631         "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2632         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2633         "nop\n"
2634         "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2635         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2636         "sched_switch\n"
2637         "# cat /sys/kernel/debug/tracing/trace_options\n"
2638         "noprint-parent nosym-offset nosym-addr noverbose\n"
2639         "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2640         "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2641         "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2642         "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2643 ;
2644
2645 static ssize_t
2646 tracing_readme_read(struct file *filp, char __user *ubuf,
2647                        size_t cnt, loff_t *ppos)
2648 {
2649         return simple_read_from_buffer(ubuf, cnt, ppos,
2650                                         readme_msg, strlen(readme_msg));
2651 }
2652
2653 static const struct file_operations tracing_readme_fops = {
2654         .open           = tracing_open_generic,
2655         .read           = tracing_readme_read,
2656 };
2657
2658 static ssize_t
2659 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2660                                 size_t cnt, loff_t *ppos)
2661 {
2662         char *buf_comm;
2663         char *file_buf;
2664         char *buf;
2665         int len = 0;
2666         int pid;
2667         int i;
2668
2669         file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2670         if (!file_buf)
2671                 return -ENOMEM;
2672
2673         buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2674         if (!buf_comm) {
2675                 kfree(file_buf);
2676                 return -ENOMEM;
2677         }
2678
2679         buf = file_buf;
2680
2681         for (i = 0; i < SAVED_CMDLINES; i++) {
2682                 int r;
2683
2684                 pid = map_cmdline_to_pid[i];
2685                 if (pid == -1 || pid == NO_CMDLINE_MAP)
2686                         continue;
2687
2688                 trace_find_cmdline(pid, buf_comm);
2689                 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2690                 buf += r;
2691                 len += r;
2692         }
2693
2694         len = simple_read_from_buffer(ubuf, cnt, ppos,
2695                                       file_buf, len);
2696
2697         kfree(file_buf);
2698         kfree(buf_comm);
2699
2700         return len;
2701 }
2702
2703 static const struct file_operations tracing_saved_cmdlines_fops = {
2704     .open       = tracing_open_generic,
2705     .read       = tracing_saved_cmdlines_read,
2706 };
2707
2708 static ssize_t
2709 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2710                   size_t cnt, loff_t *ppos)
2711 {
2712         char buf[64];
2713         int r;
2714
2715         r = sprintf(buf, "%u\n", tracer_enabled);
2716         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2717 }
2718
2719 static ssize_t
2720 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2721                    size_t cnt, loff_t *ppos)
2722 {
2723         struct trace_array *tr = filp->private_data;
2724         char buf[64];
2725         unsigned long val;
2726         int ret;
2727
2728         if (cnt >= sizeof(buf))
2729                 return -EINVAL;
2730
2731         if (copy_from_user(&buf, ubuf, cnt))
2732                 return -EFAULT;
2733
2734         buf[cnt] = 0;
2735
2736         ret = strict_strtoul(buf, 10, &val);
2737         if (ret < 0)
2738                 return ret;
2739
2740         val = !!val;
2741
2742         mutex_lock(&trace_types_lock);
2743         if (tracer_enabled ^ val) {
2744                 if (val) {
2745                         tracer_enabled = 1;
2746                         if (current_trace->start)
2747                                 current_trace->start(tr);
2748                         tracing_start();
2749                 } else {
2750                         tracer_enabled = 0;
2751                         tracing_stop();
2752                         if (current_trace->stop)
2753                                 current_trace->stop(tr);
2754                 }
2755         }
2756         mutex_unlock(&trace_types_lock);
2757
2758         *ppos += cnt;
2759
2760         return cnt;
2761 }
2762
2763 static ssize_t
2764 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2765                        size_t cnt, loff_t *ppos)
2766 {
2767         char buf[MAX_TRACER_SIZE+2];
2768         int r;
2769
2770         mutex_lock(&trace_types_lock);
2771         if (current_trace)
2772                 r = sprintf(buf, "%s\n", current_trace->name);
2773         else
2774                 r = sprintf(buf, "\n");
2775         mutex_unlock(&trace_types_lock);
2776
2777         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2778 }
2779
2780 int tracer_init(struct tracer *t, struct trace_array *tr)
2781 {
2782         tracing_reset_online_cpus(tr);
2783         return t->init(tr);
2784 }
2785
2786 static int tracing_resize_ring_buffer(unsigned long size)
2787 {
2788         int ret;
2789
2790         /*
2791          * If kernel or user changes the size of the ring buffer
2792          * we use the size that was given, and we can forget about
2793          * expanding it later.
2794          */
2795         ring_buffer_expanded = 1;
2796
2797         ret = ring_buffer_resize(global_trace.buffer, size);
2798         if (ret < 0)
2799                 return ret;
2800
2801         ret = ring_buffer_resize(max_tr.buffer, size);
2802         if (ret < 0) {
2803                 int r;
2804
2805                 r = ring_buffer_resize(global_trace.buffer,
2806                                        global_trace.entries);
2807                 if (r < 0) {
2808                         /*
2809                          * AARGH! We are left with different
2810                          * size max buffer!!!!
2811                          * The max buffer is our "snapshot" buffer.
2812                          * When a tracer needs a snapshot (one of the
2813                          * latency tracers), it swaps the max buffer
2814                          * with the saved snap shot. We succeeded to
2815                          * update the size of the main buffer, but failed to
2816                          * update the size of the max buffer. But when we tried
2817                          * to reset the main buffer to the original size, we
2818                          * failed there too. This is very unlikely to
2819                          * happen, but if it does, warn and kill all
2820                          * tracing.
2821                          */
2822                         WARN_ON(1);
2823                         tracing_disabled = 1;
2824                 }
2825                 return ret;
2826         }
2827
2828         global_trace.entries = size;
2829
2830         return ret;
2831 }
2832
2833 /**
2834  * tracing_update_buffers - used by tracing facility to expand ring buffers
2835  *
2836  * To save on memory when the tracing is never used on a system with it
2837  * configured in. The ring buffers are set to a minimum size. But once
2838  * a user starts to use the tracing facility, then they need to grow
2839  * to their default size.
2840  *
2841  * This function is to be called when a tracer is about to be used.
2842  */
2843 int tracing_update_buffers(void)
2844 {
2845         int ret = 0;
2846
2847         mutex_lock(&trace_types_lock);
2848         if (!ring_buffer_expanded)
2849                 ret = tracing_resize_ring_buffer(trace_buf_size);
2850         mutex_unlock(&trace_types_lock);
2851
2852         return ret;
2853 }
2854
2855 struct trace_option_dentry;
2856
2857 static struct trace_option_dentry *
2858 create_trace_option_files(struct tracer *tracer);
2859
2860 static void
2861 destroy_trace_option_files(struct trace_option_dentry *topts);
2862
2863 static int tracing_set_tracer(const char *buf)
2864 {
2865         static struct trace_option_dentry *topts;
2866         struct trace_array *tr = &global_trace;
2867         struct tracer *t;
2868         int ret = 0;
2869
2870         mutex_lock(&trace_types_lock);
2871
2872         if (!ring_buffer_expanded) {
2873                 ret = tracing_resize_ring_buffer(trace_buf_size);
2874                 if (ret < 0)
2875                         goto out;
2876                 ret = 0;
2877         }
2878
2879         for (t = trace_types; t; t = t->next) {
2880                 if (strcmp(t->name, buf) == 0)
2881                         break;
2882         }
2883         if (!t) {
2884                 ret = -EINVAL;
2885                 goto out;
2886         }
2887         if (t == current_trace)
2888                 goto out;
2889
2890         trace_branch_disable();
2891         if (current_trace && current_trace->reset)
2892                 current_trace->reset(tr);
2893
2894         destroy_trace_option_files(topts);
2895
2896         current_trace = t;
2897
2898         topts = create_trace_option_files(current_trace);
2899
2900         if (t->init) {
2901                 ret = tracer_init(t, tr);
2902                 if (ret)
2903                         goto out;
2904         }
2905
2906         trace_branch_enable(tr);
2907  out:
2908         mutex_unlock(&trace_types_lock);
2909
2910         return ret;
2911 }
2912
2913 static ssize_t
2914 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2915                         size_t cnt, loff_t *ppos)
2916 {
2917         char buf[MAX_TRACER_SIZE+1];
2918         int i;
2919         size_t ret;
2920         int err;
2921
2922         ret = cnt;
2923
2924         if (cnt > MAX_TRACER_SIZE)
2925                 cnt = MAX_TRACER_SIZE;
2926
2927         if (copy_from_user(&buf, ubuf, cnt))
2928                 return -EFAULT;
2929
2930         buf[cnt] = 0;
2931
2932         /* strip ending whitespace. */
2933         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2934                 buf[i] = 0;
2935
2936         err = tracing_set_tracer(buf);
2937         if (err)
2938                 return err;
2939
2940         *ppos += ret;
2941
2942         return ret;
2943 }
2944
2945 static ssize_t
2946 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2947                      size_t cnt, loff_t *ppos)
2948 {
2949         unsigned long *ptr = filp->private_data;
2950         char buf[64];
2951         int r;
2952
2953         r = snprintf(buf, sizeof(buf), "%ld\n",
2954                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2955         if (r > sizeof(buf))
2956                 r = sizeof(buf);
2957         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2958 }
2959
2960 static ssize_t
2961 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2962                       size_t cnt, loff_t *ppos)
2963 {
2964         unsigned long *ptr = filp->private_data;
2965         char buf[64];
2966         unsigned long val;
2967         int ret;
2968
2969         if (cnt >= sizeof(buf))
2970                 return -EINVAL;
2971
2972         if (copy_from_user(&buf, ubuf, cnt))
2973                 return -EFAULT;
2974
2975         buf[cnt] = 0;
2976
2977         ret = strict_strtoul(buf, 10, &val);
2978         if (ret < 0)
2979                 return ret;
2980
2981         *ptr = val * 1000;
2982
2983         return cnt;
2984 }
2985
2986 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2987 {
2988         long cpu_file = (long) inode->i_private;
2989         struct trace_iterator *iter;
2990         int ret = 0;
2991
2992         if (tracing_disabled)
2993                 return -ENODEV;
2994
2995         mutex_lock(&trace_types_lock);
2996
2997         /* create a buffer to store the information to pass to userspace */
2998         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2999         if (!iter) {
3000                 ret = -ENOMEM;
3001                 goto out;
3002         }
3003
3004         /*
3005          * We make a copy of the current tracer to avoid concurrent
3006          * changes on it while we are reading.
3007          */
3008         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3009         if (!iter->trace) {
3010                 ret = -ENOMEM;
3011                 goto fail;
3012         }
3013         if (current_trace)
3014                 *iter->trace = *current_trace;
3015
3016         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3017                 ret = -ENOMEM;
3018                 goto fail;
3019         }
3020
3021         /* trace pipe does not show start of buffer */
3022         cpumask_setall(iter->started);
3023
3024         if (trace_flags & TRACE_ITER_LATENCY_FMT)
3025                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3026
3027         iter->cpu_file = cpu_file;
3028         iter->tr = &global_trace;
3029         mutex_init(&iter->mutex);
3030         filp->private_data = iter;
3031
3032         if (iter->trace->pipe_open)
3033                 iter->trace->pipe_open(iter);
3034
3035 out:
3036         mutex_unlock(&trace_types_lock);
3037         return ret;
3038
3039 fail:
3040         kfree(iter->trace);
3041         kfree(iter);
3042         mutex_unlock(&trace_types_lock);
3043         return ret;
3044 }
3045
3046 static int tracing_release_pipe(struct inode *inode, struct file *file)
3047 {
3048         struct trace_iterator *iter = file->private_data;
3049
3050         mutex_lock(&trace_types_lock);
3051
3052         if (iter->trace->pipe_close)
3053                 iter->trace->pipe_close(iter);
3054
3055         mutex_unlock(&trace_types_lock);
3056
3057         free_cpumask_var(iter->started);
3058         mutex_destroy(&iter->mutex);
3059         kfree(iter->trace);
3060         kfree(iter);
3061
3062         return 0;
3063 }
3064
3065 static unsigned int
3066 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3067 {
3068         struct trace_iterator *iter = filp->private_data;
3069
3070         if (trace_flags & TRACE_ITER_BLOCK) {
3071                 /*
3072                  * Always select as readable when in blocking mode
3073                  */
3074                 return POLLIN | POLLRDNORM;
3075         } else {
3076                 if (!trace_empty(iter))
3077                         return POLLIN | POLLRDNORM;
3078                 poll_wait(filp, &trace_wait, poll_table);
3079                 if (!trace_empty(iter))
3080                         return POLLIN | POLLRDNORM;
3081
3082                 return 0;
3083         }
3084 }
3085
3086
3087 void default_wait_pipe(struct trace_iterator *iter)
3088 {
3089         DEFINE_WAIT(wait);
3090
3091         prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3092
3093         if (trace_empty(iter))
3094                 schedule();
3095
3096         finish_wait(&trace_wait, &wait);
3097 }
3098
3099 /*
3100  * This is a make-shift waitqueue.
3101  * A tracer might use this callback on some rare cases:
3102  *
3103  *  1) the current tracer might hold the runqueue lock when it wakes up
3104  *     a reader, hence a deadlock (sched, function, and function graph tracers)
3105  *  2) the function tracers, trace all functions, we don't want
3106  *     the overhead of calling wake_up and friends
3107  *     (and tracing them too)
3108  *
3109  *     Anyway, this is really very primitive wakeup.
3110  */
3111 void poll_wait_pipe(struct trace_iterator *iter)
3112 {
3113         set_current_state(TASK_INTERRUPTIBLE);
3114         /* sleep for 100 msecs, and try again. */
3115         schedule_timeout(HZ / 10);
3116 }
3117
3118 /* Must be called with trace_types_lock mutex held. */
3119 static int tracing_wait_pipe(struct file *filp)
3120 {
3121         struct trace_iterator *iter = filp->private_data;
3122
3123         while (trace_empty(iter)) {
3124
3125                 if ((filp->f_flags & O_NONBLOCK)) {
3126                         return -EAGAIN;
3127                 }
3128
3129                 mutex_unlock(&iter->mutex);
3130
3131                 iter->trace->wait_pipe(iter);
3132
3133                 mutex_lock(&iter->mutex);
3134
3135                 if (signal_pending(current))
3136                         return -EINTR;
3137
3138                 /*
3139                  * We block until we read something and tracing is disabled.
3140                  * We still block if tracing is disabled, but we have never
3141                  * read anything. This allows a user to cat this file, and
3142                  * then enable tracing. But after we have read something,
3143                  * we give an EOF when tracing is again disabled.
3144                  *
3145                  * iter->pos will be 0 if we haven't read anything.
3146                  */
3147                 if (!tracer_enabled && iter->pos)
3148                         break;
3149         }
3150
3151         return 1;
3152 }
3153
3154 /*
3155  * Consumer reader.
3156  */
3157 static ssize_t
3158 tracing_read_pipe(struct file *filp, char __user *ubuf,
3159                   size_t cnt, loff_t *ppos)
3160 {
3161         struct trace_iterator *iter = filp->private_data;
3162         static struct tracer *old_tracer;
3163         ssize_t sret;
3164
3165         /* return any leftover data */
3166         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3167         if (sret != -EBUSY)
3168                 return sret;
3169
3170         trace_seq_init(&iter->seq);
3171
3172         /* copy the tracer to avoid using a global lock all around */
3173         mutex_lock(&trace_types_lock);
3174         if (unlikely(old_tracer != current_trace && current_trace)) {
3175                 old_tracer = current_trace;
3176                 *iter->trace = *current_trace;
3177         }
3178         mutex_unlock(&trace_types_lock);
3179
3180         /*
3181          * Avoid more than one consumer on a single file descriptor
3182          * This is just a matter of traces coherency, the ring buffer itself
3183          * is protected.
3184          */
3185         mutex_lock(&iter->mutex);
3186         if (iter->trace->read) {
3187                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3188                 if (sret)
3189                         goto out;
3190         }
3191
3192 waitagain:
3193         sret = tracing_wait_pipe(filp);
3194         if (sret <= 0)
3195                 goto out;
3196
3197         /* stop when tracing is finished */
3198         if (trace_empty(iter)) {
3199                 sret = 0;
3200                 goto out;
3201         }
3202
3203         if (cnt >= PAGE_SIZE)
3204                 cnt = PAGE_SIZE - 1;
3205
3206         /* reset all but tr, trace, and overruns */
3207         memset(&iter->seq, 0,
3208                sizeof(struct trace_iterator) -
3209                offsetof(struct trace_iterator, seq));
3210         iter->pos = -1;
3211
3212         trace_event_read_lock();
3213         trace_access_lock(iter->cpu_file);
3214         while (find_next_entry_inc(iter) != NULL) {
3215                 enum print_line_t ret;
3216                 int len = iter->seq.len;
3217
3218                 ret = print_trace_line(iter);
3219                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3220                         /* don't print partial lines */
3221                         iter->seq.len = len;
3222                         break;
3223                 }
3224                 if (ret != TRACE_TYPE_NO_CONSUME)
3225                         trace_consume(iter);
3226
3227                 if (iter->seq.len >= cnt)
3228                         break;
3229         }
3230         trace_access_unlock(iter->cpu_file);
3231         trace_event_read_unlock();
3232
3233         /* Now copy what we have to the user */
3234         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3235         if (iter->seq.readpos >= iter->seq.len)
3236                 trace_seq_init(&iter->seq);
3237
3238         /*
3239          * If there was nothing to send to user, inspite of consuming trace
3240          * entries, go back to wait for more entries.
3241          */
3242         if (sret == -EBUSY)
3243                 goto waitagain;
3244
3245 out:
3246         mutex_unlock(&iter->mutex);
3247
3248         return sret;
3249 }
3250
3251 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3252                                      struct pipe_buffer *buf)
3253 {
3254         __free_page(buf->page);
3255 }
3256
3257 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3258                                      unsigned int idx)
3259 {
3260         __free_page(spd->pages[idx]);
3261 }
3262
3263 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3264         .can_merge              = 0,
3265         .map                    = generic_pipe_buf_map,
3266         .unmap                  = generic_pipe_buf_unmap,
3267         .confirm                = generic_pipe_buf_confirm,
3268         .release                = tracing_pipe_buf_release,
3269         .steal                  = generic_pipe_buf_steal,
3270         .get                    = generic_pipe_buf_get,
3271 };
3272
3273 static size_t
3274 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3275 {
3276         size_t count;
3277         int ret;
3278
3279         /* Seq buffer is page-sized, exactly what we need. */
3280         for (;;) {
3281                 count = iter->seq.len;
3282                 ret = print_trace_line(iter);
3283                 count = iter->seq.len - count;
3284                 if (rem < count) {
3285                         rem = 0;
3286                         iter->seq.len -= count;
3287                         break;
3288                 }
3289                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3290                         iter->seq.len -= count;
3291                         break;
3292                 }
3293
3294                 if (ret != TRACE_TYPE_NO_CONSUME)
3295                         trace_consume(iter);
3296                 rem -= count;
3297                 if (!find_next_entry_inc(iter)) {
3298                         rem = 0;
3299                         iter->ent = NULL;
3300                         break;
3301                 }
3302         }
3303
3304         return rem;
3305 }
3306
3307 static ssize_t tracing_splice_read_pipe(struct file *filp,
3308                                         loff_t *ppos,
3309                                         struct pipe_inode_info *pipe,
3310                                         size_t len,
3311                                         unsigned int flags)
3312 {
3313         struct page *pages_def[PIPE_DEF_BUFFERS];
3314         struct partial_page partial_def[PIPE_DEF_BUFFERS];
3315         struct trace_iterator *iter = filp->private_data;
3316         struct splice_pipe_desc spd = {
3317                 .pages          = pages_def,
3318                 .partial        = partial_def,
3319                 .nr_pages       = 0, /* This gets updated below. */
3320                 .flags          = flags,
3321                 .ops            = &tracing_pipe_buf_ops,
3322                 .spd_release    = tracing_spd_release_pipe,
3323         };
3324         static struct tracer *old_tracer;
3325         ssize_t ret;
3326         size_t rem;
3327         unsigned int i;
3328
3329         if (splice_grow_spd(pipe, &spd))
3330                 return -ENOMEM;
3331
3332         /* copy the tracer to avoid using a global lock all around */
3333         mutex_lock(&trace_types_lock);
3334         if (unlikely(old_tracer != current_trace && current_trace)) {
3335                 old_tracer = current_trace;
3336                 *iter->trace = *current_trace;
3337         }
3338         mutex_unlock(&trace_types_lock);
3339
3340         mutex_lock(&iter->mutex);
3341
3342         if (iter->trace->splice_read) {
3343                 ret = iter->trace->splice_read(iter, filp,
3344                                                ppos, pipe, len, flags);
3345                 if (ret)
3346                         goto out_err;
3347         }
3348
3349         ret = tracing_wait_pipe(filp);
3350         if (ret <= 0)
3351                 goto out_err;
3352
3353         if (!iter->ent && !find_next_entry_inc(iter)) {
3354                 ret = -EFAULT;
3355                 goto out_err;
3356         }
3357
3358         trace_event_read_lock();
3359         trace_access_lock(iter->cpu_file);
3360
3361         /* Fill as many pages as possible. */
3362         for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3363                 spd.pages[i] = alloc_page(GFP_KERNEL);
3364                 if (!spd.pages[i])
3365                         break;
3366
3367                 rem = tracing_fill_pipe_page(rem, iter);
3368
3369                 /* Copy the data into the page, so we can start over. */
3370                 ret = trace_seq_to_buffer(&iter->seq,
3371                                           page_address(spd.pages[i]),
3372                                           iter->seq.len);
3373                 if (ret < 0) {
3374                         __free_page(spd.pages[i]);
3375                         break;
3376              &n