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