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