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