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