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