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