mmiotrace: handle TRACE_PRINT entries
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
31 #include <linux/fs.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
34
35 #include <linux/stacktrace.h>
36
37 #include "trace.h"
38
39 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
40 unsigned long __read_mostly     tracing_thresh;
41
42 static unsigned long __read_mostly      tracing_nr_buffers;
43 static cpumask_t __read_mostly          tracing_buffer_mask;
44
45 #define for_each_tracing_cpu(cpu)       \
46         for_each_cpu_mask(cpu, tracing_buffer_mask)
47
48 static int trace_alloc_page(void);
49 static int trace_free_page(void);
50
51 static int tracing_disabled = 1;
52
53 static unsigned long tracing_pages_allocated;
54
55 long
56 ns2usecs(cycle_t nsec)
57 {
58         nsec += 500;
59         do_div(nsec, 1000);
60         return nsec;
61 }
62
63 cycle_t ftrace_now(int cpu)
64 {
65         return cpu_clock(cpu);
66 }
67
68 /*
69  * The global_trace is the descriptor that holds the tracing
70  * buffers for the live tracing. For each CPU, it contains
71  * a link list of pages that will store trace entries. The
72  * page descriptor of the pages in the memory is used to hold
73  * the link list by linking the lru item in the page descriptor
74  * to each of the pages in the buffer per CPU.
75  *
76  * For each active CPU there is a data field that holds the
77  * pages for the buffer for that CPU. Each CPU has the same number
78  * of pages allocated for its buffer.
79  */
80 static struct trace_array       global_trace;
81
82 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
83
84 /*
85  * The max_tr is used to snapshot the global_trace when a maximum
86  * latency is reached. Some tracers will use this to store a maximum
87  * trace while it continues examining live traces.
88  *
89  * The buffers for the max_tr are set up the same as the global_trace.
90  * When a snapshot is taken, the link list of the max_tr is swapped
91  * with the link list of the global_trace and the buffers are reset for
92  * the global_trace so the tracing can continue.
93  */
94 static struct trace_array       max_tr;
95
96 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
97
98 /* tracer_enabled is used to toggle activation of a tracer */
99 static int                      tracer_enabled = 1;
100
101 /* function tracing enabled */
102 int                             ftrace_function_enabled;
103
104 /*
105  * trace_nr_entries is the number of entries that is allocated
106  * for a buffer. Note, the number of entries is always rounded
107  * to ENTRIES_PER_PAGE.
108  *
109  * This number is purposely set to a low number of 16384.
110  * If the dump on oops happens, it will be much appreciated
111  * to not have to wait for all that output. Anyway this can be
112  * boot time and run time configurable.
113  */
114 #define TRACE_ENTRIES_DEFAULT   16384UL
115
116 static unsigned long            trace_nr_entries = TRACE_ENTRIES_DEFAULT;
117
118 /* trace_types holds a link list of available tracers. */
119 static struct tracer            *trace_types __read_mostly;
120
121 /* current_trace points to the tracer that is currently active */
122 static struct tracer            *current_trace __read_mostly;
123
124 /*
125  * max_tracer_type_len is used to simplify the allocating of
126  * buffers to read userspace tracer names. We keep track of
127  * the longest tracer name registered.
128  */
129 static int                      max_tracer_type_len;
130
131 /*
132  * trace_types_lock is used to protect the trace_types list.
133  * This lock is also used to keep user access serialized.
134  * Accesses from userspace will grab this lock while userspace
135  * activities happen inside the kernel.
136  */
137 static DEFINE_MUTEX(trace_types_lock);
138
139 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
140 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
141
142 /* trace_flags holds iter_ctrl options */
143 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
144
145 static notrace void no_trace_init(struct trace_array *tr)
146 {
147         int cpu;
148
149         ftrace_function_enabled = 0;
150         if(tr->ctrl)
151                 for_each_online_cpu(cpu)
152                         tracing_reset(tr->data[cpu]);
153         tracer_enabled = 0;
154 }
155
156 /* dummy trace to disable tracing */
157 static struct tracer no_tracer __read_mostly = {
158         .name           = "none",
159         .init           = no_trace_init
160 };
161
162
163 /**
164  * trace_wake_up - wake up tasks waiting for trace input
165  *
166  * Simply wakes up any task that is blocked on the trace_wait
167  * queue. These is used with trace_poll for tasks polling the trace.
168  */
169 void trace_wake_up(void)
170 {
171         /*
172          * The runqueue_is_locked() can fail, but this is the best we
173          * have for now:
174          */
175         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
176                 wake_up(&trace_wait);
177 }
178
179 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
180
181 static int __init set_nr_entries(char *str)
182 {
183         unsigned long nr_entries;
184         int ret;
185
186         if (!str)
187                 return 0;
188         ret = strict_strtoul(str, 0, &nr_entries);
189         /* nr_entries can not be zero */
190         if (ret < 0 || nr_entries == 0)
191                 return 0;
192         trace_nr_entries = nr_entries;
193         return 1;
194 }
195 __setup("trace_entries=", set_nr_entries);
196
197 unsigned long nsecs_to_usecs(unsigned long nsecs)
198 {
199         return nsecs / 1000;
200 }
201
202 /*
203  * TRACE_ITER_SYM_MASK masks the options in trace_flags that
204  * control the output of kernel symbols.
205  */
206 #define TRACE_ITER_SYM_MASK \
207         (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
208
209 /* These must match the bit postions in trace_iterator_flags */
210 static const char *trace_options[] = {
211         "print-parent",
212         "sym-offset",
213         "sym-addr",
214         "verbose",
215         "raw",
216         "hex",
217         "bin",
218         "block",
219         "stacktrace",
220         "sched-tree",
221         "ftrace_printk",
222         NULL
223 };
224
225 /*
226  * ftrace_max_lock is used to protect the swapping of buffers
227  * when taking a max snapshot. The buffers themselves are
228  * protected by per_cpu spinlocks. But the action of the swap
229  * needs its own lock.
230  *
231  * This is defined as a raw_spinlock_t in order to help
232  * with performance when lockdep debugging is enabled.
233  */
234 static raw_spinlock_t ftrace_max_lock =
235         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
236
237 /*
238  * Copy the new maximum trace into the separate maximum-trace
239  * structure. (this way the maximum trace is permanently saved,
240  * for later retrieval via /debugfs/tracing/latency_trace)
241  */
242 static void
243 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
244 {
245         struct trace_array_cpu *data = tr->data[cpu];
246
247         max_tr.cpu = cpu;
248         max_tr.time_start = data->preempt_timestamp;
249
250         data = max_tr.data[cpu];
251         data->saved_latency = tracing_max_latency;
252
253         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
254         data->pid = tsk->pid;
255         data->uid = tsk->uid;
256         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
257         data->policy = tsk->policy;
258         data->rt_priority = tsk->rt_priority;
259
260         /* record this tasks comm */
261         tracing_record_cmdline(current);
262 }
263
264 #define CHECK_COND(cond)                        \
265         if (unlikely(cond)) {                   \
266                 tracing_disabled = 1;           \
267                 WARN_ON(1);                     \
268                 return -1;                      \
269         }
270
271 /**
272  * check_pages - integrity check of trace buffers
273  *
274  * As a safty measure we check to make sure the data pages have not
275  * been corrupted.
276  */
277 int check_pages(struct trace_array_cpu *data)
278 {
279         struct page *page, *tmp;
280
281         CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
282         CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
283
284         list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
285                 CHECK_COND(page->lru.next->prev != &page->lru);
286                 CHECK_COND(page->lru.prev->next != &page->lru);
287         }
288
289         return 0;
290 }
291
292 /**
293  * head_page - page address of the first page in per_cpu buffer.
294  *
295  * head_page returns the page address of the first page in
296  * a per_cpu buffer. This also preforms various consistency
297  * checks to make sure the buffer has not been corrupted.
298  */
299 void *head_page(struct trace_array_cpu *data)
300 {
301         struct page *page;
302
303         if (list_empty(&data->trace_pages))
304                 return NULL;
305
306         page = list_entry(data->trace_pages.next, struct page, lru);
307         BUG_ON(&page->lru == &data->trace_pages);
308
309         return page_address(page);
310 }
311
312 /**
313  * trace_seq_printf - sequence printing of trace information
314  * @s: trace sequence descriptor
315  * @fmt: printf format string
316  *
317  * The tracer may use either sequence operations or its own
318  * copy to user routines. To simplify formating of a trace
319  * trace_seq_printf is used to store strings into a special
320  * buffer (@s). Then the output may be either used by
321  * the sequencer or pulled into another buffer.
322  */
323 int
324 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
325 {
326         int len = (PAGE_SIZE - 1) - s->len;
327         va_list ap;
328         int ret;
329
330         if (!len)
331                 return 0;
332
333         va_start(ap, fmt);
334         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
335         va_end(ap);
336
337         /* If we can't write it all, don't bother writing anything */
338         if (ret >= len)
339                 return 0;
340
341         s->len += ret;
342
343         return len;
344 }
345
346 /**
347  * trace_seq_puts - trace sequence printing of simple string
348  * @s: trace sequence descriptor
349  * @str: simple string to record
350  *
351  * The tracer may use either the sequence operations or its own
352  * copy to user routines. This function records a simple string
353  * into a special buffer (@s) for later retrieval by a sequencer
354  * or other mechanism.
355  */
356 static int
357 trace_seq_puts(struct trace_seq *s, const char *str)
358 {
359         int len = strlen(str);
360
361         if (len > ((PAGE_SIZE - 1) - s->len))
362                 return 0;
363
364         memcpy(s->buffer + s->len, str, len);
365         s->len += len;
366
367         return len;
368 }
369
370 static int
371 trace_seq_putc(struct trace_seq *s, unsigned char c)
372 {
373         if (s->len >= (PAGE_SIZE - 1))
374                 return 0;
375
376         s->buffer[s->len++] = c;
377
378         return 1;
379 }
380
381 static int
382 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
383 {
384         if (len > ((PAGE_SIZE - 1) - s->len))
385                 return 0;
386
387         memcpy(s->buffer + s->len, mem, len);
388         s->len += len;
389
390         return len;
391 }
392
393 #define HEX_CHARS 17
394 static const char hex2asc[] = "0123456789abcdef";
395
396 static int
397 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
398 {
399         unsigned char hex[HEX_CHARS];
400         unsigned char *data = mem;
401         unsigned char byte;
402         int i, j;
403
404         BUG_ON(len >= HEX_CHARS);
405
406 #ifdef __BIG_ENDIAN
407         for (i = 0, j = 0; i < len; i++) {
408 #else
409         for (i = len-1, j = 0; i >= 0; i--) {
410 #endif
411                 byte = data[i];
412
413                 hex[j++] = hex2asc[byte & 0x0f];
414                 hex[j++] = hex2asc[byte >> 4];
415         }
416         hex[j++] = ' ';
417
418         return trace_seq_putmem(s, hex, j);
419 }
420
421 static void
422 trace_seq_reset(struct trace_seq *s)
423 {
424         s->len = 0;
425         s->readpos = 0;
426 }
427
428 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
429 {
430         int len;
431         int ret;
432
433         if (s->len <= s->readpos)
434                 return -EBUSY;
435
436         len = s->len - s->readpos;
437         if (cnt > len)
438                 cnt = len;
439         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
440         if (ret)
441                 return -EFAULT;
442
443         s->readpos += len;
444         return cnt;
445 }
446
447 static void
448 trace_print_seq(struct seq_file *m, struct trace_seq *s)
449 {
450         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
451
452         s->buffer[len] = 0;
453         seq_puts(m, s->buffer);
454
455         trace_seq_reset(s);
456 }
457
458 /*
459  * flip the trace buffers between two trace descriptors.
460  * This usually is the buffers between the global_trace and
461  * the max_tr to record a snapshot of a current trace.
462  *
463  * The ftrace_max_lock must be held.
464  */
465 static void
466 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
467 {
468         struct list_head flip_pages;
469
470         INIT_LIST_HEAD(&flip_pages);
471
472         memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
473                 sizeof(struct trace_array_cpu) -
474                 offsetof(struct trace_array_cpu, trace_head_idx));
475
476         check_pages(tr1);
477         check_pages(tr2);
478         list_splice_init(&tr1->trace_pages, &flip_pages);
479         list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
480         list_splice_init(&flip_pages, &tr2->trace_pages);
481         BUG_ON(!list_empty(&flip_pages));
482         check_pages(tr1);
483         check_pages(tr2);
484 }
485
486 /**
487  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
488  * @tr: tracer
489  * @tsk: the task with the latency
490  * @cpu: The cpu that initiated the trace.
491  *
492  * Flip the buffers between the @tr and the max_tr and record information
493  * about which task was the cause of this latency.
494  */
495 void
496 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
497 {
498         struct trace_array_cpu *data;
499         int i;
500
501         WARN_ON_ONCE(!irqs_disabled());
502         __raw_spin_lock(&ftrace_max_lock);
503         /* clear out all the previous traces */
504         for_each_tracing_cpu(i) {
505                 data = tr->data[i];
506                 flip_trace(max_tr.data[i], data);
507                 tracing_reset(data);
508         }
509
510         __update_max_tr(tr, tsk, cpu);
511         __raw_spin_unlock(&ftrace_max_lock);
512 }
513
514 /**
515  * update_max_tr_single - only copy one trace over, and reset the rest
516  * @tr - tracer
517  * @tsk - task with the latency
518  * @cpu - the cpu of the buffer to copy.
519  *
520  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
521  */
522 void
523 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
524 {
525         struct trace_array_cpu *data = tr->data[cpu];
526         int i;
527
528         WARN_ON_ONCE(!irqs_disabled());
529         __raw_spin_lock(&ftrace_max_lock);
530         for_each_tracing_cpu(i)
531                 tracing_reset(max_tr.data[i]);
532
533         flip_trace(max_tr.data[cpu], data);
534         tracing_reset(data);
535
536         __update_max_tr(tr, tsk, cpu);
537         __raw_spin_unlock(&ftrace_max_lock);
538 }
539
540 /**
541  * register_tracer - register a tracer with the ftrace system.
542  * @type - the plugin for the tracer
543  *
544  * Register a new plugin tracer.
545  */
546 int register_tracer(struct tracer *type)
547 {
548         struct tracer *t;
549         int len;
550         int ret = 0;
551
552         if (!type->name) {
553                 pr_info("Tracer must have a name\n");
554                 return -1;
555         }
556
557         mutex_lock(&trace_types_lock);
558         for (t = trace_types; t; t = t->next) {
559                 if (strcmp(type->name, t->name) == 0) {
560                         /* already found */
561                         pr_info("Trace %s already registered\n",
562                                 type->name);
563                         ret = -1;
564                         goto out;
565                 }
566         }
567
568 #ifdef CONFIG_FTRACE_STARTUP_TEST
569         if (type->selftest) {
570                 struct tracer *saved_tracer = current_trace;
571                 struct trace_array_cpu *data;
572                 struct trace_array *tr = &global_trace;
573                 int saved_ctrl = tr->ctrl;
574                 int i;
575                 /*
576                  * Run a selftest on this tracer.
577                  * Here we reset the trace buffer, and set the current
578                  * tracer to be this tracer. The tracer can then run some
579                  * internal tracing to verify that everything is in order.
580                  * If we fail, we do not register this tracer.
581                  */
582                 for_each_tracing_cpu(i) {
583                         data = tr->data[i];
584                         if (!head_page(data))
585                                 continue;
586                         tracing_reset(data);
587                 }
588                 current_trace = type;
589                 tr->ctrl = 0;
590                 /* the test is responsible for initializing and enabling */
591                 pr_info("Testing tracer %s: ", type->name);
592                 ret = type->selftest(type, tr);
593                 /* the test is responsible for resetting too */
594                 current_trace = saved_tracer;
595                 tr->ctrl = saved_ctrl;
596                 if (ret) {
597                         printk(KERN_CONT "FAILED!\n");
598                         goto out;
599                 }
600                 /* Only reset on passing, to avoid touching corrupted buffers */
601                 for_each_tracing_cpu(i) {
602                         data = tr->data[i];
603                         if (!head_page(data))
604                                 continue;
605                         tracing_reset(data);
606                 }
607                 printk(KERN_CONT "PASSED\n");
608         }
609 #endif
610
611         type->next = trace_types;
612         trace_types = type;
613         len = strlen(type->name);
614         if (len > max_tracer_type_len)
615                 max_tracer_type_len = len;
616
617  out:
618         mutex_unlock(&trace_types_lock);
619
620         return ret;
621 }
622
623 void unregister_tracer(struct tracer *type)
624 {
625         struct tracer **t;
626         int len;
627
628         mutex_lock(&trace_types_lock);
629         for (t = &trace_types; *t; t = &(*t)->next) {
630                 if (*t == type)
631                         goto found;
632         }
633         pr_info("Trace %s not registered\n", type->name);
634         goto out;
635
636  found:
637         *t = (*t)->next;
638         if (strlen(type->name) != max_tracer_type_len)
639                 goto out;
640
641         max_tracer_type_len = 0;
642         for (t = &trace_types; *t; t = &(*t)->next) {
643                 len = strlen((*t)->name);
644                 if (len > max_tracer_type_len)
645                         max_tracer_type_len = len;
646         }
647  out:
648         mutex_unlock(&trace_types_lock);
649 }
650
651 void tracing_reset(struct trace_array_cpu *data)
652 {
653         data->trace_idx = 0;
654         data->overrun = 0;
655         data->trace_head = data->trace_tail = head_page(data);
656         data->trace_head_idx = 0;
657         data->trace_tail_idx = 0;
658 }
659
660 #define SAVED_CMDLINES 128
661 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
662 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
663 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
664 static int cmdline_idx;
665 static DEFINE_SPINLOCK(trace_cmdline_lock);
666
667 /* temporary disable recording */
668 atomic_t trace_record_cmdline_disabled __read_mostly;
669
670 static void trace_init_cmdlines(void)
671 {
672         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
673         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
674         cmdline_idx = 0;
675 }
676
677 void trace_stop_cmdline_recording(void);
678
679 static void trace_save_cmdline(struct task_struct *tsk)
680 {
681         unsigned map;
682         unsigned idx;
683
684         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
685                 return;
686
687         /*
688          * It's not the end of the world if we don't get
689          * the lock, but we also don't want to spin
690          * nor do we want to disable interrupts,
691          * so if we miss here, then better luck next time.
692          */
693         if (!spin_trylock(&trace_cmdline_lock))
694                 return;
695
696         idx = map_pid_to_cmdline[tsk->pid];
697         if (idx >= SAVED_CMDLINES) {
698                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
699
700                 map = map_cmdline_to_pid[idx];
701                 if (map <= PID_MAX_DEFAULT)
702                         map_pid_to_cmdline[map] = (unsigned)-1;
703
704                 map_pid_to_cmdline[tsk->pid] = idx;
705
706                 cmdline_idx = idx;
707         }
708
709         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
710
711         spin_unlock(&trace_cmdline_lock);
712 }
713
714 static char *trace_find_cmdline(int pid)
715 {
716         char *cmdline = "<...>";
717         unsigned map;
718
719         if (!pid)
720                 return "<idle>";
721
722         if (pid > PID_MAX_DEFAULT)
723                 goto out;
724
725         map = map_pid_to_cmdline[pid];
726         if (map >= SAVED_CMDLINES)
727                 goto out;
728
729         cmdline = saved_cmdlines[map];
730
731  out:
732         return cmdline;
733 }
734
735 void tracing_record_cmdline(struct task_struct *tsk)
736 {
737         if (atomic_read(&trace_record_cmdline_disabled))
738                 return;
739
740         trace_save_cmdline(tsk);
741 }
742
743 static inline struct list_head *
744 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
745 {
746         /*
747          * Roundrobin - but skip the head (which is not a real page):
748          */
749         next = next->next;
750         if (unlikely(next == &data->trace_pages))
751                 next = next->next;
752         BUG_ON(next == &data->trace_pages);
753
754         return next;
755 }
756
757 static inline void *
758 trace_next_page(struct trace_array_cpu *data, void *addr)
759 {
760         struct list_head *next;
761         struct page *page;
762
763         page = virt_to_page(addr);
764
765         next = trace_next_list(data, &page->lru);
766         page = list_entry(next, struct page, lru);
767
768         return page_address(page);
769 }
770
771 struct trace_entry *
772 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
773 {
774         unsigned long idx, idx_next;
775         struct trace_entry *entry;
776
777         data->trace_idx++;
778         idx = data->trace_head_idx;
779         idx_next = idx + 1;
780
781         BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
782
783         entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
784
785         if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
786                 data->trace_head = trace_next_page(data, data->trace_head);
787                 idx_next = 0;
788         }
789
790         if (data->trace_head == data->trace_tail &&
791             idx_next == data->trace_tail_idx) {
792                 /* overrun */
793                 data->overrun++;
794                 data->trace_tail_idx++;
795                 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
796                         data->trace_tail =
797                                 trace_next_page(data, data->trace_tail);
798                         data->trace_tail_idx = 0;
799                 }
800         }
801
802         data->trace_head_idx = idx_next;
803
804         return entry;
805 }
806
807 void
808 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
809 {
810         struct task_struct *tsk = current;
811         unsigned long pc;
812
813         pc = preempt_count();
814
815         entry->field.preempt_count      = pc & 0xff;
816         entry->field.pid                = (tsk) ? tsk->pid : 0;
817         entry->field.t                  = ftrace_now(raw_smp_processor_id());
818         entry->field.flags =
819                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
820                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
823 }
824
825 void
826 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827                unsigned long ip, unsigned long parent_ip, unsigned long flags)
828 {
829         struct trace_entry *entry;
830         unsigned long irq_flags;
831
832         raw_local_irq_save(irq_flags);
833         __raw_spin_lock(&data->lock);
834         entry                           = tracing_get_trace_entry(tr, data);
835         tracing_generic_entry_update(entry, flags);
836         entry->type                     = TRACE_FN;
837         entry->field.fn.ip              = ip;
838         entry->field.fn.parent_ip       = parent_ip;
839         __raw_spin_unlock(&data->lock);
840         raw_local_irq_restore(irq_flags);
841 }
842
843 void
844 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845        unsigned long ip, unsigned long parent_ip, unsigned long flags)
846 {
847         if (likely(!atomic_read(&data->disabled)))
848                 trace_function(tr, data, ip, parent_ip, flags);
849 }
850
851 void __trace_stack(struct trace_array *tr,
852                    struct trace_array_cpu *data,
853                    unsigned long flags,
854                    int skip)
855 {
856         struct trace_entry *entry;
857         struct stack_trace trace;
858
859         if (!(trace_flags & TRACE_ITER_STACKTRACE))
860                 return;
861
862         entry                   = tracing_get_trace_entry(tr, data);
863         tracing_generic_entry_update(entry, flags);
864         entry->type             = TRACE_STACK;
865
866         memset(&entry->field.stack, 0, sizeof(entry->field.stack));
867
868         trace.nr_entries        = 0;
869         trace.max_entries       = FTRACE_STACK_ENTRIES;
870         trace.skip              = skip;
871         trace.entries           = entry->field.stack.caller;
872
873         save_stack_trace(&trace);
874 }
875
876 void
877 __trace_special(void *__tr, void *__data,
878                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
879 {
880         struct trace_array_cpu *data = __data;
881         struct trace_array *tr = __tr;
882         struct trace_entry *entry;
883         unsigned long irq_flags;
884
885         raw_local_irq_save(irq_flags);
886         __raw_spin_lock(&data->lock);
887         entry                           = tracing_get_trace_entry(tr, data);
888         tracing_generic_entry_update(entry, 0);
889         entry->type                     = TRACE_SPECIAL;
890         entry->field.special.arg1       = arg1;
891         entry->field.special.arg2       = arg2;
892         entry->field.special.arg3       = arg3;
893         __trace_stack(tr, data, irq_flags, 4);
894         __raw_spin_unlock(&data->lock);
895         raw_local_irq_restore(irq_flags);
896
897         trace_wake_up();
898 }
899
900 void
901 tracing_sched_switch_trace(struct trace_array *tr,
902                            struct trace_array_cpu *data,
903                            struct task_struct *prev,
904                            struct task_struct *next,
905                            unsigned long flags)
906 {
907         struct trace_entry *entry;
908         unsigned long irq_flags;
909
910         raw_local_irq_save(irq_flags);
911         __raw_spin_lock(&data->lock);
912         entry                           = tracing_get_trace_entry(tr, data);
913         tracing_generic_entry_update(entry, flags);
914         entry->type                     = TRACE_CTX;
915         entry->field.ctx.prev_pid       = prev->pid;
916         entry->field.ctx.prev_prio      = prev->prio;
917         entry->field.ctx.prev_state     = prev->state;
918         entry->field.ctx.next_pid       = next->pid;
919         entry->field.ctx.next_prio      = next->prio;
920         entry->field.ctx.next_state     = next->state;
921         entry->field.ctx.next_cpu       = task_cpu(next);
922         __trace_stack(tr, data, flags, 5);
923         __raw_spin_unlock(&data->lock);
924         raw_local_irq_restore(irq_flags);
925 }
926
927 void
928 tracing_sched_wakeup_trace(struct trace_array *tr,
929                            struct trace_array_cpu *data,
930                            struct task_struct *wakee,
931                            struct task_struct *curr,
932                            unsigned long flags)
933 {
934         struct trace_entry *entry;
935         unsigned long irq_flags;
936
937         raw_local_irq_save(irq_flags);
938         __raw_spin_lock(&data->lock);
939         entry                   = tracing_get_trace_entry(tr, data);
940         tracing_generic_entry_update(entry, flags);
941         entry->type             = TRACE_WAKE;
942         entry->field.ctx.prev_pid       = curr->pid;
943         entry->field.ctx.prev_prio      = curr->prio;
944         entry->field.ctx.prev_state     = curr->state;
945         entry->field.ctx.next_pid       = wakee->pid;
946         entry->field.ctx.next_prio      = wakee->prio;
947         entry->field.ctx.next_state     = wakee->state;
948         entry->field.ctx.next_cpu       = task_cpu(wakee);
949         __trace_stack(tr, data, flags, 6);
950         __raw_spin_unlock(&data->lock);
951         raw_local_irq_restore(irq_flags);
952
953         trace_wake_up();
954 }
955
956 void
957 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
958 {
959         struct trace_array *tr = &global_trace;
960         struct trace_array_cpu *data;
961         unsigned long flags;
962         long disabled;
963         int cpu;
964
965         if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
966                 return;
967
968         local_irq_save(flags);
969         cpu = raw_smp_processor_id();
970         data = tr->data[cpu];
971         disabled = atomic_inc_return(&data->disabled);
972
973         if (likely(disabled == 1))
974                 __trace_special(tr, data, arg1, arg2, arg3);
975
976         atomic_dec(&data->disabled);
977         local_irq_restore(flags);
978 }
979
980 #ifdef CONFIG_FTRACE
981 static void
982 function_trace_call(unsigned long ip, unsigned long parent_ip)
983 {
984         struct trace_array *tr = &global_trace;
985         struct trace_array_cpu *data;
986         unsigned long flags;
987         long disabled;
988         int cpu;
989
990         if (unlikely(!ftrace_function_enabled))
991                 return;
992
993         if (skip_trace(ip))
994                 return;
995
996         local_irq_save(flags);
997         cpu = raw_smp_processor_id();
998         data = tr->data[cpu];
999         disabled = atomic_inc_return(&data->disabled);
1000
1001         if (likely(disabled == 1))
1002                 trace_function(tr, data, ip, parent_ip, flags);
1003
1004         atomic_dec(&data->disabled);
1005         local_irq_restore(flags);
1006 }
1007
1008 static struct ftrace_ops trace_ops __read_mostly =
1009 {
1010         .func = function_trace_call,
1011 };
1012
1013 void tracing_start_function_trace(void)
1014 {
1015         ftrace_function_enabled = 0;
1016         register_ftrace_function(&trace_ops);
1017         if (tracer_enabled)
1018                 ftrace_function_enabled = 1;
1019 }
1020
1021 void tracing_stop_function_trace(void)
1022 {
1023         ftrace_function_enabled = 0;
1024         unregister_ftrace_function(&trace_ops);
1025 }
1026 #endif
1027
1028 enum trace_file_type {
1029         TRACE_FILE_LAT_FMT      = 1,
1030 };
1031
1032 /* Return the current entry.  */
1033 static struct trace_entry *
1034 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1035                 struct trace_iterator *iter, int cpu)
1036 {
1037         struct page *page;
1038         struct trace_entry *array;
1039
1040         if (iter->next_idx[cpu] >= tr->entries ||
1041             iter->next_idx[cpu] >= data->trace_idx ||
1042             (data->trace_head == data->trace_tail &&
1043              data->trace_head_idx == data->trace_tail_idx))
1044                 return NULL;
1045
1046         if (!iter->next_page[cpu]) {
1047                 /* Initialize the iterator for this cpu trace buffer */
1048                 WARN_ON(!data->trace_tail);
1049                 page = virt_to_page(data->trace_tail);
1050                 iter->next_page[cpu] = &page->lru;
1051                 iter->next_page_idx[cpu] = data->trace_tail_idx;
1052         }
1053
1054         page = list_entry(iter->next_page[cpu], struct page, lru);
1055         BUG_ON(&data->trace_pages == &page->lru);
1056
1057         array = page_address(page);
1058
1059         WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1060         return &array[iter->next_page_idx[cpu]];
1061 }
1062
1063 /* Increment the index counter of an iterator by one */
1064 static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
1065 {
1066         iter->next_idx[cpu]++;
1067         iter->next_page_idx[cpu]++;
1068
1069         if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1070                 struct trace_array_cpu *data = iter->tr->data[cpu];
1071
1072                 iter->next_page_idx[cpu] = 0;
1073                 iter->next_page[cpu] =
1074                         trace_next_list(data, iter->next_page[cpu]);
1075         }
1076 }
1077
1078 static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1079 {
1080         iter->idx++;
1081         __trace_iterator_increment(iter, cpu);
1082 }
1083
1084 static struct trace_entry *
1085 trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1086                  struct trace_iterator *iter, int cpu)
1087 {
1088         struct list_head *next_page;
1089         struct trace_entry *ent;
1090         int idx, next_idx, next_page_idx;
1091
1092         ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1093
1094         if (likely(!ent || ent->type != TRACE_CONT))
1095                 return ent;
1096
1097         /* save the iterator details */
1098         idx             = iter->idx;
1099         next_idx        = iter->next_idx[cpu];
1100         next_page_idx   = iter->next_page_idx[cpu];
1101         next_page       = iter->next_page[cpu];
1102
1103         /* find a real entry */
1104         do {
1105                 __trace_iterator_increment(iter, cpu);
1106                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1107         } while (ent && ent->type != TRACE_CONT);
1108
1109         /* reset the iterator */
1110         iter->idx                       = idx;
1111         iter->next_idx[cpu]             = next_idx;
1112         iter->next_page_idx[cpu]        = next_page_idx;
1113         iter->next_page[cpu]            = next_page;
1114
1115         return ent;
1116 }
1117
1118 static struct trace_entry *
1119 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
1120 {
1121         struct trace_array *tr = iter->tr;
1122         struct trace_entry *ent, *next = NULL;
1123         int next_cpu = -1;
1124         int cpu;
1125
1126         for_each_tracing_cpu(cpu) {
1127                 if (!head_page(tr->data[cpu]))
1128                         continue;
1129
1130                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1131
1132                 if (ent && ent->type == TRACE_CONT) {
1133                         struct trace_array_cpu *data = tr->data[cpu];
1134
1135                         if (!inc)
1136                                 ent = trace_entry_next(tr, data, iter, cpu);
1137                         else {
1138                                 while (ent && ent->type == TRACE_CONT) {
1139                                         __trace_iterator_increment(iter, cpu);
1140                                         ent = trace_entry_idx(tr, tr->data[cpu],
1141                                                               iter, cpu);
1142                                 }
1143                         }
1144                 }
1145
1146                 /*
1147                  * Pick the entry with the smallest timestamp:
1148                  */
1149                 if (ent && (!next || ent->field.t < next->field.t)) {
1150                         next = ent;
1151                         next_cpu = cpu;
1152                 }
1153         }
1154
1155         if (ent_cpu)
1156                 *ent_cpu = next_cpu;
1157
1158         return next;
1159 }
1160
1161 /* Find the next real entry, without updating the iterator itself */
1162 static struct trace_entry *
1163 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1164 {
1165         return __find_next_entry(iter, ent_cpu, 0);
1166 }
1167
1168 /* Find the next real entry, and increment the iterator to the next entry */
1169 static void *find_next_entry_inc(struct trace_iterator *iter)
1170 {
1171         struct trace_entry *next;
1172         int next_cpu = -1;
1173
1174         next = __find_next_entry(iter, &next_cpu, 1);
1175
1176         iter->prev_ent = iter->ent;
1177         iter->prev_cpu = iter->cpu;
1178
1179         iter->ent = next;
1180         iter->cpu = next_cpu;
1181
1182         if (next)
1183                 trace_iterator_increment(iter, iter->cpu);
1184
1185         return next ? iter : NULL;
1186 }
1187
1188 static void trace_consume(struct trace_iterator *iter)
1189 {
1190         struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1191         struct trace_entry *ent;
1192
1193  again:
1194         data->trace_tail_idx++;
1195         if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1196                 data->trace_tail = trace_next_page(data, data->trace_tail);
1197                 data->trace_tail_idx = 0;
1198         }
1199
1200         /* Check if we empty it, then reset the index */
1201         if (data->trace_head == data->trace_tail &&
1202             data->trace_head_idx == data->trace_tail_idx)
1203                 data->trace_idx = 0;
1204
1205         ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1206                               iter, iter->cpu);
1207         if (ent && ent->type == TRACE_CONT)
1208                 goto again;
1209 }
1210
1211 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1212 {
1213         struct trace_iterator *iter = m->private;
1214         int i = (int)*pos;
1215         void *ent;
1216
1217         (*pos)++;
1218
1219         /* can't go backwards */
1220         if (iter->idx > i)
1221                 return NULL;
1222
1223         if (iter->idx < 0)
1224                 ent = find_next_entry_inc(iter);
1225         else
1226                 ent = iter;
1227
1228         while (ent && iter->idx < i)
1229                 ent = find_next_entry_inc(iter);
1230
1231         iter->pos = *pos;
1232
1233         return ent;
1234 }
1235
1236 static void *s_start(struct seq_file *m, loff_t *pos)
1237 {
1238         struct trace_iterator *iter = m->private;
1239         void *p = NULL;
1240         loff_t l = 0;
1241         int i;
1242
1243         mutex_lock(&trace_types_lock);
1244
1245         if (!current_trace || current_trace != iter->trace) {
1246                 mutex_unlock(&trace_types_lock);
1247                 return NULL;
1248         }
1249
1250         atomic_inc(&trace_record_cmdline_disabled);
1251
1252         /* let the tracer grab locks here if needed */
1253         if (current_trace->start)
1254                 current_trace->start(iter);
1255
1256         if (*pos != iter->pos) {
1257                 iter->ent = NULL;
1258                 iter->cpu = 0;
1259                 iter->idx = -1;
1260                 iter->prev_ent = NULL;
1261                 iter->prev_cpu = -1;
1262
1263                 for_each_tracing_cpu(i) {
1264                         iter->next_idx[i] = 0;
1265                         iter->next_page[i] = NULL;
1266                 }
1267
1268                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1269                         ;
1270
1271         } else {
1272                 l = *pos - 1;
1273                 p = s_next(m, p, &l);
1274         }
1275
1276         return p;
1277 }
1278
1279 static void s_stop(struct seq_file *m, void *p)
1280 {
1281         struct trace_iterator *iter = m->private;
1282
1283         atomic_dec(&trace_record_cmdline_disabled);
1284
1285         /* let the tracer release locks here if needed */
1286         if (current_trace && current_trace == iter->trace && iter->trace->stop)
1287                 iter->trace->stop(iter);
1288
1289         mutex_unlock(&trace_types_lock);
1290 }
1291
1292 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1293
1294 #ifdef CONFIG_KRETPROBES
1295 static inline int kretprobed(unsigned long addr)
1296 {
1297         return addr == (unsigned long)kretprobe_trampoline;
1298 }
1299 #else
1300 static inline int kretprobed(unsigned long addr)
1301 {
1302         return 0;
1303 }
1304 #endif /* CONFIG_KRETPROBES */
1305
1306 static int
1307 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1308 {
1309 #ifdef CONFIG_KALLSYMS
1310         char str[KSYM_SYMBOL_LEN];
1311
1312         kallsyms_lookup(address, NULL, NULL, NULL, str);
1313
1314         return trace_seq_printf(s, fmt, str);
1315 #endif
1316         return 1;
1317 }
1318
1319 static int
1320 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1321                      unsigned long address)
1322 {
1323 #ifdef CONFIG_KALLSYMS
1324         char str[KSYM_SYMBOL_LEN];
1325
1326         sprint_symbol(str, address);
1327         return trace_seq_printf(s, fmt, str);
1328 #endif
1329         return 1;
1330 }
1331
1332 #ifndef CONFIG_64BIT
1333 # define IP_FMT "%08lx"
1334 #else
1335 # define IP_FMT "%016lx"
1336 #endif
1337
1338 static int
1339 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1340 {
1341         int ret;
1342
1343         if (!ip)
1344                 return trace_seq_printf(s, "0");
1345
1346         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1347                 ret = seq_print_sym_offset(s, "%s", ip);
1348         else
1349                 ret = seq_print_sym_short(s, "%s", ip);
1350
1351         if (!ret)
1352                 return 0;
1353
1354         if (sym_flags & TRACE_ITER_SYM_ADDR)
1355                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1356         return ret;
1357 }
1358
1359 static void print_lat_help_header(struct seq_file *m)
1360 {
1361         seq_puts(m, "#                  _------=> CPU#            \n");
1362         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1363         seq_puts(m, "#                | / _----=> need-resched    \n");
1364         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1365         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1366         seq_puts(m, "#                |||| /                      \n");
1367         seq_puts(m, "#                |||||     delay             \n");
1368         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1369         seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1370 }
1371
1372 static void print_func_help_header(struct seq_file *m)
1373 {
1374         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1375         seq_puts(m, "#              | |       |          |         |\n");
1376 }
1377
1378
1379 static void
1380 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1381 {
1382         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1383         struct trace_array *tr = iter->tr;
1384         struct trace_array_cpu *data = tr->data[tr->cpu];
1385         struct tracer *type = current_trace;
1386         unsigned long total   = 0;
1387         unsigned long entries = 0;
1388         int cpu;
1389         const char *name = "preemption";
1390
1391         if (type)
1392                 name = type->name;
1393
1394         for_each_tracing_cpu(cpu) {
1395                 if (head_page(tr->data[cpu])) {
1396                         total += tr->data[cpu]->trace_idx;
1397                         if (tr->data[cpu]->trace_idx > tr->entries)
1398                                 entries += tr->entries;
1399                         else
1400                                 entries += tr->data[cpu]->trace_idx;
1401                 }
1402         }
1403
1404         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1405                    name, UTS_RELEASE);
1406         seq_puts(m, "-----------------------------------"
1407                  "---------------------------------\n");
1408         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1409                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1410                    nsecs_to_usecs(data->saved_latency),
1411                    entries,
1412                    total,
1413                    tr->cpu,
1414 #if defined(CONFIG_PREEMPT_NONE)
1415                    "server",
1416 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1417                    "desktop",
1418 #elif defined(CONFIG_PREEMPT)
1419                    "preempt",
1420 #else
1421                    "unknown",
1422 #endif
1423                    /* These are reserved for later use */
1424                    0, 0, 0, 0);
1425 #ifdef CONFIG_SMP
1426         seq_printf(m, " #P:%d)\n", num_online_cpus());
1427 #else
1428         seq_puts(m, ")\n");
1429 #endif
1430         seq_puts(m, "    -----------------\n");
1431         seq_printf(m, "    | task: %.16s-%d "
1432                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1433                    data->comm, data->pid, data->uid, data->nice,
1434                    data->policy, data->rt_priority);
1435         seq_puts(m, "    -----------------\n");
1436
1437         if (data->critical_start) {
1438                 seq_puts(m, " => started at: ");
1439                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1440                 trace_print_seq(m, &iter->seq);
1441                 seq_puts(m, "\n => ended at:   ");
1442                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1443                 trace_print_seq(m, &iter->seq);
1444                 seq_puts(m, "\n");
1445         }
1446
1447         seq_puts(m, "\n");
1448 }
1449
1450 static void
1451 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1452 {
1453         struct trace_field *field = &entry->field;
1454         int hardirq, softirq;
1455         char *comm;
1456
1457         comm = trace_find_cmdline(field->pid);
1458
1459         trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
1460         trace_seq_printf(s, "%3d", cpu);
1461         trace_seq_printf(s, "%c%c",
1462                         (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1463                         ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1464
1465         hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1466         softirq = field->flags & TRACE_FLAG_SOFTIRQ;
1467         if (hardirq && softirq) {
1468                 trace_seq_putc(s, 'H');
1469         } else {
1470                 if (hardirq) {
1471                         trace_seq_putc(s, 'h');
1472                 } else {
1473                         if (softirq)
1474                                 trace_seq_putc(s, 's');
1475                         else
1476                                 trace_seq_putc(s, '.');
1477                 }
1478         }
1479
1480         if (field->preempt_count)
1481                 trace_seq_printf(s, "%x", field->preempt_count);
1482         else
1483                 trace_seq_puts(s, ".");
1484 }
1485
1486 unsigned long preempt_mark_thresh = 100;
1487
1488 static void
1489 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1490                     unsigned long rel_usecs)
1491 {
1492         trace_seq_printf(s, " %4lldus", abs_usecs);
1493         if (rel_usecs > preempt_mark_thresh)
1494                 trace_seq_puts(s, "!: ");
1495         else if (rel_usecs > 1)
1496                 trace_seq_puts(s, "+: ");
1497         else
1498                 trace_seq_puts(s, " : ");
1499 }
1500
1501 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1502
1503 /*
1504  * The message is supposed to contain an ending newline.
1505  * If the printing stops prematurely, try to add a newline of our own.
1506  */
1507 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1508 {
1509         struct trace_array *tr = iter->tr;
1510         struct trace_array_cpu *data = tr->data[iter->cpu];
1511         struct trace_entry *ent;
1512         bool ok = true;
1513
1514         ent = trace_entry_idx(tr, data, iter, iter->cpu);
1515         if (!ent || ent->type != TRACE_CONT) {
1516                 trace_seq_putc(s, '\n');
1517                 return;
1518         }
1519
1520         do {
1521                 if (ok)
1522                         ok = (trace_seq_printf(s, "%s", ent->cont.buf) > 0);
1523                 __trace_iterator_increment(iter, iter->cpu);
1524                 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1525         } while (ent && ent->type == TRACE_CONT);
1526
1527         if (!ok)
1528                 trace_seq_putc(s, '\n');
1529 }
1530
1531 static int
1532 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1533 {
1534         struct trace_seq *s = &iter->seq;
1535         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1536         struct trace_entry *next_entry = find_next_entry(iter, NULL);
1537         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1538         struct trace_entry *entry = iter->ent;
1539         struct trace_field *field = &entry->field;
1540         unsigned long abs_usecs;
1541         unsigned long rel_usecs;
1542         char *comm;
1543         int S, T;
1544         int i;
1545         unsigned state;
1546
1547         if (!next_entry)
1548                 next_entry = entry;
1549
1550         if (entry->type == TRACE_CONT)
1551                 return 1;
1552
1553         rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1554         abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
1555
1556         if (verbose) {
1557                 comm = trace_find_cmdline(field->pid);
1558                 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
1559                                  " %ld.%03ldms (+%ld.%03ldms): ",
1560                                  comm,
1561                                  field->pid, cpu, field->flags,
1562                                  field->preempt_count, trace_idx,
1563                                  ns2usecs(field->t),
1564                                  abs_usecs/1000,
1565                                  abs_usecs % 1000, rel_usecs/1000,
1566                                  rel_usecs % 1000);
1567         } else {
1568                 lat_print_generic(s, entry, cpu);
1569                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1570         }
1571         switch (entry->type) {
1572         case TRACE_FN:
1573                 seq_print_ip_sym(s, field->fn.ip, sym_flags);
1574                 trace_seq_puts(s, " (");
1575                 if (kretprobed(field->fn.parent_ip))
1576                         trace_seq_puts(s, KRETPROBE_MSG);
1577                 else
1578                         seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
1579                 trace_seq_puts(s, ")\n");
1580                 break;
1581         case TRACE_CTX:
1582         case TRACE_WAKE:
1583                 T = field->ctx.next_state < sizeof(state_to_char) ?
1584                         state_to_char[field->ctx.next_state] : 'X';
1585
1586                 state = field->ctx.prev_state ?
1587                         __ffs(field->ctx.prev_state) + 1 : 0;
1588                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1589                 comm = trace_find_cmdline(field->ctx.next_pid);
1590                 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1591                                  field->ctx.prev_pid,
1592                                  field->ctx.prev_prio,
1593                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1594                                  field->ctx.next_cpu,
1595                                  field->ctx.next_pid,
1596                                  field->ctx.next_prio,
1597                                  T, comm);
1598                 break;
1599         case TRACE_SPECIAL:
1600                 trace_seq_printf(s, "# %ld %ld %ld\n",
1601                                  field->special.arg1,
1602                                  field->special.arg2,
1603                                  field->special.arg3);
1604                 break;
1605         case TRACE_STACK:
1606                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1607                         if (i)
1608                                 trace_seq_puts(s, " <= ");
1609                         seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
1610                 }
1611                 trace_seq_puts(s, "\n");
1612                 break;
1613         case TRACE_PRINT:
1614                 seq_print_ip_sym(s, field->print.ip, sym_flags);
1615                 trace_seq_printf(s, ": %s", field->print.buf);
1616                 if (field->flags & TRACE_FLAG_CONT)
1617                         trace_seq_print_cont(s, iter);
1618                 break;
1619         default:
1620                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1621         }
1622         return 1;
1623 }
1624
1625 static int print_trace_fmt(struct trace_iterator *iter)
1626 {
1627         struct trace_seq *s = &iter->seq;
1628         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1629         struct trace_entry *entry;
1630         struct trace_field *field;
1631         unsigned long usec_rem;
1632         unsigned long long t;
1633         unsigned long secs;
1634         char *comm;
1635         int ret;
1636         int S, T;
1637         int i;
1638
1639         entry = iter->ent;
1640
1641         if (entry->type == TRACE_CONT)
1642                 return 1;
1643
1644         field = &entry->field;
1645
1646         comm = trace_find_cmdline(iter->ent->field.pid);
1647
1648         t = ns2usecs(field->t);
1649         usec_rem = do_div(t, 1000000ULL);
1650         secs = (unsigned long)t;
1651
1652         ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
1653         if (!ret)
1654                 return 0;
1655         ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
1656         if (!ret)
1657                 return 0;
1658         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1659         if (!ret)
1660                 return 0;
1661
1662         switch (entry->type) {
1663         case TRACE_FN:
1664                 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
1665                 if (!ret)
1666                         return 0;
1667                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1668                                                 field->fn.parent_ip) {
1669                         ret = trace_seq_printf(s, " <-");
1670                         if (!ret)
1671                                 return 0;
1672                         if (kretprobed(field->fn.parent_ip))
1673                                 ret = trace_seq_puts(s, KRETPROBE_MSG);
1674                         else
1675                                 ret = seq_print_ip_sym(s,
1676                                                        field->fn.parent_ip,
1677                                                        sym_flags);
1678                         if (!ret)
1679                                 return 0;
1680                 }
1681                 ret = trace_seq_printf(s, "\n");
1682                 if (!ret)
1683                         return 0;
1684                 break;
1685         case TRACE_CTX:
1686         case TRACE_WAKE:
1687                 S = field->ctx.prev_state < sizeof(state_to_char) ?
1688                         state_to_char[field->ctx.prev_state] : 'X';
1689                 T = field->ctx.next_state < sizeof(state_to_char) ?
1690                         state_to_char[field->ctx.next_state] : 'X';
1691                 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1692                                        field->ctx.prev_pid,
1693                                        field->ctx.prev_prio,
1694                                        S,
1695                                        entry->type == TRACE_CTX ? "==>" : "  +",
1696                                        field->ctx.next_cpu,
1697                                        field->ctx.next_pid,
1698                                        field->ctx.next_prio,
1699                                        T);
1700                 if (!ret)
1701                         return 0;
1702                 break;
1703         case TRACE_SPECIAL:
1704                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1705                                  field->special.arg1,
1706                                  field->special.arg2,
1707                                  field->special.arg3);
1708                 if (!ret)
1709                         return 0;
1710                 break;
1711         case TRACE_STACK:
1712                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1713                         if (i) {
1714                                 ret = trace_seq_puts(s, " <= ");
1715                                 if (!ret)
1716                                         return 0;
1717                         }
1718                         ret = seq_print_ip_sym(s, field->stack.caller[i],
1719                                                sym_flags);
1720                         if (!ret)
1721                                 return 0;
1722                 }
1723                 ret = trace_seq_puts(s, "\n");
1724                 if (!ret)
1725                         return 0;
1726                 break;
1727         case TRACE_PRINT:
1728                 seq_print_ip_sym(s, field->print.ip, sym_flags);
1729                 trace_seq_printf(s, ": %s", field->print.buf);
1730                 if (field->flags & TRACE_FLAG_CONT)
1731                         trace_seq_print_cont(s, iter);
1732                 break;
1733         }
1734         return 1;
1735 }
1736
1737 static int print_raw_fmt(struct trace_iterator *iter)
1738 {
1739         struct trace_seq *s = &iter->seq;
1740         struct trace_entry *entry;
1741         struct trace_field *field;
1742         int ret;
1743         int S, T;
1744
1745         entry = iter->ent;
1746
1747         if (entry->type == TRACE_CONT)
1748                 return 1;
1749
1750         field = &entry->field;
1751
1752         ret = trace_seq_printf(s, "%d %d %llu ",
1753                 field->pid, iter->cpu, field->t);
1754         if (!ret)
1755                 return 0;
1756
1757         switch (entry->type) {
1758         case TRACE_FN:
1759                 ret = trace_seq_printf(s, "%x %x\n",
1760                                         field->fn.ip,
1761                                         field->fn.parent_ip);
1762                 if (!ret)
1763                         return 0;
1764                 break;
1765         case TRACE_CTX:
1766         case TRACE_WAKE:
1767                 S = field->ctx.prev_state < sizeof(state_to_char) ?
1768                         state_to_char[field->ctx.prev_state] : 'X';
1769                 T = field->ctx.next_state < sizeof(state_to_char) ?
1770                         state_to_char[field->ctx.next_state] : 'X';
1771                 if (entry->type == TRACE_WAKE)
1772                         S = '+';
1773                 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
1774                                        field->ctx.prev_pid,
1775                                        field->ctx.prev_prio,
1776                                        S,
1777                                        field->ctx.next_cpu,
1778                                        field->ctx.next_pid,
1779                                        field->ctx.next_prio,
1780                                        T);
1781                 if (!ret)
1782                         return 0;
1783                 break;
1784         case TRACE_SPECIAL:
1785         case TRACE_STACK:
1786                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1787                                  field->special.arg1,
1788                                  field->special.arg2,
1789                                  field->special.arg3);
1790                 if (!ret)
1791                         return 0;
1792                 break;
1793         case TRACE_PRINT:
1794                 trace_seq_printf(s, "# %lx %s",
1795                                  field->print.ip, field->print.buf);
1796                 if (field->flags & TRACE_FLAG_CONT)
1797                         trace_seq_print_cont(s, iter);
1798                 break;
1799         }
1800         return 1;
1801 }
1802
1803 #define SEQ_PUT_FIELD_RET(s, x)                         \
1804 do {                                                    \
1805         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
1806                 return 0;                               \
1807 } while (0)
1808
1809 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
1810 do {                                                    \
1811         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
1812                 return 0;                               \
1813 } while (0)
1814
1815 static int print_hex_fmt(struct trace_iterator *iter)
1816 {
1817         struct trace_seq *s = &iter->seq;
1818         unsigned char newline = '\n';
1819         struct trace_entry *entry;
1820         struct trace_field *field;
1821         int S, T;
1822
1823         entry = iter->ent;
1824
1825         if (entry->type == TRACE_CONT)
1826                 return 1;
1827
1828         field = &entry->field;
1829
1830         SEQ_PUT_HEX_FIELD_RET(s, field->pid);
1831         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1832         SEQ_PUT_HEX_FIELD_RET(s, field->t);
1833
1834         switch (entry->type) {
1835         case TRACE_FN:
1836                 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1837                 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
1838                 break;
1839         case TRACE_CTX:
1840         case TRACE_WAKE:
1841                 S = field->ctx.prev_state < sizeof(state_to_char) ?
1842                         state_to_char[field->ctx.prev_state] : 'X';
1843                 T = field->ctx.next_state < sizeof(state_to_char) ?
1844                         state_to_char[field->ctx.next_state] : 'X';
1845                 if (entry->type == TRACE_WAKE)
1846                         S = '+';
1847                 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1848                 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
1849                 SEQ_PUT_HEX_FIELD_RET(s, S);
1850                 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_cpu);
1851                 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1852                 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
1853                 SEQ_PUT_HEX_FIELD_RET(s, T);
1854                 break;
1855         case TRACE_SPECIAL:
1856         case TRACE_STACK:
1857                 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1858                 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1859                 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
1860                 break;
1861         }
1862         SEQ_PUT_FIELD_RET(s, newline);
1863
1864         return 1;
1865 }
1866
1867 static int print_bin_fmt(struct trace_iterator *iter)
1868 {
1869         struct trace_seq *s = &iter->seq;
1870         struct trace_entry *entry;
1871         struct trace_field *field;
1872
1873         entry = iter->ent;
1874
1875         if (entry->type == TRACE_CONT)
1876                 return 1;
1877
1878         field = &entry->field;
1879
1880         SEQ_PUT_FIELD_RET(s, field->pid);
1881         SEQ_PUT_FIELD_RET(s, field->cpu);
1882         SEQ_PUT_FIELD_RET(s, field->t);
1883
1884         switch (entry->type) {
1885         case TRACE_FN:
1886                 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1887                 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
1888                 break;
1889         case TRACE_CTX:
1890                 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1891                 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1892                 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1893                 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1894                 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1895                 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
1896                 break;
1897         case TRACE_SPECIAL:
1898         case TRACE_STACK:
1899                 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1900                 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1901                 SEQ_PUT_FIELD_RET(s, field->special.arg3);
1902                 break;
1903         }
1904         return 1;
1905 }
1906
1907 static int trace_empty(struct trace_iterator *iter)
1908 {
1909         struct trace_array_cpu *data;
1910         int cpu;
1911
1912         for_each_tracing_cpu(cpu) {
1913                 data = iter->tr->data[cpu];
1914
1915                 if (head_page(data) && data->trace_idx &&
1916                     (data->trace_tail != data->trace_head ||
1917                      data->trace_tail_idx != data->trace_head_idx))
1918                         return 0;
1919         }
1920         return 1;
1921 }
1922
1923 static int print_trace_line(struct trace_iterator *iter)
1924 {
1925         if (iter->trace && iter->trace->print_line)
1926                 return iter->trace->print_line(iter);
1927
1928         if (trace_flags & TRACE_ITER_BIN)
1929                 return print_bin_fmt(iter);
1930
1931         if (trace_flags & TRACE_ITER_HEX)
1932                 return print_hex_fmt(iter);
1933
1934         if (trace_flags & TRACE_ITER_RAW)
1935                 return print_raw_fmt(iter);
1936
1937         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1938                 return print_lat_fmt(iter, iter->idx, iter->cpu);
1939
1940         return print_trace_fmt(iter);
1941 }
1942
1943 static int s_show(struct seq_file *m, void *v)
1944 {
1945         struct trace_iterator *iter = v;
1946
1947         if (iter->ent == NULL) {
1948                 if (iter->tr) {
1949                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1950                         seq_puts(m, "#\n");
1951                 }
1952                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1953                         /* print nothing if the buffers are empty */
1954                         if (trace_empty(iter))
1955                                 return 0;
1956                         print_trace_header(m, iter);
1957                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1958                                 print_lat_help_header(m);
1959                 } else {
1960                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1961                                 print_func_help_header(m);
1962                 }
1963         } else {
1964                 print_trace_line(iter);
1965                 trace_print_seq(m, &iter->seq);
1966         }
1967
1968         return 0;
1969 }
1970
1971 static struct seq_operations tracer_seq_ops = {
1972         .start          = s_start,
1973         .next           = s_next,
1974         .stop           = s_stop,
1975         .show           = s_show,
1976 };
1977
1978 static struct trace_iterator *
1979 __tracing_open(struct inode *inode, struct file *file, int *ret)
1980 {
1981         struct trace_iterator *iter;
1982
1983         if (tracing_disabled) {
1984                 *ret = -ENODEV;
1985                 return NULL;
1986         }
1987
1988         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1989         if (!iter) {
1990                 *ret = -ENOMEM;
1991                 goto out;
1992         }
1993
1994         mutex_lock(&trace_types_lock);
1995         if (current_trace && current_trace->print_max)
1996                 iter->tr = &max_tr;
1997         else
1998                 iter->tr = inode->i_private;
1999         iter->trace = current_trace;
2000         iter->pos = -1;
2001
2002         /* TODO stop tracer */
2003         *ret = seq_open(file, &tracer_seq_ops);
2004         if (!*ret) {
2005                 struct seq_file *m = file->private_data;
2006                 m->private = iter;
2007
2008                 /* stop the trace while dumping */
2009                 if (iter->tr->ctrl) {
2010                         tracer_enabled = 0;
2011                         ftrace_function_enabled = 0;
2012                 }
2013
2014                 if (iter->trace && iter->trace->open)
2015                         iter->trace->open(iter);
2016         } else {
2017                 kfree(iter);
2018                 iter = NULL;
2019         }
2020         mutex_unlock(&trace_types_lock);
2021
2022  out:
2023         return iter;
2024 }
2025
2026 int tracing_open_generic(struct inode *inode, struct file *filp)
2027 {
2028         if (tracing_disabled)
2029                 return -ENODEV;
2030
2031         filp->private_data = inode->i_private;
2032         return 0;
2033 }
2034
2035 int tracing_release(struct inode *inode, struct file *file)
2036 {
2037         struct seq_file *m = (struct seq_file *)file->private_data;
2038         struct trace_iterator *iter = m->private;
2039
2040         mutex_lock(&trace_types_lock);
2041         if (iter->trace && iter->trace->close)
2042                 iter->trace->close(iter);
2043
2044         /* reenable tracing if it was previously enabled */
2045         if (iter->tr->ctrl) {
2046                 tracer_enabled = 1;
2047                 /*
2048                  * It is safe to enable function tracing even if it
2049                  * isn't used
2050                  */
2051                 ftrace_function_enabled = 1;
2052         }
2053         mutex_unlock(&trace_types_lock);
2054
2055         seq_release(inode, file);
2056         kfree(iter);
2057         return 0;
2058 }
2059
2060 static int tracing_open(struct inode *inode, struct file *file)
2061 {
2062         int ret;
2063
2064         __tracing_open(inode, file, &ret);
2065
2066         return ret;
2067 }
2068
2069 static int tracing_lt_open(struct inode *inode, struct file *file)
2070 {
2071         struct trace_iterator *iter;
2072         int ret;
2073
2074         iter = __tracing_open(inode, file, &ret);
2075
2076         if (!ret)
2077                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2078
2079         return ret;
2080 }
2081
2082
2083 static void *
2084 t_next(struct seq_file *m, void *v, loff_t *pos)
2085 {
2086         struct tracer *t = m->private;
2087
2088         (*pos)++;
2089
2090         if (t)
2091                 t = t->next;
2092
2093         m->private = t;
2094
2095         return t;
2096 }
2097
2098 static void *t_start(struct seq_file *m, loff_t *pos)
2099 {
2100         struct tracer *t = m->private;
2101         loff_t l = 0;
2102
2103         mutex_lock(&trace_types_lock);
2104         for (; t && l < *pos; t = t_next(m, t, &l))
2105                 ;
2106
2107         return t;
2108 }
2109
2110 static void t_stop(struct seq_file *m, void *p)
2111 {
2112         mutex_unlock(&trace_types_lock);
2113 }
2114
2115 static int t_show(struct seq_file *m, void *v)
2116 {
2117         struct tracer *t = v;
2118
2119         if (!t)
2120                 return 0;
2121
2122         seq_printf(m, "%s", t->name);
2123         if (t->next)
2124                 seq_putc(m, ' ');
2125         else
2126                 seq_putc(m, '\n');
2127
2128         return 0;
2129 }
2130
2131 static struct seq_operations show_traces_seq_ops = {
2132         .start          = t_start,
2133         .next           = t_next,
2134         .stop           = t_stop,
2135         .show           = t_show,
2136 };
2137
2138 static int show_traces_open(struct inode *inode, struct file *file)
2139 {
2140         int ret;
2141
2142         if (tracing_disabled)
2143                 return -ENODEV;
2144
2145         ret = seq_open(file, &show_traces_seq_ops);
2146         if (!ret) {
2147                 struct seq_file *m = file->private_data;
2148                 m->private = trace_types;
2149         }
2150
2151         return ret;
2152 }
2153
2154 static struct file_operations tracing_fops = {
2155         .open           = tracing_open,
2156         .read           = seq_read,
2157         .llseek         = seq_lseek,
2158         .release        = tracing_release,
2159 };
2160
2161 static struct file_operations tracing_lt_fops = {
2162         .open           = tracing_lt_open,
2163         .read           = seq_read,
2164         .llseek         = seq_lseek,
2165         .release        = tracing_release,
2166 };
2167
2168 static struct file_operations show_traces_fops = {
2169         .open           = show_traces_open,
2170         .read           = seq_read,
2171         .release        = seq_release,
2172 };
2173
2174 /*
2175  * Only trace on a CPU if the bitmask is set:
2176  */
2177 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2178
2179 /*
2180  * When tracing/tracing_cpu_mask is modified then this holds
2181  * the new bitmask we are about to install:
2182  */
2183 static cpumask_t tracing_cpumask_new;
2184
2185 /*
2186  * The tracer itself will not take this lock, but still we want
2187  * to provide a consistent cpumask to user-space:
2188  */
2189 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2190
2191 /*
2192  * Temporary storage for the character representation of the
2193  * CPU bitmask (and one more byte for the newline):
2194  */
2195 static char mask_str[NR_CPUS + 1];
2196
2197 static ssize_t
2198 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2199                      size_t count, loff_t *ppos)
2200 {
2201         int len;
2202
2203         mutex_lock(&tracing_cpumask_update_lock);
2204
2205         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2206         if (count - len < 2) {
2207                 count = -EINVAL;
2208                 goto out_err;
2209         }
2210         len += sprintf(mask_str + len, "\n");
2211         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2212
2213 out_err:
2214         mutex_unlock(&tracing_cpumask_update_lock);
2215
2216         return count;
2217 }
2218
2219 static ssize_t
2220 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2221                       size_t count, loff_t *ppos)
2222 {
2223         int err, cpu;
2224
2225         mutex_lock(&tracing_cpumask_update_lock);
2226         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2227         if (err)
2228                 goto err_unlock;
2229
2230         raw_local_irq_disable();
2231         __raw_spin_lock(&ftrace_max_lock);
2232         for_each_tracing_cpu(cpu) {
2233                 /*
2234                  * Increase/decrease the disabled counter if we are
2235                  * about to flip a bit in the cpumask:
2236                  */
2237                 if (cpu_isset(cpu, tracing_cpumask) &&
2238                                 !cpu_isset(cpu, tracing_cpumask_new)) {
2239                         atomic_inc(&global_trace.data[cpu]->disabled);
2240                 }
2241                 if (!cpu_isset(cpu, tracing_cpumask) &&
2242                                 cpu_isset(cpu, tracing_cpumask_new)) {
2243                         atomic_dec(&global_trace.data[cpu]->disabled);
2244                 }
2245         }
2246         __raw_spin_unlock(&ftrace_max_lock);
2247         raw_local_irq_enable();
2248
2249         tracing_cpumask = tracing_cpumask_new;
2250
2251         mutex_unlock(&tracing_cpumask_update_lock);
2252
2253         return count;
2254
2255 err_unlock:
2256         mutex_unlock(&tracing_cpumask_update_lock);
2257
2258         return err;
2259 }
2260
2261 static struct file_operations tracing_cpumask_fops = {
2262         .open           = tracing_open_generic,
2263         .read           = tracing_cpumask_read,
2264         .write          = tracing_cpumask_write,
2265 };
2266
2267 static ssize_t
2268 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2269                        size_t cnt, loff_t *ppos)
2270 {
2271         char *buf;
2272         int r = 0;
2273         int len = 0;
2274         int i;
2275
2276         /* calulate max size */
2277         for (i = 0; trace_options[i]; i++) {
2278                 len += strlen(trace_options[i]);
2279                 len += 3; /* "no" and space */
2280         }
2281
2282         /* +2 for \n and \0 */
2283         buf = kmalloc(len + 2, GFP_KERNEL);
2284         if (!buf)
2285                 return -ENOMEM;
2286
2287         for (i = 0; trace_options[i]; i++) {
2288                 if (trace_flags & (1 << i))
2289                         r += sprintf(buf + r, "%s ", trace_options[i]);
2290                 else
2291                         r += sprintf(buf + r, "no%s ", trace_options[i]);
2292         }
2293
2294         r += sprintf(buf + r, "\n");
2295         WARN_ON(r >= len + 2);
2296
2297         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2298
2299         kfree(buf);
2300
2301         return r;
2302 }
2303
2304 static ssize_t
2305 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2306                         size_t cnt, loff_t *ppos)
2307 {
2308         char buf[64];
2309         char *cmp = buf;
2310         int neg = 0;
2311         int i;
2312
2313         if (cnt >= sizeof(buf))
2314                 return -EINVAL;
2315
2316         if (copy_from_user(&buf, ubuf, cnt))
2317                 return -EFAULT;
2318
2319         buf[cnt] = 0;
2320
2321         if (strncmp(buf, "no", 2) == 0) {
2322                 neg = 1;
2323                 cmp += 2;
2324         }
2325
2326         for (i = 0; trace_options[i]; i++) {
2327                 int len = strlen(trace_options[i]);
2328
2329                 if (strncmp(cmp, trace_options[i], len) == 0) {
2330                         if (neg)
2331                                 trace_flags &= ~(1 << i);
2332                         else
2333                                 trace_flags |= (1 << i);
2334                         break;
2335                 }
2336         }
2337         /*
2338          * If no option could be set, return an error:
2339          */
2340         if (!trace_options[i])
2341                 return -EINVAL;
2342
2343         filp->f_pos += cnt;
2344
2345         return cnt;
2346 }
2347
2348 static struct file_operations tracing_iter_fops = {
2349         .open           = tracing_open_generic,
2350         .read           = tracing_iter_ctrl_read,
2351         .write          = tracing_iter_ctrl_write,
2352 };
2353
2354 static const char readme_msg[] =
2355         "tracing mini-HOWTO:\n\n"
2356         "# mkdir /debug\n"
2357         "# mount -t debugfs nodev /debug\n\n"
2358         "# cat /debug/tracing/available_tracers\n"
2359         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2360         "# cat /debug/tracing/current_tracer\n"
2361         "none\n"
2362         "# echo sched_switch > /debug/tracing/current_tracer\n"
2363         "# cat /debug/tracing/current_tracer\n"
2364         "sched_switch\n"
2365         "# cat /debug/tracing/iter_ctrl\n"
2366         "noprint-parent nosym-offset nosym-addr noverbose\n"
2367         "# echo print-parent > /debug/tracing/iter_ctrl\n"
2368         "# echo 1 > /debug/tracing/tracing_enabled\n"
2369         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2370         "echo 0 > /debug/tracing/tracing_enabled\n"
2371 ;
2372
2373 static ssize_t
2374 tracing_readme_read(struct file *filp, char __user *ubuf,
2375                        size_t cnt, loff_t *ppos)
2376 {
2377         return simple_read_from_buffer(ubuf, cnt, ppos,
2378                                         readme_msg, strlen(readme_msg));
2379 }
2380
2381 static struct file_operations tracing_readme_fops = {
2382         .open           = tracing_open_generic,
2383         .read           = tracing_readme_read,
2384 };
2385
2386 static ssize_t
2387 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2388                   size_t cnt, loff_t *ppos)
2389 {
2390         struct trace_array *tr = filp->private_data;
2391         char buf[64];
2392         int r;
2393
2394         r = sprintf(buf, "%ld\n", tr->ctrl);
2395         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2396 }
2397
2398 static ssize_t
2399 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2400                    size_t cnt, loff_t *ppos)
2401 {
2402         struct trace_array *tr = filp->private_data;
2403         char buf[64];
2404         long val;
2405         int ret;
2406
2407         if (cnt >= sizeof(buf))
2408                 return -EINVAL;
2409
2410         if (copy_from_user(&buf, ubuf, cnt))
2411                 return -EFAULT;
2412
2413         buf[cnt] = 0;
2414
2415         ret = strict_strtoul(buf, 10, &val);
2416         if (ret < 0)
2417                 return ret;
2418
2419         val = !!val;
2420
2421         mutex_lock(&trace_types_lock);
2422         if (tr->ctrl ^ val) {
2423                 if (val)
2424                         tracer_enabled = 1;
2425                 else
2426                         tracer_enabled = 0;
2427
2428                 tr->ctrl = val;
2429
2430                 if (current_trace && current_trace->ctrl_update)
2431                         current_trace->ctrl_update(tr);
2432         }
2433         mutex_unlock(&trace_types_lock);
2434
2435         filp->f_pos += cnt;
2436
2437         return cnt;
2438 }
2439
2440 static ssize_t
2441 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2442                        size_t cnt, loff_t *ppos)
2443 {
2444         char buf[max_tracer_type_len+2];
2445         int r;
2446
2447         mutex_lock(&trace_types_lock);
2448         if (current_trace)
2449                 r = sprintf(buf, "%s\n", current_trace->name);
2450         else
2451                 r = sprintf(buf, "\n");
2452         mutex_unlock(&trace_types_lock);
2453
2454         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2455 }
2456
2457 static ssize_t
2458 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2459                         size_t cnt, loff_t *ppos)
2460 {
2461         struct trace_array *tr = &global_trace;
2462         struct tracer *t;
2463         char buf[max_tracer_type_len+1];
2464         int i;
2465
2466         if (cnt > max_tracer_type_len)
2467                 cnt = max_tracer_type_len;
2468
2469         if (copy_from_user(&buf, ubuf, cnt))
2470                 return -EFAULT;
2471
2472         buf[cnt] = 0;
2473
2474         /* strip ending whitespace. */
2475         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2476                 buf[i] = 0;
2477
2478         mutex_lock(&trace_types_lock);
2479         for (t = trace_types; t; t = t->next) {
2480                 if (strcmp(t->name, buf) == 0)
2481                         break;
2482         }
2483         if (!t || t == current_trace)
2484                 goto out;
2485
2486         if (current_trace && current_trace->reset)
2487                 current_trace->reset(tr);
2488
2489         current_trace = t;
2490         if (t->init)
2491                 t->init(tr);
2492
2493  out:
2494         mutex_unlock(&trace_types_lock);
2495
2496         filp->f_pos += cnt;
2497
2498         return cnt;
2499 }
2500
2501 static ssize_t
2502 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2503                      size_t cnt, loff_t *ppos)
2504 {
2505         unsigned long *ptr = filp->private_data;
2506         char buf[64];
2507         int r;
2508
2509         r = snprintf(buf, sizeof(buf), "%ld\n",
2510                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2511         if (r > sizeof(buf))
2512                 r = sizeof(buf);
2513         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2514 }
2515
2516 static ssize_t
2517 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2518                       size_t cnt, loff_t *ppos)
2519 {
2520         long *ptr = filp->private_data;
2521         char buf[64];
2522         long val;
2523         int ret;
2524
2525         if (cnt >= sizeof(buf))
2526                 return -EINVAL;
2527
2528         if (copy_from_user(&buf, ubuf, cnt))
2529                 return -EFAULT;
2530
2531         buf[cnt] = 0;
2532
2533         ret = strict_strtoul(buf, 10, &val);
2534         if (ret < 0)
2535                 return ret;
2536
2537         *ptr = val * 1000;
2538
2539         return cnt;
2540 }
2541
2542 static atomic_t tracing_reader;
2543
2544 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2545 {
2546         struct trace_iterator *iter;
2547
2548         if (tracing_disabled)
2549                 return -ENODEV;
2550
2551         /* We only allow for reader of the pipe */
2552         if (atomic_inc_return(&tracing_reader) != 1) {
2553                 atomic_dec(&tracing_reader);
2554                 return -EBUSY;
2555         }
2556
2557         /* create a buffer to store the information to pass to userspace */
2558         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2559         if (!iter)
2560                 return -ENOMEM;
2561
2562         mutex_lock(&trace_types_lock);
2563         iter->tr = &global_trace;
2564         iter->trace = current_trace;
2565         filp->private_data = iter;
2566
2567         if (iter->trace->pipe_open)
2568                 iter->trace->pipe_open(iter);
2569         mutex_unlock(&trace_types_lock);
2570
2571         return 0;
2572 }
2573
2574 static int tracing_release_pipe(struct inode *inode, struct file *file)
2575 {
2576         struct trace_iterator *iter = file->private_data;
2577
2578         kfree(iter);
2579         atomic_dec(&tracing_reader);
2580
2581         return 0;
2582 }
2583
2584 static unsigned int
2585 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2586 {
2587         struct trace_iterator *iter = filp->private_data;
2588
2589         if (trace_flags & TRACE_ITER_BLOCK) {
2590                 /*
2591                  * Always select as readable when in blocking mode
2592                  */
2593                 return POLLIN | POLLRDNORM;
2594         } else {
2595                 if (!trace_empty(iter))
2596                         return POLLIN | POLLRDNORM;
2597                 poll_wait(filp, &trace_wait, poll_table);
2598                 if (!trace_empty(iter))
2599                         return POLLIN | POLLRDNORM;
2600
2601                 return 0;
2602         }
2603 }
2604
2605 /*
2606  * Consumer reader.
2607  */
2608 static ssize_t
2609 tracing_read_pipe(struct file *filp, char __user *ubuf,
2610                   size_t cnt, loff_t *ppos)
2611 {
2612         struct trace_iterator *iter = filp->private_data;
2613         struct trace_array_cpu *data;
2614         static cpumask_t mask;
2615         unsigned long flags;
2616 #ifdef CONFIG_FTRACE
2617         int ftrace_save;
2618 #endif
2619         int cpu;
2620         ssize_t sret;
2621
2622         /* return any leftover data */
2623         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2624         if (sret != -EBUSY)
2625                 return sret;
2626         sret = 0;
2627
2628         trace_seq_reset(&iter->seq);
2629
2630         mutex_lock(&trace_types_lock);
2631         if (iter->trace->read) {
2632                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2633                 if (sret)
2634                         goto out;
2635         }
2636
2637         while (trace_empty(iter)) {
2638
2639                 if ((filp->f_flags & O_NONBLOCK)) {
2640                         sret = -EAGAIN;
2641                         goto out;
2642                 }
2643
2644                 /*
2645                  * This is a make-shift waitqueue. The reason we don't use
2646                  * an actual wait queue is because:
2647                  *  1) we only ever have one waiter
2648                  *  2) the tracing, traces all functions, we don't want
2649                  *     the overhead of calling wake_up and friends
2650                  *     (and tracing them too)
2651                  *     Anyway, this is really very primitive wakeup.
2652                  */
2653                 set_current_state(TASK_INTERRUPTIBLE);
2654                 iter->tr->waiter = current;
2655
2656                 mutex_unlock(&trace_types_lock);
2657
2658                 /* sleep for 100 msecs, and try again. */
2659                 schedule_timeout(HZ/10);
2660
2661                 mutex_lock(&trace_types_lock);
2662
2663                 iter->tr->waiter = NULL;
2664
2665                 if (signal_pending(current)) {
2666                         sret = -EINTR;
2667                         goto out;
2668                 }
2669
2670                 if (iter->trace != current_trace)
2671                         goto out;
2672
2673                 /*
2674                  * We block until we read something and tracing is disabled.
2675                  * We still block if tracing is disabled, but we have never
2676                  * read anything. This allows a user to cat this file, and
2677                  * then enable tracing. But after we have read something,
2678                  * we give an EOF when tracing is again disabled.
2679                  *
2680                  * iter->pos will be 0 if we haven't read anything.
2681                  */
2682                 if (!tracer_enabled && iter->pos)
2683                         break;
2684
2685                 continue;
2686         }
2687
2688         /* stop when tracing is finished */
2689         if (trace_empty(iter))
2690                 goto out;
2691
2692         if (cnt >= PAGE_SIZE)
2693                 cnt = PAGE_SIZE - 1;
2694
2695         /* reset all but tr, trace, and overruns */
2696         memset(&iter->seq, 0,
2697                sizeof(struct trace_iterator) -
2698                offsetof(struct trace_iterator, seq));
2699         iter->pos = -1;
2700
2701         /*
2702          * We need to stop all tracing on all CPUS to read the
2703          * the next buffer. This is a bit expensive, but is
2704          * not done often. We fill all what we can read,
2705          * and then release the locks again.
2706          */
2707
2708         cpus_clear(mask);
2709         local_irq_save(flags);
2710 #ifdef CONFIG_FTRACE
2711         ftrace_save = ftrace_enabled;
2712         ftrace_enabled = 0;
2713 #endif
2714         smp_wmb();
2715         for_each_tracing_cpu(cpu) {
2716                 data = iter->tr->data[cpu];
2717
2718                 if (!head_page(data) || !data->trace_idx)
2719                         continue;
2720
2721                 atomic_inc(&data->disabled);
2722                 cpu_set(cpu, mask);
2723         }
2724
2725         for_each_cpu_mask(cpu, mask) {
2726                 data = iter->tr->data[cpu];
2727                 __raw_spin_lock(&data->lock);
2728
2729                 if (data->overrun > iter->last_overrun[cpu])
2730                         iter->overrun[cpu] +=
2731                                 data->overrun - iter->last_overrun[cpu];
2732                 iter->last_overrun[cpu] = data->overrun;
2733         }
2734
2735         while (find_next_entry_inc(iter) != NULL) {
2736                 int ret;
2737                 int len = iter->seq.len;
2738
2739                 ret = print_trace_line(iter);
2740                 if (!ret) {
2741                         /* don't print partial lines */
2742                         iter->seq.len = len;
2743                         break;
2744                 }
2745
2746                 trace_consume(iter);
2747
2748                 if (iter->seq.len >= cnt)
2749                         break;
2750         }
2751
2752         for_each_cpu_mask(cpu, mask) {
2753                 data = iter->tr->data[cpu];
2754                 __raw_spin_unlock(&data->lock);
2755         }
2756
2757         for_each_cpu_mask(cpu, mask) {
2758                 data = iter->tr->data[cpu];
2759                 atomic_dec(&data->disabled);
2760         }
2761 #ifdef CONFIG_FTRACE
2762         ftrace_enabled = ftrace_save;
2763 #endif
2764         local_irq_restore(flags);
2765
2766         /* Now copy what we have to the user */
2767         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2768         if (iter->seq.readpos >= iter->seq.len)
2769                 trace_seq_reset(&iter->seq);
2770         if (sret == -EBUSY)
2771                 sret = 0;
2772
2773 out:
2774         mutex_unlock(&trace_types_lock);
2775
2776         return sret;
2777 }
2778
2779 static ssize_t
2780 tracing_entries_read(struct file *filp, char __user *ubuf,
2781                      size_t cnt, loff_t *ppos)
2782 {
2783         struct trace_array *tr = filp->private_data;
2784         char buf[64];
2785         int r;
2786
2787         r = sprintf(buf, "%lu\n", tr->entries);
2788         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2789 }
2790
2791 static ssize_t
2792 tracing_entries_write(struct file *filp, const char __user *ubuf,
2793                       size_t cnt, loff_t *ppos)
2794 {
2795         unsigned long val;
2796         char buf[64];
2797         int i, ret;
2798
2799         if (cnt >= sizeof(buf))
2800                 return -EINVAL;
2801
2802         if (copy_from_user(&buf, ubuf, cnt))
2803                 return -EFAULT;
2804
2805         buf[cnt] = 0;
2806
2807         ret = strict_strtoul(buf, 10, &val);
2808         if (ret < 0)
2809                 return ret;
2810
2811         /* must have at least 1 entry */
2812         if (!val)
2813                 return -EINVAL;
2814
2815         mutex_lock(&trace_types_lock);
2816
2817         if (current_trace != &no_tracer) {
2818                 cnt = -EBUSY;
2819                 pr_info("ftrace: set current_tracer to none"
2820                         " before modifying buffer size\n");
2821                 goto out;
2822         }
2823
2824         if (val > global_trace.entries) {
2825                 long pages_requested;
2826                 unsigned long freeable_pages;
2827
2828                 /* make sure we have enough memory before mapping */
2829                 pages_requested =
2830                         (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2831
2832                 /* account for each buffer (and max_tr) */
2833                 pages_requested *= tracing_nr_buffers * 2;
2834
2835                 /* Check for overflow */
2836                 if (pages_requested < 0) {
2837                         cnt = -ENOMEM;
2838                         goto out;
2839                 }
2840
2841                 freeable_pages = determine_dirtyable_memory();
2842
2843                 /* we only allow to request 1/4 of useable memory */
2844                 if (pages_requested >
2845                     ((freeable_pages + tracing_pages_allocated) / 4)) {
2846                         cnt = -ENOMEM;
2847                         goto out;
2848                 }
2849
2850                 while (global_trace.entries < val) {
2851                         if (trace_alloc_page()) {
2852                                 cnt = -ENOMEM;
2853                                 goto out;
2854                         }
2855                         /* double check that we don't go over the known pages */
2856                         if (tracing_pages_allocated > pages_requested)
2857                                 break;
2858                 }
2859
2860         } else {
2861                 /* include the number of entries in val (inc of page entries) */
2862                 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2863                         trace_free_page();
2864         }
2865
2866         /* check integrity */
2867         for_each_tracing_cpu(i)
2868                 check_pages(global_trace.data[i]);
2869
2870         filp->f_pos += cnt;
2871
2872         /* If check pages failed, return ENOMEM */
2873         if (tracing_disabled)
2874                 cnt = -ENOMEM;
2875  out:
2876         max_tr.entries = global_trace.entries;
2877         mutex_unlock(&trace_types_lock);
2878
2879         return cnt;
2880 }
2881
2882 static struct file_operations tracing_max_lat_fops = {
2883         .open           = tracing_open_generic,
2884         .read           = tracing_max_lat_read,
2885         .write          = tracing_max_lat_write,
2886 };
2887
2888 static struct file_operations tracing_ctrl_fops = {
2889         .open           = tracing_open_generic,
2890         .read           = tracing_ctrl_read,
2891         .write          = tracing_ctrl_write,
2892 };
2893
2894 static struct file_operations set_tracer_fops = {
2895         .open           = tracing_open_generic,
2896         .read           = tracing_set_trace_read,
2897         .write          = tracing_set_trace_write,
2898 };
2899
2900 static struct file_operations tracing_pipe_fops = {
2901         .open           = tracing_open_pipe,
2902         .poll           = tracing_poll_pipe,
2903         .read           = tracing_read_pipe,
2904         .release        = tracing_release_pipe,
2905 };
2906
2907 static struct file_operations tracing_entries_fops = {
2908         .open           = tracing_open_generic,
2909         .read           = tracing_entries_read,
2910         .write          = tracing_entries_write,
2911 };
2912
2913 #ifdef CONFIG_DYNAMIC_FTRACE
2914
2915 static ssize_t
2916 tracing_read_long(struct file *filp, char __user *ubuf,
2917                   size_t cnt, loff_t *ppos)
2918 {
2919         unsigned long *p = filp->private_data;
2920         char buf[64];
2921         int r;
2922
2923         r = sprintf(buf, "%ld\n", *p);
2924
2925         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2926 }
2927
2928 static struct file_operations tracing_read_long_fops = {
2929         .open           = tracing_open_generic,
2930         .read           = tracing_read_long,
2931 };
2932 #endif
2933
2934 static struct dentry *d_tracer;
2935
2936 struct dentry *tracing_init_dentry(void)
2937 {
2938         static int once;
2939
2940         if (d_tracer)
2941                 return d_tracer;
2942
2943         d_tracer = debugfs_create_dir("tracing", NULL);
2944
2945         if (!d_tracer && !once) {
2946                 once = 1;
2947                 pr_warning("Could not create debugfs directory 'tracing'\n");
2948                 return NULL;
2949         }
2950
2951         return d_tracer;
2952 }
2953
2954 #ifdef CONFIG_FTRACE_SELFTEST
2955 /* Let selftest have access to static functions in this file */
2956 #include "trace_selftest.c"
2957 #endif
2958
2959 static __init void tracer_init_debugfs(void)
2960 {
2961         struct dentry *d_tracer;
2962         struct dentry *entry;
2963
2964         d_tracer = tracing_init_dentry();
2965
2966         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2967                                     &global_trace, &tracing_ctrl_fops);
2968         if (!entry)
2969                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2970
2971         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2972                                     NULL, &tracing_iter_fops);
2973         if (!entry)
2974                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2975
2976         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2977                                     NULL, &tracing_cpumask_fops);
2978         if (!entry)
2979                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2980
2981         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2982                                     &global_trace, &tracing_lt_fops);
2983         if (!entry)
2984                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2985
2986         entry = debugfs_create_file("trace", 0444, d_tracer,
2987                                     &global_trace, &tracing_fops);
2988         if (!entry)
2989                 pr_warning("Could not create debugfs 'trace' entry\n");
2990
2991         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2992                                     &global_trace, &show_traces_fops);
2993         if (!entry)
2994                 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2995
2996         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2997                                     &global_trace, &set_tracer_fops);
2998         if (!entry)
2999                 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3000
3001         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3002                                     &tracing_max_latency,
3003                                     &tracing_max_lat_fops);
3004         if (!entry)
3005                 pr_warning("Could not create debugfs "
3006                            "'tracing_max_latency' entry\n");
3007
3008         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3009                                     &tracing_thresh, &tracing_max_lat_fops);
3010         if (!entry)
3011                 pr_warning("Could not create debugfs "
3012                            "'tracing_thresh' entry\n");
3013         entry = debugfs_create_file("README", 0644, d_tracer,
3014                                     NULL, &tracing_readme_fops);
3015         if (!entry)
3016                 pr_warning("Could not create debugfs 'README' entry\n");
3017
3018         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3019                                     NULL, &tracing_pipe_fops);
3020         if (!entry)
3021                 pr_warning("Could not create debugfs "
3022                            "'trace_pipe' entry\n");
3023
3024         entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3025                                     &global_trace, &tracing_entries_fops);
3026         if (!entry)
3027                 pr_warning("Could not create debugfs "
3028                            "'trace_entries' entry\n");
3029
3030 #ifdef CONFIG_DYNAMIC_FTRACE
3031         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3032                                     &ftrace_update_tot_cnt,
3033                                     &tracing_read_long_fops);
3034         if (!entry)
3035                 pr_warning("Could not create debugfs "
3036                            "'dyn_ftrace_total_info' entry\n");
3037 #endif
3038 #ifdef CONFIG_SYSPROF_TRACER
3039         init_tracer_sysprof_debugfs(d_tracer);
3040 #endif
3041 }
3042
3043 #define TRACE_BUF_SIZE 1024
3044 #define TRACE_PRINT_BUF_SIZE \
3045         (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3046 #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3047
3048 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3049 {
3050         static DEFINE_SPINLOCK(trace_buf_lock);
3051         static char trace_buf[TRACE_BUF_SIZE];
3052
3053         struct trace_array *tr = &global_trace;
3054         struct trace_array_cpu *data;
3055         struct trace_entry *entry;
3056         unsigned long flags;
3057         long disabled;
3058         int cpu, len = 0, write, written = 0;
3059
3060         if (current_trace == &no_tracer || !tr->ctrl || tracing_disabled)
3061                 return 0;
3062
3063         local_irq_save(flags);
3064         cpu = raw_smp_processor_id();
3065         data = tr->data[cpu];
3066         disabled = atomic_inc_return(&data->disabled);
3067
3068         if (unlikely(disabled != 1))
3069                 goto out;
3070
3071         spin_lock(&trace_buf_lock);
3072         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3073
3074         len = min(len, TRACE_BUF_SIZE-1);
3075         trace_buf[len] = 0;
3076
3077         __raw_spin_lock(&data->lock);
3078         entry                           = tracing_get_trace_entry(tr, data);
3079         tracing_generic_entry_update(entry, flags);
3080         entry->type                     = TRACE_PRINT;
3081         entry->field.print.ip           = ip;
3082
3083         write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3084
3085         memcpy(&entry->field.print.buf, trace_buf, write);
3086         entry->field.print.buf[write] = 0;
3087         written = write;
3088
3089         if (written != len)
3090                 entry->field.flags |= TRACE_FLAG_CONT;
3091
3092         while (written != len) {
3093                 entry = tracing_get_trace_entry(tr, data);
3094
3095                 entry->type = TRACE_CONT;
3096                 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3097                 memcpy(&entry->cont.buf, trace_buf+written, write);
3098                 entry->cont.buf[write] = 0;
3099                 written += write;
3100         }
3101         __raw_spin_unlock(&data->lock);
3102
3103         spin_unlock(&trace_buf_lock);
3104
3105  out:
3106         atomic_dec(&data->disabled);
3107         local_irq_restore(flags);
3108
3109         return len;
3110 }
3111 EXPORT_SYMBOL_GPL(trace_vprintk);
3112
3113 int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3114 {
3115         int ret;
3116         va_list ap;
3117
3118         if (!(trace_flags & TRACE_ITER_PRINTK))
3119                 return 0;
3120
3121         va_start(ap, fmt);
3122         ret = trace_vprintk(ip, fmt, ap);
3123         va_end(ap);
3124         return ret;
3125 }
3126 EXPORT_SYMBOL_GPL(__ftrace_printk);
3127
3128 static int trace_panic_handler(struct notifier_block *this,
3129                                unsigned long event, void *unused)
3130 {
3131         ftrace_dump();
3132         return NOTIFY_OK;
3133 }
3134
3135 static struct notifier_block trace_panic_notifier = {
3136         .notifier_call  = trace_panic_handler,
3137         .next           = NULL,
3138         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
3139 };
3140
3141 static int trace_die_handler(struct notifier_block *self,
3142                              unsigned long val,
3143                              void *data)
3144 {
3145         switch (val) {
3146         case DIE_OOPS:
3147                 ftrace_dump();
3148                 break;
3149         default:
3150                 break;
3151         }
3152         return NOTIFY_OK;
3153 }
3154
3155 static struct notifier_block trace_die_notifier = {
3156         .notifier_call = trace_die_handler,
3157         .priority = 200
3158 };
3159
3160 /*
3161  * printk is set to max of 1024, we really don't need it that big.
3162  * Nothing should be printing 1000 characters anyway.
3163  */
3164 #define TRACE_MAX_PRINT         1000
3165
3166 /*
3167  * Define here KERN_TRACE so that we have one place to modify
3168  * it if we decide to change what log level the ftrace dump
3169  * should be at.
3170  */
3171 #define KERN_TRACE              KERN_INFO
3172
3173 static void
3174 trace_printk_seq(struct trace_seq *s)
3175 {
3176         /* Probably should print a warning here. */
3177         if (s->len >= 1000)
3178                 s->len = 1000;
3179
3180         /* should be zero ended, but we are paranoid. */
3181         s->buffer[s->len] = 0;
3182
3183         printk(KERN_TRACE "%s", s->buffer);
3184
3185         trace_seq_reset(s);
3186 }
3187
3188
3189 void ftrace_dump(void)
3190 {
3191         static DEFINE_SPINLOCK(ftrace_dump_lock);
3192         /* use static because iter can be a bit big for the stack */
3193         static struct trace_iterator iter;
3194         struct trace_array_cpu *data;
3195         static cpumask_t mask;
3196         static int dump_ran;
3197         unsigned long flags;
3198         int cnt = 0;
3199         int cpu;
3200
3201         /* only one dump */
3202         spin_lock_irqsave(&ftrace_dump_lock, flags);
3203         if (dump_ran)
3204                 goto out;
3205
3206         dump_ran = 1;
3207
3208         /* No turning back! */
3209         ftrace_kill_atomic();
3210
3211         printk(KERN_TRACE "Dumping ftrace buffer:\n");
3212
3213         iter.tr = &global_trace;
3214         iter.trace = current_trace;
3215
3216         /*
3217          * We need to stop all tracing on all CPUS to read the
3218          * the next buffer. This is a bit expensive, but is
3219          * not done often. We fill all what we can read,
3220          * and then release the locks again.
3221          */
3222
3223         cpus_clear(mask);
3224
3225         for_each_tracing_cpu(cpu) {
3226                 data = iter.tr->data[cpu];
3227
3228                 if (!head_page(data) || !data->trace_idx)
3229                         continue;
3230
3231                 atomic_inc(&data->disabled);
3232                 cpu_set(cpu, mask);
3233         }
3234
3235         for_each_cpu_mask(cpu, mask) {
3236                 data = iter.tr->data[cpu];
3237                 __raw_spin_lock(&data->lock);
3238
3239                 if (data->overrun > iter.last_overrun[cpu])
3240                         iter.overrun[cpu] +=
3241                                 data->overrun - iter.last_overrun[cpu];
3242                 iter.last_overrun[cpu] = data->overrun;
3243         }
3244
3245         while (!trace_empty(&iter)) {
3246
3247                 if (!cnt)
3248                         printk(KERN_TRACE "---------------------------------\n");
3249
3250                 cnt++;
3251
3252                 /* reset all but tr, trace, and overruns */
3253                 memset(&iter.seq, 0,
3254                        sizeof(struct trace_iterator) -
3255                        offsetof(struct trace_iterator, seq));
3256                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3257                 iter.pos = -1;
3258
3259                 if (find_next_entry_inc(&iter) != NULL) {
3260                         print_trace_line(&iter);
3261                         trace_consume(&iter);
3262                 }
3263
3264                 trace_printk_seq(&iter.seq);
3265         }
3266
3267         if (!cnt)
3268                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
3269         else
3270                 printk(KERN_TRACE "---------------------------------\n");
3271
3272         for_each_cpu_mask(cpu, mask) {
3273                 data = iter.tr->data[cpu];
3274                 __raw_spin_unlock(&data->lock);
3275         }
3276
3277         for_each_cpu_mask(cpu, mask) {
3278                 data = iter.tr->data[cpu];
3279                 atomic_dec(&data->disabled);
3280         }
3281
3282
3283  out:
3284         spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3285 }
3286
3287 static int trace_alloc_page(void)
3288 {
3289         struct trace_array_cpu *data;
3290         struct page *page, *tmp;
3291         LIST_HEAD(pages);
3292         void *array;
3293         unsigned pages_allocated = 0;
3294         int i;
3295
3296         /* first allocate a page for each CPU */
3297         for_each_tracing_cpu(i) {
3298                 array = (void *)__get_free_page(GFP_KERNEL);
3299                 if (array == NULL) {
3300                         printk(KERN_ERR "tracer: failed to allocate page"
3301                                "for trace buffer!\n");
3302                         goto free_pages;
3303                 }
3304
3305                 pages_allocated++;
3306                 page = virt_to_page(array);
3307                 list_add(&page->lru, &pages);
3308
3309 /* Only allocate if we are actually using the max trace */
3310 #ifdef CONFIG_TRACER_MAX_TRACE
3311                 array = (void *)__get_free_page(GFP_KERNEL);
3312                 if (array == NULL) {
3313                         printk(KERN_ERR "tracer: failed to allocate page"
3314                                "for trace buffer!\n");
3315                         goto free_pages;
3316                 }
3317                 pages_allocated++;
3318                 page = virt_to_page(array);
3319                 list_add(&page->lru, &pages);
3320 #endif
3321         }
3322
3323         /* Now that we successfully allocate a page per CPU, add them */
3324         for_each_tracing_cpu(i) {
3325                 data = global_trace.data[i];
3326                 page = list_entry(pages.next, struct page, lru);
3327                 list_del_init(&page->lru);
3328                 list_add_tail(&page->lru, &data->trace_pages);
3329                 ClearPageLRU(page);
3330
3331 #ifdef CONFIG_TRACER_MAX_TRACE
3332                 data = max_tr.data[i];
3333                 page = list_entry(pages.next, struct page, lru);
3334                 list_del_init(&page->lru);
3335                 list_add_tail(&page->lru, &data->trace_pages);
3336                 SetPageLRU(page);
3337 #endif
3338         }
3339         tracing_pages_allocated += pages_allocated;
3340         global_trace.entries += ENTRIES_PER_PAGE;
3341
3342         return 0;
3343
3344  free_pages:
3345         list_for_each_entry_safe(page, tmp, &pages, lru) {
3346                 list_del_init(&page->lru);
3347                 __free_page(page);
3348         }
3349         return -ENOMEM;
3350 }
3351
3352 static int trace_free_page(void)
3353 {
3354         struct trace_array_cpu *data;
3355         struct page *page;
3356         struct list_head *p;
3357         int i;
3358         int ret = 0;
3359
3360         /* free one page from each buffer */
3361         for_each_tracing_cpu(i) {
3362                 data = global_trace.data[i];
3363                 p = data->trace_pages.next;
3364                 if (p == &data->trace_pages) {
3365                         /* should never happen */
3366                         WARN_ON(1);
3367                         tracing_disabled = 1;
3368                         ret = -1;
3369                         break;
3370                 }
3371                 page = list_entry(p, struct page, lru);
3372                 ClearPageLRU(page);
3373                 list_del(&page->lru);
3374                 tracing_pages_allocated--;
3375                 tracing_pages_allocated--;
3376                 __free_page(page);
3377
3378                 tracing_reset(data);
3379
3380 #ifdef CONFIG_TRACER_MAX_TRACE
3381                 data = max_tr.data[i];
3382                 p = data->trace_pages.next;
3383                 if (p == &data->trace_pages) {
3384                         /* should never happen */
3385                         WARN_ON(1);
3386                         tracing_disabled = 1;
3387                         ret = -1;
3388                         break;
3389                 }
3390                 page = list_entry(p, struct page, lru);
3391                 ClearPageLRU(page);
3392                 list_del(&page->lru);
3393                 __free_page(page);
3394
3395                 tracing_reset(data);
3396 #endif
3397         }
3398         global_trace.entries -= ENTRIES_PER_PAGE;
3399
3400         return ret;
3401 }
3402
3403 __init static int tracer_alloc_buffers(void)
3404 {
3405         struct trace_array_cpu *data;
3406         void *array;
3407         struct page *page;
3408         int pages = 0;
3409         int ret = -ENOMEM;
3410         int i;
3411
3412         /* TODO: make the number of buffers hot pluggable with CPUS */
3413         tracing_nr_buffers = num_possible_cpus();
3414         tracing_buffer_mask = cpu_possible_map;
3415
3416         /* Allocate the first page for all buffers */
3417         for_each_tracing_cpu(i) {
3418                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
3419                 max_tr.data[i] = &per_cpu(max_data, i);
3420
3421                 array = (void *)__get_free_page(GFP_KERNEL);
3422                 if (array == NULL) {
3423                         printk(KERN_ERR "tracer: failed to allocate page"
3424                                "for trace buffer!\n");
3425                         goto free_buffers;
3426                 }
3427
3428                 /* set the array to the list */
3429                 INIT_LIST_HEAD(&data->trace_pages);
3430                 page = virt_to_page(array);
3431                 list_add(&page->lru, &data->trace_pages);
3432                 /* use the LRU flag to differentiate the two buffers */
3433                 ClearPageLRU(page);
3434
3435                 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3436                 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3437
3438 /* Only allocate if we are actually using the max trace */
3439 #ifdef CONFIG_TRACER_MAX_TRACE
3440                 array = (void *)__get_free_page(GFP_KERNEL);
3441                 if (array == NULL) {
3442                         printk(KERN_ERR "tracer: failed to allocate page"
3443                                "for trace buffer!\n");
3444                         goto free_buffers;
3445                 }
3446
3447                 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3448                 page = virt_to_page(array);
3449                 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3450                 SetPageLRU(page);
3451 #endif
3452         }
3453
3454         /*
3455          * Since we allocate by orders of pages, we may be able to
3456          * round up a bit.
3457          */
3458         global_trace.entries = ENTRIES_PER_PAGE;
3459         pages++;
3460
3461         while (global_trace.entries < trace_nr_entries) {
3462                 if (trace_alloc_page())
3463                         break;
3464                 pages++;
3465         }
3466         max_tr.entries = global_trace.entries;
3467
3468         pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3469                 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
3470         pr_info("   actual entries %ld\n", global_trace.entries);
3471
3472         tracer_init_debugfs();
3473
3474         trace_init_cmdlines();
3475
3476         register_tracer(&no_tracer);
3477         current_trace = &no_tracer;
3478
3479         /* All seems OK, enable tracing */
3480         global_trace.ctrl = tracer_enabled;
3481         tracing_disabled = 0;
3482
3483         atomic_notifier_chain_register(&panic_notifier_list,
3484                                        &trace_panic_notifier);
3485
3486         register_die_notifier(&trace_die_notifier);
3487
3488         return 0;
3489
3490  free_buffers:
3491         for (i-- ; i >= 0; i--) {
3492                 struct page *page, *tmp;
3493                 struct trace_array_cpu *data = global_trace.data[i];
3494
3495                 if (data) {
3496                         list_for_each_entry_safe(page, tmp,
3497                                                  &data->trace_pages, lru) {
3498                                 list_del_init(&page->lru);
3499                                 __free_page(page);
3500                         }
3501                 }
3502
3503 #ifdef CONFIG_TRACER_MAX_TRACE
3504                 data = max_tr.data[i];
3505                 if (data) {
3506                         list_for_each_entry_safe(page, tmp,
3507                                                  &data->trace_pages, lru) {
3508                                 list_del_init(&page->lru);
3509                                 __free_page(page);
3510                         }
3511                 }
3512 #endif
3513         }
3514         return ret;
3515 }
3516 fs_initcall(tracer_alloc_buffers);