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