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