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