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