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