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