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