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