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