71777c8fe36b3691d3f074dba3da8a4e849cacb0
[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_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         NULL
429 };
430
431 static struct {
432         u64 (*func)(void);
433         const char *name;
434 } trace_clocks[] = {
435         { trace_clock_local,    "local" },
436         { trace_clock_global,   "global" },
437 };
438
439 int trace_clock_id;
440
441 /*
442  * trace_parser_get_init - gets the buffer for trace parser
443  */
444 int trace_parser_get_init(struct trace_parser *parser, int size)
445 {
446         memset(parser, 0, sizeof(*parser));
447
448         parser->buffer = kmalloc(size, GFP_KERNEL);
449         if (!parser->buffer)
450                 return 1;
451
452         parser->size = size;
453         return 0;
454 }
455
456 /*
457  * trace_parser_put - frees the buffer for trace parser
458  */
459 void trace_parser_put(struct trace_parser *parser)
460 {
461         kfree(parser->buffer);
462 }
463
464 /*
465  * trace_get_user - reads the user input string separated by  space
466  * (matched by isspace(ch))
467  *
468  * For each string found the 'struct trace_parser' is updated,
469  * and the function returns.
470  *
471  * Returns number of bytes read.
472  *
473  * See kernel/trace/trace.h for 'struct trace_parser' details.
474  */
475 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
476         size_t cnt, loff_t *ppos)
477 {
478         char ch;
479         size_t read = 0;
480         ssize_t ret;
481
482         if (!*ppos)
483                 trace_parser_clear(parser);
484
485         ret = get_user(ch, ubuf++);
486         if (ret)
487                 goto out;
488
489         read++;
490         cnt--;
491
492         /*
493          * The parser is not finished with the last write,
494          * continue reading the user input without skipping spaces.
495          */
496         if (!parser->cont) {
497                 /* skip white space */
498                 while (cnt && isspace(ch)) {
499                         ret = get_user(ch, ubuf++);
500                         if (ret)
501                                 goto out;
502                         read++;
503                         cnt--;
504                 }
505
506                 /* only spaces were written */
507                 if (isspace(ch)) {
508                         *ppos += read;
509                         ret = read;
510                         goto out;
511                 }
512
513                 parser->idx = 0;
514         }
515
516         /* read the non-space input */
517         while (cnt && !isspace(ch)) {
518                 if (parser->idx < parser->size - 1)
519                         parser->buffer[parser->idx++] = ch;
520                 else {
521                         ret = -EINVAL;
522                         goto out;
523                 }
524                 ret = get_user(ch, ubuf++);
525                 if (ret)
526                         goto out;
527                 read++;
528                 cnt--;
529         }
530
531         /* We either got finished input or we have to wait for another call. */
532         if (isspace(ch)) {
533                 parser->buffer[parser->idx] = 0;
534                 parser->cont = false;
535         } else {
536                 parser->cont = true;
537                 parser->buffer[parser->idx++] = ch;
538         }
539
540         *ppos += read;
541         ret = read;
542
543 out:
544         return ret;
545 }
546
547 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
548 {
549         int len;
550         int ret;
551
552         if (!cnt)
553                 return 0;
554
555         if (s->len <= s->readpos)
556                 return -EBUSY;
557
558         len = s->len - s->readpos;
559         if (cnt > len)
560                 cnt = len;
561         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
562         if (ret == cnt)
563                 return -EFAULT;
564
565         cnt -= ret;
566
567         s->readpos += cnt;
568         return cnt;
569 }
570
571 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
572 {
573         int len;
574         void *ret;
575
576         if (s->len <= s->readpos)
577                 return -EBUSY;
578
579         len = s->len - s->readpos;
580         if (cnt > len)
581                 cnt = len;
582         ret = memcpy(buf, s->buffer + s->readpos, cnt);
583         if (!ret)
584                 return -EFAULT;
585
586         s->readpos += cnt;
587         return cnt;
588 }
589
590 /*
591  * ftrace_max_lock is used to protect the swapping of buffers
592  * when taking a max snapshot. The buffers themselves are
593  * protected by per_cpu spinlocks. But the action of the swap
594  * needs its own lock.
595  *
596  * This is defined as a arch_spinlock_t in order to help
597  * with performance when lockdep debugging is enabled.
598  *
599  * It is also used in other places outside the update_max_tr
600  * so it needs to be defined outside of the
601  * CONFIG_TRACER_MAX_TRACE.
602  */
603 static arch_spinlock_t ftrace_max_lock =
604         (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
605
606 unsigned long __read_mostly     tracing_thresh;
607
608 #ifdef CONFIG_TRACER_MAX_TRACE
609 unsigned long __read_mostly     tracing_max_latency;
610
611 /*
612  * Copy the new maximum trace into the separate maximum-trace
613  * structure. (this way the maximum trace is permanently saved,
614  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
615  */
616 static void
617 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
618 {
619         struct trace_array_cpu *data = tr->data[cpu];
620         struct trace_array_cpu *max_data;
621
622         max_tr.cpu = cpu;
623         max_tr.time_start = data->preempt_timestamp;
624
625         max_data = max_tr.data[cpu];
626         max_data->saved_latency = tracing_max_latency;
627         max_data->critical_start = data->critical_start;
628         max_data->critical_end = data->critical_end;
629
630         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
631         max_data->pid = tsk->pid;
632         max_data->uid = task_uid(tsk);
633         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
634         max_data->policy = tsk->policy;
635         max_data->rt_priority = tsk->rt_priority;
636
637         /* record this tasks comm */
638         tracing_record_cmdline(tsk);
639 }
640
641 /**
642  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
643  * @tr: tracer
644  * @tsk: the task with the latency
645  * @cpu: The cpu that initiated the trace.
646  *
647  * Flip the buffers between the @tr and the max_tr and record information
648  * about which task was the cause of this latency.
649  */
650 void
651 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
652 {
653         struct ring_buffer *buf = tr->buffer;
654
655         if (trace_stop_count)
656                 return;
657
658         WARN_ON_ONCE(!irqs_disabled());
659         if (!current_trace->use_max_tr) {
660                 WARN_ON_ONCE(1);
661                 return;
662         }
663         arch_spin_lock(&ftrace_max_lock);
664
665         tr->buffer = max_tr.buffer;
666         max_tr.buffer = buf;
667
668         __update_max_tr(tr, tsk, cpu);
669         arch_spin_unlock(&ftrace_max_lock);
670 }
671
672 /**
673  * update_max_tr_single - only copy one trace over, and reset the rest
674  * @tr - tracer
675  * @tsk - task with the latency
676  * @cpu - the cpu of the buffer to copy.
677  *
678  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
679  */
680 void
681 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
682 {
683         int ret;
684
685         if (trace_stop_count)
686                 return;
687
688         WARN_ON_ONCE(!irqs_disabled());
689         if (!current_trace->use_max_tr) {
690                 WARN_ON_ONCE(1);
691                 return;
692         }
693
694         arch_spin_lock(&ftrace_max_lock);
695
696         ftrace_disable_cpu();
697
698         ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
699
700         if (ret == -EBUSY) {
701                 /*
702                  * We failed to swap the buffer due to a commit taking
703                  * place on this CPU. We fail to record, but we reset
704                  * the max trace buffer (no one writes directly to it)
705                  * and flag that it failed.
706                  */
707                 trace_array_printk(&max_tr, _THIS_IP_,
708                         "Failed to swap buffers due to commit in progress\n");
709         }
710
711         ftrace_enable_cpu();
712
713         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
714
715         __update_max_tr(tr, tsk, cpu);
716         arch_spin_unlock(&ftrace_max_lock);
717 }
718 #endif /* CONFIG_TRACER_MAX_TRACE */
719
720 /**
721  * register_tracer - register a tracer with the ftrace system.
722  * @type - the plugin for the tracer
723  *
724  * Register a new plugin tracer.
725  */
726 int register_tracer(struct tracer *type)
727 __releases(kernel_lock)
728 __acquires(kernel_lock)
729 {
730         struct tracer *t;
731         int ret = 0;
732
733         if (!type->name) {
734                 pr_info("Tracer must have a name\n");
735                 return -1;
736         }
737
738         if (strlen(type->name) >= MAX_TRACER_SIZE) {
739                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
740                 return -1;
741         }
742
743         mutex_lock(&trace_types_lock);
744
745         tracing_selftest_running = true;
746
747         for (t = trace_types; t; t = t->next) {
748                 if (strcmp(type->name, t->name) == 0) {
749                         /* already found */
750                         pr_info("Tracer %s already registered\n",
751                                 type->name);
752                         ret = -1;
753                         goto out;
754                 }
755         }
756
757         if (!type->set_flag)
758                 type->set_flag = &dummy_set_flag;
759         if (!type->flags)
760                 type->flags = &dummy_tracer_flags;
761         else
762                 if (!type->flags->opts)
763                         type->flags->opts = dummy_tracer_opt;
764         if (!type->wait_pipe)
765                 type->wait_pipe = default_wait_pipe;
766
767
768 #ifdef CONFIG_FTRACE_STARTUP_TEST
769         if (type->selftest && !tracing_selftest_disabled) {
770                 struct tracer *saved_tracer = current_trace;
771                 struct trace_array *tr = &global_trace;
772
773                 /*
774                  * Run a selftest on this tracer.
775                  * Here we reset the trace buffer, and set the current
776                  * tracer to be this tracer. The tracer can then run some
777                  * internal tracing to verify that everything is in order.
778                  * If we fail, we do not register this tracer.
779                  */
780                 tracing_reset_online_cpus(tr);
781
782                 current_trace = type;
783
784                 /* If we expanded the buffers, make sure the max is expanded too */
785                 if (ring_buffer_expanded && type->use_max_tr)
786                         ring_buffer_resize(max_tr.buffer, trace_buf_size);
787
788                 /* the test is responsible for initializing and enabling */
789                 pr_info("Testing tracer %s: ", type->name);
790                 ret = type->selftest(type, tr);
791                 /* the test is responsible for resetting too */
792                 current_trace = saved_tracer;
793                 if (ret) {
794                         printk(KERN_CONT "FAILED!\n");
795                         goto out;
796                 }
797                 /* Only reset on passing, to avoid touching corrupted buffers */
798                 tracing_reset_online_cpus(tr);
799
800                 /* Shrink the max buffer again */
801                 if (ring_buffer_expanded && type->use_max_tr)
802                         ring_buffer_resize(max_tr.buffer, 1);
803
804                 printk(KERN_CONT "PASSED\n");
805         }
806 #endif
807
808         type->next = trace_types;
809         trace_types = type;
810
811  out:
812         tracing_selftest_running = false;
813         mutex_unlock(&trace_types_lock);
814
815         if (ret || !default_bootup_tracer)
816                 goto out_unlock;
817
818         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
819                 goto out_unlock;
820
821         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
822         /* Do we want this tracer to start on bootup? */
823         tracing_set_tracer(type->name);
824         default_bootup_tracer = NULL;
825         /* disable other selftests, since this will break it. */
826         tracing_selftest_disabled = 1;
827 #ifdef CONFIG_FTRACE_STARTUP_TEST
828         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
829                type->name);
830 #endif
831
832  out_unlock:
833         return ret;
834 }
835
836 void unregister_tracer(struct tracer *type)
837 {
838         struct tracer **t;
839
840         mutex_lock(&trace_types_lock);
841         for (t = &trace_types; *t; t = &(*t)->next) {
842                 if (*t == type)
843                         goto found;
844         }
845         pr_info("Tracer %s not registered\n", type->name);
846         goto out;
847
848  found:
849         *t = (*t)->next;
850
851         if (type == current_trace && tracer_enabled) {
852                 tracer_enabled = 0;
853                 tracing_stop();
854                 if (current_trace->stop)
855                         current_trace->stop(&global_trace);
856                 current_trace = &nop_trace;
857         }
858 out:
859         mutex_unlock(&trace_types_lock);
860 }
861
862 static void __tracing_reset(struct ring_buffer *buffer, int cpu)
863 {
864         ftrace_disable_cpu();
865         ring_buffer_reset_cpu(buffer, cpu);
866         ftrace_enable_cpu();
867 }
868
869 void tracing_reset(struct trace_array *tr, int cpu)
870 {
871         struct ring_buffer *buffer = tr->buffer;
872
873         ring_buffer_record_disable(buffer);
874
875         /* Make sure all commits have finished */
876         synchronize_sched();
877         __tracing_reset(buffer, cpu);
878
879         ring_buffer_record_enable(buffer);
880 }
881
882 void tracing_reset_online_cpus(struct trace_array *tr)
883 {
884         struct ring_buffer *buffer = tr->buffer;
885         int cpu;
886
887         ring_buffer_record_disable(buffer);
888
889         /* Make sure all commits have finished */
890         synchronize_sched();
891
892         tr->time_start = ftrace_now(tr->cpu);
893
894         for_each_online_cpu(cpu)
895                 __tracing_reset(buffer, cpu);
896
897         ring_buffer_record_enable(buffer);
898 }
899
900 void tracing_reset_current(int cpu)
901 {
902         tracing_reset(&global_trace, cpu);
903 }
904
905 void tracing_reset_current_online_cpus(void)
906 {
907         tracing_reset_online_cpus(&global_trace);
908 }
909
910 #define SAVED_CMDLINES 128
911 #define NO_CMDLINE_MAP UINT_MAX
912 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
913 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
914 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
915 static int cmdline_idx;
916 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
917
918 /* temporary disable recording */
919 static atomic_t trace_record_cmdline_disabled __read_mostly;
920
921 static void trace_init_cmdlines(void)
922 {
923         memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
924         memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
925         cmdline_idx = 0;
926 }
927
928 int is_tracing_stopped(void)
929 {
930         return trace_stop_count;
931 }
932
933 /**
934  * ftrace_off_permanent - disable all ftrace code permanently
935  *
936  * This should only be called when a serious anomally has
937  * been detected.  This will turn off the function tracing,
938  * ring buffers, and other tracing utilites. It takes no
939  * locks and can be called from any context.
940  */
941 void ftrace_off_permanent(void)
942 {
943         tracing_disabled = 1;
944         ftrace_stop();
945         tracing_off_permanent();
946 }
947
948 /**
949  * tracing_start - quick start of the tracer
950  *
951  * If tracing is enabled but was stopped by tracing_stop,
952  * this will start the tracer back up.
953  */
954 void tracing_start(void)
955 {
956         struct ring_buffer *buffer;
957         unsigned long flags;
958
959         if (tracing_disabled)
960                 return;
961
962         spin_lock_irqsave(&tracing_start_lock, flags);
963         if (--trace_stop_count) {
964                 if (trace_stop_count < 0) {
965                         /* Someone screwed up their debugging */
966                         WARN_ON_ONCE(1);
967                         trace_stop_count = 0;
968                 }
969                 goto out;
970         }
971
972         /* Prevent the buffers from switching */
973         arch_spin_lock(&ftrace_max_lock);
974
975         buffer = global_trace.buffer;
976         if (buffer)
977                 ring_buffer_record_enable(buffer);
978
979         buffer = max_tr.buffer;
980         if (buffer)
981                 ring_buffer_record_enable(buffer);
982
983         arch_spin_unlock(&ftrace_max_lock);
984
985         ftrace_start();
986  out:
987         spin_unlock_irqrestore(&tracing_start_lock, flags);
988 }
989
990 /**
991  * tracing_stop - quick stop of the tracer
992  *
993  * Light weight way to stop tracing. Use in conjunction with
994  * tracing_start.
995  */
996 void tracing_stop(void)
997 {
998         struct ring_buffer *buffer;
999         unsigned long flags;
1000
1001         ftrace_stop();
1002         spin_lock_irqsave(&tracing_start_lock, flags);
1003         if (trace_stop_count++)
1004                 goto out;
1005
1006         /* Prevent the buffers from switching */
1007         arch_spin_lock(&ftrace_max_lock);
1008
1009         buffer = global_trace.buffer;
1010         if (buffer)
1011                 ring_buffer_record_disable(buffer);
1012
1013         buffer = max_tr.buffer;
1014         if (buffer)
1015                 ring_buffer_record_disable(buffer);
1016
1017         arch_spin_unlock(&ftrace_max_lock);
1018
1019  out:
1020         spin_unlock_irqrestore(&tracing_start_lock, flags);
1021 }
1022
1023 void trace_stop_cmdline_recording(void);
1024
1025 static void trace_save_cmdline(struct task_struct *tsk)
1026 {
1027         unsigned pid, idx;
1028
1029         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1030                 return;
1031
1032         /*
1033          * It's not the end of the world if we don't get
1034          * the lock, but we also don't want to spin
1035          * nor do we want to disable interrupts,
1036          * so if we miss here, then better luck next time.
1037          */
1038         if (!arch_spin_trylock(&trace_cmdline_lock))
1039                 return;
1040
1041         idx = map_pid_to_cmdline[tsk->pid];
1042         if (idx == NO_CMDLINE_MAP) {
1043                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1044
1045                 /*
1046                  * Check whether the cmdline buffer at idx has a pid
1047                  * mapped. We are going to overwrite that entry so we
1048                  * need to clear the map_pid_to_cmdline. Otherwise we
1049                  * would read the new comm for the old pid.
1050                  */
1051                 pid = map_cmdline_to_pid[idx];
1052                 if (pid != NO_CMDLINE_MAP)
1053                         map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1054
1055                 map_cmdline_to_pid[idx] = tsk->pid;
1056                 map_pid_to_cmdline[tsk->pid] = idx;
1057
1058                 cmdline_idx = idx;
1059         }
1060
1061         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1062
1063         arch_spin_unlock(&trace_cmdline_lock);
1064 }
1065
1066 void trace_find_cmdline(int pid, char comm[])
1067 {
1068         unsigned map;
1069
1070         if (!pid) {
1071                 strcpy(comm, "<idle>");
1072                 return;
1073         }
1074
1075         if (WARN_ON_ONCE(pid < 0)) {
1076                 strcpy(comm, "<XXX>");
1077                 return;
1078         }
1079
1080         if (pid > PID_MAX_DEFAULT) {
1081                 strcpy(comm, "<...>");
1082                 return;
1083         }
1084
1085         preempt_disable();
1086         arch_spin_lock(&trace_cmdline_lock);
1087         map = map_pid_to_cmdline[pid];
1088         if (map != NO_CMDLINE_MAP)
1089                 strcpy(comm, saved_cmdlines[map]);
1090         else
1091                 strcpy(comm, "<...>");
1092
1093         arch_spin_unlock(&trace_cmdline_lock);
1094         preempt_enable();
1095 }
1096
1097 void tracing_record_cmdline(struct task_struct *tsk)
1098 {
1099         if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1100             !tracing_is_on())
1101                 return;
1102
1103         trace_save_cmdline(tsk);
1104 }
1105
1106 void
1107 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1108                              int pc)
1109 {
1110         struct task_struct *tsk = current;
1111
1112         entry->preempt_count            = pc & 0xff;
1113         entry->pid                      = (tsk) ? tsk->pid : 0;
1114         entry->padding                  = 0;
1115         entry->flags =
1116 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1117                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1118 #else
1119                 TRACE_FLAG_IRQS_NOSUPPORT |
1120 #endif
1121                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1122                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1123                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1124 }
1125 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1126
1127 struct ring_buffer_event *
1128 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1129                           int type,
1130                           unsigned long len,
1131                           unsigned long flags, int pc)
1132 {
1133         struct ring_buffer_event *event;
1134
1135         event = ring_buffer_lock_reserve(buffer, len);
1136         if (event != NULL) {
1137                 struct trace_entry *ent = ring_buffer_event_data(event);
1138
1139                 tracing_generic_entry_update(ent, flags, pc);
1140                 ent->type = type;
1141         }
1142
1143         return event;
1144 }
1145
1146 static inline void
1147 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1148                              struct ring_buffer_event *event,
1149                              unsigned long flags, int pc,
1150                              int wake)
1151 {
1152         ring_buffer_unlock_commit(buffer, event);
1153
1154         ftrace_trace_stack(buffer, flags, 6, pc);
1155         ftrace_trace_userstack(buffer, flags, pc);
1156
1157         if (wake)
1158                 trace_wake_up();
1159 }
1160
1161 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1162                                 struct ring_buffer_event *event,
1163                                 unsigned long flags, int pc)
1164 {
1165         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1166 }
1167
1168 struct ring_buffer_event *
1169 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1170                                   int type, unsigned long len,
1171                                   unsigned long flags, int pc)
1172 {
1173         *current_rb = global_trace.buffer;
1174         return trace_buffer_lock_reserve(*current_rb,
1175                                          type, len, flags, pc);
1176 }
1177 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1178
1179 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1180                                         struct ring_buffer_event *event,
1181                                         unsigned long flags, int pc)
1182 {
1183         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1184 }
1185 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1186
1187 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1188                                        struct ring_buffer_event *event,
1189                                        unsigned long flags, int pc)
1190 {
1191         __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1192 }
1193 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1194
1195 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1196                                          struct ring_buffer_event *event)
1197 {
1198         ring_buffer_discard_commit(buffer, event);
1199 }
1200 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1201
1202 void
1203 trace_function(struct trace_array *tr,
1204                unsigned long ip, unsigned long parent_ip, unsigned long flags,
1205                int pc)
1206 {
1207         struct ftrace_event_call *call = &event_function;
1208         struct ring_buffer *buffer = tr->buffer;
1209         struct ring_buffer_event *event;
1210         struct ftrace_entry *entry;
1211
1212         /* If we are reading the ring buffer, don't trace */
1213         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1214                 return;
1215
1216         event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1217                                           flags, pc);
1218         if (!event)
1219                 return;
1220         entry   = ring_buffer_event_data(event);
1221         entry->ip                       = ip;
1222         entry->parent_ip                = parent_ip;
1223
1224         if (!filter_check_discard(call, entry, buffer, event))
1225                 ring_buffer_unlock_commit(buffer, event);
1226 }
1227
1228 void
1229 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1230        unsigned long ip, unsigned long parent_ip, unsigned long flags,
1231        int pc)
1232 {
1233         if (likely(!atomic_read(&data->disabled)))
1234                 trace_function(tr, ip, parent_ip, flags, pc);
1235 }
1236
1237 #ifdef CONFIG_STACKTRACE
1238 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1239                                  unsigned long flags,
1240                                  int skip, int pc)
1241 {
1242         struct ftrace_event_call *call = &event_kernel_stack;
1243         struct ring_buffer_event *event;
1244         struct stack_entry *entry;
1245         struct stack_trace trace;
1246
1247         event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1248                                           sizeof(*entry), flags, pc);
1249         if (!event)
1250                 return;
1251         entry   = ring_buffer_event_data(event);
1252         memset(&entry->caller, 0, sizeof(entry->caller));
1253
1254         trace.nr_entries        = 0;
1255         trace.max_entries       = FTRACE_STACK_ENTRIES;
1256         trace.skip              = skip;
1257         trace.entries           = entry->caller;
1258
1259         save_stack_trace(&trace);
1260         if (!filter_check_discard(call, entry, buffer, event))
1261                 ring_buffer_unlock_commit(buffer, event);
1262 }
1263
1264 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1265                         int skip, int pc)
1266 {
1267         if (!(trace_flags & TRACE_ITER_STACKTRACE))
1268                 return;
1269
1270         __ftrace_trace_stack(buffer, flags, skip, pc);
1271 }
1272
1273 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1274                    int pc)
1275 {
1276         __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1277 }
1278
1279 /**
1280  * trace_dump_stack - record a stack back trace in the trace buffer
1281  */
1282 void trace_dump_stack(void)
1283 {
1284         unsigned long flags;
1285
1286         if (tracing_disabled || tracing_selftest_running)
1287                 return;
1288
1289         local_save_flags(flags);
1290
1291         /* skipping 3 traces, seems to get us at the caller of this function */
1292         __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1293 }
1294
1295 static DEFINE_PER_CPU(int, user_stack_count);
1296
1297 void
1298 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1299 {
1300         struct ftrace_event_call *call = &event_user_stack;
1301         struct ring_buffer_event *event;
1302         struct userstack_entry *entry;
1303         struct stack_trace trace;
1304
1305         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1306                 return;
1307
1308         /*
1309          * NMIs can not handle page faults, even with fix ups.
1310          * The save user stack can (and often does) fault.
1311          */
1312         if (unlikely(in_nmi()))
1313                 return;
1314
1315         /*
1316          * prevent recursion, since the user stack tracing may
1317          * trigger other kernel events.
1318          */
1319         preempt_disable();
1320         if (__this_cpu_read(user_stack_count))
1321                 goto out;
1322
1323         __this_cpu_inc(user_stack_count);
1324
1325         event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1326                                           sizeof(*entry), flags, pc);
1327         if (!event)
1328                 goto out_drop_count;
1329         entry   = ring_buffer_event_data(event);
1330
1331         entry->tgid             = current->tgid;
1332         memset(&entry->caller, 0, sizeof(entry->caller));
1333
1334         trace.nr_entries        = 0;
1335         trace.max_entries       = FTRACE_STACK_ENTRIES;
1336         trace.skip              = 0;
1337         trace.entries           = entry->caller;
1338
1339         save_stack_trace_user(&trace);
1340         if (!filter_check_discard(call, entry, buffer, event))
1341                 ring_buffer_unlock_commit(buffer, event);
1342
1343  out_drop_count:
1344         __this_cpu_dec(user_stack_count);
1345  out:
1346         preempt_enable();
1347 }
1348
1349 #ifdef UNUSED
1350 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1351 {
1352         ftrace_trace_userstack(tr, flags, preempt_count());
1353 }
1354 #endif /* UNUSED */
1355
1356 #endif /* CONFIG_STACKTRACE */
1357
1358 /**
1359  * trace_vbprintk - write binary msg to tracing buffer
1360  *
1361  */
1362 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1363 {
1364         static arch_spinlock_t trace_buf_lock =
1365                 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1366         static u32 trace_buf[TRACE_BUF_SIZE];
1367
1368         struct ftrace_event_call *call = &event_bprint;
1369         struct ring_buffer_event *event;
1370         struct ring_buffer *buffer;
1371         struct trace_array *tr = &global_trace;
1372         struct trace_array_cpu *data;
1373         struct bprint_entry *entry;
1374         unsigned long flags;
1375         int disable;
1376         int cpu, len = 0, size, pc;
1377
1378         if (unlikely(tracing_selftest_running || tracing_disabled))
1379                 return 0;
1380
1381         /* Don't pollute graph traces with trace_vprintk internals */
1382         pause_graph_tracing();
1383
1384         pc = preempt_count();
1385         preempt_disable_notrace();
1386         cpu = raw_smp_processor_id();
1387         data = tr->data[cpu];
1388
1389         disable = atomic_inc_return(&data->disabled);
1390         if (unlikely(disable != 1))
1391                 goto out;
1392
1393         /* Lockdep uses trace_printk for lock tracing */
1394         local_irq_save(flags);
1395         arch_spin_lock(&trace_buf_lock);
1396         len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1397
1398         if (len > TRACE_BUF_SIZE || len < 0)
1399                 goto out_unlock;
1400
1401         size = sizeof(*entry) + sizeof(u32) * len;
1402         buffer = tr->buffer;
1403         event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1404                                           flags, pc);
1405         if (!event)
1406                 goto out_unlock;
1407         entry = ring_buffer_event_data(event);
1408         entry->ip                       = ip;
1409         entry->fmt                      = fmt;
1410
1411         memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1412         if (!filter_check_discard(call, entry, buffer, event)) {
1413                 ring_buffer_unlock_commit(buffer, event);
1414                 ftrace_trace_stack(buffer, flags, 6, pc);
1415         }
1416
1417 out_unlock:
1418         arch_spin_unlock(&trace_buf_lock);
1419         local_irq_restore(flags);
1420
1421 out:
1422         atomic_dec_return(&data->disabled);
1423         preempt_enable_notrace();
1424         unpause_graph_tracing();
1425
1426         return len;
1427 }
1428 EXPORT_SYMBOL_GPL(trace_vbprintk);
1429
1430 int trace_array_printk(struct trace_array *tr,
1431                        unsigned long ip, const char *fmt, ...)
1432 {
1433         int ret;
1434         va_list ap;
1435
1436         if (!(trace_flags & TRACE_ITER_PRINTK))
1437                 return 0;
1438
1439         va_start(ap, fmt);
1440         ret = trace_array_vprintk(tr, ip, fmt, ap);
1441         va_end(ap);
1442         return ret;
1443 }
1444
1445 int trace_array_vprintk(struct trace_array *tr,
1446                         unsigned long ip, const char *fmt, va_list args)
1447 {
1448         static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1449         static char trace_buf[TRACE_BUF_SIZE];
1450
1451         struct ftrace_event_call *call = &event_print;
1452         struct ring_buffer_event *event;
1453         struct ring_buffer *buffer;
1454         struct trace_array_cpu *data;
1455         int cpu, len = 0, size, pc;
1456         struct print_entry *entry;
1457         unsigned long irq_flags;
1458         int disable;
1459
1460         if (tracing_disabled || tracing_selftest_running)
1461                 return 0;
1462
1463         pc = preempt_count();
1464         preempt_disable_notrace();
1465         cpu = raw_smp_processor_id();
1466         data = tr->data[cpu];
1467
1468         disable = atomic_inc_return(&data->disabled);
1469         if (unlikely(disable != 1))
1470                 goto out;
1471
1472         pause_graph_tracing();
1473         raw_local_irq_save(irq_flags);
1474         arch_spin_lock(&trace_buf_lock);
1475         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1476
1477         size = sizeof(*entry) + len + 1;
1478         buffer = tr->buffer;
1479         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1480                                           irq_flags, pc);
1481         if (!event)
1482                 goto out_unlock;
1483         entry = ring_buffer_event_data(event);
1484         entry->ip = ip;
1485
1486         memcpy(&entry->buf, trace_buf, len);
1487         entry->buf[len] = '\0';
1488         if (!filter_check_discard(call, entry, buffer, event)) {
1489                 ring_buffer_unlock_commit(buffer, event);
1490                 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1491         }
1492
1493  out_unlock:
1494         arch_spin_unlock(&trace_buf_lock);
1495         raw_local_irq_restore(irq_flags);
1496         unpause_graph_tracing();
1497  out:
1498         atomic_dec_return(&data->disabled);
1499         preempt_enable_notrace();
1500
1501         return len;
1502 }
1503
1504 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1505 {
1506         return trace_array_vprintk(&global_trace, ip, fmt, args);
1507 }
1508 EXPORT_SYMBOL_GPL(trace_vprintk);
1509
1510 static void trace_iterator_increment(struct trace_iterator *iter)
1511 {
1512         /* Don't allow ftrace to trace into the ring buffers */
1513         ftrace_disable_cpu();
1514
1515         iter->idx++;
1516         if (iter->buffer_iter[iter->cpu])
1517                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1518
1519         ftrace_enable_cpu();
1520 }
1521
1522 static struct trace_entry *
1523 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1524                 unsigned long *lost_events)
1525 {
1526         struct ring_buffer_event *event;
1527         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1528
1529         /* Don't allow ftrace to trace into the ring buffers */
1530         ftrace_disable_cpu();
1531
1532         if (buf_iter)
1533                 event = ring_buffer_iter_peek(buf_iter, ts);
1534         else
1535                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1536                                          lost_events);
1537
1538         ftrace_enable_cpu();
1539
1540         return event ? ring_buffer_event_data(event) : NULL;
1541 }
1542
1543 static struct trace_entry *
1544 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1545                   unsigned long *missing_events, u64 *ent_ts)
1546 {
1547         struct ring_buffer *buffer = iter->tr->buffer;
1548         struct trace_entry *ent, *next = NULL;
1549         unsigned long lost_events = 0, next_lost = 0;
1550         int cpu_file = iter->cpu_file;
1551         u64 next_ts = 0, ts;
1552         int next_cpu = -1;
1553         int cpu;
1554
1555         /*
1556          * If we are in a per_cpu trace file, don't bother by iterating over
1557          * all cpu and peek directly.
1558          */
1559         if (cpu_file > TRACE_PIPE_ALL_CPU) {
1560                 if (ring_buffer_empty_cpu(buffer, cpu_file))
1561                         return NULL;
1562                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
1563                 if (ent_cpu)
1564                         *ent_cpu = cpu_file;
1565
1566                 return ent;
1567         }
1568
1569         for_each_tracing_cpu(cpu) {
1570
1571                 if (ring_buffer_empty_cpu(buffer, cpu))
1572                         continue;
1573
1574                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
1575
1576                 /*
1577                  * Pick the entry with the smallest timestamp:
1578                  */
1579                 if (ent && (!next || ts < next_ts)) {
1580                         next = ent;
1581                         next_cpu = cpu;
1582                         next_ts = ts;
1583                         next_lost = lost_events;
1584                 }
1585         }
1586
1587         if (ent_cpu)
1588                 *ent_cpu = next_cpu;
1589
1590         if (ent_ts)
1591                 *ent_ts = next_ts;
1592
1593         if (missing_events)
1594                 *missing_events = next_lost;
1595
1596         return next;
1597 }
1598
1599 /* Find the next real entry, without updating the iterator itself */
1600 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1601                                           int *ent_cpu, u64 *ent_ts)
1602 {
1603         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
1604 }
1605
1606 /* Find the next real entry, and increment the iterator to the next entry */
1607 void *trace_find_next_entry_inc(struct trace_iterator *iter)
1608 {
1609         iter->ent = __find_next_entry(iter, &iter->cpu,
1610                                       &iter->lost_events, &iter->ts);
1611
1612         if (iter->ent)
1613                 trace_iterator_increment(iter);
1614
1615         return iter->ent ? iter : NULL;
1616 }
1617
1618 static void trace_consume(struct trace_iterator *iter)
1619 {
1620         /* Don't allow ftrace to trace into the ring buffers */
1621         ftrace_disable_cpu();
1622         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1623                             &iter->lost_events);
1624         ftrace_enable_cpu();
1625 }
1626
1627 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1628 {
1629         struct trace_iterator *iter = m->private;
1630         int i = (int)*pos;
1631         void *ent;
1632
1633         WARN_ON_ONCE(iter->leftover);
1634
1635         (*pos)++;
1636
1637         /* can't go backwards */
1638         if (iter->idx > i)
1639                 return NULL;
1640
1641         if (iter->idx < 0)
1642                 ent = trace_find_next_entry_inc(iter);
1643         else
1644                 ent = iter;
1645
1646         while (ent && iter->idx < i)
1647                 ent = trace_find_next_entry_inc(iter);
1648
1649         iter->pos = *pos;
1650
1651         return ent;
1652 }
1653
1654 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1655 {
1656         struct trace_array *tr = iter->tr;
1657         struct ring_buffer_event *event;
1658         struct ring_buffer_iter *buf_iter;
1659         unsigned long entries = 0;
1660         u64 ts;
1661
1662         tr->data[cpu]->skipped_entries = 0;
1663
1664         if (!iter->buffer_iter[cpu])
1665                 return;
1666
1667         buf_iter = iter->buffer_iter[cpu];
1668         ring_buffer_iter_reset(buf_iter);
1669
1670         /*
1671          * We could have the case with the max latency tracers
1672          * that a reset never took place on a cpu. This is evident
1673          * by the timestamp being before the start of the buffer.
1674          */
1675         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1676                 if (ts >= iter->tr->time_start)
1677                         break;
1678                 entries++;
1679                 ring_buffer_read(buf_iter, NULL);
1680         }
1681
1682         tr->data[cpu]->skipped_entries = entries;
1683 }
1684
1685 /*
1686  * The current tracer is copied to avoid a global locking
1687  * all around.
1688  */
1689 static void *s_start(struct seq_file *m, loff_t *pos)
1690 {
1691         struct trace_iterator *iter = m->private;
1692         static struct tracer *old_tracer;
1693         int cpu_file = iter->cpu_file;
1694         void *p = NULL;
1695         loff_t l = 0;
1696         int cpu;
1697
1698         /* copy the tracer to avoid using a global lock all around */
1699         mutex_lock(&trace_types_lock);
1700         if (unlikely(old_tracer != current_trace && current_trace)) {
1701                 old_tracer = current_trace;
1702                 *iter->trace = *current_trace;
1703         }
1704         mutex_unlock(&trace_types_lock);
1705
1706         atomic_inc(&trace_record_cmdline_disabled);
1707
1708         if (*pos != iter->pos) {
1709                 iter->ent = NULL;
1710                 iter->cpu = 0;
1711                 iter->idx = -1;
1712
1713                 ftrace_disable_cpu();
1714
1715                 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1716                         for_each_tracing_cpu(cpu)
1717                                 tracing_iter_reset(iter, cpu);
1718                 } else
1719                         tracing_iter_reset(iter, cpu_file);
1720
1721                 ftrace_enable_cpu();
1722
1723                 iter->leftover = 0;
1724                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1725                         ;
1726
1727         } else {
1728                 /*
1729                  * If we overflowed the seq_file before, then we want
1730                  * to just reuse the trace_seq buffer again.
1731                  */
1732                 if (iter->leftover)
1733                         p = iter;
1734                 else {
1735                         l = *pos - 1;
1736                         p = s_next(m, p, &l);
1737                 }
1738         }
1739
1740         trace_event_read_lock();
1741         trace_access_lock(cpu_file);
1742         return p;
1743 }
1744
1745 static void s_stop(struct seq_file *m, void *p)
1746 {
1747         struct trace_iterator *iter = m->private;
1748
1749         atomic_dec(&trace_record_cmdline_disabled);
1750         trace_access_unlock(iter->cpu_file);
1751         trace_event_read_unlock();
1752 }
1753
1754 static void print_lat_help_header(struct seq_file *m)
1755 {
1756         seq_puts(m, "#                  _------=> CPU#            \n");
1757         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1758         seq_puts(m, "#                | / _----=> need-resched    \n");
1759         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1760         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1761         seq_puts(m, "#                |||| /     delay             \n");
1762         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1763         seq_puts(m, "#     \\   /      |||||  \\    |   /           \n");
1764 }
1765
1766 static void print_func_help_header(struct seq_file *m)
1767 {
1768         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1769         seq_puts(m, "#              | |       |          |         |\n");
1770 }
1771
1772
1773 void
1774 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1775 {
1776         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1777         struct trace_array *tr = iter->tr;
1778         struct trace_array_cpu *data = tr->data[tr->cpu];
1779         struct tracer *type = current_trace;
1780         unsigned long entries = 0;
1781         unsigned long total = 0;
1782         unsigned long count;
1783         const char *name = "preemption";
1784         int cpu;
1785
1786         if (type)
1787                 name = type->name;
1788
1789
1790         for_each_tracing_cpu(cpu) {
1791                 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1792                 /*
1793                  * If this buffer has skipped entries, then we hold all
1794                  * entries for the trace and we need to ignore the
1795                  * ones before the time stamp.
1796                  */
1797                 if (tr->data[cpu]->skipped_entries) {
1798                         count -= tr->data[cpu]->skipped_entries;
1799                         /* total is the same as the entries */
1800                         total += count;
1801                 } else
1802                         total += count +
1803                                 ring_buffer_overrun_cpu(tr->buffer, cpu);
1804                 entries += count;
1805         }
1806
1807         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1808                    name, UTS_RELEASE);
1809         seq_puts(m, "# -----------------------------------"
1810                  "---------------------------------\n");
1811         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1812                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1813                    nsecs_to_usecs(data->saved_latency),
1814                    entries,
1815                    total,
1816                    tr->cpu,
1817 #if defined(CONFIG_PREEMPT_NONE)
1818                    "server",
1819 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1820                    "desktop",
1821 #elif defined(CONFIG_PREEMPT)
1822                    "preempt",
1823 #else
1824                    "unknown",
1825 #endif
1826                    /* These are reserved for later use */
1827                    0, 0, 0, 0);
1828 #ifdef CONFIG_SMP
1829         seq_printf(m, " #P:%d)\n", num_online_cpus());
1830 #else
1831         seq_puts(m, ")\n");
1832 #endif
1833         seq_puts(m, "#    -----------------\n");
1834         seq_printf(m, "#    | task: %.16s-%d "
1835                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1836                    data->comm, data->pid, data->uid, data->nice,
1837                    data->policy, data->rt_priority);
1838         seq_puts(m, "#    -----------------\n");
1839
1840         if (data->critical_start) {
1841                 seq_puts(m, "#  => started at: ");
1842                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1843                 trace_print_seq(m, &iter->seq);
1844                 seq_puts(m, "\n#  => ended at:   ");
1845                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1846                 trace_print_seq(m, &iter->seq);
1847                 seq_puts(m, "\n#\n");
1848         }
1849
1850         seq_puts(m, "#\n");
1851 }
1852
1853 static void test_cpu_buff_start(struct trace_iterator *iter)
1854 {
1855         struct trace_seq *s = &iter->seq;
1856
1857         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1858                 return;
1859
1860         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1861                 return;
1862
1863         if (cpumask_test_cpu(iter->cpu, iter->started))
1864                 return;
1865
1866         if (iter->tr->data[iter->cpu]->skipped_entries)
1867                 return;
1868
1869         cpumask_set_cpu(iter->cpu, iter->started);
1870
1871         /* Don't print started cpu buffer for the first entry of the trace */
1872         if (iter->idx > 1)
1873                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1874                                 iter->cpu);
1875 }
1876
1877 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1878 {
1879         struct trace_seq *s = &iter->seq;
1880         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1881         struct trace_entry *entry;
1882         struct trace_event *event;
1883
1884         entry = iter->ent;
1885
1886         test_cpu_buff_start(iter);
1887
1888         event = ftrace_find_event(entry->type);
1889
1890         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1891                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1892                         if (!trace_print_lat_context(iter))
1893                                 goto partial;
1894                 } else {
1895                         if (!trace_print_context(iter))
1896                                 goto partial;
1897                 }
1898         }
1899
1900         if (event)
1901                 return event->funcs->trace(iter, sym_flags, event);
1902
1903         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1904                 goto partial;
1905
1906         return TRACE_TYPE_HANDLED;
1907 partial:
1908         return TRACE_TYPE_PARTIAL_LINE;
1909 }
1910
1911 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1912 {
1913         struct trace_seq *s = &iter->seq;
1914         struct trace_entry *entry;
1915         struct trace_event *event;
1916
1917         entry = iter->ent;
1918
1919         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1920                 if (!trace_seq_printf(s, "%d %d %llu ",
1921                                       entry->pid, iter->cpu, iter->ts))
1922                         goto partial;
1923         }
1924
1925         event = ftrace_find_event(entry->type);
1926         if (event)
1927                 return event->funcs->raw(iter, 0, event);
1928
1929         if (!trace_seq_printf(s, "%d ?\n", entry->type))
1930                 goto partial;
1931
1932         return TRACE_TYPE_HANDLED;
1933 partial:
1934         return TRACE_TYPE_PARTIAL_LINE;
1935 }
1936
1937 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1938 {
1939         struct trace_seq *s = &iter->seq;
1940         unsigned char newline = '\n';
1941         struct trace_entry *entry;
1942         struct trace_event *event;
1943
1944         entry = iter->ent;
1945
1946         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1947                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1948                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1949                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1950         }
1951
1952         event = ftrace_find_event(entry->type);
1953         if (event) {
1954                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
1955                 if (ret != TRACE_TYPE_HANDLED)
1956                         return ret;
1957         }
1958
1959         SEQ_PUT_FIELD_RET(s, newline);
1960
1961         return TRACE_TYPE_HANDLED;
1962 }
1963
1964 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1965 {
1966         struct trace_seq *s = &iter->seq;
1967         struct trace_entry *entry;
1968         struct trace_event *event;
1969
1970         entry = iter->ent;
1971
1972         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1973                 SEQ_PUT_FIELD_RET(s, entry->pid);
1974                 SEQ_PUT_FIELD_RET(s, iter->cpu);
1975                 SEQ_PUT_FIELD_RET(s, iter->ts);
1976         }
1977
1978         event = ftrace_find_event(entry->type);
1979         return event ? event->funcs->binary(iter, 0, event) :
1980                 TRACE_TYPE_HANDLED;
1981 }
1982
1983 int trace_empty(struct trace_iterator *iter)
1984 {
1985         int cpu;
1986
1987         /* If we are looking at one CPU buffer, only check that one */
1988         if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1989                 cpu = iter->cpu_file;
1990                 if (iter->buffer_iter[cpu]) {
1991                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1992                                 return 0;
1993                 } else {
1994                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1995                                 return 0;
1996                 }
1997                 return 1;
1998         }
1999
2000         for_each_tracing_cpu(cpu) {
2001                 if (iter->buffer_iter[cpu]) {
2002                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2003                                 return 0;
2004                 } else {
2005                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2006                                 return 0;
2007                 }
2008         }
2009
2010         return 1;
2011 }
2012
2013 /*  Called with trace_event_read_lock() held. */
2014 enum print_line_t print_trace_line(struct trace_iterator *iter)
2015 {
2016         enum print_line_t ret;
2017
2018         if (iter->lost_events &&
2019             !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2020                                  iter->cpu, iter->lost_events))
2021                 return TRACE_TYPE_PARTIAL_LINE;
2022
2023         if (iter->trace && iter->trace->print_line) {
2024                 ret = iter->trace->print_line(iter);
2025                 if (ret != TRACE_TYPE_UNHANDLED)
2026                         return ret;
2027         }
2028
2029         if (iter->ent->type == TRACE_BPRINT &&
2030                         trace_flags & TRACE_ITER_PRINTK &&
2031                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2032                 return trace_print_bprintk_msg_only(iter);
2033
2034         if (iter->ent->type == TRACE_PRINT &&
2035                         trace_flags & TRACE_ITER_PRINTK &&
2036                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2037                 return trace_print_printk_msg_only(iter);
2038
2039         if (trace_flags & TRACE_ITER_BIN)
2040                 return print_bin_fmt(iter);
2041
2042         if (trace_flags & TRACE_ITER_HEX)
2043                 return print_hex_fmt(iter);
2044
2045         if (trace_flags & TRACE_ITER_RAW)
2046                 return print_raw_fmt(iter);
2047
2048         return print_trace_fmt(iter);
2049 }
2050
2051 void trace_default_header(struct seq_file *m)
2052 {
2053         struct trace_iterator *iter = m->private;
2054
2055         if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2056                 /* print nothing if the buffers are empty */
2057                 if (trace_empty(iter))
2058                         return;
2059                 print_trace_header(m, iter);
2060                 if (!(trace_flags & TRACE_ITER_VERBOSE))
2061                         print_lat_help_header(m);
2062         } else {
2063                 if (!(trace_flags & TRACE_ITER_VERBOSE))
2064                         print_func_help_header(m);
2065         }
2066 }
2067
2068 static int s_show(struct seq_file *m, void *v)
2069 {
2070         struct trace_iterator *iter = v;
2071         int ret;
2072
2073         if (iter->ent == NULL) {
2074                 if (iter->tr) {
2075                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
2076                         seq_puts(m, "#\n");
2077                 }
2078                 if (iter->trace && iter->trace->print_header)
2079                         iter->trace->print_header(m);
2080                 else
2081                         trace_default_header(m);
2082
2083         } else if (iter->leftover) {
2084                 /*
2085                  * If we filled the seq_file buffer earlier, we
2086                  * want to just show it now.
2087                  */
2088                 ret = trace_print_seq(m, &iter->seq);
2089
2090                 /* ret should this time be zero, but you never know */
2091                 iter->leftover = ret;
2092
2093         } else {
2094                 print_trace_line(iter);
2095                 ret = trace_print_seq(m, &iter->seq);
2096                 /*
2097                  * If we overflow the seq_file buffer, then it will
2098                  * ask us for this data again at start up.
2099                  * Use that instead.
2100                  *  ret is 0 if seq_file write succeeded.
2101                  *        -1 otherwise.
2102                  */
2103                 iter->leftover = ret;
2104         }
2105
2106         return 0;
2107 }
2108
2109 static const struct seq_operations tracer_seq_ops = {
2110         .start          = s_start,
2111         .next           = s_next,
2112         .stop           = s_stop,
2113         .show           = s_show,
2114 };
2115
2116 static struct trace_iterator *
2117 __tracing_open(struct inode *inode, struct file *file)
2118 {
2119         long cpu_file = (long) inode->i_private;
2120         void *fail_ret = ERR_PTR(-ENOMEM);
2121         struct trace_iterator *iter;
2122         struct seq_file *m;
2123         int cpu, ret;
2124
2125         if (tracing_disabled)
2126                 return ERR_PTR(-ENODEV);
2127
2128         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2129         if (!iter)
2130                 return ERR_PTR(-ENOMEM);
2131
2132         /*
2133          * We make a copy of the current tracer to avoid concurrent
2134          * changes on it while we are reading.
2135          */
2136         mutex_lock(&trace_types_lock);
2137         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2138         if (!iter->trace)
2139                 goto fail;
2140
2141         if (current_trace)
2142                 *iter->trace = *current_trace;
2143
2144         if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2145                 goto fail;
2146
2147         if (current_trace && current_trace->print_max)
2148                 iter->tr = &max_tr;
2149         else
2150                 iter->tr = &global_trace;
2151         iter->pos = -1;
2152         mutex_init(&iter->mutex);
2153         iter->cpu_file = cpu_file;
2154
2155         /* Notify the tracer early; before we stop tracing. */
2156         if (iter->trace && iter->trace->open)
2157                 iter->trace->open(iter);
2158
2159         /* Annotate start of buffers if we had overruns */
2160         if (ring_buffer_overruns(iter->tr->buffer))
2161                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2162
2163         /* stop the trace while dumping */
2164         tracing_stop();
2165
2166         if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2167                 for_each_tracing_cpu(cpu) {
2168                         iter->buffer_iter[cpu] =
2169                                 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2170                 }
2171                 ring_buffer_read_prepare_sync();
2172                 for_each_tracing_cpu(cpu) {
2173                         ring_buffer_read_start(iter->buffer_iter[cpu]);
2174                         tracing_iter_reset(iter, cpu);
2175                 }
2176         } else {
2177                 cpu = iter->cpu_file;
2178                 iter->buffer_iter[cpu] =
2179                         ring_buffer_read_prepare(iter->tr->buffer, cpu);
2180                 ring_buffer_read_prepare_sync();
2181                 ring_buffer_read_start(iter->buffer_iter[cpu]);
2182                 tracing_iter_reset(iter, cpu);
2183         }
2184
2185         ret = seq_open(file, &tracer_seq_ops);
2186         if (ret < 0) {
2187                 fail_ret = ERR_PTR(ret);
2188                 goto fail_buffer;
2189         }
2190
2191         m = file->private_data;
2192         m->private = iter;
2193
2194         mutex_unlock(&trace_types_lock);
2195
2196         return iter;
2197
2198  fail_buffer:
2199         for_each_tracing_cpu(cpu) {
2200                 if (iter->buffer_iter[cpu])
2201                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2202         }
2203         free_cpumask_var(iter->started);
2204         tracing_start();
2205  fail:
2206         mutex_unlock(&trace_types_lock);
2207         kfree(iter->trace);
2208         kfree(iter);
2209
2210         return fail_ret;
2211 }
2212
2213 int tracing_open_generic(struct inode *inode, struct file *filp)
2214 {
2215         if (tracing_disabled)
2216                 return -ENODEV;
2217
2218         filp->private_data = inode->i_private;
2219         return 0;
2220 }
2221
2222 static int tracing_release(struct inode *inode, struct file *file)
2223 {
2224         struct seq_file *m = file->private_data;
2225         struct trace_iterator *iter;
2226         int cpu;
2227
2228         if (!(file->f_mode & FMODE_READ))
2229                 return 0;
2230
2231         iter = m->private;
2232
2233         mutex_lock(&trace_types_lock);
2234         for_each_tracing_cpu(cpu) {
2235                 if (iter->buffer_iter[cpu])
2236                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2237         }
2238
2239         if (iter->trace && iter->trace->close)
2240                 iter->trace->close(iter);
2241
2242         /* reenable tracing if it was previously enabled */
2243         tracing_start();
2244         mutex_unlock(&trace_types_lock);
2245
2246         seq_release(inode, file);
2247         mutex_destroy(&iter->mutex);
2248         free_cpumask_var(iter->started);
2249         kfree(iter->trace);
2250         kfree(iter);
2251         return 0;
2252 }
2253
2254 static int tracing_open(struct inode *inode, struct file *file)
2255 {
2256         struct trace_iterator *iter;
2257         int ret = 0;
2258
2259         /* If this file was open for write, then erase contents */
2260         if ((file->f_mode & FMODE_WRITE) &&
2261             (file->f_flags & O_TRUNC)) {
2262                 long cpu = (long) inode->i_private;
2263
2264                 if (cpu == TRACE_PIPE_ALL_CPU)
2265                         tracing_reset_online_cpus(&global_trace);
2266                 else
2267                         tracing_reset(&global_trace, cpu);
2268         }
2269
2270         if (file->f_mode & FMODE_READ) {
2271                 iter = __tracing_open(inode, file);
2272                 if (IS_ERR(iter))
2273                         ret = PTR_ERR(iter);
2274                 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2275                         iter->iter_flags |= TRACE_FILE_LAT_FMT;
2276         }
2277         return ret;
2278 }
2279
2280 static void *
2281 t_next(struct seq_file *m, void *v, loff_t *pos)
2282 {
2283         struct tracer *t = v;
2284
2285         (*pos)++;
2286
2287         if (t)
2288                 t = t->next;
2289
2290         return t;
2291 }
2292
2293 static void *t_start(struct seq_file *m, loff_t *pos)
2294 {
2295         struct tracer *t;
2296         loff_t l = 0;
2297
2298         mutex_lock(&trace_types_lock);
2299         for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2300                 ;
2301
2302         return t;
2303 }
2304
2305 static void t_stop(struct seq_file *m, void *p)
2306 {
2307         mutex_unlock(&trace_types_lock);
2308 }
2309
2310 static int t_show(struct seq_file *m, void *v)
2311 {
2312         struct tracer *t = v;
2313
2314         if (!t)
2315                 return 0;
2316
2317         seq_printf(m, "%s", t->name);
2318         if (t->next)
2319                 seq_putc(m, ' ');
2320         else
2321                 seq_putc(m, '\n');
2322
2323         return 0;
2324 }
2325
2326 static const struct seq_operations show_traces_seq_ops = {
2327         .start          = t_start,
2328         .next           = t_next,
2329         .stop           = t_stop,
2330         .show           = t_show,
2331 };
2332
2333 static int show_traces_open(struct inode *inode, struct file *file)
2334 {
2335         if (tracing_disabled)
2336                 return -ENODEV;
2337
2338         return seq_open(file, &show_traces_seq_ops);
2339 }
2340
2341 static ssize_t
2342 tracing_write_stub(struct file *filp, const char __user *ubuf,
2343                    size_t count, loff_t *ppos)
2344 {
2345         return count;
2346 }
2347
2348 static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2349 {
2350         if (file->f_mode & FMODE_READ)
2351                 return seq_lseek(file, offset, origin);
2352         else
2353                 return 0;
2354 }
2355
2356 static const struct file_operations tracing_fops = {
2357         .open           = tracing_open,
2358         .read           = seq_read,
2359         .write          = tracing_write_stub,
2360         .llseek         = tracing_seek,
2361         .release        = tracing_release,
2362 };
2363
2364 static const struct file_operations show_traces_fops = {
2365         .open           = show_traces_open,
2366         .read           = seq_read,
2367         .release        = seq_release,
2368         .llseek         = seq_lseek,
2369 };
2370
2371 /*
2372  * Only trace on a CPU if the bitmask is set:
2373  */
2374 static cpumask_var_t tracing_cpumask;
2375
2376 /*
2377  * The tracer itself will not take this lock, but still we want
2378  * to provide a consistent cpumask to user-space:
2379  */
2380 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2381
2382 /*
2383  * Temporary storage for the character representation of the
2384  * CPU bitmask (and one more byte for the newline):
2385  */
2386 static char mask_str[NR_CPUS + 1];
2387
2388 static ssize_t
2389 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2390                      size_t count, loff_t *ppos)
2391 {
2392         int len;
2393
2394         mutex_lock(&tracing_cpumask_update_lock);
2395
2396         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2397         if (count - len < 2) {
2398                 count = -EINVAL;
2399                 goto out_err;
2400         }
2401         len += sprintf(mask_str + len, "\n");
2402         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2403
2404 out_err:
2405         mutex_unlock(&tracing_cpumask_update_lock);
2406
2407         return count;
2408 }
2409
2410 static ssize_t
2411 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2412                       size_t count, loff_t *ppos)
2413 {
2414         int err, cpu;
2415         cpumask_var_t tracing_cpumask_new;
2416
2417         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2418                 return -ENOMEM;
2419
2420         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2421         if (err)
2422                 goto err_unlock;
2423
2424         mutex_lock(&tracing_cpumask_update_lock);
2425
2426         local_irq_disable();
2427         arch_spin_lock(&ftrace_max_lock);
2428         for_each_tracing_cpu(cpu) {
2429                 /*
2430                  * Increase/decrease the disabled counter if we are
2431                  * about to flip a bit in the cpumask:
2432                  */
2433                 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2434                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2435                         atomic_inc(&global_trace.data[cpu]->disabled);
2436                 }
2437                 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2438                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2439                         atomic_dec(&global_trace.data[cpu]->disabled);
2440                 }
2441         }
2442         arch_spin_unlock(&ftrace_max_lock);
2443         local_irq_enable();
2444
2445         cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2446
2447         mutex_unlock(&tracing_cpumask_update_lock);
2448         free_cpumask_var(tracing_cpumask_new);
2449
2450         return count;
2451
2452 err_unlock:
2453         free_cpumask_var(tracing_cpumask_new);
2454
2455         return err;
2456 }
2457
2458 static const struct file_operations tracing_cpumask_fops = {
2459         .open           = tracing_open_generic,
2460         .read           = tracing_cpumask_read,
2461         .write          = tracing_cpumask_write,
2462         .llseek         = generic_file_llseek,
2463 };
2464
2465 static int tracing_trace_options_show(struct seq_file *m, void *v)
2466 {
2467         struct tracer_opt *trace_opts;
2468         u32 tracer_flags;
2469         int i;
2470
2471         mutex_lock(&trace_types_lock);
2472         tracer_flags = current_trace->flags->val;
2473         trace_opts = current_trace->flags->opts;
2474
2475         for (i = 0; trace_options[i]; i++) {
2476                 if (trace_flags & (1 << i))
2477                         seq_printf(m, "%s\n", trace_options[i]);
2478                 else
2479                         seq_printf(m, "no%s\n", trace_options[i]);
2480         }
2481
2482         for (i = 0; trace_opts[i].name; i++) {
2483                 if (tracer_flags & trace_opts[i].bit)
2484                         seq_printf(m, "%s\n", trace_opts[i].name);
2485                 else
2486                         seq_printf(m, "no%s\n", trace_opts[i].name);
2487         }
2488         mutex_unlock(&trace_types_lock);
2489
2490         return 0;
2491 }
2492
2493 static int __set_tracer_option(struct tracer *trace,
2494                                struct tracer_flags *tracer_flags,
2495                                struct tracer_opt *opts, int neg)
2496 {
2497         int ret;
2498
2499         ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
2500         if (ret)
2501                 return ret;
2502
2503         if (neg)
2504                 tracer_flags->val &= ~opts->bit;
2505         else
2506                 tracer_flags->val |= opts->bit;
2507         return 0;
2508 }
2509
2510 /* Try to assign a tracer specific option */
2511 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2512 {
2513         struct tracer_flags *tracer_flags = trace->flags;
2514         struct tracer_opt *opts = NULL;
2515         int i;
2516
2517         for (i = 0; tracer_flags->opts[i].name; i++) {
2518                 opts = &tracer_flags->opts[i];
2519
2520                 if (strcmp(cmp, opts->name) == 0)
2521                         return __set_tracer_option(trace, trace->flags,
2522                                                    opts, neg);
2523         }
2524
2525         return -EINVAL;
2526 }
2527
2528 static void set_tracer_flags(unsigned int mask, int enabled)
2529 {
2530         /* do nothing if flag is already set */
2531         if (!!(trace_flags & mask) == !!enabled)
2532                 return;
2533
2534         if (enabled)
2535                 trace_flags |= mask;
2536         else
2537                 trace_flags &= ~mask;
2538
2539         if (mask == TRACE_ITER_RECORD_CMD)
2540                 trace_event_enable_cmd_record(enabled);
2541
2542         if (mask == TRACE_ITER_OVERWRITE)
2543                 ring_buffer_change_overwrite(global_trace.buffer, enabled);
2544 }
2545
2546 static ssize_t
2547 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2548                         size_t cnt, loff_t *ppos)
2549 {
2550         char buf[64];
2551         char *cmp;
2552         int neg = 0;
2553         int ret;
2554         int i;
2555
2556         if (cnt >= sizeof(buf))
2557                 return -EINVAL;
2558
2559         if (copy_from_user(&buf, ubuf, cnt))
2560                 return -EFAULT;
2561
2562         buf[cnt] = 0;
2563         cmp = strstrip(buf);
2564
2565         if (strncmp(cmp, "no", 2) == 0) {
2566                 neg = 1;
2567                 cmp += 2;
2568         }
2569
2570         for (i = 0; trace_options[i]; i++) {
2571                 if (strcmp(cmp, trace_options[i]) == 0) {
2572                         set_tracer_flags(1 << i, !neg);
2573                         break;
2574                 }
2575         }
2576
2577         /* If no option could be set, test the specific tracer options */
2578         if (!trace_options[i]) {
2579                 mutex_lock(&trace_types_lock);
2580                 ret = set_tracer_option(current_trace, cmp, neg);
2581                 mutex_unlock(&trace_types_lock);
2582                 if (ret)
2583                         return ret;
2584         }
2585
2586         *ppos += cnt;
2587
2588         return cnt;
2589 }
2590
2591 static int tracing_trace_options_open(struct inode *inode, struct file *file)
2592 {
2593         if (tracing_disabled)
2594                 return -ENODEV;
2595         return single_open(file, tracing_trace_options_show, NULL);
2596 }
2597
2598 static const struct file_operations tracing_iter_fops = {
2599         .open           = tracing_trace_options_open,
2600         .read           = seq_read,
2601         .llseek         = seq_lseek,
2602         .release        = single_release,
2603         .write          = tracing_trace_options_write,
2604 };
2605
2606 static const char readme_msg[] =
2607         "tracing mini-HOWTO:\n\n"
2608         "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2609         "# cat /sys/kernel/debug/tracing/available_tracers\n"
2610         "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2611         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2612         "nop\n"
2613         "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2614         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2615         "sched_switch\n"
2616         "# cat /sys/kernel/debug/tracing/trace_options\n"
2617         "noprint-parent nosym-offset nosym-addr noverbose\n"
2618         "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2619         "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2620         "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2621         "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2622 ;
2623
2624 static ssize_t
2625 tracing_readme_read(struct file *filp, char __user *ubuf,
2626                        size_t cnt, loff_t *ppos)
2627 {
2628         return simple_read_from_buffer(ubuf, cnt, ppos,
2629                                         readme_msg, strlen(readme_msg));
2630 }
2631
2632 static const struct file_operations tracing_readme_fops = {
2633         .open           = tracing_open_generic,
2634         .read           = tracing_readme_read,
2635         .llseek         = generic_file_llseek,
2636 };
2637
2638 static ssize_t
2639 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2640                                 size_t cnt, loff_t *ppos)
2641 {
2642         char *buf_comm;
2643         char *file_buf;
2644         char *buf;
2645         int len = 0;
2646         int pid;
2647         int i;
2648
2649         file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2650         if (!file_buf)
2651                 return -ENOMEM;
2652
2653         buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2654         if (!buf_comm) {
2655                 kfree(file_buf);
2656                 return -ENOMEM;
2657         }
2658
2659         buf = file_buf;
2660
2661         for (i = 0; i < SAVED_CMDLINES; i++) {
2662                 int r;
2663
2664                 pid = map_cmdline_to_pid[i];
2665                 if (pid == -1 || pid == NO_CMDLINE_MAP)
2666                         continue;
2667
2668                 trace_find_cmdline(pid, buf_comm);
2669                 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2670                 buf += r;
2671                 len += r;
2672         }
2673
2674         len = simple_read_from_buffer(ubuf, cnt, ppos,
2675                                       file_buf, len);
2676
2677         kfree(file_buf);
2678         kfree(buf_comm);
2679
2680         return len;
2681 }
2682
2683 static const struct file_operations tracing_saved_cmdlines_fops = {
2684     .open       = tracing_open_generic,
2685     .read       = tracing_saved_cmdlines_read,
2686     .llseek     = generic_file_llseek,
2687 };
2688
2689 static ssize_t
2690 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2691                   size_t cnt, loff_t *ppos)
2692 {
2693         char buf[64];
2694         int r;
2695
2696         r = sprintf(buf, "%u\n", tracer_enabled);
2697         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2698 }
2699
2700 static ssize_t
2701 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2702                    size_t cnt, loff_t *ppos)
2703 {
2704         struct trace_array *tr = filp->private_data;
2705         char buf[64];
2706         unsigned long val;
2707         int ret;
2708
2709         if (cnt >= sizeof(buf))
2710                 return -EINVAL;
2711
2712         if (copy_from_user(&buf, ubuf, cnt))
2713                 return -EFAULT;
2714
2715         buf[cnt] = 0;
2716
2717         ret = strict_strtoul(buf, 10, &val);
2718         if (ret < 0)
2719                 return ret;
2720
2721         val = !!val;
2722
2723         mutex_lock(&trace_types_lock);
2724         if (tracer_enabled ^ val) {
2725
2726                 /* Only need to warn if this is used to change the state */
2727                 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2728
2729                 if (val) {
2730                         tracer_enabled = 1;
2731                         if (current_trace->start)
2732                                 current_trace->start(tr);
2733                         tracing_start();
2734                 } else {
2735                         tracer_enabled = 0;
2736                         tracing_stop();
2737                         if (current_trace->stop)
2738                                 current_trace->stop(tr);
2739                 }
2740         }
2741         mutex_unlock(&trace_types_lock);
2742
2743         *ppos += cnt;
2744
2745         return cnt;
2746 }
2747
2748 static ssize_t
2749 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2750                        size_t cnt, loff_t *ppos)
2751 {
2752         char buf[MAX_TRACER_SIZE+2];
2753         int r;
2754
2755         mutex_lock(&trace_types_lock);
2756         if (current_trace)
2757                 r = sprintf(buf, "%s\n", current_trace->name);
2758         else
2759                 r = sprintf(buf, "\n");
2760         mutex_unlock(&trace_types_lock);
2761
2762         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2763 }
2764
2765 int tracer_init(struct tracer *t, struct trace_array *tr)
2766 {
2767         tracing_reset_online_cpus(tr);
2768         return t->init(tr);
2769 }
2770
2771 static int tracing_resize_ring_buffer(unsigned long size)
2772 {
2773         int ret;
2774
2775         /*
2776          * If kernel or user changes the size of the ring buffer
2777          * we use the size that was given, and we can forget about
2778          * expanding it later.
2779          */
2780         ring_buffer_expanded = 1;
2781
2782         ret = ring_buffer_resize(global_trace.buffer, size);
2783         if (ret < 0)
2784                 return ret;
2785
2786         if (!current_trace->use_max_tr)
2787                 goto out;
2788
2789         ret = ring_buffer_resize(max_tr.buffer, size);
2790         if (ret < 0) {
2791                 int r;
2792
2793                 r = ring_buffer_resize(global_trace.buffer,
2794                                        global_trace.entries);
2795                 if (r < 0) {
2796                         /*
2797                          * AARGH! We are left with different
2798                          * size max buffer!!!!
2799                          * The max buffer is our "snapshot" buffer.
2800                          * When a tracer needs a snapshot (one of the
2801                          * latency tracers), it swaps the max buffer
2802                          * with the saved snap shot. We succeeded to
2803                          * update the size of the main buffer, but failed to
2804                          * update the size of the max buffer. But when we tried
2805                          * to reset the main buffer to the original size, we
2806                          * failed there too. This is very unlikely to
2807                          * happen, but if it does, warn and kill all
2808                          * tracing.
2809                          */
2810                         WARN_ON(1);
2811                         tracing_disabled = 1;
2812                 }
2813                 return ret;
2814         }
2815
2816         max_tr.entries = size;
2817  out:
2818         global_trace.entries = size;
2819
2820         return ret;
2821 }
2822
2823
2824 /**
2825  * tracing_update_buffers - used by tracing facility to expand ring buffers
2826  *
2827  * To save on memory when the tracing is never used on a system with it
2828  * configured in. The ring buffers are set to a minimum size. But once
2829  * a user starts to use the tracing facility, then they need to grow
2830  * to their default size.
2831  *
2832  * This function is to be called when a tracer is about to be used.
2833  */
2834 int tracing_update_buffers(void)
2835 {
2836         int ret = 0;
2837
2838         mutex_lock(&trace_types_lock);
2839         if (!ring_buffer_expanded)
2840                 ret = tracing_resize_ring_buffer(trace_buf_size);
2841         mutex_unlock(&trace_types_lock);
2842
2843         return ret;
2844 }
2845
2846 struct trace_option_dentry;
2847
2848 static struct trace_option_dentry *
2849 create_trace_option_files(struct tracer *tracer);
2850
2851 static void
2852 destroy_trace_option_files(struct trace_option_dentry *topts);
2853
2854 static int tracing_set_tracer(const char *buf)
2855 {
2856         static struct trace_option_dentry *topts;
2857         struct trace_array *tr = &global_trace;
2858         struct tracer *t;
2859         int ret = 0;
2860
2861         mutex_lock(&trace_types_lock);
2862
2863         if (!ring_buffer_expanded) {
2864                 ret = tracing_resize_ring_buffer(trace_buf_size);
2865                 if (ret < 0)
2866                         goto out;
2867                 ret = 0;
2868         }
2869
2870         for (t = trace_types; t; t = t->next) {
2871                 if (strcmp(t->name, buf) == 0)
2872                         break;
2873         }
2874         if (!t) {
2875                 ret = -EINVAL;
2876                 goto out;
2877         }
2878         if (t == current_trace)
2879                 goto out;
2880
2881         trace_branch_disable();
2882         if (current_trace && current_trace->reset)
2883                 current_trace->reset(tr);
2884         if (current_trace && current_trace->use_max_tr) {
2885                 /*
2886                  * We don't free the ring buffer. instead, resize it because
2887                  * The max_tr ring buffer has some state (e.g. ring->clock) and
2888                  * we want preserve it.
2889                  */
2890                 ring_buffer_resize(max_tr.buffer, 1);
2891                 max_tr.entries = 1;
2892         }
2893         destroy_trace_option_files(topts);
2894
2895         current_trace = t;
2896
2897         topts = create_trace_option_files(current_trace);
2898         if (current_trace->use_max_tr) {
2899                 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2900                 if (ret < 0)
2901                         goto out;
2902                 max_tr.entries = global_trace.entries;
2903         }
2904
2905         if (t->init) {
2906                 ret = tracer_init(t, tr);
2907                 if (ret)
2908                         goto out;
2909         }
2910
2911         trace_branch_enable(tr);
2912  out:
2913         mutex_unlock(&trace_types_lock);
2914
2915         return ret;
2916 }
2917
2918 static ssize_t
2919 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2920                         size_t cnt, loff_t *ppos)
2921 {
2922         char buf[MAX_TRACER_SIZE+1];
2923         int i;
2924         size_t ret;
2925         int err;
2926
2927         ret = cnt;
2928
2929         if (cnt > MAX_TRACER_SIZE)
2930                 cnt = MAX_TRACER_SIZE;
2931
2932         if (copy_from_user(&buf, ubuf, cnt))
2933                 return -EFAULT;
2934
2935         buf[cnt] = 0;
2936
2937         /* strip ending whitespace. */
2938         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2939                 buf[i] = 0;
2940
2941         err = tracing_set_tracer(buf);
2942         if (err)
2943                 return err;
2944
2945         *ppos += ret;
2946
2947         return ret;
2948 }
2949
2950 static ssize_t
2951 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2952                      size_t cnt, loff_t *ppos)
2953 {
2954         unsigned long *ptr = filp->private_data;
2955         char buf[64];
2956         int r;
2957
2958         r = snprintf(buf, sizeof(buf), "%ld\n",
2959                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2960         if (r > sizeof(buf))
2961                 r = sizeof(buf);
2962         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2963 }
2964
2965 static ssize_t
2966 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2967                       size_t cnt, loff_t *ppos)
2968 {
2969         unsigned long *ptr = filp->private_data;
2970         char buf[64];
2971         unsigned long val;
2972         int ret;
2973
2974         if (cnt >= sizeof(buf))
2975                 return -EINVAL;
2976
2977         if (copy_from_user(&buf, ubuf, cnt))
2978                 return -EFAULT;
2979
2980         buf[cnt] = 0;
2981
2982         ret = strict_strtoul(buf, 10, &val);
2983         if (ret < 0)
2984                 return ret;
2985
2986         *ptr = val * 1000;
2987
2988         return cnt;
2989 }
2990
2991 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2992 {
2993         long cpu_file = (long) inode->i_private;
2994         struct trace_iterator *iter;
2995         int ret = 0;
2996
2997         if (tracing_disabled)
2998                 return -ENODEV;
2999
3000         mutex_lock(&trace_types_lock);
3001
3002         /* create a buffer to store the information to pass to userspace */
3003         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3004         if (!iter) {
3005                 ret = -ENOMEM;
3006                 goto out;
3007         }
3008
3009         /*
3010          * We make a copy of the current tracer to avoid concurrent
3011          * changes on it while we are reading.
3012          */
3013         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3014         if (!iter->trace) {
3015                 ret = -ENOMEM;
3016                 goto fail;
3017         }
3018         if (current_trace)
3019                 *iter->trace = *current_trace;
3020
3021         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3022                 ret = -ENOMEM;
3023                 goto fail;
3024         }
3025
3026         /* trace pipe does not show start of buffer */
3027         cpumask_setall(iter->started);
3028
3029         if (trace_flags & TRACE_ITER_LATENCY_FMT)
3030                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3031
3032         iter->cpu_file = cpu_file;
3033         iter->tr = &global_trace;
3034         mutex_init(&iter->mutex);
3035         filp->private_data = iter;
3036
3037         if (iter->trace->pipe_open)
3038                 iter->trace->pipe_open(iter);
3039
3040         nonseekable_open(inode, filp);
3041 out:
3042         mutex_unlock(&trace_types_lock);
3043         return ret;
3044
3045 fail:
3046         kfree(iter->trace);
3047         kfree(iter);
3048         mutex_unlock(&trace_types_lock);
3049         return ret;
3050 }
3051
3052 static int tracing_release_pipe(struct inode *inode, struct file *file)
3053 {
3054         struct trace_iterator *iter = file->private_data;
3055
3056         mutex_lock(&trace_types_lock);
3057
3058         if (iter->trace->pipe_close)
3059                 iter->trace->pipe_close(iter);
3060
3061         mutex_unlock(&trace_types_lock);
3062
3063         free_cpumask_var(iter->started);
3064         mutex_destroy(&iter->mutex);
3065         kfree(iter->trace);
3066         kfree(iter);
3067
3068         return 0;
3069 }
3070
3071 static unsigned int
3072 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3073 {
3074         struct trace_iterator *iter = filp->private_data;
3075
3076         if (trace_flags & TRACE_ITER_BLOCK) {
3077                 /*
3078                  * Always select as readable when in blocking mode
3079                  */
3080                 return POLLIN | POLLRDNORM;
3081         } else {
3082                 if (!trace_empty(iter))
3083                         return POLLIN | POLLRDNORM;
3084                 poll_wait(filp, &trace_wait, poll_table);
3085                 if (!trace_empty(iter))
3086                         return POLLIN | POLLRDNORM;
3087
3088                 return 0;
3089         }
3090 }
3091
3092
3093 void default_wait_pipe(struct trace_iterator *iter)
3094 {
3095         DEFINE_WAIT(wait);
3096
3097         prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3098
3099         if (trace_empty(iter))
3100                 schedule();
3101
3102         finish_wait(&trace_wait, &wait);
3103 }
3104
3105 /*
3106  * This is a make-shift waitqueue.
3107  * A tracer might use this callback on some rare cases:
3108  *
3109  *  1) the current tracer might hold the runqueue lock when it wakes up
3110  *     a reader, hence a deadlock (sched, function, and function graph tracers)
3111  *  2) the function tracers, trace all functions, we don't want
3112  *     the overhead of calling wake_up and friends
3113  *     (and tracing them too)
3114  *
3115  *     Anyway, this is really very primitive wakeup.
3116  */
3117 void poll_wait_pipe(struct trace_iterator *iter)
3118 {
3119         set_current_state(TASK_INTERRUPTIBLE);
3120         /* sleep for 100 msecs, and try again. */
3121         schedule_timeout(HZ / 10);
3122 }
3123
3124 /* Must be called with trace_types_lock mutex held. */
3125 static int tracing_wait_pipe(struct file *filp)
3126 {
3127         struct trace_iterator *iter = filp->private_data;
3128
3129         while (trace_empty(iter)) {
3130
3131                 if ((filp->f_flags & O_NONBLOCK)) {
3132                         return -EAGAIN;
3133                 }
3134
3135                 mutex_unlock(&iter->mutex);
3136
3137                 iter->trace->wait_pipe(iter);
3138
3139                 mutex_lock(&iter->mutex);
3140
3141                 if (signal_pending(current))
3142                         return -EINTR;
3143
3144                 /*
3145                  * We block until we read something and tracing is disabled.
3146                  * We still block if tracing is disabled, but we have never
3147                  * read anything. This allows a user to cat this file, and
3148                  * then enable tracing. But after we have read something,
3149                  * we give an EOF when tracing is again disabled.
3150                  *
3151                  * iter->pos will be 0 if we haven't read anything.
3152                  */
3153                 if (!tracer_enabled && iter->pos)
3154                         break;
3155         }
3156
3157         return 1;
3158 }
3159
3160 /*
3161  * Consumer reader.
3162  */
3163 static ssize_t
3164 tracing_read_pipe(struct file *filp, char __user *ubuf,
3165                   size_t cnt, loff_t *ppos)
3166 {
3167         struct trace_iterator *iter = filp->private_data;
3168         static struct tracer *old_tracer;
3169         ssize_t sret;
3170
3171         /* return any leftover data */
3172         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3173         if (sret != -EBUSY)
3174                 return sret;
3175
3176         trace_seq_init(&iter->seq);
3177
3178         /* copy the tracer to avoid using a global lock all around */
3179         mutex_lock(&trace_types_lock);
3180         if (unlikely(old_tracer != current_trace && current_trace)) {
3181                 old_tracer = current_trace;
3182                 *iter->trace = *current_trace;
3183         }
3184         mutex_unlock(&trace_types_lock);
3185
3186         /*
3187          * Avoid more than one consumer on a single file descriptor
3188          * This is just a matter of traces coherency, the ring buffer itself
3189          * is protected.
3190          */
3191         mutex_lock(&iter->mutex);
3192         if (iter->trace->read) {
3193                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3194                 if (sret)
3195                         goto out;
3196         }
3197
3198 waitagain:
3199         sret = tracing_wait_pipe(filp);
3200         if (sret <= 0)
3201                 goto out;
3202
3203         /* stop when tracing is finished */
3204         if (trace_empty(iter)) {
3205                 sret = 0;
3206                 goto out;
3207         }
3208
3209         if (cnt >= PAGE_SIZE)
3210                 cnt = PAGE_SIZE - 1;
3211
3212         /* reset all but tr, trace, and overruns */
3213         memset(&iter->seq, 0,
3214                sizeof(struct trace_iterator) -
3215                offsetof(struct trace_iterator, seq));
3216         iter->pos = -1;
3217
3218         trace_event_read_lock();
3219         trace_access_lock(iter->cpu_file);
3220         while (trace_find_next_entry_inc(iter) != NULL) {
3221                 enum print_line_t ret;
3222                 int len = iter->seq.len;
3223
3224                 ret = print_trace_line(iter);
3225                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3226                         /* don't print partial lines */
3227                         iter->seq.len = len;
3228                         break;
3229                 }
3230                 if (ret != TRACE_TYPE_NO_CONSUME)
3231                         trace_consume(iter);
3232
3233                 if (iter->seq.len >= cnt)
3234                         break;
3235
3236                 /*
3237                  * Setting the full flag means we reached the trace_seq buffer
3238                  * size and we should leave by partial output condition above.
3239                  * One of the trace_seq_* functions is not used properly.
3240                  */
3241                 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3242                           iter->ent->type);
3243         }
3244         trace_access_unlock(iter->cpu_file);
3245         trace_event_read_unlock();
3246
3247         /* Now copy what we have to the user */
3248         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3249         if (iter->seq.readpos >= iter->seq.len)
3250                 trace_seq_init(&iter->seq);
3251
3252         /*
3253          * If there was nothing to send to user, in spite of consuming trace
3254          * entries, go back to wait for more entries.
3255          */
3256         if (sret == -EBUSY)
3257                 goto waitagain;
3258
3259 out:
3260         mutex_unlock(&iter->mutex);
3261
3262         return sret;
3263 }
3264
3265 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3266                                      struct pipe_buffer *buf)
3267 {
3268         __free_page(buf->page);
3269 }
3270
3271 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3272                                      unsigned int idx)
3273 {
3274         __free_page(spd->pages[idx]);
3275 }
3276
3277 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3278         .can_merge              = 0,
3279         .map                    = generic_pipe_buf_map,
3280         .unmap                  = generic_pipe_buf_unmap,
3281         .confirm                = generic_pipe_buf_confirm,
3282         .release                = tracing_pipe_buf_release,
3283         .steal                  = generic_pipe_buf_steal,
3284         .get                    = generic_pipe_buf_get,
3285 };
3286
3287 static size_t
3288 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3289 {
3290         size_t count;
3291         int ret;
3292
3293         /* Seq buffer is page-sized, exactly what we need. */
3294         for (;;) {
3295                 count = iter->seq.len;
3296                 ret = print_trace_line(iter);
3297                 count = iter->seq.len - count;
3298                 if (rem < count) {
3299                         rem = 0;
3300                         iter->seq.len -= count;
3301                         break;
3302                 }
3303                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3304                         iter->seq.len -= count;
3305                         break;
3306                 }
3307
3308                 if (ret != TRACE_TYPE_NO_CONSUME)
3309                         trace_consume(iter);
3310                 rem -= count;
3311                 if (!trace_find_next_entry_inc(iter))   {
3312                         rem = 0;
3313                         iter->ent = NULL;
3314                         break;
3315                 }
3316         }
3317
3318         return rem;
3319 }
3320
3321 static ssize_t tracing_splice_read_pipe(struct file *filp,
3322                                         loff_t *ppos,
3323                                         struct pipe_inode_info *pipe,
3324                                         size_t len,
3325                                         unsigned int flags)
3326 {
3327         struct page *pages_def[PIPE_DEF_BUFFERS];
3328         struct partial_page partial_def[PIPE_DEF_BUFFERS];
3329         struct trace_iterator *iter = filp->private_data;
3330         struct splice_pipe_desc spd = {
3331                 .pages          = pages_def,
3332                 .partial        = partial_def,
3333                 .nr_pages       = 0, /* This gets updated below. */
3334                 .flags          = flags,
3335                 .ops            = &tracing_pipe_buf_ops,
3336                 .spd_release    = tracing_spd_release_pipe,
3337         };
3338         static struct tracer *old_tracer;
3339         ssize_t ret;
3340         size_t rem;
3341         unsigned int i;
3342
3343         if (splice_grow_spd(pipe, &spd))
3344                 return -ENOMEM;
3345
3346         /* copy the tracer to avoid using a global lock all around */
3347         mutex_lock(&trace_types_lock);
3348         if (unlikely(old_tracer != current_trace && current_trace)) {
3349                 old_tracer = current_trace;
3350                 *iter->trace = *current_trace;
3351         }
3352         mutex_unlock(&trace_types_lock);
3353
3354         mutex_lock(&iter->mutex);
3355
3356         if (iter->trace->splice_read) {
3357                 ret = iter->trace->splice_read(iter, filp,
3358                                                ppos, pipe, len, flags);
3359                 if (ret)
3360                         goto out_err;
3361         }
3362
3363         ret = tracing_wait_pipe(filp);
3364         if (ret <= 0)
3365                 goto out_err;
3366
3367         if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3368                 ret = -EFAULT;
3369                 goto out_err;
3370         }
3371
3372         trace_event_read_lock();
3373         trace_access_lock(iter->cpu_file);
3374
3375         /* Fill as many pages as possible. */
3376         for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3377                 spd.pages[i] = alloc_page(GFP_KERNEL);
3378                 if (!spd.pages[i])
3379                         break;
3380
3381                 rem = tracing_fill_pipe_page(rem, iter);
3382
3383                 /* Copy the data into the page, so we can start over. */
3384                 ret = trace_seq_to_buffer(&iter->seq,
3385                                           page_address(spd.pages[i]),
3386                                           iter->seq.len);
3387                 if (ret < 0) {
3388                         __free_page(spd.pages[i]);
3389                         break;
3390                 }
3391                 spd.partial[i].offset = 0;
3392                 spd.partial[i].len = iter->seq.len;
3393
3394                 trace_seq_init(&iter->seq);
3395         }
3396
3397         trace_access_unlock(iter->cpu_file);
3398         trace_event_read_unlock();
3399         mutex_unlock(&iter->mutex);
3400
3401         spd.nr_pages = i;
3402
3403         ret = splice_to_pipe(pipe, &spd);
3404 out:
3405         splice_shrink_spd(pipe, &spd);
3406         return ret;
3407
3408 out_err:
3409         mutex_unlock(&iter->mutex);
3410         goto out;
3411 }
3412
3413 static ssize_t
3414 tracing_entries_read(struct file *filp, char __user *ubuf,
3415                      size_t cnt, loff_t *ppos)
3416 {
3417         struct trace_array *tr = filp->private_data;
3418         char buf[96];
3419         int r;
3420
3421         mutex_lock(&trace_types_lock);
3422         if (!ring_buffer_expanded)
3423                 r = sprintf(buf, "%lu (expanded: %lu)\n",
3424                             tr->entries >> 10,
3425                             trace_buf_size >> 10);
3426         else
3427                 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3428         mutex_unlock(&trace_types_lock);
3429
3430         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3431 }
3432
3433 static ssize_t
3434 tracing_entries_write(struct file *filp, const char __user *ubuf,
3435                       size_t cnt, loff_t *ppos)
3436 {
3437         unsigned long val;
3438         char buf[64];
3439         int ret, cpu;
3440
3441         if (cnt >= sizeof(buf))
3442                 return -EINVAL;
3443
3444         if (copy_from_user(&buf, ubuf, cnt))
3445                 return -EFAULT;
3446
3447         buf[cnt] = 0;
3448
3449         ret = strict_strtoul(buf, 10, &val);
3450         if (ret < 0)
3451                 return ret;
3452
3453         /* must have at least 1 entry */
3454         if (!val)
3455                 return -EINVAL;
3456
3457         mutex_lock(&trace_types_lock);
3458
3459         tracing_stop();
3460
3461         /* disable all cpu buffers */
3462         for_each_tracing_cpu(cpu) {
3463                 if (global_trace.data[cpu])
3464                         atomic_inc(&global_trace.data[cpu]->disabled);
3465                 if (max_tr.data[cpu])
3466                         atomic_inc(&max_tr.data[cpu]->disabled);
3467         }
3468
3469         /* value is in KB */
3470         val <<= 10;
3471
3472         if (val != global_trace.entries) {
3473                 ret = tracing_resize_ring_buffer(val);
3474                 if (ret < 0) {
3475                         cnt = ret;
3476                         goto out;
3477                 }
3478         }
3479
3480         *ppos += cnt;
3481
3482         /* If check pages failed, return ENOMEM */
3483         if (tracing_disabled)
3484                 cnt = -ENOMEM;
3485  out:
3486         for_each_tracing_cpu(cpu) {
3487                 if (global_trace.data[cpu])
3488                         atomic_dec(&global_trace.data[cpu]->disabled);
3489                 if (max_tr.data[cpu])
3490                         atomic_dec(&max_tr.data[cpu]->disabled);
3491         }
3492
3493         tracing_start();
3494         mutex_unlock(&trace_types_lock);
3495
3496         return cnt;
3497 }
3498
3499 static int mark_printk(const char *fmt, ...)
3500 {
3501         int ret;
3502         va_list args;
3503         va_start(args, fmt);
3504         ret = trace_vprintk(0, fmt, args);
3505         va_end(args);
3506         return ret;
3507 }
3508
3509 static ssize_t
3510 tracing_mark_write(struct file *filp, const char __user *ubuf,
3511                                         size_t cnt, loff_t *fpos)
3512 {
3513         char *buf;
3514         size_t written;
3515
3516         if (tracing_disabled)
3517                 return -EINVAL;
3518
3519         if (cnt > TRACE_BUF_SIZE)
3520                 cnt = TRACE_BUF_SIZE;
3521
3522         buf = kmalloc(cnt + 2, GFP_KERNEL);
3523         if (buf == NULL)
3524                 return -ENOMEM;
3525
3526         if (copy_from_user(buf, ubuf, cnt)) {
3527                 kfree(buf);
3528                 return -EFAULT;
3529         }
3530         if (buf[cnt-1] != '\n') {
3531                 buf[cnt] = '\n';
3532                 buf[cnt+1] = '\0';
3533         } else
3534                 buf[cnt] = '\0';
3535
3536         written = mark_printk("%s", buf);
3537         kfree(buf);
3538         *fpos += written;
3539
3540         /* don't tell userspace we wrote more - it might confuse them */
3541         if (written > cnt)
3542                 written = cnt;
3543
3544         return written;
3545 }
3546
3547 static int tracing_clock_show(struct seq_file *m, void *v)
3548 {
3549         int i;
3550
3551         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3552                 seq_printf(m,
3553                         "%s%s%s%s", i ? " " : "",
3554                         i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3555                         i == trace_clock_id ? "]" : "");
3556         seq_putc(m, '\n');
3557
3558         return 0;
3559 }
3560
3561 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3562                                    size_t cnt, loff_t *fpos)
3563 {
3564         char buf[64];
3565         const char *clockstr;
3566         int i;
3567
3568         if (cnt >= sizeof(buf))
3569                 return -EINVAL;
3570
3571         if (copy_from_user(&buf, ubuf, cnt))
3572                 return -EFAULT;
3573
3574         buf[cnt] = 0;
3575
3576         clockstr = strstrip(buf);
3577
3578         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3579                 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3580                         break;
3581         }
3582         if (i == ARRAY_SIZE(trace_clocks))
3583                 return -EINVAL;
3584
3585         trace_clock_id = i;
3586
3587         mutex_lock(&trace_types_lock);
3588
3589         ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3590         if (max_tr.buffer)
3591                 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3592
3593         mutex_unlock(&trace_types_lock);
3594
3595         *fpos += cnt;
3596
3597         return cnt;
3598 }
3599
3600 static int tracing_clock_open(struct inode *inode, struct file *file)
3601 {
3602         if (tracing_disabled)
3603                 return -ENODEV;
3604         return single_open(file, tracing_clock_show, NULL);
3605 }
3606
3607 static const struct file_operations tracing_max_lat_fops = {
3608         .open           = tracing_open_generic,
3609         .read           = tracing_max_lat_read,
3610         .write          = tracing_max_lat_write,
3611         .llseek         = generic_file_llseek,
3612 };
3613
3614 static const struct file_operations tracing_ctrl_fops = {
3615         .open           = tracing_open_generic,
3616         .read           = tracing_ctrl_read,
3617         .write          = tracing_ctrl_write,
3618         .llseek         = generic_file_llseek,
3619 };
3620
3621 static const struct file_operations set_tracer_fops = {
3622         .open           = tracing_open_generic,
3623         .read           = tracing_set_trace_read,
3624         .write          = tracing_set_trace_write,
3625         .llseek         = generic_file_llseek,
3626 };
3627
3628 static const struct file_operations tracing_pipe_fops = {
3629         .open           = tracing_open_pipe,
3630         .poll           = tracing_poll_pipe,
3631         .read           = tracing_read_pipe,
3632         .splice_read    = tracing_splice_read_pipe,
3633         .release        = tracing_release_pipe,
3634         .llseek         = no_llseek,
3635 };
3636
3637 static const struct file_operations tracing_entries_fops = {
3638         .open           = tracing_open_generic,
3639         .read           = tracing_entries_read,
3640         .write          = tracing_entries_write,
3641         .llseek         = generic_file_llseek,
3642 };
3643
3644 static const struct file_operations tracing_mark_fops = {
3645         .open           = tracing_open_generic,
3646         .write          = tracing_mark_write,
3647         .llseek         = generic_file_llseek,
3648 };
3649
3650 static const struct file_operations trace_clock_fops = {
3651         .open           = tracing_clock_open,
3652         .read           = seq_read,
3653         .llseek         = seq_lseek,
3654         .release        = single_release,
3655         .write          = tracing_clock_write,
3656 };
3657
3658 struct ftrace_buffer_info {
3659         struct trace_array      *tr;
3660         void                    *spare;
3661         int                     cpu;
3662         unsigned int            read;
3663 };
3664
3665 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3666 {
3667         int cpu = (int)(long)inode->i_private;
3668         struct ftrace_buffer_info *info;
3669
3670         if (tracing_disabled)
3671                 return -ENODEV;
3672
3673         info = kzalloc(sizeof(*info), GFP_KERNEL);
3674         if (!info)
3675                 return -ENOMEM;
3676
3677         info->tr        = &global_trace;
3678         info->cpu       = cpu;
3679         info->spare     = NULL;
3680         /* Force reading ring buffer for first read */
3681         info->read      = (unsigned int)-1;
3682
3683         filp->private_data = info;
3684
3685         return nonseekable_open(inode, filp);
3686 }
3687
3688 static ssize_t
3689 tracing_buffers_read(struct file *filp, char __user *ubuf,
3690                      size_t count, loff_t *ppos)
3691 {
3692         struct ftrace_buffer_info *info = filp->private_data;
3693         ssize_t ret;
3694         size_t size;
3695
3696         if (!count)
3697                 return 0;
3698
3699         if (!info->spare)
3700                 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3701         if (!info->spare)
3702                 return -ENOMEM;
3703
3704         /* Do we have previous read data to read? */
3705         if (info->read < PAGE_SIZE)
3706                 goto read;
3707
3708         info->read = 0;
3709
3710         trace_access_lock(info->cpu);
3711         ret = ring_buffer_read_page(info->tr->buffer,
3712                                     &info->spare,
3713                                     count,
3714                                     info->cpu, 0);
3715         trace_access_unlock(info->cpu);
3716         if (ret < 0)
3717                 return 0;
3718
3719 read:
3720         size = PAGE_SIZE - info->read;
3721         if (size > count)
3722                 size = count;
3723
3724         ret = copy_to_user(ubuf, info->spare + info->read, size);
3725         if (ret == size)
3726                 return -EFAULT;
3727         size -= ret;
3728
3729         *ppos += size;
3730         info->read += size;
3731
3732         return size;
3733 }
3734
3735 static int tracing_buffers_release(struct inode *inode, struct file *file)
3736 {
3737         struct ftrace_buffer_info *info = file->private_data;
3738
3739         if (info->spare)
3740                 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3741         kfree(info);
3742
3743         return 0;
3744 }
3745
3746 struct buffer_ref {
3747         struct ring_buffer      *buffer;
3748         void                    *page;
3749         int                     ref;
3750 };
3751
3752 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3753                                     struct pipe_buffer *buf)
3754 {
3755         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3756
3757         if (--ref->ref)
3758                 return;
3759
3760         ring_buffer_free_read_page(ref->buffer, ref->page);
3761         kfree(ref);
3762         buf->private = 0;
3763 }
3764
3765 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3766                                  struct pipe_buffer *buf)
3767 {
3768         return 1;
3769 }
3770
3771 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3772                                 struct pipe_buffer *buf)
3773 {
3774         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3775
3776         ref->ref++;
3777 }
3778
3779 /* Pipe buffer operations for a buffer. */
3780 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
3781         .can_merge              = 0,
3782         .map                    = generic_pipe_buf_map,
3783         .unmap                  = generic_pipe_buf_unmap,
3784         .confirm                = generic_pipe_buf_confirm,
3785         .release                = buffer_pipe_buf_release,
3786         .steal                  = buffer_pipe_buf_steal,
3787         .get                    = buffer_pipe_buf_get,
3788 };
3789
3790 /*
3791  * Callback from splice_to_pipe(), if we need to release some pages
3792  * at the end of the spd in case we error'ed out in filling the pipe.
3793  */
3794 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3795 {
3796         struct buffer_ref *ref =
3797                 (struct buffer_ref *)spd->partial[i].private;
3798
3799         if (--ref->ref)
3800                 return;
3801
3802         ring_buffer_free_read_page(ref->buffer, ref->page);
3803         kfree(ref);
3804         spd->partial[i].private = 0;
3805 }
3806
3807 static ssize_t
3808 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3809                             struct pipe_inode_info *pipe, size_t len,
3810                             unsigned int flags)
3811 {
3812         struct ftrace_buffer_info *info = file->private_data;
3813         struct partial_page partial_def[PIPE_DEF_BUFFERS];
3814         struct page *pages_def[PIPE_DEF_BUFFERS];
3815         struct splice_pipe_desc spd = {
3816                 .pages          = pages_def,
3817                 .partial        = partial_def,
3818                 .flags          = flags,
3819                 .ops            = &buffer_pipe_buf_ops,
3820                 .spd_release    = buffer_spd_release,
3821         };
3822         struct buffer_ref *ref;
3823         int entries, size, i;
3824         size_t ret;
3825
3826         if (splice_grow_spd(pipe, &spd))
3827                 return -ENOMEM;
3828
3829         if (*ppos & (PAGE_SIZE - 1)) {
3830                 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3831                 ret = -EINVAL;
3832                 goto out;
3833         }
3834
3835         if (len & (PAGE_SIZE - 1)) {
3836                 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3837                 if (len < PAGE_SIZE) {
3838                         ret = -EINVAL;
3839                         goto out;
3840                 }
3841                 len &= PAGE_MASK;
3842         }
3843
3844         trace_access_lock(info->cpu);
3845         entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3846
3847         for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
3848                 struct page *page;
3849                 int r;
3850
3851                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3852                 if (!ref)
3853                         break;
3854
3855                 ref->ref = 1;
3856                 ref->buffer = info->tr->buffer;
3857                 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3858                 if (!ref->page) {
3859                         kfree(ref);
3860                         break;
3861                 }
3862
3863                 r = ring_buffer_read_page(ref->buffer, &ref->page,
3864                                           len, info->cpu, 1);
3865                 if (r < 0) {
3866                         ring_buffer_free_read_page(ref->buffer,
3867                                                    ref->page);
3868                         kfree(ref);
3869                         break;
3870                 }
3871
3872                 /*
3873                  * zero out any left over data, this is going to
3874                  * user land.
3875                  */
3876                 size = ring_buffer_page_len(ref->page);
3877                 if (size < PAGE_SIZE)
3878                         memset(ref->page + size, 0, PAGE_SIZE - size);
3879
3880                 page = virt_to_page(ref->page);
3881
3882                 spd.pages[i] = page;
3883                 spd.partial[i].len = PAGE_SIZE;
3884                 spd.partial[i].offset = 0;
3885                 spd.partial[i].private = (unsigned long)ref;
3886                 spd.nr_pages++;
3887                 *ppos += PAGE_SIZE;
3888
3889                 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3890         }
3891
3892         trace_access_unlock(info->cpu);
3893         spd.nr_pages = i;
3894
3895         /* did we read anything? */
3896         if (!spd.nr_pages) {
3897                 if (flags & SPLICE_F_NONBLOCK)
3898                         ret = -EAGAIN;
3899                 else
3900                         ret = 0;
3901                 /* TODO: block */
3902                 goto out;
3903         }
3904
3905         ret = splice_to_pipe(pipe, &spd);
3906         splice_shrink_spd(pipe, &spd);
3907 out:
3908         return ret;
3909 }
3910
3911 static const struct file_operations tracing_buffers_fops = {
3912         .open           = tracing_buffers_open,
3913         .read           = tracing_buffers_read,
3914         .release        = tracing_buffers_release,
3915         .splice_read    = tracing_buffers_splice_read,
3916         .llseek         = no_llseek,
3917 };
3918
3919 static ssize_t
3920 tracing_stats_read(struct file *filp, char __user *ubuf,
3921                    size_t count, loff_t *ppos)
3922 {
3923         unsigned long cpu = (unsigned long)filp->private_data;
3924         struct trace_array *tr = &global_trace;
3925         struct trace_seq *s;
3926         unsigned long cnt;
3927
3928         s = kmalloc(sizeof(*s), GFP_KERNEL);
3929         if (!s)
3930                 return -ENOMEM;
3931
3932         trace_seq_init(s);
3933
3934         cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3935         trace_seq_printf(s, "entries: %ld\n", cnt);
3936
3937         cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3938         trace_seq_printf(s, "overrun: %ld\n", cnt);
3939
3940         cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3941         trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3942
3943         count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3944
3945         kfree(s);
3946
3947         return count;
3948 }
3949
3950 static const struct file_operations tracing_stats_fops = {
3951         .open           = tracing_open_generic,
3952         .read           = tracing_stats_read,
3953         .llseek         = generic_file_llseek,
3954 };
3955
3956 #ifdef CONFIG_DYNAMIC_FTRACE
3957
3958 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3959 {
3960         return 0;
3961 }
3962
3963 static ssize_t
3964 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3965                   size_t cnt, loff_t *ppos)
3966 {
3967         static char ftrace_dyn_info_buffer[1024];
3968         static DEFINE_MUTEX(dyn_info_mutex);
3969         unsigned long *p = filp->private_data;
3970         char *buf = ftrace_dyn_info_buffer;
3971         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3972         int r;
3973
3974         mutex_lock(&dyn_info_mutex);
3975         r = sprintf(buf, "%ld ", *p);
3976
3977         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3978         buf[r++] = '\n';
3979
3980         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3981
3982         mutex_unlock(&dyn_info_mutex);
3983
3984         return r;
3985 }
3986
3987 static const struct file_operations tracing_dyn_info_fops = {
3988         .open           = tracing_open_generic,
3989         .read           = tracing_read_dyn_info,
3990         .llseek         = generic_file_llseek,
3991 };
3992 #endif
3993
3994 static struct dentry *d_tracer;
3995
3996 struct dentry *tracing_init_dentry(void)
3997 {
3998         static int once;
3999
4000         if (d_tracer)
4001                 return d_tracer;
4002
4003         if (!debugfs_initialized())
4004                 return NULL;
4005
4006         d_tracer = debugfs_create_dir("tracing", NULL);
4007
4008         if (!d_tracer && !once) {
4009                 once = 1;
4010                 pr_warning("Could not create debugfs directory 'tracing'\n");
4011                 return NULL;
4012         }
4013
4014         return d_tracer;
4015 }
4016
4017 static struct dentry *d_percpu;
4018
4019 struct dentry *tracing_dentry_percpu(void)
4020 {
4021         static int once;
4022         struct dentry *d_tracer;
4023
4024         if (d_percpu)
4025                 return d_percpu;
4026
4027         d_tracer = tracing_init_dentry();
4028
4029         if (!d_tracer)
4030                 return NULL;
4031
4032         d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4033
4034         if (!d_percpu && !once) {
4035                 once = 1;
4036                 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4037                 return NULL;
4038         }
4039
4040         return d_percpu;
4041 }
4042
4043 static void tracing_init_debugfs_percpu(long cpu)
4044 {
4045         struct dentry *d_percpu = tracing_dentry_percpu();
4046         struct dentry *d_cpu;
4047         char cpu_dir[30]; /* 30 characters should be more than enough */
4048
4049         snprintf(cpu_dir, 30, "cpu%ld", cpu);
4050         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4051         if (!d_cpu) {
4052                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4053                 return;
4054         }
4055
4056         /* per cpu trace_pipe */
4057         trace_create_file("trace_pipe", 0444, d_cpu,
4058                         (void *) cpu, &tracing_pipe_fops);
4059
4060         /* per cpu trace */
4061         trace_create_file("trace", 0644, d_cpu,
4062                         (void *) cpu, &tracing_fops);
4063
4064         trace_create_file("trace_pipe_raw", 0444, d_cpu,
4065                         (void *) cpu, &tracing_buffers_fops);
4066
4067         trace_create_file("stats", 0444, d_cpu,
4068                         (void *) cpu, &tracing_stats_fops);
4069 }
4070
4071 #ifdef CONFIG_FTRACE_SELFTEST
4072 /* Let selftest have access to static functions in this file */
4073 #include "trace_selftest.c"
4074 #endif
4075
4076 struct trace_option_dentry {
4077         struct tracer_opt               *opt;
4078         struct tracer_flags             *flags;
4079         struct dentry                   *entry;
4080 };
4081
4082 static ssize_t
4083 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4084                         loff_t *ppos)
4085 {
4086         struct trace_option_dentry *topt = filp->private_data;
4087         char *buf;
4088
4089         if (topt->flags->val & topt->opt->bit)
4090                 buf = "1\n";
4091         else
4092                 buf = "0\n";
4093
4094         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4095 }
4096
4097 static ssize_t
4098 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4099                          loff_t *ppos)
4100 {
4101         struct trace_option_dentry *topt = filp->private_data;
4102         unsigned long val;
4103         char buf[64];
4104         int ret;
4105
4106         if (cnt >= sizeof(buf))
4107                 return -EINVAL;
4108
4109         if (copy_from_user(&buf, ubuf, cnt))
4110                 return -EFAULT;
4111
4112         buf[cnt] = 0;
4113
4114         ret = strict_strtoul(buf, 10, &val);
4115         if (ret < 0)
4116                 return ret;
4117
4118         if (val != 0 && val != 1)
4119                 return -EINVAL;
4120
4121         if (!!(topt->flags->val & topt->opt->bit) != val) {
4122                 mutex_lock(&trace_types_lock);
4123                 ret = __set_tracer_option(current_trace, topt->flags,
4124                                           topt->opt, !val);
4125                 mutex_unlock(&trace_types_lock);
4126                 if (ret)
4127                         return ret;
4128         }
4129
4130         *ppos += cnt;
4131
4132         return cnt;
4133 }
4134
4135
4136 static const struct file_operations trace_options_fops = {
4137         .open = tracing_open_generic,
4138         .read = trace_options_read,
4139         .write = trace_options_write,
4140         .llseek = generic_file_llseek,
4141 };
4142
4143 static ssize_t
4144 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4145                         loff_t *ppos)
4146 {
4147         long index = (long)filp->private_data;
4148         char *buf;
4149
4150         if (trace_flags & (1 << index))
4151                 buf = "1\n";
4152         else
4153                 buf = "0\n";
4154
4155         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4156 }
4157
4158 static ssize_t
4159 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4160                          loff_t *ppos)
4161 {
4162         long index = (long)filp->private_data;
4163         char buf[64];
4164         unsigned long val;
4165         int ret;
4166
4167         if (cnt >= sizeof(buf))
4168                 return -EINVAL;
4169
4170         if (copy_from_user(&buf, ubuf, cnt))
4171                 return -EFAULT;
4172
4173         buf[cnt] = 0;
4174
4175         ret = strict_strtoul(buf, 10, &val);
4176         if (ret < 0)
4177                 return ret;
4178
4179         if (val != 0 && val != 1)
4180                 return -EINVAL;
4181         set_tracer_flags(1 << index, val);
4182
4183         *ppos += cnt;
4184
4185         return cnt;
4186 }
4187
4188 static const struct file_operations trace_options_core_fops = {
4189         .open = tracing_open_generic,
4190         .read = trace_options_core_read,
4191         .write = trace_options_core_write,
4192         .llseek = generic_file_llseek,
4193 };
4194
4195 struct dentry *trace_create_file(const char *name,
4196                                  mode_t mode,
4197                                  struct dentry *parent,
4198                                  void *data,
4199                                  const struct file_operations *fops)
4200 {
4201         struct dentry *ret;
4202
4203         ret = debugfs_create_file(name, mode, parent, data, fops);
4204         if (!ret)
4205                 pr_warning("Could not create debugfs '%s' entry\n", name);
4206
4207         return ret;
4208 }
4209
4210
4211 static struct dentry *trace_options_init_dentry(void)
4212 {
4213         struct dentry *d_tracer;
4214         static struct dentry *t_options;
4215
4216         if (t_options)
4217                 return t_options;
4218
4219         d_tracer = tracing_init_dentry();
4220         if (!d_tracer)
4221                 return NULL;
4222
4223         t_options = debugfs_create_dir("options", d_tracer);
4224         if (!t_options) {
4225                 pr_warning("Could not create debugfs directory 'options'\n");
4226                 return NULL;
4227         }
4228
4229         return t_options;
4230 }
4231
4232 static void
4233 create_trace_option_file(struct trace_option_dentry *topt,
4234                          struct tracer_flags *flags,
4235                          struct tracer_opt *opt)
4236 {
4237         struct dentry *t_options;
4238
4239         t_options = trace_options_init_dentry();
4240         if (!t_options)
4241                 return;
4242
4243         topt->flags = flags;
4244         topt->opt = opt;
4245
4246         topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4247                                     &trace_options_fops);
4248
4249 }
4250
4251 static struct trace_option_dentry *
4252 create_trace_option_files(struct tracer *tracer)
4253 {
4254         struct trace_option_dentry *topts;
4255         struct tracer_flags *flags;
4256         struct tracer_opt *opts;
4257         int cnt;
4258
4259         if (!tracer)
4260                 return NULL;
4261
4262         flags = tracer->flags;
4263
4264         if (!flags || !flags->opts)
4265                 return NULL;
4266
4267         opts = flags->opts;
4268
4269         for (cnt = 0; opts[cnt].name; cnt++)
4270                 ;
4271
4272         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4273         if (!topts)
4274                 return NULL;
4275
4276         for (cnt = 0; opts[cnt].name; cnt++)
4277                 create_trace_option_file(&topts[cnt], flags,
4278                                          &opts[cnt]);
4279
4280         return topts;
4281 }
4282
4283 static void
4284 destroy_trace_option_files(struct trace_option_dentry *topts)
4285 {
4286         int cnt;
4287
4288         if (!topts)
4289                 return;
4290
4291         for (cnt = 0; topts[cnt].opt; cnt++) {
4292                 if (topts[cnt].entry)
4293                         debugfs_remove(topts[cnt].entry);
4294         }
4295
4296         kfree(topts);
4297 }
4298
4299 static struct dentry *
4300 create_trace_option_core_file(const char *option, long index)
4301 {
4302         struct dentry *t_options;
4303
4304         t_options = trace_options_init_dentry();
4305         if (!t_options)
4306                 return NULL;
4307
4308         return trace_create_file(option, 0644, t_options, (void *)index,
4309                                     &trace_options_core_fops);
4310 }
4311
4312 static __init void create_trace_options_dir(void)
4313 {
4314         struct dentry *t_options;
4315         int i;
4316
4317         t_options = trace_options_init_dentry();
4318         if (!t_options)
4319                 return;
4320
4321         for (i = 0; trace_options[i]; i++)
4322                 create_trace_option_core_file(trace_options[i], i);
4323 }
4324
4325 static __init int tracer_init_debugfs(void)
4326 {
4327         struct dentry *d_tracer;
4328         int cpu;
4329
4330         trace_access_lock_init();
4331
4332         d_tracer = tracing_init_dentry();
4333
4334         trace_create_file("tracing_enabled", 0644, d_tracer,
4335                         &global_trace, &tracing_ctrl_fops);
4336
4337         trace_create_file("trace_options", 0644, d_tracer,
4338                         NULL, &tracing_iter_fops);
4339
4340         trace_create_file("tracing_cpumask", 0644, d_tracer,
4341                         NULL, &tracing_cpumask_fops);
4342
4343         trace_create_file("trace", 0644, d_tracer,
4344                         (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4345
4346         trace_create_file("available_tracers", 0444, d_tracer,
4347                         &global_trace, &show_traces_fops);
4348
4349         trace_create_file("current_tracer", 0644, d_tracer,
4350                         &global_trace, &set_tracer_fops);
4351
4352 #ifdef CONFIG_TRACER_MAX_TRACE
4353         trace_create_file("tracing_max_latency", 0644, d_tracer,
4354                         &tracing_max_latency, &tracing_max_lat_fops);
4355 #endif
4356
4357         trace_create_file("tracing_thresh", 0644, d_tracer,
4358                         &tracing_thresh, &tracing_max_lat_fops);
4359
4360         trace_create_file("README", 0444, d_tracer,
4361                         NULL, &tracing_readme_fops);
4362
4363         trace_create_file("trace_pipe", 0444, d_tracer,
4364                         (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4365
4366         trace_create_file("buffer_size_kb", 0644, d_tracer,
4367                         &global_trace, &tracing_entries_fops);
4368
4369         trace_create_file("trace_marker", 0220, d_tracer,
4370                         NULL, &tracing_mark_fops);
4371
4372         trace_create_file("saved_cmdlines", 0444, d_tracer,
4373                         NULL, &tracing_saved_cmdlines_fops);
4374
4375         trace_create_file("trace_clock", 0644, d_tracer, NULL,
4376                           &trace_clock_fops);
4377
4378 #ifdef CONFIG_DYNAMIC_FTRACE
4379         trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4380                         &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4381 #endif
4382
4383         create_trace_options_dir();
4384
4385         for_each_tracing_cpu(cpu)
4386                 tracing_init_debugfs_percpu(cpu);
4387
4388         return 0;
4389 }
4390
4391 static int trace_panic_handler(struct notifier_block *this,
4392                                unsigned long event, void *unused)
4393 {
4394         if (ftrace_dump_on_oops)
4395                 ftrace_dump(ftrace_dump_on_oops);
4396         return NOTIFY_OK;
4397 }
4398
4399 static struct notifier_block trace_panic_notifier = {
4400         .notifier_call  = trace_panic_handler,
4401         .next           = NULL,
4402         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
4403 };
4404
4405 static int trace_die_handler(struct notifier_block *self,
4406                              unsigned long val,
4407                              void *data)
4408 {
4409         switch (val) {
4410         case DIE_OOPS:
4411                 if (ftrace_dump_on_oops)
4412                         ftrace_dump(ftrace_dump_on_oops);
4413                 break;
4414         default:
4415                 break;
4416         }
4417         return NOTIFY_OK;
4418 }
4419
4420 static struct notifier_block trace_die_notifier = {
4421         .notifier_call = trace_die_handler,
4422         .priority = 200
4423 };
4424
4425 /*
4426  * printk is set to max of 1024, we really don't need it that big.
4427  * Nothing should be printing 1000 characters anyway.
4428  */
4429 #define TRACE_MAX_PRINT         1000
4430
4431 /*
4432  * Define here KERN_TRACE so that we have one place to modify
4433  * it if we decide to change what log level the ftrace dump
4434  * should be at.
4435  */
4436 #define KERN_TRACE              KERN_EMERG
4437
4438 void
4439 trace_printk_seq(struct trace_seq *s)
4440 {
4441         /* Probably should print a warning here. */
4442         if (s->len >= 1000)
4443                 s->len = 1000;
4444
4445         /* should be zero ended, but we are paranoid. */
4446         s->buffer[s->len] = 0;
4447
4448         printk(KERN_TRACE "%s", s->buffer);
4449
4450         trace_seq_init(s);
4451 }
4452
4453 void trace_init_global_iter(struct trace_iterator *iter)
4454 {
4455         iter->tr = &global_trace;
4456         iter->trace = current_trace;
4457         iter->cpu_file = TRACE_PIPE_ALL_CPU;
4458 }
4459
4460 static void
4461 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
4462 {
4463         static arch_spinlock_t ftrace_dump_lock =
4464                 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
4465         /* use static because iter can be a bit big for the stack */
4466         static struct trace_iterator iter;
4467         unsigned int old_userobj;
4468         static int dump_ran;
4469         unsigned long flags;
4470         int cnt = 0, cpu;
4471
4472         /* only one dump */
4473         local_irq_save(flags);
4474         arch_spin_lock(&ftrace_dump_lock);
4475         if (dump_ran)
4476                 goto out;
4477
4478         dump_ran = 1;
4479
4480         tracing_off();
4481
4482         if (disable_tracing)
4483                 ftrace_kill();
4484
4485         trace_init_global_iter(&iter);
4486
4487         for_each_tracing_cpu(cpu) {
4488                 atomic_inc(&iter.tr->data[cpu]->disabled);
4489         }
4490
4491         old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4492
4493         /* don't look at user memory in panic mode */
4494         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4495
4496         /* Simulate the iterator */
4497         iter.tr = &global_trace;
4498         iter.trace = current_trace;
4499
4500         switch (oops_dump_mode) {
4501         case DUMP_ALL:
4502                 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4503                 break;
4504         case DUMP_ORIG:
4505                 iter.cpu_file = raw_smp_processor_id();
4506                 break;
4507         case DUMP_NONE:
4508                 goto out_enable;
4509         default:
4510                 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4511                 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4512         }
4513
4514         printk(KERN_TRACE "Dumping ftrace buffer:\n");
4515
4516         /*
4517          * We need to stop all tracing on all CPUS to read the
4518          * the next buffer. This is a bit expensive, but is
4519          * not done often. We fill all what we can read,
4520          * and then release the locks again.
4521          */
4522
4523         while (!trace_empty(&iter)) {
4524
4525                 if (!cnt)
4526                         printk(KERN_TRACE "---------------------------------\n");
4527
4528                 cnt++;
4529
4530                 /* reset all but tr, trace, and overruns */
4531                 memset(&iter.seq, 0,
4532                        sizeof(struct trace_iterator) -
4533                        offsetof(struct trace_iterator, seq));
4534                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4535                 iter.pos = -1;
4536
4537                 if (trace_find_next_entry_inc(&iter) != NULL) {
4538                         int ret;
4539
4540                         ret = print_trace_line(&iter);
4541                         if (ret != TRACE_TYPE_NO_CONSUME)
4542                                 trace_consume(&iter);
4543                 }
4544
4545                 trace_printk_seq(&iter.seq);
4546         }
4547
4548         if (!cnt)
4549                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
4550         else
4551                 printk(KERN_TRACE "---------------------------------\n");
4552
4553  out_enable:
4554         /* Re-enable tracing if requested */
4555         if (!disable_tracing) {
4556                 trace_flags |= old_userobj;
4557
4558                 for_each_tracing_cpu(cpu) {
4559                         atomic_dec(&iter.tr->data[cpu]->disabled);
4560                 }
4561                 tracing_on();
4562         }
4563
4564  out:
4565         arch_spin_unlock(&ftrace_dump_lock);
4566         local_irq_restore(flags);
4567 }
4568
4569 /* By default: disable tracing after the dump */
4570 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
4571 {
4572         __ftrace_dump(true, oops_dump_mode);
4573 }
4574
4575 __init static int tracer_alloc_buffers(void)
4576 {
4577         int ring_buf_size;
4578         enum ring_buffer_flags rb_flags;
4579         int i;
4580         int ret = -ENOMEM;
4581
4582
4583         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4584                 goto out;
4585
4586         if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4587                 goto out_free_buffer_mask;
4588
4589         /* To save memory, keep the ring buffer size to its minimum */
4590         if (ring_buffer_expanded)
4591                 ring_buf_size = trace_buf_size;
4592         else
4593                 ring_buf_size = 1;
4594
4595         rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4596
4597         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4598         cpumask_copy(tracing_cpumask, cpu_all_mask);
4599
4600         /* TODO: make the number of buffers hot pluggable with CPUS */
4601         global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
4602         if (!global_trace.buffer) {
4603                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4604                 WARN_ON(1);
4605                 goto out_free_cpumask;
4606         }
4607         global_trace.entries = ring_buffer_size(global_trace.buffer);
4608
4609
4610 #ifdef CONFIG_TRACER_MAX_TRACE
4611         max_tr.buffer = ring_buffer_alloc(1, rb_flags);
4612         if (!max_tr.buffer) {
4613                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4614                 WARN_ON(1);
4615                 ring_buffer_free(global_trace.buffer);
4616                 goto out_free_cpumask;
4617         }
4618         max_tr.entries = 1;
4619 #endif
4620
4621         /* Allocate the first page for all buffers */
4622         for_each_tracing_cpu(i) {
4623                 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4624                 max_tr.data[i] = &per_cpu(max_tr_data, i);
4625         }
4626
4627         trace_init_cmdlines();
4628
4629         register_tracer(&nop_trace);
4630         current_trace = &nop_trace;
4631         /* All seems OK, enable tracing */
4632         tracing_disabled = 0;
4633
4634         atomic_notifier_chain_register(&panic_notifier_list,
4635                                        &trace_panic_notifier);
4636
4637         register_die_notifier(&trace_die_notifier);
4638
4639         return 0;
4640
4641 out_free_cpumask:
4642         free_cpumask_var(tracing_cpumask);
4643 out_free_buffer_mask:
4644         free_cpumask_var(tracing_buffer_mask);
4645 out:
4646         return ret;
4647 }
4648
4649 __init static int clear_boot_tracer(void)
4650 {
4651         /*
4652          * The default tracer at boot buffer is an init section.
4653          * This function is called in lateinit. If we did not
4654          * find the boot tracer, then clear it out, to prevent
4655          * later registration from accessing the buffer that is
4656          * about to be freed.
4657          */
4658         if (!default_bootup_tracer)
4659                 return 0;
4660
4661         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4662                default_bootup_tracer);
4663         default_bootup_tracer = NULL;
4664
4665         return 0;
4666 }
4667
4668 early_initcall(tracer_alloc_buffers);
4669 fs_initcall(tracer_init_debugfs);
4670 late_initcall(clear_boot_tracer);