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