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