ftrace: simple clean ups
[linux-2.6] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/poll.h>
28 #include <linux/gfp.h>
29 #include <linux/fs.h>
30
31 #include <linux/stacktrace.h>
32
33 #include "trace.h"
34
35 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
36 unsigned long __read_mostly     tracing_thresh;
37
38 /* dummy trace to disable tracing */
39 static struct tracer no_tracer __read_mostly = {
40         .name           = "none",
41 };
42
43 static int trace_alloc_page(void);
44 static int trace_free_page(void);
45
46 static int tracing_disabled = 1;
47
48 long
49 ns2usecs(cycle_t nsec)
50 {
51         nsec += 500;
52         do_div(nsec, 1000);
53         return nsec;
54 }
55
56 cycle_t ftrace_now(int cpu)
57 {
58         return cpu_clock(cpu);
59 }
60
61 static struct trace_array       global_trace;
62
63 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
64
65 static struct trace_array       max_tr;
66
67 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
68
69 static int                      tracer_enabled = 1;
70 static unsigned long            trace_nr_entries = 65536UL;
71
72 static struct tracer            *trace_types __read_mostly;
73 static struct tracer            *current_trace __read_mostly;
74 static int                      max_tracer_type_len;
75
76 static DEFINE_MUTEX(trace_types_lock);
77 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
78
79 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
80
81 void trace_wake_up(void)
82 {
83         /*
84          * The runqueue_is_locked() can fail, but this is the best we
85          * have for now:
86          */
87         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
88                 wake_up(&trace_wait);
89 }
90
91 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
92
93 static int __init set_nr_entries(char *str)
94 {
95         if (!str)
96                 return 0;
97         trace_nr_entries = simple_strtoul(str, &str, 0);
98         return 1;
99 }
100 __setup("trace_entries=", set_nr_entries);
101
102 unsigned long nsecs_to_usecs(unsigned long nsecs)
103 {
104         return nsecs / 1000;
105 }
106
107 enum trace_flag_type {
108         TRACE_FLAG_IRQS_OFF             = 0x01,
109         TRACE_FLAG_NEED_RESCHED         = 0x02,
110         TRACE_FLAG_HARDIRQ              = 0x04,
111         TRACE_FLAG_SOFTIRQ              = 0x08,
112 };
113
114 #define TRACE_ITER_SYM_MASK \
115         (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
116
117 /* These must match the bit postions above */
118 static const char *trace_options[] = {
119         "print-parent",
120         "sym-offset",
121         "sym-addr",
122         "verbose",
123         "raw",
124         "hex",
125         "bin",
126         "block",
127         "stacktrace",
128         "sched-tree",
129         NULL
130 };
131
132 static raw_spinlock_t ftrace_max_lock =
133         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
134
135 /*
136  * Copy the new maximum trace into the separate maximum-trace
137  * structure. (this way the maximum trace is permanently saved,
138  * for later retrieval via /debugfs/tracing/latency_trace)
139  */
140 static void
141 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
142 {
143         struct trace_array_cpu *data = tr->data[cpu];
144
145         max_tr.cpu = cpu;
146         max_tr.time_start = data->preempt_timestamp;
147
148         data = max_tr.data[cpu];
149         data->saved_latency = tracing_max_latency;
150
151         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
152         data->pid = tsk->pid;
153         data->uid = tsk->uid;
154         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
155         data->policy = tsk->policy;
156         data->rt_priority = tsk->rt_priority;
157
158         /* record this tasks comm */
159         tracing_record_cmdline(current);
160 }
161
162 void check_pages(struct trace_array_cpu *data)
163 {
164         struct page *page, *tmp;
165
166         BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
167         BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
168
169         list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
170                 BUG_ON(page->lru.next->prev != &page->lru);
171                 BUG_ON(page->lru.prev->next != &page->lru);
172         }
173 }
174
175 void *head_page(struct trace_array_cpu *data)
176 {
177         struct page *page;
178
179         check_pages(data);
180         if (list_empty(&data->trace_pages))
181                 return NULL;
182
183         page = list_entry(data->trace_pages.next, struct page, lru);
184         BUG_ON(&page->lru == &data->trace_pages);
185
186         return page_address(page);
187 }
188
189 int
190 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
191 {
192         int len = (PAGE_SIZE - 1) - s->len;
193         va_list ap;
194         int ret;
195
196         if (!len)
197                 return 0;
198
199         va_start(ap, fmt);
200         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
201         va_end(ap);
202
203         /* If we can't write it all, don't bother writing anything */
204         if (ret >= len)
205                 return 0;
206
207         s->len += ret;
208
209         return len;
210 }
211
212 static int
213 trace_seq_puts(struct trace_seq *s, const char *str)
214 {
215         int len = strlen(str);
216
217         if (len > ((PAGE_SIZE - 1) - s->len))
218                 return 0;
219
220         memcpy(s->buffer + s->len, str, len);
221         s->len += len;
222
223         return len;
224 }
225
226 static int
227 trace_seq_putc(struct trace_seq *s, unsigned char c)
228 {
229         if (s->len >= (PAGE_SIZE - 1))
230                 return 0;
231
232         s->buffer[s->len++] = c;
233
234         return 1;
235 }
236
237 static int
238 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
239 {
240         if (len > ((PAGE_SIZE - 1) - s->len))
241                 return 0;
242
243         memcpy(s->buffer + s->len, mem, len);
244         s->len += len;
245
246         return len;
247 }
248
249 #define HEX_CHARS 17
250 static const char hex2asc[] = "0123456789abcdef";
251
252 static int
253 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
254 {
255         unsigned char hex[HEX_CHARS];
256         unsigned char *data = mem;
257         unsigned char byte;
258         int i, j;
259
260         BUG_ON(len >= HEX_CHARS);
261
262 #ifdef __BIG_ENDIAN
263         for (i = 0, j = 0; i < len; i++) {
264 #else
265         for (i = len-1, j = 0; i >= 0; i--) {
266 #endif
267                 byte = data[i];
268
269                 hex[j++] = hex2asc[byte & 0x0f];
270                 hex[j++] = hex2asc[byte >> 4];
271         }
272         hex[j++] = ' ';
273
274         return trace_seq_putmem(s, hex, j);
275 }
276
277 static void
278 trace_seq_reset(struct trace_seq *s)
279 {
280         s->len = 0;
281 }
282
283 static void
284 trace_print_seq(struct seq_file *m, struct trace_seq *s)
285 {
286         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
287
288         s->buffer[len] = 0;
289         seq_puts(m, s->buffer);
290
291         trace_seq_reset(s);
292 }
293
294 static void
295 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
296 {
297         struct list_head flip_pages;
298
299         INIT_LIST_HEAD(&flip_pages);
300
301         memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
302                 sizeof(struct trace_array_cpu) -
303                 offsetof(struct trace_array_cpu, trace_head_idx));
304
305         check_pages(tr1);
306         check_pages(tr2);
307         list_splice_init(&tr1->trace_pages, &flip_pages);
308         list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
309         list_splice_init(&flip_pages, &tr2->trace_pages);
310         BUG_ON(!list_empty(&flip_pages));
311         check_pages(tr1);
312         check_pages(tr2);
313 }
314
315 void
316 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
317 {
318         struct trace_array_cpu *data;
319         int i;
320
321         WARN_ON_ONCE(!irqs_disabled());
322         __raw_spin_lock(&ftrace_max_lock);
323         /* clear out all the previous traces */
324         for_each_possible_cpu(i) {
325                 data = tr->data[i];
326                 flip_trace(max_tr.data[i], data);
327                 tracing_reset(data);
328         }
329
330         __update_max_tr(tr, tsk, cpu);
331         __raw_spin_unlock(&ftrace_max_lock);
332 }
333
334 /**
335  * update_max_tr_single - only copy one trace over, and reset the rest
336  * @tr - tracer
337  * @tsk - task with the latency
338  * @cpu - the cpu of the buffer to copy.
339  */
340 void
341 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
342 {
343         struct trace_array_cpu *data = tr->data[cpu];
344         int i;
345
346         WARN_ON_ONCE(!irqs_disabled());
347         __raw_spin_lock(&ftrace_max_lock);
348         for_each_possible_cpu(i)
349                 tracing_reset(max_tr.data[i]);
350
351         flip_trace(max_tr.data[cpu], data);
352         tracing_reset(data);
353
354         __update_max_tr(tr, tsk, cpu);
355         __raw_spin_unlock(&ftrace_max_lock);
356 }
357
358 int register_tracer(struct tracer *type)
359 {
360         struct tracer *t;
361         int len;
362         int ret = 0;
363
364         if (!type->name) {
365                 pr_info("Tracer must have a name\n");
366                 return -1;
367         }
368
369         mutex_lock(&trace_types_lock);
370         for (t = trace_types; t; t = t->next) {
371                 if (strcmp(type->name, t->name) == 0) {
372                         /* already found */
373                         pr_info("Trace %s already registered\n",
374                                 type->name);
375                         ret = -1;
376                         goto out;
377                 }
378         }
379
380 #ifdef CONFIG_FTRACE_STARTUP_TEST
381         if (type->selftest) {
382                 struct tracer *saved_tracer = current_trace;
383                 struct trace_array_cpu *data;
384                 struct trace_array *tr = &global_trace;
385                 int saved_ctrl = tr->ctrl;
386                 int i;
387                 /*
388                  * Run a selftest on this tracer.
389                  * Here we reset the trace buffer, and set the current
390                  * tracer to be this tracer. The tracer can then run some
391                  * internal tracing to verify that everything is in order.
392                  * If we fail, we do not register this tracer.
393                  */
394                 for_each_possible_cpu(i) {
395                         data = tr->data[i];
396                         if (!head_page(data))
397                                 continue;
398                         tracing_reset(data);
399                 }
400                 current_trace = type;
401                 tr->ctrl = 0;
402                 /* the test is responsible for initializing and enabling */
403                 pr_info("Testing tracer %s: ", type->name);
404                 ret = type->selftest(type, tr);
405                 /* the test is responsible for resetting too */
406                 current_trace = saved_tracer;
407                 tr->ctrl = saved_ctrl;
408                 if (ret) {
409                         printk(KERN_CONT "FAILED!\n");
410                         goto out;
411                 }
412                 /* Only reset on passing, to avoid touching corrupted buffers */
413                 for_each_possible_cpu(i) {
414                         data = tr->data[i];
415                         if (!head_page(data))
416                                 continue;
417                         tracing_reset(data);
418                 }
419                 printk(KERN_CONT "PASSED\n");
420         }
421 #endif
422
423         type->next = trace_types;
424         trace_types = type;
425         len = strlen(type->name);
426         if (len > max_tracer_type_len)
427                 max_tracer_type_len = len;
428
429  out:
430         mutex_unlock(&trace_types_lock);
431
432         return ret;
433 }
434
435 void unregister_tracer(struct tracer *type)
436 {
437         struct tracer **t;
438         int len;
439
440         mutex_lock(&trace_types_lock);
441         for (t = &trace_types; *t; t = &(*t)->next) {
442                 if (*t == type)
443                         goto found;
444         }
445         pr_info("Trace %s not registered\n", type->name);
446         goto out;
447
448  found:
449         *t = (*t)->next;
450         if (strlen(type->name) != max_tracer_type_len)
451                 goto out;
452
453         max_tracer_type_len = 0;
454         for (t = &trace_types; *t; t = &(*t)->next) {
455                 len = strlen((*t)->name);
456                 if (len > max_tracer_type_len)
457                         max_tracer_type_len = len;
458         }
459  out:
460         mutex_unlock(&trace_types_lock);
461 }
462
463 void tracing_reset(struct trace_array_cpu *data)
464 {
465         data->trace_idx = 0;
466         data->trace_head = data->trace_tail = head_page(data);
467         data->trace_head_idx = 0;
468         data->trace_tail_idx = 0;
469 }
470
471 #define SAVED_CMDLINES 128
472 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
473 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
474 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
475 static int cmdline_idx;
476 static DEFINE_SPINLOCK(trace_cmdline_lock);
477 atomic_t trace_record_cmdline_disabled;
478
479 static void trace_init_cmdlines(void)
480 {
481         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
482         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
483         cmdline_idx = 0;
484 }
485
486 void trace_stop_cmdline_recording(void);
487
488 static void trace_save_cmdline(struct task_struct *tsk)
489 {
490         unsigned map;
491         unsigned idx;
492
493         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
494                 return;
495
496         /*
497          * It's not the end of the world if we don't get
498          * the lock, but we also don't want to spin
499          * nor do we want to disable interrupts,
500          * so if we miss here, then better luck next time.
501          */
502         if (!spin_trylock(&trace_cmdline_lock))
503                 return;
504
505         idx = map_pid_to_cmdline[tsk->pid];
506         if (idx >= SAVED_CMDLINES) {
507                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
508
509                 map = map_cmdline_to_pid[idx];
510                 if (map <= PID_MAX_DEFAULT)
511                         map_pid_to_cmdline[map] = (unsigned)-1;
512
513                 map_pid_to_cmdline[tsk->pid] = idx;
514
515                 cmdline_idx = idx;
516         }
517
518         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
519
520         spin_unlock(&trace_cmdline_lock);
521 }
522
523 static char *trace_find_cmdline(int pid)
524 {
525         char *cmdline = "<...>";
526         unsigned map;
527
528         if (!pid)
529                 return "<idle>";
530
531         if (pid > PID_MAX_DEFAULT)
532                 goto out;
533
534         map = map_pid_to_cmdline[pid];
535         if (map >= SAVED_CMDLINES)
536                 goto out;
537
538         cmdline = saved_cmdlines[map];
539
540  out:
541         return cmdline;
542 }
543
544 void tracing_record_cmdline(struct task_struct *tsk)
545 {
546         if (atomic_read(&trace_record_cmdline_disabled))
547                 return;
548
549         trace_save_cmdline(tsk);
550 }
551
552 static inline struct list_head *
553 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
554 {
555         /*
556          * Roundrobin - but skip the head (which is not a real page):
557          */
558         next = next->next;
559         if (unlikely(next == &data->trace_pages))
560                 next = next->next;
561         BUG_ON(next == &data->trace_pages);
562
563         return next;
564 }
565
566 static inline void *
567 trace_next_page(struct trace_array_cpu *data, void *addr)
568 {
569         struct list_head *next;
570         struct page *page;
571
572         page = virt_to_page(addr);
573
574         next = trace_next_list(data, &page->lru);
575         page = list_entry(next, struct page, lru);
576
577         return page_address(page);
578 }
579
580 static inline struct trace_entry *
581 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
582 {
583         unsigned long idx, idx_next;
584         struct trace_entry *entry;
585
586         data->trace_idx++;
587         idx = data->trace_head_idx;
588         idx_next = idx + 1;
589
590         BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
591
592         entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
593
594         if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
595                 data->trace_head = trace_next_page(data, data->trace_head);
596                 idx_next = 0;
597         }
598
599         if (data->trace_head == data->trace_tail &&
600             idx_next == data->trace_tail_idx) {
601                 /* overrun */
602                 data->trace_tail_idx++;
603                 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
604                         data->trace_tail =
605                                 trace_next_page(data, data->trace_tail);
606                         data->trace_tail_idx = 0;
607                 }
608         }
609
610         data->trace_head_idx = idx_next;
611
612         return entry;
613 }
614
615 static inline void
616 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
617 {
618         struct task_struct *tsk = current;
619         unsigned long pc;
620
621         pc = preempt_count();
622
623         entry->preempt_count    = pc & 0xff;
624         entry->pid              = (tsk) ? tsk->pid : 0;
625         entry->t                = ftrace_now(raw_smp_processor_id());
626         entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
627                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
628                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
629                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
630 }
631
632 void
633 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
634                unsigned long ip, unsigned long parent_ip, unsigned long flags)
635 {
636         struct trace_entry *entry;
637         unsigned long irq_flags;
638
639         raw_local_irq_save(irq_flags);
640         __raw_spin_lock(&data->lock);
641         entry                   = tracing_get_trace_entry(tr, data);
642         tracing_generic_entry_update(entry, flags);
643         entry->type             = TRACE_FN;
644         entry->fn.ip            = ip;
645         entry->fn.parent_ip     = parent_ip;
646         __raw_spin_unlock(&data->lock);
647         raw_local_irq_restore(irq_flags);
648 }
649
650 void
651 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
652        unsigned long ip, unsigned long parent_ip, unsigned long flags)
653 {
654         if (likely(!atomic_read(&data->disabled)))
655                 trace_function(tr, data, ip, parent_ip, flags);
656 }
657
658 void
659 __trace_special(void *__tr, void *__data,
660                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
661 {
662         struct trace_array_cpu *data = __data;
663         struct trace_array *tr = __tr;
664         struct trace_entry *entry;
665         unsigned long irq_flags;
666
667         raw_local_irq_save(irq_flags);
668         __raw_spin_lock(&data->lock);
669         entry                   = tracing_get_trace_entry(tr, data);
670         tracing_generic_entry_update(entry, 0);
671         entry->type             = TRACE_SPECIAL;
672         entry->special.arg1     = arg1;
673         entry->special.arg2     = arg2;
674         entry->special.arg3     = arg3;
675         __raw_spin_unlock(&data->lock);
676         raw_local_irq_restore(irq_flags);
677
678         trace_wake_up();
679 }
680
681 void __trace_stack(struct trace_array *tr,
682                    struct trace_array_cpu *data,
683                    unsigned long flags,
684                    int skip)
685 {
686         struct trace_entry *entry;
687         struct stack_trace trace;
688
689         if (!(trace_flags & TRACE_ITER_STACKTRACE))
690                 return;
691
692         entry                   = tracing_get_trace_entry(tr, data);
693         tracing_generic_entry_update(entry, flags);
694         entry->type             = TRACE_STACK;
695
696         memset(&entry->stack, 0, sizeof(entry->stack));
697
698         trace.nr_entries        = 0;
699         trace.max_entries       = FTRACE_STACK_ENTRIES;
700         trace.skip              = skip;
701         trace.entries           = entry->stack.caller;
702
703         save_stack_trace(&trace);
704 }
705
706 void
707 tracing_sched_switch_trace(struct trace_array *tr,
708                            struct trace_array_cpu *data,
709                            struct task_struct *prev,
710                            struct task_struct *next,
711                            unsigned long flags)
712 {
713         struct trace_entry *entry;
714         unsigned long irq_flags;
715
716         raw_local_irq_save(irq_flags);
717         __raw_spin_lock(&data->lock);
718         entry                   = tracing_get_trace_entry(tr, data);
719         tracing_generic_entry_update(entry, flags);
720         entry->type             = TRACE_CTX;
721         entry->ctx.prev_pid     = prev->pid;
722         entry->ctx.prev_prio    = prev->prio;
723         entry->ctx.prev_state   = prev->state;
724         entry->ctx.next_pid     = next->pid;
725         entry->ctx.next_prio    = next->prio;
726         entry->ctx.next_state   = next->state;
727         __trace_stack(tr, data, flags, 4);
728         __raw_spin_unlock(&data->lock);
729         raw_local_irq_restore(irq_flags);
730 }
731
732 void
733 tracing_sched_wakeup_trace(struct trace_array *tr,
734                            struct trace_array_cpu *data,
735                            struct task_struct *wakee,
736                            struct task_struct *curr,
737                            unsigned long flags)
738 {
739         struct trace_entry *entry;
740         unsigned long irq_flags;
741
742         raw_local_irq_save(irq_flags);
743         __raw_spin_lock(&data->lock);
744         entry                   = tracing_get_trace_entry(tr, data);
745         tracing_generic_entry_update(entry, flags);
746         entry->type             = TRACE_WAKE;
747         entry->ctx.prev_pid     = curr->pid;
748         entry->ctx.prev_prio    = curr->prio;
749         entry->ctx.prev_state   = curr->state;
750         entry->ctx.next_pid     = wakee->pid;
751         entry->ctx.next_prio    = wakee->prio;
752         entry->ctx.next_state   = wakee->state;
753         __trace_stack(tr, data, flags, 5);
754         __raw_spin_unlock(&data->lock);
755         raw_local_irq_restore(irq_flags);
756
757         trace_wake_up();
758 }
759
760 #ifdef CONFIG_FTRACE
761 static void
762 function_trace_call(unsigned long ip, unsigned long parent_ip)
763 {
764         struct trace_array *tr = &global_trace;
765         struct trace_array_cpu *data;
766         unsigned long flags;
767         long disabled;
768         int cpu;
769
770         if (unlikely(!tracer_enabled))
771                 return;
772
773         local_irq_save(flags);
774         cpu = raw_smp_processor_id();
775         data = tr->data[cpu];
776         disabled = atomic_inc_return(&data->disabled);
777
778         if (likely(disabled == 1))
779                 trace_function(tr, data, ip, parent_ip, flags);
780
781         atomic_dec(&data->disabled);
782         local_irq_restore(flags);
783 }
784
785 static struct ftrace_ops trace_ops __read_mostly =
786 {
787         .func = function_trace_call,
788 };
789
790 void tracing_start_function_trace(void)
791 {
792         register_ftrace_function(&trace_ops);
793 }
794
795 void tracing_stop_function_trace(void)
796 {
797         unregister_ftrace_function(&trace_ops);
798 }
799 #endif
800
801 enum trace_file_type {
802         TRACE_FILE_LAT_FMT      = 1,
803 };
804
805 static struct trace_entry *
806 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
807                 struct trace_iterator *iter, int cpu)
808 {
809         struct page *page;
810         struct trace_entry *array;
811
812         if (iter->next_idx[cpu] >= tr->entries ||
813             iter->next_idx[cpu] >= data->trace_idx ||
814             (data->trace_head == data->trace_tail &&
815              data->trace_head_idx == data->trace_tail_idx))
816                 return NULL;
817
818         if (!iter->next_page[cpu]) {
819                 /* Initialize the iterator for this cpu trace buffer */
820                 WARN_ON(!data->trace_tail);
821                 page = virt_to_page(data->trace_tail);
822                 iter->next_page[cpu] = &page->lru;
823                 iter->next_page_idx[cpu] = data->trace_tail_idx;
824         }
825
826         page = list_entry(iter->next_page[cpu], struct page, lru);
827         BUG_ON(&data->trace_pages == &page->lru);
828
829         array = page_address(page);
830
831         WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
832         return &array[iter->next_page_idx[cpu]];
833 }
834
835 static struct trace_entry *
836 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
837 {
838         struct trace_array *tr = iter->tr;
839         struct trace_entry *ent, *next = NULL;
840         int next_cpu = -1;
841         int cpu;
842
843         for_each_possible_cpu(cpu) {
844                 if (!head_page(tr->data[cpu]))
845                         continue;
846                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
847                 /*
848                  * Pick the entry with the smallest timestamp:
849                  */
850                 if (ent && (!next || ent->t < next->t)) {
851                         next = ent;
852                         next_cpu = cpu;
853                 }
854         }
855
856         if (ent_cpu)
857                 *ent_cpu = next_cpu;
858
859         return next;
860 }
861
862 static void trace_iterator_increment(struct trace_iterator *iter)
863 {
864         iter->idx++;
865         iter->next_idx[iter->cpu]++;
866         iter->next_page_idx[iter->cpu]++;
867
868         if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
869                 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
870
871                 iter->next_page_idx[iter->cpu] = 0;
872                 iter->next_page[iter->cpu] =
873                         trace_next_list(data, iter->next_page[iter->cpu]);
874         }
875 }
876
877 static void trace_consume(struct trace_iterator *iter)
878 {
879         struct trace_array_cpu *data = iter->tr->data[iter->cpu];
880
881         data->trace_tail_idx++;
882         if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
883                 data->trace_tail = trace_next_page(data, data->trace_tail);
884                 data->trace_tail_idx = 0;
885         }
886
887         /* Check if we empty it, then reset the index */
888         if (data->trace_head == data->trace_tail &&
889             data->trace_head_idx == data->trace_tail_idx)
890                 data->trace_idx = 0;
891 }
892
893 static void *find_next_entry_inc(struct trace_iterator *iter)
894 {
895         struct trace_entry *next;
896         int next_cpu = -1;
897
898         next = find_next_entry(iter, &next_cpu);
899
900         iter->prev_ent = iter->ent;
901         iter->prev_cpu = iter->cpu;
902
903         iter->ent = next;
904         iter->cpu = next_cpu;
905
906         if (next)
907                 trace_iterator_increment(iter);
908
909         return next ? iter : NULL;
910 }
911
912 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
913 {
914         struct trace_iterator *iter = m->private;
915         void *last_ent = iter->ent;
916         int i = (int)*pos;
917         void *ent;
918
919         (*pos)++;
920
921         /* can't go backwards */
922         if (iter->idx > i)
923                 return NULL;
924
925         if (iter->idx < 0)
926                 ent = find_next_entry_inc(iter);
927         else
928                 ent = iter;
929
930         while (ent && iter->idx < i)
931                 ent = find_next_entry_inc(iter);
932
933         iter->pos = *pos;
934
935         if (last_ent && !ent)
936                 seq_puts(m, "\n\nvim:ft=help\n");
937
938         return ent;
939 }
940
941 static void *s_start(struct seq_file *m, loff_t *pos)
942 {
943         struct trace_iterator *iter = m->private;
944         void *p = NULL;
945         loff_t l = 0;
946         int i;
947
948         mutex_lock(&trace_types_lock);
949
950         if (!current_trace || current_trace != iter->trace) {
951                 mutex_unlock(&trace_types_lock);
952                 return NULL;
953         }
954
955         atomic_inc(&trace_record_cmdline_disabled);
956
957         /* let the tracer grab locks here if needed */
958         if (current_trace->start)
959                 current_trace->start(iter);
960
961         if (*pos != iter->pos) {
962                 iter->ent = NULL;
963                 iter->cpu = 0;
964                 iter->idx = -1;
965                 iter->prev_ent = NULL;
966                 iter->prev_cpu = -1;
967
968                 for_each_possible_cpu(i) {
969                         iter->next_idx[i] = 0;
970                         iter->next_page[i] = NULL;
971                 }
972
973                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
974                         ;
975
976         } else {
977                 l = *pos - 1;
978                 p = s_next(m, p, &l);
979         }
980
981         return p;
982 }
983
984 static void s_stop(struct seq_file *m, void *p)
985 {
986         struct trace_iterator *iter = m->private;
987
988         atomic_dec(&trace_record_cmdline_disabled);
989
990         /* let the tracer release locks here if needed */
991         if (current_trace && current_trace == iter->trace && iter->trace->stop)
992                 iter->trace->stop(iter);
993
994         mutex_unlock(&trace_types_lock);
995 }
996
997 static int
998 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
999 {
1000 #ifdef CONFIG_KALLSYMS
1001         char str[KSYM_SYMBOL_LEN];
1002
1003         kallsyms_lookup(address, NULL, NULL, NULL, str);
1004
1005         return trace_seq_printf(s, fmt, str);
1006 #endif
1007         return 1;
1008 }
1009
1010 static int
1011 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1012                      unsigned long address)
1013 {
1014 #ifdef CONFIG_KALLSYMS
1015         char str[KSYM_SYMBOL_LEN];
1016
1017         sprint_symbol(str, address);
1018         return trace_seq_printf(s, fmt, str);
1019 #endif
1020         return 1;
1021 }
1022
1023 #ifndef CONFIG_64BIT
1024 # define IP_FMT "%08lx"
1025 #else
1026 # define IP_FMT "%016lx"
1027 #endif
1028
1029 static int
1030 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1031 {
1032         int ret;
1033
1034         if (!ip)
1035                 return trace_seq_printf(s, "0");
1036
1037         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1038                 ret = seq_print_sym_offset(s, "%s", ip);
1039         else
1040                 ret = seq_print_sym_short(s, "%s", ip);
1041
1042         if (!ret)
1043                 return 0;
1044
1045         if (sym_flags & TRACE_ITER_SYM_ADDR)
1046                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1047         return ret;
1048 }
1049
1050 static void print_lat_help_header(struct seq_file *m)
1051 {
1052         seq_puts(m, "#                _------=> CPU#            \n");
1053         seq_puts(m, "#               / _-----=> irqs-off        \n");
1054         seq_puts(m, "#              | / _----=> need-resched    \n");
1055         seq_puts(m, "#              || / _---=> hardirq/softirq \n");
1056         seq_puts(m, "#              ||| / _--=> preempt-depth   \n");
1057         seq_puts(m, "#              |||| /                      \n");
1058         seq_puts(m, "#              |||||     delay             \n");
1059         seq_puts(m, "#  cmd     pid ||||| time  |   caller      \n");
1060         seq_puts(m, "#     \\   /    |||||   \\   |   /           \n");
1061 }
1062
1063 static void print_func_help_header(struct seq_file *m)
1064 {
1065         seq_puts(m, "#           TASK-PID   CPU#    TIMESTAMP  FUNCTION\n");
1066         seq_puts(m, "#              | |      |          |         |\n");
1067 }
1068
1069
1070 static void
1071 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1072 {
1073         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1074         struct trace_array *tr = iter->tr;
1075         struct trace_array_cpu *data = tr->data[tr->cpu];
1076         struct tracer *type = current_trace;
1077         unsigned long total   = 0;
1078         unsigned long entries = 0;
1079         int cpu;
1080         const char *name = "preemption";
1081
1082         if (type)
1083                 name = type->name;
1084
1085         for_each_possible_cpu(cpu) {
1086                 if (head_page(tr->data[cpu])) {
1087                         total += tr->data[cpu]->trace_idx;
1088                         if (tr->data[cpu]->trace_idx > tr->entries)
1089                                 entries += tr->entries;
1090                         else
1091                                 entries += tr->data[cpu]->trace_idx;
1092                 }
1093         }
1094
1095         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1096                    name, UTS_RELEASE);
1097         seq_puts(m, "-----------------------------------"
1098                  "---------------------------------\n");
1099         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1100                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1101                    nsecs_to_usecs(data->saved_latency),
1102                    entries,
1103                    total,
1104                    tr->cpu,
1105 #if defined(CONFIG_PREEMPT_NONE)
1106                    "server",
1107 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1108                    "desktop",
1109 #elif defined(CONFIG_PREEMPT_DESKTOP)
1110                    "preempt",
1111 #else
1112                    "unknown",
1113 #endif
1114                    /* These are reserved for later use */
1115                    0, 0, 0, 0);
1116 #ifdef CONFIG_SMP
1117         seq_printf(m, " #P:%d)\n", num_online_cpus());
1118 #else
1119         seq_puts(m, ")\n");
1120 #endif
1121         seq_puts(m, "    -----------------\n");
1122         seq_printf(m, "    | task: %.16s-%d "
1123                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1124                    data->comm, data->pid, data->uid, data->nice,
1125                    data->policy, data->rt_priority);
1126         seq_puts(m, "    -----------------\n");
1127
1128         if (data->critical_start) {
1129                 seq_puts(m, " => started at: ");
1130                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1131                 trace_print_seq(m, &iter->seq);
1132                 seq_puts(m, "\n => ended at:   ");
1133                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1134                 trace_print_seq(m, &iter->seq);
1135                 seq_puts(m, "\n");
1136         }
1137
1138         seq_puts(m, "\n");
1139 }
1140
1141 static void
1142 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1143 {
1144         int hardirq, softirq;
1145         char *comm;
1146
1147         comm = trace_find_cmdline(entry->pid);
1148
1149         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1150         trace_seq_printf(s, "%d", cpu);
1151         trace_seq_printf(s, "%c%c",
1152                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1153                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1154
1155         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1156         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1157         if (hardirq && softirq) {
1158                 trace_seq_putc(s, 'H');
1159         } else {
1160                 if (hardirq) {
1161                         trace_seq_putc(s, 'h');
1162                 } else {
1163                         if (softirq)
1164                                 trace_seq_putc(s, 's');
1165                         else
1166                                 trace_seq_putc(s, '.');
1167                 }
1168         }
1169
1170         if (entry->preempt_count)
1171                 trace_seq_printf(s, "%x", entry->preempt_count);
1172         else
1173                 trace_seq_puts(s, ".");
1174 }
1175
1176 unsigned long preempt_mark_thresh = 100;
1177
1178 static void
1179 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1180                     unsigned long rel_usecs)
1181 {
1182         trace_seq_printf(s, " %4lldus", abs_usecs);
1183         if (rel_usecs > preempt_mark_thresh)
1184                 trace_seq_puts(s, "!: ");
1185         else if (rel_usecs > 1)
1186                 trace_seq_puts(s, "+: ");
1187         else
1188                 trace_seq_puts(s, " : ");
1189 }
1190
1191 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1192
1193 static int
1194 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1195 {
1196         struct trace_seq *s = &iter->seq;
1197         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1198         struct trace_entry *next_entry = find_next_entry(iter, NULL);
1199         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1200         struct trace_entry *entry = iter->ent;
1201         unsigned long abs_usecs;
1202         unsigned long rel_usecs;
1203         char *comm;
1204         int S, T;
1205         int i;
1206         unsigned state;
1207
1208         if (!next_entry)
1209                 next_entry = entry;
1210         rel_usecs = ns2usecs(next_entry->t - entry->t);
1211         abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1212
1213         if (verbose) {
1214                 comm = trace_find_cmdline(entry->pid);
1215                 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1216                                  " %ld.%03ldms (+%ld.%03ldms): ",
1217                                  comm,
1218                                  entry->pid, cpu, entry->flags,
1219                                  entry->preempt_count, trace_idx,
1220                                  ns2usecs(entry->t),
1221                                  abs_usecs/1000,
1222                                  abs_usecs % 1000, rel_usecs/1000,
1223                                  rel_usecs % 1000);
1224         } else {
1225                 lat_print_generic(s, entry, cpu);
1226                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1227         }
1228         switch (entry->type) {
1229         case TRACE_FN:
1230                 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1231                 trace_seq_puts(s, " (");
1232                 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1233                 trace_seq_puts(s, ")\n");
1234                 break;
1235         case TRACE_CTX:
1236         case TRACE_WAKE:
1237                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1238                         state_to_char[entry->ctx.next_state] : 'X';
1239
1240                 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1241                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1242                 comm = trace_find_cmdline(entry->ctx.next_pid);
1243                 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1244                                  entry->ctx.prev_pid,
1245                                  entry->ctx.prev_prio,
1246                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1247                                  entry->ctx.next_pid,
1248                                  entry->ctx.next_prio,
1249                                  T, comm);
1250                 break;
1251         case TRACE_SPECIAL:
1252                 trace_seq_printf(s, "# %ld %ld %ld\n",
1253                                  entry->special.arg1,
1254                                  entry->special.arg2,
1255                                  entry->special.arg3);
1256                 break;
1257         case TRACE_STACK:
1258                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1259                         if (i)
1260                                 trace_seq_puts(s, " <= ");
1261                         seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1262                 }
1263                 trace_seq_puts(s, "\n");
1264                 break;
1265         default:
1266                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1267         }
1268         return 1;
1269 }
1270
1271 static int print_trace_fmt(struct trace_iterator *iter)
1272 {
1273         struct trace_seq *s = &iter->seq;
1274         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1275         struct trace_entry *entry;
1276         unsigned long usec_rem;
1277         unsigned long long t;
1278         unsigned long secs;
1279         char *comm;
1280         int ret;
1281         int S, T;
1282         int i;
1283
1284         entry = iter->ent;
1285
1286         comm = trace_find_cmdline(iter->ent->pid);
1287
1288         t = ns2usecs(entry->t);
1289         usec_rem = do_div(t, 1000000ULL);
1290         secs = (unsigned long)t;
1291
1292         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1293         if (!ret)
1294                 return 0;
1295         ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1296         if (!ret)
1297                 return 0;
1298         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1299         if (!ret)
1300                 return 0;
1301
1302         switch (entry->type) {
1303         case TRACE_FN:
1304                 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1305                 if (!ret)
1306                         return 0;
1307                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1308                                                 entry->fn.parent_ip) {
1309                         ret = trace_seq_printf(s, " <-");
1310                         if (!ret)
1311                                 return 0;
1312                         ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1313                                                sym_flags);
1314                         if (!ret)
1315                                 return 0;
1316                 }
1317                 ret = trace_seq_printf(s, "\n");
1318                 if (!ret)
1319                         return 0;
1320                 break;
1321         case TRACE_CTX:
1322         case TRACE_WAKE:
1323                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1324                         state_to_char[entry->ctx.prev_state] : 'X';
1325                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1326                         state_to_char[entry->ctx.next_state] : 'X';
1327                 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1328                                        entry->ctx.prev_pid,
1329                                        entry->ctx.prev_prio,
1330                                        S,
1331                                        entry->type == TRACE_CTX ? "==>" : "  +",
1332                                        entry->ctx.next_pid,
1333                                        entry->ctx.next_prio,
1334                                        T);
1335                 if (!ret)
1336                         return 0;
1337                 break;
1338         case TRACE_SPECIAL:
1339                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1340                                  entry->special.arg1,
1341                                  entry->special.arg2,
1342                                  entry->special.arg3);
1343                 if (!ret)
1344                         return 0;
1345                 break;
1346         case TRACE_STACK:
1347                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1348                         if (i) {
1349                                 ret = trace_seq_puts(s, " <= ");
1350                                 if (!ret)
1351                                         return 0;
1352                         }
1353                         ret = seq_print_ip_sym(s, entry->stack.caller[i],
1354                                                sym_flags);
1355                         if (!ret)
1356                                 return 0;
1357                 }
1358                 ret = trace_seq_puts(s, "\n");
1359                 if (!ret)
1360                         return 0;
1361                 break;
1362         }
1363         return 1;
1364 }
1365
1366 static int print_raw_fmt(struct trace_iterator *iter)
1367 {
1368         struct trace_seq *s = &iter->seq;
1369         struct trace_entry *entry;
1370         int ret;
1371         int S, T;
1372
1373         entry = iter->ent;
1374
1375         ret = trace_seq_printf(s, "%d %d %llu ",
1376                 entry->pid, iter->cpu, entry->t);
1377         if (!ret)
1378                 return 0;
1379
1380         switch (entry->type) {
1381         case TRACE_FN:
1382                 ret = trace_seq_printf(s, "%x %x\n",
1383                                         entry->fn.ip, entry->fn.parent_ip);
1384                 if (!ret)
1385                         return 0;
1386                 break;
1387         case TRACE_CTX:
1388         case TRACE_WAKE:
1389                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1390                         state_to_char[entry->ctx.prev_state] : 'X';
1391                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1392                         state_to_char[entry->ctx.next_state] : 'X';
1393                 if (entry->type == TRACE_WAKE)
1394                         S = '+';
1395                 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1396                                        entry->ctx.prev_pid,
1397                                        entry->ctx.prev_prio,
1398                                        S,
1399                                        entry->ctx.next_pid,
1400                                        entry->ctx.next_prio,
1401                                        T);
1402                 if (!ret)
1403                         return 0;
1404                 break;
1405         case TRACE_SPECIAL:
1406         case TRACE_STACK:
1407                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1408                                  entry->special.arg1,
1409                                  entry->special.arg2,
1410                                  entry->special.arg3);
1411                 if (!ret)
1412                         return 0;
1413                 break;
1414         }
1415         return 1;
1416 }
1417
1418 #define SEQ_PUT_FIELD_RET(s, x)                         \
1419 do {                                                    \
1420         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
1421                 return 0;                               \
1422 } while (0)
1423
1424 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
1425 do {                                                    \
1426         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
1427                 return 0;                               \
1428 } while (0)
1429
1430 static int print_hex_fmt(struct trace_iterator *iter)
1431 {
1432         struct trace_seq *s = &iter->seq;
1433         unsigned char newline = '\n';
1434         struct trace_entry *entry;
1435         int S, T;
1436
1437         entry = iter->ent;
1438
1439         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1440         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1441         SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1442
1443         switch (entry->type) {
1444         case TRACE_FN:
1445                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1446                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1447                 break;
1448         case TRACE_CTX:
1449         case TRACE_WAKE:
1450                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1451                         state_to_char[entry->ctx.prev_state] : 'X';
1452                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1453                         state_to_char[entry->ctx.next_state] : 'X';
1454                 if (entry->type == TRACE_WAKE)
1455                         S = '+';
1456                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1457                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1458                 SEQ_PUT_HEX_FIELD_RET(s, S);
1459                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1460                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1461                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1462                 SEQ_PUT_HEX_FIELD_RET(s, T);
1463                 break;
1464         case TRACE_SPECIAL:
1465         case TRACE_STACK:
1466                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1467                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1468                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1469                 break;
1470         }
1471         SEQ_PUT_FIELD_RET(s, newline);
1472
1473         return 1;
1474 }
1475
1476 static int print_bin_fmt(struct trace_iterator *iter)
1477 {
1478         struct trace_seq *s = &iter->seq;
1479         struct trace_entry *entry;
1480
1481         entry = iter->ent;
1482
1483         SEQ_PUT_FIELD_RET(s, entry->pid);
1484         SEQ_PUT_FIELD_RET(s, entry->cpu);
1485         SEQ_PUT_FIELD_RET(s, entry->t);
1486
1487         switch (entry->type) {
1488         case TRACE_FN:
1489                 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1490                 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1491                 break;
1492         case TRACE_CTX:
1493                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1494                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1495                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1496                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1497                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1498                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1499                 break;
1500         case TRACE_SPECIAL:
1501         case TRACE_STACK:
1502                 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1503                 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1504                 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1505                 break;
1506         }
1507         return 1;
1508 }
1509
1510 static int trace_empty(struct trace_iterator *iter)
1511 {
1512         struct trace_array_cpu *data;
1513         int cpu;
1514
1515         for_each_possible_cpu(cpu) {
1516                 data = iter->tr->data[cpu];
1517
1518                 if (head_page(data) && data->trace_idx &&
1519                     (data->trace_tail != data->trace_head ||
1520                      data->trace_tail_idx != data->trace_head_idx))
1521                         return 0;
1522         }
1523         return 1;
1524 }
1525
1526 static int print_trace_line(struct trace_iterator *iter)
1527 {
1528         if (iter->trace && iter->trace->print_line)
1529                 return iter->trace->print_line(iter);
1530
1531         if (trace_flags & TRACE_ITER_BIN)
1532                 return print_bin_fmt(iter);
1533
1534         if (trace_flags & TRACE_ITER_HEX)
1535                 return print_hex_fmt(iter);
1536
1537         if (trace_flags & TRACE_ITER_RAW)
1538                 return print_raw_fmt(iter);
1539
1540         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1541                 return print_lat_fmt(iter, iter->idx, iter->cpu);
1542
1543         return print_trace_fmt(iter);
1544 }
1545
1546 static int s_show(struct seq_file *m, void *v)
1547 {
1548         struct trace_iterator *iter = v;
1549
1550         if (iter->ent == NULL) {
1551                 if (iter->tr) {
1552                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1553                         seq_puts(m, "#\n");
1554                 }
1555                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1556                         /* print nothing if the buffers are empty */
1557                         if (trace_empty(iter))
1558                                 return 0;
1559                         print_trace_header(m, iter);
1560                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1561                                 print_lat_help_header(m);
1562                 } else {
1563                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1564                                 print_func_help_header(m);
1565                 }
1566         } else {
1567                 print_trace_line(iter);
1568                 trace_print_seq(m, &iter->seq);
1569         }
1570
1571         return 0;
1572 }
1573
1574 static struct seq_operations tracer_seq_ops = {
1575         .start          = s_start,
1576         .next           = s_next,
1577         .stop           = s_stop,
1578         .show           = s_show,
1579 };
1580
1581 static struct trace_iterator *
1582 __tracing_open(struct inode *inode, struct file *file, int *ret)
1583 {
1584         struct trace_iterator *iter;
1585
1586         if (tracing_disabled) {
1587                 *ret = -ENODEV;
1588                 return NULL;
1589         }
1590
1591         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1592         if (!iter) {
1593                 *ret = -ENOMEM;
1594                 goto out;
1595         }
1596
1597         mutex_lock(&trace_types_lock);
1598         if (current_trace && current_trace->print_max)
1599                 iter->tr = &max_tr;
1600         else
1601                 iter->tr = inode->i_private;
1602         iter->trace = current_trace;
1603         iter->pos = -1;
1604
1605         /* TODO stop tracer */
1606         *ret = seq_open(file, &tracer_seq_ops);
1607         if (!*ret) {
1608                 struct seq_file *m = file->private_data;
1609                 m->private = iter;
1610
1611                 /* stop the trace while dumping */
1612                 if (iter->tr->ctrl)
1613                         tracer_enabled = 0;
1614
1615                 if (iter->trace && iter->trace->open)
1616                         iter->trace->open(iter);
1617         } else {
1618                 kfree(iter);
1619                 iter = NULL;
1620         }
1621         mutex_unlock(&trace_types_lock);
1622
1623  out:
1624         return iter;
1625 }
1626
1627 int tracing_open_generic(struct inode *inode, struct file *filp)
1628 {
1629         if (tracing_disabled)
1630                 return -ENODEV;
1631
1632         filp->private_data = inode->i_private;
1633         return 0;
1634 }
1635
1636 int tracing_release(struct inode *inode, struct file *file)
1637 {
1638         struct seq_file *m = (struct seq_file *)file->private_data;
1639         struct trace_iterator *iter = m->private;
1640
1641         mutex_lock(&trace_types_lock);
1642         if (iter->trace && iter->trace->close)
1643                 iter->trace->close(iter);
1644
1645         /* reenable tracing if it was previously enabled */
1646         if (iter->tr->ctrl)
1647                 tracer_enabled = 1;
1648         mutex_unlock(&trace_types_lock);
1649
1650         seq_release(inode, file);
1651         kfree(iter);
1652         return 0;
1653 }
1654
1655 static int tracing_open(struct inode *inode, struct file *file)
1656 {
1657         int ret;
1658
1659         __tracing_open(inode, file, &ret);
1660
1661         return ret;
1662 }
1663
1664 static int tracing_lt_open(struct inode *inode, struct file *file)
1665 {
1666         struct trace_iterator *iter;
1667         int ret;
1668
1669         iter = __tracing_open(inode, file, &ret);
1670
1671         if (!ret)
1672                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1673
1674         return ret;
1675 }
1676
1677
1678 static void *
1679 t_next(struct seq_file *m, void *v, loff_t *pos)
1680 {
1681         struct tracer *t = m->private;
1682
1683         (*pos)++;
1684
1685         if (t)
1686                 t = t->next;
1687
1688         m->private = t;
1689
1690         return t;
1691 }
1692
1693 static void *t_start(struct seq_file *m, loff_t *pos)
1694 {
1695         struct tracer *t = m->private;
1696         loff_t l = 0;
1697
1698         mutex_lock(&trace_types_lock);
1699         for (; t && l < *pos; t = t_next(m, t, &l))
1700                 ;
1701
1702         return t;
1703 }
1704
1705 static void t_stop(struct seq_file *m, void *p)
1706 {
1707         mutex_unlock(&trace_types_lock);
1708 }
1709
1710 static int t_show(struct seq_file *m, void *v)
1711 {
1712         struct tracer *t = v;
1713
1714         if (!t)
1715                 return 0;
1716
1717         seq_printf(m, "%s", t->name);
1718         if (t->next)
1719                 seq_putc(m, ' ');
1720         else
1721                 seq_putc(m, '\n');
1722
1723         return 0;
1724 }
1725
1726 static struct seq_operations show_traces_seq_ops = {
1727         .start          = t_start,
1728         .next           = t_next,
1729         .stop           = t_stop,
1730         .show           = t_show,
1731 };
1732
1733 static int show_traces_open(struct inode *inode, struct file *file)
1734 {
1735         int ret;
1736
1737         if (tracing_disabled)
1738                 return -ENODEV;
1739
1740         ret = seq_open(file, &show_traces_seq_ops);
1741         if (!ret) {
1742                 struct seq_file *m = file->private_data;
1743                 m->private = trace_types;
1744         }
1745
1746         return ret;
1747 }
1748
1749 static struct file_operations tracing_fops = {
1750         .open           = tracing_open,
1751         .read           = seq_read,
1752         .llseek         = seq_lseek,
1753         .release        = tracing_release,
1754 };
1755
1756 static struct file_operations tracing_lt_fops = {
1757         .open           = tracing_lt_open,
1758         .read           = seq_read,
1759         .llseek         = seq_lseek,
1760         .release        = tracing_release,
1761 };
1762
1763 static struct file_operations show_traces_fops = {
1764         .open           = show_traces_open,
1765         .read           = seq_read,
1766         .release        = seq_release,
1767 };
1768
1769 /*
1770  * Only trace on a CPU if the bitmask is set:
1771  */
1772 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1773
1774 /*
1775  * When tracing/tracing_cpu_mask is modified then this holds
1776  * the new bitmask we are about to install:
1777  */
1778 static cpumask_t tracing_cpumask_new;
1779
1780 /*
1781  * The tracer itself will not take this lock, but still we want
1782  * to provide a consistent cpumask to user-space:
1783  */
1784 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1785
1786 /*
1787  * Temporary storage for the character representation of the
1788  * CPU bitmask (and one more byte for the newline):
1789  */
1790 static char mask_str[NR_CPUS + 1];
1791
1792 static ssize_t
1793 tracing_cpumask_read(struct file *filp, char __user *ubuf,
1794                      size_t count, loff_t *ppos)
1795 {
1796         int len;
1797
1798         mutex_lock(&tracing_cpumask_update_lock);
1799
1800         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1801         if (count - len < 2) {
1802                 count = -EINVAL;
1803                 goto out_err;
1804         }
1805         len += sprintf(mask_str + len, "\n");
1806         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1807
1808 out_err:
1809         mutex_unlock(&tracing_cpumask_update_lock);
1810
1811         return count;
1812 }
1813
1814 static ssize_t
1815 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1816                       size_t count, loff_t *ppos)
1817 {
1818         int err, cpu;
1819
1820         mutex_lock(&tracing_cpumask_update_lock);
1821         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1822         if (err)
1823                 goto err_unlock;
1824
1825         raw_local_irq_disable();
1826         __raw_spin_lock(&ftrace_max_lock);
1827         for_each_possible_cpu(cpu) {
1828                 /*
1829                  * Increase/decrease the disabled counter if we are
1830                  * about to flip a bit in the cpumask:
1831                  */
1832                 if (cpu_isset(cpu, tracing_cpumask) &&
1833                                 !cpu_isset(cpu, tracing_cpumask_new)) {
1834                         atomic_inc(&global_trace.data[cpu]->disabled);
1835                 }
1836                 if (!cpu_isset(cpu, tracing_cpumask) &&
1837                                 cpu_isset(cpu, tracing_cpumask_new)) {
1838                         atomic_dec(&global_trace.data[cpu]->disabled);
1839                 }
1840         }
1841         __raw_spin_unlock(&ftrace_max_lock);
1842         raw_local_irq_enable();
1843
1844         tracing_cpumask = tracing_cpumask_new;
1845
1846         mutex_unlock(&tracing_cpumask_update_lock);
1847
1848         return count;
1849
1850 err_unlock:
1851         mutex_unlock(&tracing_cpumask_update_lock);
1852
1853         return err;
1854 }
1855
1856 static struct file_operations tracing_cpumask_fops = {
1857         .open           = tracing_open_generic,
1858         .read           = tracing_cpumask_read,
1859         .write          = tracing_cpumask_write,
1860 };
1861
1862 static ssize_t
1863 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1864                        size_t cnt, loff_t *ppos)
1865 {
1866         char *buf;
1867         int r = 0;
1868         int len = 0;
1869         int i;
1870
1871         /* calulate max size */
1872         for (i = 0; trace_options[i]; i++) {
1873                 len += strlen(trace_options[i]);
1874                 len += 3; /* "no" and space */
1875         }
1876
1877         /* +2 for \n and \0 */
1878         buf = kmalloc(len + 2, GFP_KERNEL);
1879         if (!buf)
1880                 return -ENOMEM;
1881
1882         for (i = 0; trace_options[i]; i++) {
1883                 if (trace_flags & (1 << i))
1884                         r += sprintf(buf + r, "%s ", trace_options[i]);
1885                 else
1886                         r += sprintf(buf + r, "no%s ", trace_options[i]);
1887         }
1888
1889         r += sprintf(buf + r, "\n");
1890         WARN_ON(r >= len + 2);
1891
1892         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1893
1894         kfree(buf);
1895
1896         return r;
1897 }
1898
1899 static ssize_t
1900 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1901                         size_t cnt, loff_t *ppos)
1902 {
1903         char buf[64];
1904         char *cmp = buf;
1905         int neg = 0;
1906         int i;
1907
1908         if (cnt >= sizeof(buf))
1909                 return -EINVAL;
1910
1911         if (copy_from_user(&buf, ubuf, cnt))
1912                 return -EFAULT;
1913
1914         buf[cnt] = 0;
1915
1916         if (strncmp(buf, "no", 2) == 0) {
1917                 neg = 1;
1918                 cmp += 2;
1919         }
1920
1921         for (i = 0; trace_options[i]; i++) {
1922                 int len = strlen(trace_options[i]);
1923
1924                 if (strncmp(cmp, trace_options[i], len) == 0) {
1925                         if (neg)
1926                                 trace_flags &= ~(1 << i);
1927                         else
1928                                 trace_flags |= (1 << i);
1929                         break;
1930                 }
1931         }
1932         /*
1933          * If no option could be set, return an error:
1934          */
1935         if (!trace_options[i])
1936                 return -EINVAL;
1937
1938         filp->f_pos += cnt;
1939
1940         return cnt;
1941 }
1942
1943 static struct file_operations tracing_iter_fops = {
1944         .open           = tracing_open_generic,
1945         .read           = tracing_iter_ctrl_read,
1946         .write          = tracing_iter_ctrl_write,
1947 };
1948
1949 static const char readme_msg[] =
1950         "tracing mini-HOWTO:\n\n"
1951         "# mkdir /debug\n"
1952         "# mount -t debugfs nodev /debug\n\n"
1953         "# cat /debug/tracing/available_tracers\n"
1954         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1955         "# cat /debug/tracing/current_tracer\n"
1956         "none\n"
1957         "# echo sched_switch > /debug/tracing/current_tracer\n"
1958         "# cat /debug/tracing/current_tracer\n"
1959         "sched_switch\n"
1960         "# cat /debug/tracing/iter_ctrl\n"
1961         "noprint-parent nosym-offset nosym-addr noverbose\n"
1962         "# echo print-parent > /debug/tracing/iter_ctrl\n"
1963         "# echo 1 > /debug/tracing/tracing_enabled\n"
1964         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1965         "echo 0 > /debug/tracing/tracing_enabled\n"
1966 ;
1967
1968 static ssize_t
1969 tracing_readme_read(struct file *filp, char __user *ubuf,
1970                        size_t cnt, loff_t *ppos)
1971 {
1972         return simple_read_from_buffer(ubuf, cnt, ppos,
1973                                         readme_msg, strlen(readme_msg));
1974 }
1975
1976 static struct file_operations tracing_readme_fops = {
1977         .open           = tracing_open_generic,
1978         .read           = tracing_readme_read,
1979 };
1980
1981 static ssize_t
1982 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1983                   size_t cnt, loff_t *ppos)
1984 {
1985         struct trace_array *tr = filp->private_data;
1986         char buf[64];
1987         int r;
1988
1989         r = sprintf(buf, "%ld\n", tr->ctrl);
1990         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1991 }
1992
1993 static ssize_t
1994 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1995                    size_t cnt, loff_t *ppos)
1996 {
1997         struct trace_array *tr = filp->private_data;
1998         long val;
1999         char buf[64];
2000
2001         if (cnt >= sizeof(buf))
2002                 return -EINVAL;
2003
2004         if (copy_from_user(&buf, ubuf, cnt))
2005                 return -EFAULT;
2006
2007         buf[cnt] = 0;
2008
2009         val = simple_strtoul(buf, NULL, 10);
2010
2011         val = !!val;
2012
2013         mutex_lock(&trace_types_lock);
2014         if (tr->ctrl ^ val) {
2015                 if (val)
2016                         tracer_enabled = 1;
2017                 else
2018                         tracer_enabled = 0;
2019
2020                 tr->ctrl = val;
2021
2022                 if (current_trace && current_trace->ctrl_update)
2023                         current_trace->ctrl_update(tr);
2024         }
2025         mutex_unlock(&trace_types_lock);
2026
2027         filp->f_pos += cnt;
2028
2029         return cnt;
2030 }
2031
2032 static ssize_t
2033 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2034                        size_t cnt, loff_t *ppos)
2035 {
2036         char buf[max_tracer_type_len+2];
2037         int r;
2038
2039         mutex_lock(&trace_types_lock);
2040         if (current_trace)
2041                 r = sprintf(buf, "%s\n", current_trace->name);
2042         else
2043                 r = sprintf(buf, "\n");
2044         mutex_unlock(&trace_types_lock);
2045
2046         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2047 }
2048
2049 static ssize_t
2050 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2051                         size_t cnt, loff_t *ppos)
2052 {
2053         struct trace_array *tr = &global_trace;
2054         struct tracer *t;
2055         char buf[max_tracer_type_len+1];
2056         int i;
2057
2058         if (cnt > max_tracer_type_len)
2059                 cnt = max_tracer_type_len;
2060
2061         if (copy_from_user(&buf, ubuf, cnt))
2062                 return -EFAULT;
2063
2064         buf[cnt] = 0;
2065
2066         /* strip ending whitespace. */
2067         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2068                 buf[i] = 0;
2069
2070         mutex_lock(&trace_types_lock);
2071         for (t = trace_types; t; t = t->next) {
2072                 if (strcmp(t->name, buf) == 0)
2073                         break;
2074         }
2075         if (!t || t == current_trace)
2076                 goto out;
2077
2078         if (current_trace && current_trace->reset)
2079                 current_trace->reset(tr);
2080
2081         current_trace = t;
2082         if (t->init)
2083                 t->init(tr);
2084
2085  out:
2086         mutex_unlock(&trace_types_lock);
2087
2088         filp->f_pos += cnt;
2089
2090         return cnt;
2091 }
2092
2093 static ssize_t
2094 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2095                      size_t cnt, loff_t *ppos)
2096 {
2097         unsigned long *ptr = filp->private_data;
2098         char buf[64];
2099         int r;
2100
2101         r = snprintf(buf, sizeof(buf), "%ld\n",
2102                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2103         if (r > sizeof(buf))
2104                 r = sizeof(buf);
2105         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2106 }
2107
2108 static ssize_t
2109 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2110                       size_t cnt, loff_t *ppos)
2111 {
2112         long *ptr = filp->private_data;
2113         long val;
2114         char buf[64];
2115
2116         if (cnt >= sizeof(buf))
2117                 return -EINVAL;
2118
2119         if (copy_from_user(&buf, ubuf, cnt))
2120                 return -EFAULT;
2121
2122         buf[cnt] = 0;
2123
2124         val = simple_strtoul(buf, NULL, 10);
2125
2126         *ptr = val * 1000;
2127
2128         return cnt;
2129 }
2130
2131 static atomic_t tracing_reader;
2132
2133 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2134 {
2135         struct trace_iterator *iter;
2136
2137         if (tracing_disabled)
2138                 return -ENODEV;
2139
2140         /* We only allow for reader of the pipe */
2141         if (atomic_inc_return(&tracing_reader) != 1) {
2142                 atomic_dec(&tracing_reader);
2143                 return -EBUSY;
2144         }
2145
2146         /* create a buffer to store the information to pass to userspace */
2147         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2148         if (!iter)
2149                 return -ENOMEM;
2150
2151         iter->tr = &global_trace;
2152         iter->trace = current_trace;
2153
2154         filp->private_data = iter;
2155
2156         return 0;
2157 }
2158
2159 static int tracing_release_pipe(struct inode *inode, struct file *file)
2160 {
2161         struct trace_iterator *iter = file->private_data;
2162
2163         kfree(iter);
2164         atomic_dec(&tracing_reader);
2165
2166         return 0;
2167 }
2168
2169 static unsigned int
2170 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2171 {
2172         struct trace_iterator *iter = filp->private_data;
2173
2174         if (trace_flags & TRACE_ITER_BLOCK) {
2175                 /*
2176                  * Always select as readable when in blocking mode
2177                  */
2178                 return POLLIN | POLLRDNORM;
2179         } else {
2180                 if (!trace_empty(iter))
2181                         return POLLIN | POLLRDNORM;
2182                 poll_wait(filp, &trace_wait, poll_table);
2183                 if (!trace_empty(iter))
2184                         return POLLIN | POLLRDNORM;
2185
2186                 return 0;
2187         }
2188 }
2189
2190 /*
2191  * Consumer reader.
2192  */
2193 static ssize_t
2194 tracing_read_pipe(struct file *filp, char __user *ubuf,
2195                   size_t cnt, loff_t *ppos)
2196 {
2197         struct trace_iterator *iter = filp->private_data;
2198         struct trace_array_cpu *data;
2199         struct trace_array *tr = iter->tr;
2200         struct tracer *tracer = iter->trace;
2201         static cpumask_t mask;
2202         static int start;
2203         unsigned long flags;
2204 #ifdef CONFIG_FTRACE
2205         int ftrace_save;
2206 #endif
2207         int read = 0;
2208         int cpu;
2209         int len;
2210         int ret;
2211
2212         /* return any leftover data */
2213         if (iter->seq.len > start) {
2214                 len = iter->seq.len - start;
2215                 if (cnt > len)
2216                         cnt = len;
2217                 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2218                 if (ret)
2219                         cnt = -EFAULT;
2220
2221                 start += len;
2222
2223                 return cnt;
2224         }
2225
2226         trace_seq_reset(&iter->seq);
2227         start = 0;
2228
2229         while (trace_empty(iter)) {
2230
2231                 if ((filp->f_flags & O_NONBLOCK))
2232                         return -EAGAIN;
2233
2234                 /*
2235                  * This is a make-shift waitqueue. The reason we don't use
2236                  * an actual wait queue is because:
2237                  *  1) we only ever have one waiter
2238                  *  2) the tracing, traces all functions, we don't want
2239                  *     the overhead of calling wake_up and friends
2240                  *     (and tracing them too)
2241                  *     Anyway, this is really very primitive wakeup.
2242                  */
2243                 set_current_state(TASK_INTERRUPTIBLE);
2244                 iter->tr->waiter = current;
2245
2246                 /* sleep for one second, and try again. */
2247                 schedule_timeout(HZ);
2248
2249                 iter->tr->waiter = NULL;
2250
2251                 if (signal_pending(current))
2252                         return -EINTR;
2253
2254                 if (iter->trace != current_trace)
2255                         return 0;
2256
2257                 /*
2258                  * We block until we read something and tracing is disabled.
2259                  * We still block if tracing is disabled, but we have never
2260                  * read anything. This allows a user to cat this file, and
2261                  * then enable tracing. But after we have read something,
2262                  * we give an EOF when tracing is again disabled.
2263                  *
2264                  * iter->pos will be 0 if we haven't read anything.
2265                  */
2266                 if (!tracer_enabled && iter->pos)
2267                         break;
2268
2269                 continue;
2270         }
2271
2272         /* stop when tracing is finished */
2273         if (trace_empty(iter))
2274                 return 0;
2275
2276         if (cnt >= PAGE_SIZE)
2277                 cnt = PAGE_SIZE - 1;
2278
2279         memset(iter, 0, sizeof(*iter));
2280         iter->tr = tr;
2281         iter->trace = tracer;
2282         iter->pos = -1;
2283
2284         /*
2285          * We need to stop all tracing on all CPUS to read the
2286          * the next buffer. This is a bit expensive, but is
2287          * not done often. We fill all what we can read,
2288          * and then release the locks again.
2289          */
2290
2291         cpus_clear(mask);
2292         local_irq_save(flags);
2293 #ifdef CONFIG_FTRACE
2294         ftrace_save = ftrace_enabled;
2295         ftrace_enabled = 0;
2296 #endif
2297         smp_wmb();
2298         for_each_possible_cpu(cpu) {
2299                 data = iter->tr->data[cpu];
2300
2301                 if (!head_page(data) || !data->trace_idx)
2302                         continue;
2303
2304                 atomic_inc(&data->disabled);
2305                 cpu_set(cpu, mask);
2306         }
2307
2308         for_each_cpu_mask(cpu, mask) {
2309                 data = iter->tr->data[cpu];
2310                 __raw_spin_lock(&data->lock);
2311         }
2312
2313         while (find_next_entry_inc(iter) != NULL) {
2314                 int len = iter->seq.len;
2315
2316                 ret = print_trace_line(iter);
2317                 if (!ret) {
2318                         /* don't print partial lines */
2319                         iter->seq.len = len;
2320                         break;
2321                 }
2322
2323                 trace_consume(iter);
2324
2325                 if (iter->seq.len >= cnt)
2326                         break;
2327         }
2328
2329         for_each_cpu_mask(cpu, mask) {
2330                 data = iter->tr->data[cpu];
2331                 __raw_spin_unlock(&data->lock);
2332         }
2333
2334         for_each_cpu_mask(cpu, mask) {
2335                 data = iter->tr->data[cpu];
2336                 atomic_dec(&data->disabled);
2337         }
2338 #ifdef CONFIG_FTRACE
2339         ftrace_enabled = ftrace_save;
2340 #endif
2341         local_irq_restore(flags);
2342
2343         /* Now copy what we have to the user */
2344         read = iter->seq.len;
2345         if (read > cnt)
2346                 read = cnt;
2347
2348         ret = copy_to_user(ubuf, iter->seq.buffer, read);
2349
2350         if (read < iter->seq.len)
2351                 start = read;
2352         else
2353                 trace_seq_reset(&iter->seq);
2354
2355         if (ret)
2356                 read = -EFAULT;
2357
2358         return read;
2359 }
2360
2361 static ssize_t
2362 tracing_entries_read(struct file *filp, char __user *ubuf,
2363                      size_t cnt, loff_t *ppos)
2364 {
2365         struct trace_array *tr = filp->private_data;
2366         char buf[64];
2367         int r;
2368
2369         r = sprintf(buf, "%lu\n", tr->entries);
2370         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2371 }
2372
2373 static ssize_t
2374 tracing_entries_write(struct file *filp, const char __user *ubuf,
2375                       size_t cnt, loff_t *ppos)
2376 {
2377         unsigned long val;
2378         char buf[64];
2379
2380         if (cnt >= sizeof(buf))
2381                 return -EINVAL;
2382
2383         if (copy_from_user(&buf, ubuf, cnt))
2384                 return -EFAULT;
2385
2386         buf[cnt] = 0;
2387
2388         val = simple_strtoul(buf, NULL, 10);
2389
2390         /* must have at least 1 entry */
2391         if (!val)
2392                 return -EINVAL;
2393
2394         mutex_lock(&trace_types_lock);
2395
2396         if (current_trace != &no_tracer) {
2397                 cnt = -EBUSY;
2398                 pr_info("ftrace: set current_tracer to none"
2399                         " before modifying buffer size\n");
2400                 goto out;
2401         }
2402
2403         if (val > global_trace.entries) {
2404                 while (global_trace.entries < val) {
2405                         if (trace_alloc_page()) {
2406                                 cnt = -ENOMEM;
2407                                 goto out;
2408                         }
2409                 }
2410         } else {
2411                 /* include the number of entries in val (inc of page entries) */
2412                 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2413                         trace_free_page();
2414         }
2415
2416         filp->f_pos += cnt;
2417
2418  out:
2419         max_tr.entries = global_trace.entries;
2420         mutex_unlock(&trace_types_lock);
2421
2422         return cnt;
2423 }
2424
2425 static struct file_operations tracing_max_lat_fops = {
2426         .open           = tracing_open_generic,
2427         .read           = tracing_max_lat_read,
2428         .write          = tracing_max_lat_write,
2429 };
2430
2431 static struct file_operations tracing_ctrl_fops = {
2432         .open           = tracing_open_generic,
2433         .read           = tracing_ctrl_read,
2434         .write          = tracing_ctrl_write,
2435 };
2436
2437 static struct file_operations set_tracer_fops = {
2438         .open           = tracing_open_generic,
2439         .read           = tracing_set_trace_read,
2440         .write          = tracing_set_trace_write,
2441 };
2442
2443 static struct file_operations tracing_pipe_fops = {
2444         .open           = tracing_open_pipe,
2445         .poll           = tracing_poll_pipe,
2446         .read           = tracing_read_pipe,
2447         .release        = tracing_release_pipe,
2448 };
2449
2450 static struct file_operations tracing_entries_fops = {
2451         .open           = tracing_open_generic,
2452         .read           = tracing_entries_read,
2453         .write          = tracing_entries_write,
2454 };
2455
2456 #ifdef CONFIG_DYNAMIC_FTRACE
2457
2458 static ssize_t
2459 tracing_read_long(struct file *filp, char __user *ubuf,
2460                   size_t cnt, loff_t *ppos)
2461 {
2462         unsigned long *p = filp->private_data;
2463         char buf[64];
2464         int r;
2465
2466         r = sprintf(buf, "%ld\n", *p);
2467
2468         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2469 }
2470
2471 static struct file_operations tracing_read_long_fops = {
2472         .open           = tracing_open_generic,
2473         .read           = tracing_read_long,
2474 };
2475 #endif
2476
2477 static struct dentry *d_tracer;
2478
2479 struct dentry *tracing_init_dentry(void)
2480 {
2481         static int once;
2482
2483         if (d_tracer)
2484                 return d_tracer;
2485
2486         d_tracer = debugfs_create_dir("tracing", NULL);
2487
2488         if (!d_tracer && !once) {
2489                 once = 1;
2490                 pr_warning("Could not create debugfs directory 'tracing'\n");
2491                 return NULL;
2492         }
2493
2494         return d_tracer;
2495 }
2496
2497 #ifdef CONFIG_FTRACE_SELFTEST
2498 /* Let selftest have access to static functions in this file */
2499 #include "trace_selftest.c"
2500 #endif
2501
2502 static __init void tracer_init_debugfs(void)
2503 {
2504         struct dentry *d_tracer;
2505         struct dentry *entry;
2506
2507         d_tracer = tracing_init_dentry();
2508
2509         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2510                                     &global_trace, &tracing_ctrl_fops);
2511         if (!entry)
2512                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2513
2514         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2515                                     NULL, &tracing_iter_fops);
2516         if (!entry)
2517                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2518
2519         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2520                                     NULL, &tracing_cpumask_fops);
2521         if (!entry)
2522                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2523
2524         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2525                                     &global_trace, &tracing_lt_fops);
2526         if (!entry)
2527                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2528
2529         entry = debugfs_create_file("trace", 0444, d_tracer,
2530                                     &global_trace, &tracing_fops);
2531         if (!entry)
2532                 pr_warning("Could not create debugfs 'trace' entry\n");
2533
2534         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2535                                     &global_trace, &show_traces_fops);
2536         if (!entry)
2537                 pr_warning("Could not create debugfs 'trace' entry\n");
2538
2539         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2540                                     &global_trace, &set_tracer_fops);
2541         if (!entry)
2542                 pr_warning("Could not create debugfs 'trace' entry\n");
2543
2544         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2545                                     &tracing_max_latency,
2546                                     &tracing_max_lat_fops);
2547         if (!entry)
2548                 pr_warning("Could not create debugfs "
2549                            "'tracing_max_latency' entry\n");
2550
2551         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2552                                     &tracing_thresh, &tracing_max_lat_fops);
2553         if (!entry)
2554                 pr_warning("Could not create debugfs "
2555                            "'tracing_threash' entry\n");
2556         entry = debugfs_create_file("README", 0644, d_tracer,
2557                                     NULL, &tracing_readme_fops);
2558         if (!entry)
2559                 pr_warning("Could not create debugfs 'README' entry\n");
2560
2561         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2562                                     NULL, &tracing_pipe_fops);
2563         if (!entry)
2564                 pr_warning("Could not create debugfs "
2565                            "'tracing_threash' entry\n");
2566
2567         entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2568                                     &global_trace, &tracing_entries_fops);
2569         if (!entry)
2570                 pr_warning("Could not create debugfs "
2571                            "'tracing_threash' entry\n");
2572
2573 #ifdef CONFIG_DYNAMIC_FTRACE
2574         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2575                                     &ftrace_update_tot_cnt,
2576                                     &tracing_read_long_fops);
2577         if (!entry)
2578                 pr_warning("Could not create debugfs "
2579                            "'dyn_ftrace_total_info' entry\n");
2580 #endif
2581 }
2582
2583 static int trace_alloc_page(void)
2584 {
2585         struct trace_array_cpu *data;
2586         struct page *page, *tmp;
2587         LIST_HEAD(pages);
2588         void *array;
2589         int i;
2590
2591         /* first allocate a page for each CPU */
2592         for_each_possible_cpu(i) {
2593                 array = (void *)__get_free_page(GFP_KERNEL);
2594                 if (array == NULL) {
2595                         printk(KERN_ERR "tracer: failed to allocate page"
2596                                "for trace buffer!\n");
2597                         goto free_pages;
2598                 }
2599
2600                 page = virt_to_page(array);
2601                 list_add(&page->lru, &pages);
2602
2603 /* Only allocate if we are actually using the max trace */
2604 #ifdef CONFIG_TRACER_MAX_TRACE
2605                 array = (void *)__get_free_page(GFP_KERNEL);
2606                 if (array == NULL) {
2607                         printk(KERN_ERR "tracer: failed to allocate page"
2608                                "for trace buffer!\n");
2609                         goto free_pages;
2610                 }
2611                 page = virt_to_page(array);
2612                 list_add(&page->lru, &pages);
2613 #endif
2614         }
2615
2616         /* Now that we successfully allocate a page per CPU, add them */
2617         for_each_possible_cpu(i) {
2618                 data = global_trace.data[i];
2619                 page = list_entry(pages.next, struct page, lru);
2620                 list_del_init(&page->lru);
2621                 list_add_tail(&page->lru, &data->trace_pages);
2622                 ClearPageLRU(page);
2623
2624 #ifdef CONFIG_TRACER_MAX_TRACE
2625                 data = max_tr.data[i];
2626                 page = list_entry(pages.next, struct page, lru);
2627                 list_del_init(&page->lru);
2628                 list_add_tail(&page->lru, &data->trace_pages);
2629                 SetPageLRU(page);
2630 #endif
2631         }
2632         global_trace.entries += ENTRIES_PER_PAGE;
2633
2634         return 0;
2635
2636  free_pages:
2637         list_for_each_entry_safe(page, tmp, &pages, lru) {
2638                 list_del_init(&page->lru);
2639                 __free_page(page);
2640         }
2641         return -ENOMEM;
2642 }
2643
2644 static int trace_free_page(void)
2645 {
2646         struct trace_array_cpu *data;
2647         struct page *page;
2648         struct list_head *p;
2649         int i;
2650         int ret = 0;
2651
2652         /* free one page from each buffer */
2653         for_each_possible_cpu(i) {
2654                 data = global_trace.data[i];
2655                 p = data->trace_pages.next;
2656                 if (p == &data->trace_pages) {
2657                         /* should never happen */
2658                         WARN_ON(1);
2659                         tracing_disabled = 1;
2660                         ret = -1;
2661                         break;
2662                 }
2663                 page = list_entry(p, struct page, lru);
2664                 ClearPageLRU(page);
2665                 list_del(&page->lru);
2666                 __free_page(page);
2667
2668                 tracing_reset(data);
2669
2670 #ifdef CONFIG_TRACER_MAX_TRACE
2671                 data = max_tr.data[i];
2672                 p = data->trace_pages.next;
2673                 if (p == &data->trace_pages) {
2674                         /* should never happen */
2675                         WARN_ON(1);
2676                         tracing_disabled = 1;
2677                         ret = -1;
2678                         break;
2679                 }
2680                 page = list_entry(p, struct page, lru);
2681                 ClearPageLRU(page);
2682                 list_del(&page->lru);
2683                 __free_page(page);
2684
2685                 tracing_reset(data);
2686 #endif
2687         }
2688         global_trace.entries -= ENTRIES_PER_PAGE;
2689
2690         return ret;
2691 }
2692
2693 __init static int tracer_alloc_buffers(void)
2694 {
2695         struct trace_array_cpu *data;
2696         void *array;
2697         struct page *page;
2698         int pages = 0;
2699         int ret = -ENOMEM;
2700         int i;
2701
2702         global_trace.ctrl = tracer_enabled;
2703
2704         /* Allocate the first page for all buffers */
2705         for_each_possible_cpu(i) {
2706                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2707                 max_tr.data[i] = &per_cpu(max_data, i);
2708
2709                 array = (void *)__get_free_page(GFP_KERNEL);
2710                 if (array == NULL) {
2711                         printk(KERN_ERR "tracer: failed to allocate page"
2712                                "for trace buffer!\n");
2713                         goto free_buffers;
2714                 }
2715
2716                 /* set the array to the list */
2717                 INIT_LIST_HEAD(&data->trace_pages);
2718                 page = virt_to_page(array);
2719                 list_add(&page->lru, &data->trace_pages);
2720                 /* use the LRU flag to differentiate the two buffers */
2721                 ClearPageLRU(page);
2722
2723                 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2724                 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2725
2726 /* Only allocate if we are actually using the max trace */
2727 #ifdef CONFIG_TRACER_MAX_TRACE
2728                 array = (void *)__get_free_page(GFP_KERNEL);
2729                 if (array == NULL) {
2730                         printk(KERN_ERR "tracer: failed to allocate page"
2731                                "for trace buffer!\n");
2732                         goto free_buffers;
2733                 }
2734
2735                 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2736                 page = virt_to_page(array);
2737                 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2738                 SetPageLRU(page);
2739 #endif
2740         }
2741
2742         /*
2743          * Since we allocate by orders of pages, we may be able to
2744          * round up a bit.
2745          */
2746         global_trace.entries = ENTRIES_PER_PAGE;
2747         pages++;
2748
2749         while (global_trace.entries < trace_nr_entries) {
2750                 if (trace_alloc_page())
2751                         break;
2752                 pages++;
2753         }
2754         max_tr.entries = global_trace.entries;
2755
2756         pr_info("tracer: %d pages allocated for %ld",
2757                 pages, trace_nr_entries);
2758         pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2759         pr_info("   actual entries %ld\n", global_trace.entries);
2760
2761         tracer_init_debugfs();
2762
2763         trace_init_cmdlines();
2764
2765         register_tracer(&no_tracer);
2766         current_trace = &no_tracer;
2767
2768         /* All seems OK, enable tracing */
2769         tracing_disabled = 0;
2770
2771         return 0;
2772
2773  free_buffers:
2774         for (i-- ; i >= 0; i--) {
2775                 struct page *page, *tmp;
2776                 struct trace_array_cpu *data = global_trace.data[i];
2777
2778                 if (data) {
2779                         list_for_each_entry_safe(page, tmp,
2780                                                  &data->trace_pages, lru) {
2781                                 list_del_init(&page->lru);
2782                                 __free_page(page);
2783                         }
2784                 }
2785
2786 #ifdef CONFIG_TRACER_MAX_TRACE
2787                 data = max_tr.data[i];
2788                 if (data) {
2789                         list_for_each_entry_safe(page, tmp,
2790                                                  &data->trace_pages, lru) {
2791                                 list_del_init(&page->lru);
2792                                 __free_page(page);
2793                         }
2794                 }
2795 #endif
2796         }
2797         return ret;
2798 }
2799 fs_initcall(tracer_alloc_buffers);