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