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