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