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