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