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