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