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