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