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