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