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