ftrace: mmiotrace, updates
[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/poll.h>
28 #include <linux/gfp.h>
29 #include <linux/fs.h>
30 #include <linux/writeback.h>
31
32 #include <linux/stacktrace.h>
33
34 #include "trace.h"
35
36 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
37 unsigned long __read_mostly     tracing_thresh;
38
39 static unsigned long __read_mostly      tracing_nr_buffers;
40 static cpumask_t __read_mostly          tracing_buffer_mask;
41
42 #define for_each_tracing_cpu(cpu)       \
43         for_each_cpu_mask(cpu, tracing_buffer_mask)
44
45 /* dummy trace to disable tracing */
46 static struct tracer no_tracer __read_mostly = {
47         .name           = "none",
48 };
49
50 static int trace_alloc_page(void);
51 static int trace_free_page(void);
52
53 static int tracing_disabled = 1;
54
55 static unsigned long tracing_pages_allocated;
56
57 long
58 ns2usecs(cycle_t nsec)
59 {
60         nsec += 500;
61         do_div(nsec, 1000);
62         return nsec;
63 }
64
65 cycle_t ftrace_now(int cpu)
66 {
67         return cpu_clock(cpu);
68 }
69
70 /*
71  * The global_trace is the descriptor that holds the tracing
72  * buffers for the live tracing. For each CPU, it contains
73  * a link list of pages that will store trace entries. The
74  * page descriptor of the pages in the memory is used to hold
75  * the link list by linking the lru item in the page descriptor
76  * to each of the pages in the buffer per CPU.
77  *
78  * For each active CPU there is a data field that holds the
79  * pages for the buffer for that CPU. Each CPU has the same number
80  * of pages allocated for its buffer.
81  */
82 static struct trace_array       global_trace;
83
84 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
85
86 /*
87  * The max_tr is used to snapshot the global_trace when a maximum
88  * latency is reached. Some tracers will use this to store a maximum
89  * trace while it continues examining live traces.
90  *
91  * The buffers for the max_tr are set up the same as the global_trace.
92  * When a snapshot is taken, the link list of the max_tr is swapped
93  * with the link list of the global_trace and the buffers are reset for
94  * the global_trace so the tracing can continue.
95  */
96 static struct trace_array       max_tr;
97
98 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
99
100 /* tracer_enabled is used to toggle activation of a tracer */
101 static int                      tracer_enabled = 1;
102
103 /*
104  * trace_nr_entries is the number of entries that is allocated
105  * for a buffer. Note, the number of entries is always rounded
106  * to ENTRIES_PER_PAGE.
107  */
108 static unsigned long            trace_nr_entries = 65536UL;
109
110 /* trace_types holds a link list of available tracers. */
111 static struct tracer            *trace_types __read_mostly;
112
113 /* current_trace points to the tracer that is currently active */
114 static struct tracer            *current_trace __read_mostly;
115
116 /*
117  * max_tracer_type_len is used to simplify the allocating of
118  * buffers to read userspace tracer names. We keep track of
119  * the longest tracer name registered.
120  */
121 static int                      max_tracer_type_len;
122
123 /*
124  * trace_types_lock is used to protect the trace_types list.
125  * This lock is also used to keep user access serialized.
126  * Accesses from userspace will grab this lock while userspace
127  * activities happen inside the kernel.
128  */
129 static DEFINE_MUTEX(trace_types_lock);
130
131 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
132 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
133
134 /* trace_flags holds iter_ctrl options */
135 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
136
137 /**
138  * trace_wake_up - wake up tasks waiting for trace input
139  *
140  * Simply wakes up any task that is blocked on the trace_wait
141  * queue. These is used with trace_poll for tasks polling the trace.
142  */
143 void trace_wake_up(void)
144 {
145         /*
146          * The runqueue_is_locked() can fail, but this is the best we
147          * have for now:
148          */
149         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
150                 wake_up(&trace_wait);
151 }
152
153 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
154
155 static int __init set_nr_entries(char *str)
156 {
157         unsigned long nr_entries;
158         int ret;
159
160         if (!str)
161                 return 0;
162         ret = strict_strtoul(str, 0, &nr_entries);
163         /* nr_entries can not be zero */
164         if (ret < 0 || nr_entries == 0)
165                 return 0;
166         trace_nr_entries = nr_entries;
167         return 1;
168 }
169 __setup("trace_entries=", set_nr_entries);
170
171 unsigned long nsecs_to_usecs(unsigned long nsecs)
172 {
173         return nsecs / 1000;
174 }
175
176 /*
177  * trace_flag_type is an enumeration that holds different
178  * states when a trace occurs. These are:
179  *  IRQS_OFF    - interrupts were disabled
180  *  NEED_RESCED - reschedule is requested
181  *  HARDIRQ     - inside an interrupt handler
182  *  SOFTIRQ     - inside a softirq handler
183  */
184 enum trace_flag_type {
185         TRACE_FLAG_IRQS_OFF             = 0x01,
186         TRACE_FLAG_NEED_RESCHED         = 0x02,
187         TRACE_FLAG_HARDIRQ              = 0x04,
188         TRACE_FLAG_SOFTIRQ              = 0x08,
189 };
190
191 /*
192  * TRACE_ITER_SYM_MASK masks the options in trace_flags that
193  * control the output of kernel symbols.
194  */
195 #define TRACE_ITER_SYM_MASK \
196         (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
197
198 /* These must match the bit postions in trace_iterator_flags */
199 static const char *trace_options[] = {
200         "print-parent",
201         "sym-offset",
202         "sym-addr",
203         "verbose",
204         "raw",
205         "hex",
206         "bin",
207         "block",
208         "stacktrace",
209         "sched-tree",
210         NULL
211 };
212
213 /*
214  * ftrace_max_lock is used to protect the swapping of buffers
215  * when taking a max snapshot. The buffers themselves are
216  * protected by per_cpu spinlocks. But the action of the swap
217  * needs its own lock.
218  *
219  * This is defined as a raw_spinlock_t in order to help
220  * with performance when lockdep debugging is enabled.
221  */
222 static raw_spinlock_t ftrace_max_lock =
223         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
224
225 /*
226  * Copy the new maximum trace into the separate maximum-trace
227  * structure. (this way the maximum trace is permanently saved,
228  * for later retrieval via /debugfs/tracing/latency_trace)
229  */
230 static void
231 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
232 {
233         struct trace_array_cpu *data = tr->data[cpu];
234
235         max_tr.cpu = cpu;
236         max_tr.time_start = data->preempt_timestamp;
237
238         data = max_tr.data[cpu];
239         data->saved_latency = tracing_max_latency;
240
241         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
242         data->pid = tsk->pid;
243         data->uid = tsk->uid;
244         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
245         data->policy = tsk->policy;
246         data->rt_priority = tsk->rt_priority;
247
248         /* record this tasks comm */
249         tracing_record_cmdline(current);
250 }
251
252 /**
253  * check_pages - integrity check of trace buffers
254  *
255  * As a safty measure we check to make sure the data pages have not
256  * been corrupted. TODO: configure to disable this because it adds
257  * a bit of overhead.
258  */
259 void check_pages(struct trace_array_cpu *data)
260 {
261         struct page *page, *tmp;
262
263         BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
264         BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
265
266         list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
267                 BUG_ON(page->lru.next->prev != &page->lru);
268                 BUG_ON(page->lru.prev->next != &page->lru);
269         }
270 }
271
272 /**
273  * head_page - page address of the first page in per_cpu buffer.
274  *
275  * head_page returns the page address of the first page in
276  * a per_cpu buffer. This also preforms various consistency
277  * checks to make sure the buffer has not been corrupted.
278  */
279 void *head_page(struct trace_array_cpu *data)
280 {
281         struct page *page;
282
283         check_pages(data);
284         if (list_empty(&data->trace_pages))
285                 return NULL;
286
287         page = list_entry(data->trace_pages.next, struct page, lru);
288         BUG_ON(&page->lru == &data->trace_pages);
289
290         return page_address(page);
291 }
292
293 /**
294  * trace_seq_printf - sequence printing of trace information
295  * @s: trace sequence descriptor
296  * @fmt: printf format string
297  *
298  * The tracer may use either sequence operations or its own
299  * copy to user routines. To simplify formating of a trace
300  * trace_seq_printf is used to store strings into a special
301  * buffer (@s). Then the output may be either used by
302  * the sequencer or pulled into another buffer.
303  */
304 int
305 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
306 {
307         int len = (PAGE_SIZE - 1) - s->len;
308         va_list ap;
309         int ret;
310
311         if (!len)
312                 return 0;
313
314         va_start(ap, fmt);
315         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
316         va_end(ap);
317
318         /* If we can't write it all, don't bother writing anything */
319         if (ret >= len)
320                 return 0;
321
322         s->len += ret;
323
324         return len;
325 }
326
327 /**
328  * trace_seq_puts - trace sequence printing of simple string
329  * @s: trace sequence descriptor
330  * @str: simple string to record
331  *
332  * The tracer may use either the sequence operations or its own
333  * copy to user routines. This function records a simple string
334  * into a special buffer (@s) for later retrieval by a sequencer
335  * or other mechanism.
336  */
337 static int
338 trace_seq_puts(struct trace_seq *s, const char *str)
339 {
340         int len = strlen(str);
341
342         if (len > ((PAGE_SIZE - 1) - s->len))
343                 return 0;
344
345         memcpy(s->buffer + s->len, str, len);
346         s->len += len;
347
348         return len;
349 }
350
351 static int
352 trace_seq_putc(struct trace_seq *s, unsigned char c)
353 {
354         if (s->len >= (PAGE_SIZE - 1))
355                 return 0;
356
357         s->buffer[s->len++] = c;
358
359         return 1;
360 }
361
362 static int
363 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
364 {
365         if (len > ((PAGE_SIZE - 1) - s->len))
366                 return 0;
367
368         memcpy(s->buffer + s->len, mem, len);
369         s->len += len;
370
371         return len;
372 }
373
374 #define HEX_CHARS 17
375 static const char hex2asc[] = "0123456789abcdef";
376
377 static int
378 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
379 {
380         unsigned char hex[HEX_CHARS];
381         unsigned char *data = mem;
382         unsigned char byte;
383         int i, j;
384
385         BUG_ON(len >= HEX_CHARS);
386
387 #ifdef __BIG_ENDIAN
388         for (i = 0, j = 0; i < len; i++) {
389 #else
390         for (i = len-1, j = 0; i >= 0; i--) {
391 #endif
392                 byte = data[i];
393
394                 hex[j++] = hex2asc[byte & 0x0f];
395                 hex[j++] = hex2asc[byte >> 4];
396         }
397         hex[j++] = ' ';
398
399         return trace_seq_putmem(s, hex, j);
400 }
401
402 static void
403 trace_seq_reset(struct trace_seq *s)
404 {
405         s->len = 0;
406         s->readpos = 0;
407 }
408
409 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
410 {
411         int len;
412         int ret;
413
414         if (s->len <= s->readpos)
415                 return -EBUSY;
416
417         len = s->len - s->readpos;
418         if (cnt > len)
419                 cnt = len;
420         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
421         if (ret)
422                 return -EFAULT;
423
424         s->readpos += len;
425         return cnt;
426 }
427
428 static void
429 trace_print_seq(struct seq_file *m, struct trace_seq *s)
430 {
431         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
432
433         s->buffer[len] = 0;
434         seq_puts(m, s->buffer);
435
436         trace_seq_reset(s);
437 }
438
439 /*
440  * flip the trace buffers between two trace descriptors.
441  * This usually is the buffers between the global_trace and
442  * the max_tr to record a snapshot of a current trace.
443  *
444  * The ftrace_max_lock must be held.
445  */
446 static void
447 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
448 {
449         struct list_head flip_pages;
450
451         INIT_LIST_HEAD(&flip_pages);
452
453         memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
454                 sizeof(struct trace_array_cpu) -
455                 offsetof(struct trace_array_cpu, trace_head_idx));
456
457         check_pages(tr1);
458         check_pages(tr2);
459         list_splice_init(&tr1->trace_pages, &flip_pages);
460         list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
461         list_splice_init(&flip_pages, &tr2->trace_pages);
462         BUG_ON(!list_empty(&flip_pages));
463         check_pages(tr1);
464         check_pages(tr2);
465 }
466
467 /**
468  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
469  * @tr: tracer
470  * @tsk: the task with the latency
471  * @cpu: The cpu that initiated the trace.
472  *
473  * Flip the buffers between the @tr and the max_tr and record information
474  * about which task was the cause of this latency.
475  */
476 void
477 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
478 {
479         struct trace_array_cpu *data;
480         int i;
481
482         WARN_ON_ONCE(!irqs_disabled());
483         __raw_spin_lock(&ftrace_max_lock);
484         /* clear out all the previous traces */
485         for_each_tracing_cpu(i) {
486                 data = tr->data[i];
487                 flip_trace(max_tr.data[i], data);
488                 tracing_reset(data);
489         }
490
491         __update_max_tr(tr, tsk, cpu);
492         __raw_spin_unlock(&ftrace_max_lock);
493 }
494
495 /**
496  * update_max_tr_single - only copy one trace over, and reset the rest
497  * @tr - tracer
498  * @tsk - task with the latency
499  * @cpu - the cpu of the buffer to copy.
500  *
501  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
502  */
503 void
504 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
505 {
506         struct trace_array_cpu *data = tr->data[cpu];
507         int i;
508
509         WARN_ON_ONCE(!irqs_disabled());
510         __raw_spin_lock(&ftrace_max_lock);
511         for_each_tracing_cpu(i)
512                 tracing_reset(max_tr.data[i]);
513
514         flip_trace(max_tr.data[cpu], data);
515         tracing_reset(data);
516
517         __update_max_tr(tr, tsk, cpu);
518         __raw_spin_unlock(&ftrace_max_lock);
519 }
520
521 /**
522  * register_tracer - register a tracer with the ftrace system.
523  * @type - the plugin for the tracer
524  *
525  * Register a new plugin tracer.
526  */
527 int register_tracer(struct tracer *type)
528 {
529         struct tracer *t;
530         int len;
531         int ret = 0;
532
533         if (!type->name) {
534                 pr_info("Tracer must have a name\n");
535                 return -1;
536         }
537
538         mutex_lock(&trace_types_lock);
539         for (t = trace_types; t; t = t->next) {
540                 if (strcmp(type->name, t->name) == 0) {
541                         /* already found */
542                         pr_info("Trace %s already registered\n",
543                                 type->name);
544                         ret = -1;
545                         goto out;
546                 }
547         }
548
549 #ifdef CONFIG_FTRACE_STARTUP_TEST
550         if (type->selftest) {
551                 struct tracer *saved_tracer = current_trace;
552                 struct trace_array_cpu *data;
553                 struct trace_array *tr = &global_trace;
554                 int saved_ctrl = tr->ctrl;
555                 int i;
556                 /*
557                  * Run a selftest on this tracer.
558                  * Here we reset the trace buffer, and set the current
559                  * tracer to be this tracer. The tracer can then run some
560                  * internal tracing to verify that everything is in order.
561                  * If we fail, we do not register this tracer.
562                  */
563                 for_each_tracing_cpu(i) {
564                         data = tr->data[i];
565                         if (!head_page(data))
566                                 continue;
567                         tracing_reset(data);
568                 }
569                 current_trace = type;
570                 tr->ctrl = 0;
571                 /* the test is responsible for initializing and enabling */
572                 pr_info("Testing tracer %s: ", type->name);
573                 ret = type->selftest(type, tr);
574                 /* the test is responsible for resetting too */
575                 current_trace = saved_tracer;
576                 tr->ctrl = saved_ctrl;
577                 if (ret) {
578                         printk(KERN_CONT "FAILED!\n");
579                         goto out;
580                 }
581                 /* Only reset on passing, to avoid touching corrupted buffers */
582                 for_each_tracing_cpu(i) {
583                         data = tr->data[i];
584                         if (!head_page(data))
585                                 continue;
586                         tracing_reset(data);
587                 }
588                 printk(KERN_CONT "PASSED\n");
589         }
590 #endif
591
592         type->next = trace_types;
593         trace_types = type;
594         len = strlen(type->name);
595         if (len > max_tracer_type_len)
596                 max_tracer_type_len = len;
597
598  out:
599         mutex_unlock(&trace_types_lock);
600
601         return ret;
602 }
603
604 void unregister_tracer(struct tracer *type)
605 {
606         struct tracer **t;
607         int len;
608
609         mutex_lock(&trace_types_lock);
610         for (t = &trace_types; *t; t = &(*t)->next) {
611                 if (*t == type)
612                         goto found;
613         }
614         pr_info("Trace %s not registered\n", type->name);
615         goto out;
616
617  found:
618         *t = (*t)->next;
619         if (strlen(type->name) != max_tracer_type_len)
620                 goto out;
621
622         max_tracer_type_len = 0;
623         for (t = &trace_types; *t; t = &(*t)->next) {
624                 len = strlen((*t)->name);
625                 if (len > max_tracer_type_len)
626                         max_tracer_type_len = len;
627         }
628  out:
629         mutex_unlock(&trace_types_lock);
630 }
631
632 void tracing_reset(struct trace_array_cpu *data)
633 {
634         data->trace_idx = 0;
635         data->overrun = 0;
636         data->trace_head = data->trace_tail = head_page(data);
637         data->trace_head_idx = 0;
638         data->trace_tail_idx = 0;
639 }
640
641 #define SAVED_CMDLINES 128
642 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
643 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
644 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
645 static int cmdline_idx;
646 static DEFINE_SPINLOCK(trace_cmdline_lock);
647
648 /* trace in all context switches */
649 atomic_t trace_record_cmdline_enabled __read_mostly;
650
651 /* temporary disable recording */
652 atomic_t trace_record_cmdline_disabled __read_mostly;
653
654 static void trace_init_cmdlines(void)
655 {
656         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
657         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
658         cmdline_idx = 0;
659 }
660
661 void trace_stop_cmdline_recording(void);
662
663 static void trace_save_cmdline(struct task_struct *tsk)
664 {
665         unsigned map;
666         unsigned idx;
667
668         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
669                 return;
670
671         /*
672          * It's not the end of the world if we don't get
673          * the lock, but we also don't want to spin
674          * nor do we want to disable interrupts,
675          * so if we miss here, then better luck next time.
676          */
677         if (!spin_trylock(&trace_cmdline_lock))
678                 return;
679
680         idx = map_pid_to_cmdline[tsk->pid];
681         if (idx >= SAVED_CMDLINES) {
682                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
683
684                 map = map_cmdline_to_pid[idx];
685                 if (map <= PID_MAX_DEFAULT)
686                         map_pid_to_cmdline[map] = (unsigned)-1;
687
688                 map_pid_to_cmdline[tsk->pid] = idx;
689
690                 cmdline_idx = idx;
691         }
692
693         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
694
695         spin_unlock(&trace_cmdline_lock);
696 }
697
698 static char *trace_find_cmdline(int pid)
699 {
700         char *cmdline = "<...>";
701         unsigned map;
702
703         if (!pid)
704                 return "<idle>";
705
706         if (pid > PID_MAX_DEFAULT)
707                 goto out;
708
709         map = map_pid_to_cmdline[pid];
710         if (map >= SAVED_CMDLINES)
711                 goto out;
712
713         cmdline = saved_cmdlines[map];
714
715  out:
716         return cmdline;
717 }
718
719 void tracing_record_cmdline(struct task_struct *tsk)
720 {
721         if (atomic_read(&trace_record_cmdline_disabled))
722                 return;
723
724         trace_save_cmdline(tsk);
725 }
726
727 static inline struct list_head *
728 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
729 {
730         /*
731          * Roundrobin - but skip the head (which is not a real page):
732          */
733         next = next->next;
734         if (unlikely(next == &data->trace_pages))
735                 next = next->next;
736         BUG_ON(next == &data->trace_pages);
737
738         return next;
739 }
740
741 static inline void *
742 trace_next_page(struct trace_array_cpu *data, void *addr)
743 {
744         struct list_head *next;
745         struct page *page;
746
747         page = virt_to_page(addr);
748
749         next = trace_next_list(data, &page->lru);
750         page = list_entry(next, struct page, lru);
751
752         return page_address(page);
753 }
754
755 static inline struct trace_entry *
756 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
757 {
758         unsigned long idx, idx_next;
759         struct trace_entry *entry;
760
761         data->trace_idx++;
762         idx = data->trace_head_idx;
763         idx_next = idx + 1;
764
765         BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
766
767         entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
768
769         if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
770                 data->trace_head = trace_next_page(data, data->trace_head);
771                 idx_next = 0;
772         }
773
774         if (data->trace_head == data->trace_tail &&
775             idx_next == data->trace_tail_idx) {
776                 /* overrun */
777                 data->overrun++;
778                 data->trace_tail_idx++;
779                 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
780                         data->trace_tail =
781                                 trace_next_page(data, data->trace_tail);
782                         data->trace_tail_idx = 0;
783                 }
784         }
785
786         data->trace_head_idx = idx_next;
787
788         return entry;
789 }
790
791 static inline void
792 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
793 {
794         struct task_struct *tsk = current;
795         unsigned long pc;
796
797         pc = preempt_count();
798
799         entry->preempt_count    = pc & 0xff;
800         entry->pid              = (tsk) ? tsk->pid : 0;
801         entry->t                = ftrace_now(raw_smp_processor_id());
802         entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
803                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
804                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
805                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
806 }
807
808 void
809 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
810                unsigned long ip, unsigned long parent_ip, unsigned long flags)
811 {
812         struct trace_entry *entry;
813         unsigned long irq_flags;
814
815         raw_local_irq_save(irq_flags);
816         __raw_spin_lock(&data->lock);
817         entry                   = tracing_get_trace_entry(tr, data);
818         tracing_generic_entry_update(entry, flags);
819         entry->type             = TRACE_FN;
820         entry->fn.ip            = ip;
821         entry->fn.parent_ip     = parent_ip;
822         __raw_spin_unlock(&data->lock);
823         raw_local_irq_restore(irq_flags);
824 }
825
826 void
827 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
828        unsigned long ip, unsigned long parent_ip, unsigned long flags)
829 {
830         if (likely(!atomic_read(&data->disabled)))
831                 trace_function(tr, data, ip, parent_ip, flags);
832 }
833
834 #ifdef CONFIG_MMIOTRACE
835 void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
836                                                 struct mmiotrace_rw *rw)
837 {
838         struct trace_entry *entry;
839         unsigned long irq_flags;
840
841         spin_lock_irqsave(&data->lock, irq_flags);
842         entry                   = tracing_get_trace_entry(tr, data);
843         tracing_generic_entry_update(entry, 0);
844         entry->type             = TRACE_MMIO_RW;
845         entry->mmiorw           = *rw;
846         spin_unlock_irqrestore(&data->lock, irq_flags);
847
848         trace_wake_up();
849 }
850
851 void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
852                                                 struct mmiotrace_map *map)
853 {
854         struct trace_entry *entry;
855         unsigned long irq_flags;
856
857         spin_lock_irqsave(&data->lock, irq_flags);
858         entry                   = tracing_get_trace_entry(tr, data);
859         tracing_generic_entry_update(entry, 0);
860         entry->type             = TRACE_MMIO_MAP;
861         entry->mmiomap          = *map;
862         spin_unlock_irqrestore(&data->lock, irq_flags);
863
864         trace_wake_up();
865 }
866 #endif
867
868 void __trace_stack(struct trace_array *tr,
869                    struct trace_array_cpu *data,
870                    unsigned long flags,
871                    int skip)
872 {
873         struct trace_entry *entry;
874         struct stack_trace trace;
875
876         if (!(trace_flags & TRACE_ITER_STACKTRACE))
877                 return;
878
879         entry                   = tracing_get_trace_entry(tr, data);
880         tracing_generic_entry_update(entry, flags);
881         entry->type             = TRACE_STACK;
882
883         memset(&entry->stack, 0, sizeof(entry->stack));
884
885         trace.nr_entries        = 0;
886         trace.max_entries       = FTRACE_STACK_ENTRIES;
887         trace.skip              = skip;
888         trace.entries           = entry->stack.caller;
889
890         save_stack_trace(&trace);
891 }
892
893 void
894 __trace_special(void *__tr, void *__data,
895                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
896 {
897         struct trace_array_cpu *data = __data;
898         struct trace_array *tr = __tr;
899         struct trace_entry *entry;
900         unsigned long irq_flags;
901
902         raw_local_irq_save(irq_flags);
903         __raw_spin_lock(&data->lock);
904         entry                   = tracing_get_trace_entry(tr, data);
905         tracing_generic_entry_update(entry, 0);
906         entry->type             = TRACE_SPECIAL;
907         entry->special.arg1     = arg1;
908         entry->special.arg2     = arg2;
909         entry->special.arg3     = arg3;
910         __trace_stack(tr, data, irq_flags, 4);
911         __raw_spin_unlock(&data->lock);
912         raw_local_irq_restore(irq_flags);
913
914         trace_wake_up();
915 }
916
917 void
918 tracing_sched_switch_trace(struct trace_array *tr,
919                            struct trace_array_cpu *data,
920                            struct task_struct *prev,
921                            struct task_struct *next,
922                            unsigned long flags)
923 {
924         struct trace_entry *entry;
925         unsigned long irq_flags;
926
927         raw_local_irq_save(irq_flags);
928         __raw_spin_lock(&data->lock);
929         entry                   = tracing_get_trace_entry(tr, data);
930         tracing_generic_entry_update(entry, flags);
931         entry->type             = TRACE_CTX;
932         entry->ctx.prev_pid     = prev->pid;
933         entry->ctx.prev_prio    = prev->prio;
934         entry->ctx.prev_state   = prev->state;
935         entry->ctx.next_pid     = next->pid;
936         entry->ctx.next_prio    = next->prio;
937         entry->ctx.next_state   = next->state;
938         __trace_stack(tr, data, flags, 5);
939         __raw_spin_unlock(&data->lock);
940         raw_local_irq_restore(irq_flags);
941 }
942
943 void
944 tracing_sched_wakeup_trace(struct trace_array *tr,
945                            struct trace_array_cpu *data,
946                            struct task_struct *wakee,
947                            struct task_struct *curr,
948                            unsigned long flags)
949 {
950         struct trace_entry *entry;
951         unsigned long irq_flags;
952
953         raw_local_irq_save(irq_flags);
954         __raw_spin_lock(&data->lock);
955         entry                   = tracing_get_trace_entry(tr, data);
956         tracing_generic_entry_update(entry, flags);
957         entry->type             = TRACE_WAKE;
958         entry->ctx.prev_pid     = curr->pid;
959         entry->ctx.prev_prio    = curr->prio;
960         entry->ctx.prev_state   = curr->state;
961         entry->ctx.next_pid     = wakee->pid;
962         entry->ctx.next_prio    = wakee->prio;
963         entry->ctx.next_state   = wakee->state;
964         __trace_stack(tr, data, flags, 6);
965         __raw_spin_unlock(&data->lock);
966         raw_local_irq_restore(irq_flags);
967
968         trace_wake_up();
969 }
970
971 #ifdef CONFIG_FTRACE
972 static void
973 function_trace_call(unsigned long ip, unsigned long parent_ip)
974 {
975         struct trace_array *tr = &global_trace;
976         struct trace_array_cpu *data;
977         unsigned long flags;
978         long disabled;
979         int cpu;
980
981         if (unlikely(!tracer_enabled))
982                 return;
983
984         local_irq_save(flags);
985         cpu = raw_smp_processor_id();
986         data = tr->data[cpu];
987         disabled = atomic_inc_return(&data->disabled);
988
989         if (likely(disabled == 1))
990                 trace_function(tr, data, ip, parent_ip, flags);
991
992         atomic_dec(&data->disabled);
993         local_irq_restore(flags);
994 }
995
996 static struct ftrace_ops trace_ops __read_mostly =
997 {
998         .func = function_trace_call,
999 };
1000
1001 void tracing_start_function_trace(void)
1002 {
1003         register_ftrace_function(&trace_ops);
1004 }
1005
1006 void tracing_stop_function_trace(void)
1007 {
1008         unregister_ftrace_function(&trace_ops);
1009 }
1010 #endif
1011
1012 enum trace_file_type {
1013         TRACE_FILE_LAT_FMT      = 1,
1014 };
1015
1016 static struct trace_entry *
1017 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1018                 struct trace_iterator *iter, int cpu)
1019 {
1020         struct page *page;
1021         struct trace_entry *array;
1022
1023         if (iter->next_idx[cpu] >= tr->entries ||
1024             iter->next_idx[cpu] >= data->trace_idx ||
1025             (data->trace_head == data->trace_tail &&
1026              data->trace_head_idx == data->trace_tail_idx))
1027                 return NULL;
1028
1029         if (!iter->next_page[cpu]) {
1030                 /* Initialize the iterator for this cpu trace buffer */
1031                 WARN_ON(!data->trace_tail);
1032                 page = virt_to_page(data->trace_tail);
1033                 iter->next_page[cpu] = &page->lru;
1034                 iter->next_page_idx[cpu] = data->trace_tail_idx;
1035         }
1036
1037         page = list_entry(iter->next_page[cpu], struct page, lru);
1038         BUG_ON(&data->trace_pages == &page->lru);
1039
1040         array = page_address(page);
1041
1042         WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
1043         return &array[iter->next_page_idx[cpu]];
1044 }
1045
1046 static struct trace_entry *
1047 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1048 {
1049         struct trace_array *tr = iter->tr;
1050         struct trace_entry *ent, *next = NULL;
1051         int next_cpu = -1;
1052         int cpu;
1053
1054         for_each_tracing_cpu(cpu) {
1055                 if (!head_page(tr->data[cpu]))
1056                         continue;
1057                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1058                 /*
1059                  * Pick the entry with the smallest timestamp:
1060                  */
1061                 if (ent && (!next || ent->t < next->t)) {
1062                         next = ent;
1063                         next_cpu = cpu;
1064                 }
1065         }
1066
1067         if (ent_cpu)
1068                 *ent_cpu = next_cpu;
1069
1070         return next;
1071 }
1072
1073 static void trace_iterator_increment(struct trace_iterator *iter)
1074 {
1075         iter->idx++;
1076         iter->next_idx[iter->cpu]++;
1077         iter->next_page_idx[iter->cpu]++;
1078
1079         if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1080                 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1081
1082                 iter->next_page_idx[iter->cpu] = 0;
1083                 iter->next_page[iter->cpu] =
1084                         trace_next_list(data, iter->next_page[iter->cpu]);
1085         }
1086 }
1087
1088 static void trace_consume(struct trace_iterator *iter)
1089 {
1090         struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1091
1092         data->trace_tail_idx++;
1093         if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1094                 data->trace_tail = trace_next_page(data, data->trace_tail);
1095                 data->trace_tail_idx = 0;
1096         }
1097
1098         /* Check if we empty it, then reset the index */
1099         if (data->trace_head == data->trace_tail &&
1100             data->trace_head_idx == data->trace_tail_idx)
1101                 data->trace_idx = 0;
1102 }
1103
1104 static void *find_next_entry_inc(struct trace_iterator *iter)
1105 {
1106         struct trace_entry *next;
1107         int next_cpu = -1;
1108
1109         next = find_next_entry(iter, &next_cpu);
1110
1111         iter->prev_ent = iter->ent;
1112         iter->prev_cpu = iter->cpu;
1113
1114         iter->ent = next;
1115         iter->cpu = next_cpu;
1116
1117         if (next)
1118                 trace_iterator_increment(iter);
1119
1120         return next ? iter : NULL;
1121 }
1122
1123 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1124 {
1125         struct trace_iterator *iter = m->private;
1126         void *last_ent = iter->ent;
1127         int i = (int)*pos;
1128         void *ent;
1129
1130         (*pos)++;
1131
1132         /* can't go backwards */
1133         if (iter->idx > i)
1134                 return NULL;
1135
1136         if (iter->idx < 0)
1137                 ent = find_next_entry_inc(iter);
1138         else
1139                 ent = iter;
1140
1141         while (ent && iter->idx < i)
1142                 ent = find_next_entry_inc(iter);
1143
1144         iter->pos = *pos;
1145
1146         if (last_ent && !ent)
1147                 seq_puts(m, "\n\nvim:ft=help\n");
1148
1149         return ent;
1150 }
1151
1152 static void *s_start(struct seq_file *m, loff_t *pos)
1153 {
1154         struct trace_iterator *iter = m->private;
1155         void *p = NULL;
1156         loff_t l = 0;
1157         int i;
1158
1159         mutex_lock(&trace_types_lock);
1160
1161         if (!current_trace || current_trace != iter->trace) {
1162                 mutex_unlock(&trace_types_lock);
1163                 return NULL;
1164         }
1165
1166         atomic_inc(&trace_record_cmdline_disabled);
1167
1168         /* let the tracer grab locks here if needed */
1169         if (current_trace->start)
1170                 current_trace->start(iter);
1171
1172         if (*pos != iter->pos) {
1173                 iter->ent = NULL;
1174                 iter->cpu = 0;
1175                 iter->idx = -1;
1176                 iter->prev_ent = NULL;
1177                 iter->prev_cpu = -1;
1178
1179                 for_each_tracing_cpu(i) {
1180                         iter->next_idx[i] = 0;
1181                         iter->next_page[i] = NULL;
1182                 }
1183
1184                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1185                         ;
1186
1187         } else {
1188                 l = *pos - 1;
1189                 p = s_next(m, p, &l);
1190         }
1191
1192         return p;
1193 }
1194
1195 static void s_stop(struct seq_file *m, void *p)
1196 {
1197         struct trace_iterator *iter = m->private;
1198
1199         atomic_dec(&trace_record_cmdline_disabled);
1200
1201         /* let the tracer release locks here if needed */
1202         if (current_trace && current_trace == iter->trace && iter->trace->stop)
1203                 iter->trace->stop(iter);
1204
1205         mutex_unlock(&trace_types_lock);
1206 }
1207
1208 static int
1209 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1210 {
1211 #ifdef CONFIG_KALLSYMS
1212         char str[KSYM_SYMBOL_LEN];
1213
1214         kallsyms_lookup(address, NULL, NULL, NULL, str);
1215
1216         return trace_seq_printf(s, fmt, str);
1217 #endif
1218         return 1;
1219 }
1220
1221 static int
1222 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1223                      unsigned long address)
1224 {
1225 #ifdef CONFIG_KALLSYMS
1226         char str[KSYM_SYMBOL_LEN];
1227
1228         sprint_symbol(str, address);
1229         return trace_seq_printf(s, fmt, str);
1230 #endif
1231         return 1;
1232 }
1233
1234 #ifndef CONFIG_64BIT
1235 # define IP_FMT "%08lx"
1236 #else
1237 # define IP_FMT "%016lx"
1238 #endif
1239
1240 static int
1241 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1242 {
1243         int ret;
1244
1245         if (!ip)
1246                 return trace_seq_printf(s, "0");
1247
1248         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1249                 ret = seq_print_sym_offset(s, "%s", ip);
1250         else
1251                 ret = seq_print_sym_short(s, "%s", ip);
1252
1253         if (!ret)
1254                 return 0;
1255
1256         if (sym_flags & TRACE_ITER_SYM_ADDR)
1257                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1258         return ret;
1259 }
1260
1261 static void print_lat_help_header(struct seq_file *m)
1262 {
1263         seq_puts(m, "#                _------=> CPU#            \n");
1264         seq_puts(m, "#               / _-----=> irqs-off        \n");
1265         seq_puts(m, "#              | / _----=> need-resched    \n");
1266         seq_puts(m, "#              || / _---=> hardirq/softirq \n");
1267         seq_puts(m, "#              ||| / _--=> preempt-depth   \n");
1268         seq_puts(m, "#              |||| /                      \n");
1269         seq_puts(m, "#              |||||     delay             \n");
1270         seq_puts(m, "#  cmd     pid ||||| time  |   caller      \n");
1271         seq_puts(m, "#     \\   /    |||||   \\   |   /           \n");
1272 }
1273
1274 static void print_func_help_header(struct seq_file *m)
1275 {
1276         seq_puts(m, "#           TASK-PID   CPU#    TIMESTAMP  FUNCTION\n");
1277         seq_puts(m, "#              | |      |          |         |\n");
1278 }
1279
1280
1281 static void
1282 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1283 {
1284         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1285         struct trace_array *tr = iter->tr;
1286         struct trace_array_cpu *data = tr->data[tr->cpu];
1287         struct tracer *type = current_trace;
1288         unsigned long total   = 0;
1289         unsigned long entries = 0;
1290         int cpu;
1291         const char *name = "preemption";
1292
1293         if (type)
1294                 name = type->name;
1295
1296         for_each_tracing_cpu(cpu) {
1297                 if (head_page(tr->data[cpu])) {
1298                         total += tr->data[cpu]->trace_idx;
1299                         if (tr->data[cpu]->trace_idx > tr->entries)
1300                                 entries += tr->entries;
1301                         else
1302                                 entries += tr->data[cpu]->trace_idx;
1303                 }
1304         }
1305
1306         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1307                    name, UTS_RELEASE);
1308         seq_puts(m, "-----------------------------------"
1309                  "---------------------------------\n");
1310         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1311                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1312                    nsecs_to_usecs(data->saved_latency),
1313                    entries,
1314                    total,
1315                    tr->cpu,
1316 #if defined(CONFIG_PREEMPT_NONE)
1317                    "server",
1318 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1319                    "desktop",
1320 #elif defined(CONFIG_PREEMPT_DESKTOP)
1321                    "preempt",
1322 #else
1323                    "unknown",
1324 #endif
1325                    /* These are reserved for later use */
1326                    0, 0, 0, 0);
1327 #ifdef CONFIG_SMP
1328         seq_printf(m, " #P:%d)\n", num_online_cpus());
1329 #else
1330         seq_puts(m, ")\n");
1331 #endif
1332         seq_puts(m, "    -----------------\n");
1333         seq_printf(m, "    | task: %.16s-%d "
1334                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1335                    data->comm, data->pid, data->uid, data->nice,
1336                    data->policy, data->rt_priority);
1337         seq_puts(m, "    -----------------\n");
1338
1339         if (data->critical_start) {
1340                 seq_puts(m, " => started at: ");
1341                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1342                 trace_print_seq(m, &iter->seq);
1343                 seq_puts(m, "\n => ended at:   ");
1344                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1345                 trace_print_seq(m, &iter->seq);
1346                 seq_puts(m, "\n");
1347         }
1348
1349         seq_puts(m, "\n");
1350 }
1351
1352 static void
1353 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1354 {
1355         int hardirq, softirq;
1356         char *comm;
1357
1358         comm = trace_find_cmdline(entry->pid);
1359
1360         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1361         trace_seq_printf(s, "%d", cpu);
1362         trace_seq_printf(s, "%c%c",
1363                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1364                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1365
1366         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1367         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1368         if (hardirq && softirq) {
1369                 trace_seq_putc(s, 'H');
1370         } else {
1371                 if (hardirq) {
1372                         trace_seq_putc(s, 'h');
1373                 } else {
1374                         if (softirq)
1375                                 trace_seq_putc(s, 's');
1376                         else
1377                                 trace_seq_putc(s, '.');
1378                 }
1379         }
1380
1381         if (entry->preempt_count)
1382                 trace_seq_printf(s, "%x", entry->preempt_count);
1383         else
1384                 trace_seq_puts(s, ".");
1385 }
1386
1387 unsigned long preempt_mark_thresh = 100;
1388
1389 static void
1390 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1391                     unsigned long rel_usecs)
1392 {
1393         trace_seq_printf(s, " %4lldus", abs_usecs);
1394         if (rel_usecs > preempt_mark_thresh)
1395                 trace_seq_puts(s, "!: ");
1396         else if (rel_usecs > 1)
1397                 trace_seq_puts(s, "+: ");
1398         else
1399                 trace_seq_puts(s, " : ");
1400 }
1401
1402 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1403
1404 static int
1405 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1406 {
1407         struct trace_seq *s = &iter->seq;
1408         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1409         struct trace_entry *next_entry = find_next_entry(iter, NULL);
1410         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1411         struct trace_entry *entry = iter->ent;
1412         unsigned long abs_usecs;
1413         unsigned long rel_usecs;
1414         char *comm;
1415         int S, T;
1416         int i;
1417         unsigned state;
1418
1419         if (!next_entry)
1420                 next_entry = entry;
1421         rel_usecs = ns2usecs(next_entry->t - entry->t);
1422         abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1423
1424         if (verbose) {
1425                 comm = trace_find_cmdline(entry->pid);
1426                 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1427                                  " %ld.%03ldms (+%ld.%03ldms): ",
1428                                  comm,
1429                                  entry->pid, cpu, entry->flags,
1430                                  entry->preempt_count, trace_idx,
1431                                  ns2usecs(entry->t),
1432                                  abs_usecs/1000,
1433                                  abs_usecs % 1000, rel_usecs/1000,
1434                                  rel_usecs % 1000);
1435         } else {
1436                 lat_print_generic(s, entry, cpu);
1437                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1438         }
1439         switch (entry->type) {
1440         case TRACE_FN:
1441                 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1442                 trace_seq_puts(s, " (");
1443                 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1444                 trace_seq_puts(s, ")\n");
1445                 break;
1446         case TRACE_CTX:
1447         case TRACE_WAKE:
1448                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1449                         state_to_char[entry->ctx.next_state] : 'X';
1450
1451                 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1452                 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
1453                 comm = trace_find_cmdline(entry->ctx.next_pid);
1454                 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
1455                                  entry->ctx.prev_pid,
1456                                  entry->ctx.prev_prio,
1457                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1458                                  entry->ctx.next_pid,
1459                                  entry->ctx.next_prio,
1460                                  T, comm);
1461                 break;
1462         case TRACE_SPECIAL:
1463                 trace_seq_printf(s, "# %ld %ld %ld\n",
1464                                  entry->special.arg1,
1465                                  entry->special.arg2,
1466                                  entry->special.arg3);
1467                 break;
1468         case TRACE_STACK:
1469                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1470                         if (i)
1471                                 trace_seq_puts(s, " <= ");
1472                         seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1473                 }
1474                 trace_seq_puts(s, "\n");
1475                 break;
1476         default:
1477                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1478         }
1479         return 1;
1480 }
1481
1482 static int print_trace_fmt(struct trace_iterator *iter)
1483 {
1484         struct trace_seq *s = &iter->seq;
1485         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1486         struct trace_entry *entry;
1487         unsigned long usec_rem;
1488         unsigned long long t;
1489         unsigned long secs;
1490         char *comm;
1491         int ret;
1492         int S, T;
1493         int i;
1494
1495         entry = iter->ent;
1496
1497         comm = trace_find_cmdline(iter->ent->pid);
1498
1499         t = ns2usecs(entry->t);
1500         usec_rem = do_div(t, 1000000ULL);
1501         secs = (unsigned long)t;
1502
1503         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1504         if (!ret)
1505                 return 0;
1506         ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1507         if (!ret)
1508                 return 0;
1509         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1510         if (!ret)
1511                 return 0;
1512
1513         switch (entry->type) {
1514         case TRACE_FN:
1515                 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1516                 if (!ret)
1517                         return 0;
1518                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1519                                                 entry->fn.parent_ip) {
1520                         ret = trace_seq_printf(s, " <-");
1521                         if (!ret)
1522                                 return 0;
1523                         ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1524                                                sym_flags);
1525                         if (!ret)
1526                                 return 0;
1527                 }
1528                 ret = trace_seq_printf(s, "\n");
1529                 if (!ret)
1530                         return 0;
1531                 break;
1532         case TRACE_CTX:
1533         case TRACE_WAKE:
1534                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1535                         state_to_char[entry->ctx.prev_state] : 'X';
1536                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1537                         state_to_char[entry->ctx.next_state] : 'X';
1538                 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
1539                                        entry->ctx.prev_pid,
1540                                        entry->ctx.prev_prio,
1541                                        S,
1542                                        entry->type == TRACE_CTX ? "==>" : "  +",
1543                                        entry->ctx.next_pid,
1544                                        entry->ctx.next_prio,
1545                                        T);
1546                 if (!ret)
1547                         return 0;
1548                 break;
1549         case TRACE_SPECIAL:
1550                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1551                                  entry->special.arg1,
1552                                  entry->special.arg2,
1553                                  entry->special.arg3);
1554                 if (!ret)
1555                         return 0;
1556                 break;
1557         case TRACE_STACK:
1558                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1559                         if (i) {
1560                                 ret = trace_seq_puts(s, " <= ");
1561                                 if (!ret)
1562                                         return 0;
1563                         }
1564                         ret = seq_print_ip_sym(s, entry->stack.caller[i],
1565                                                sym_flags);
1566                         if (!ret)
1567                                 return 0;
1568                 }
1569                 ret = trace_seq_puts(s, "\n");
1570                 if (!ret)
1571                         return 0;
1572                 break;
1573         }
1574         return 1;
1575 }
1576
1577 static int print_raw_fmt(struct trace_iterator *iter)
1578 {
1579         struct trace_seq *s = &iter->seq;
1580         struct trace_entry *entry;
1581         int ret;
1582         int S, T;
1583
1584         entry = iter->ent;
1585
1586         ret = trace_seq_printf(s, "%d %d %llu ",
1587                 entry->pid, iter->cpu, entry->t);
1588         if (!ret)
1589                 return 0;
1590
1591         switch (entry->type) {
1592         case TRACE_FN:
1593                 ret = trace_seq_printf(s, "%x %x\n",
1594                                         entry->fn.ip, entry->fn.parent_ip);
1595                 if (!ret)
1596                         return 0;
1597                 break;
1598         case TRACE_CTX:
1599         case TRACE_WAKE:
1600                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1601                         state_to_char[entry->ctx.prev_state] : 'X';
1602                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1603                         state_to_char[entry->ctx.next_state] : 'X';
1604                 if (entry->type == TRACE_WAKE)
1605                         S = '+';
1606                 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
1607                                        entry->ctx.prev_pid,
1608                                        entry->ctx.prev_prio,
1609                                        S,
1610                                        entry->ctx.next_pid,
1611                                        entry->ctx.next_prio,
1612                                        T);
1613                 if (!ret)
1614                         return 0;
1615                 break;
1616         case TRACE_SPECIAL:
1617         case TRACE_STACK:
1618                 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
1619                                  entry->special.arg1,
1620                                  entry->special.arg2,
1621                                  entry->special.arg3);
1622                 if (!ret)
1623                         return 0;
1624                 break;
1625         }
1626         return 1;
1627 }
1628
1629 #define SEQ_PUT_FIELD_RET(s, x)                         \
1630 do {                                                    \
1631         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
1632                 return 0;                               \
1633 } while (0)
1634
1635 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
1636 do {                                                    \
1637         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
1638                 return 0;                               \
1639 } while (0)
1640
1641 static int print_hex_fmt(struct trace_iterator *iter)
1642 {
1643         struct trace_seq *s = &iter->seq;
1644         unsigned char newline = '\n';
1645         struct trace_entry *entry;
1646         int S, T;
1647
1648         entry = iter->ent;
1649
1650         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1651         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1652         SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1653
1654         switch (entry->type) {
1655         case TRACE_FN:
1656                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1657                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1658                 break;
1659         case TRACE_CTX:
1660         case TRACE_WAKE:
1661                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1662                         state_to_char[entry->ctx.prev_state] : 'X';
1663                 T = entry->ctx.next_state < sizeof(state_to_char) ?
1664                         state_to_char[entry->ctx.next_state] : 'X';
1665                 if (entry->type == TRACE_WAKE)
1666                         S = '+';
1667                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1668                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1669                 SEQ_PUT_HEX_FIELD_RET(s, S);
1670                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1671                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1672                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1673                 SEQ_PUT_HEX_FIELD_RET(s, T);
1674                 break;
1675         case TRACE_SPECIAL:
1676         case TRACE_STACK:
1677                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1678                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1679                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1680                 break;
1681         }
1682         SEQ_PUT_FIELD_RET(s, newline);
1683
1684         return 1;
1685 }
1686
1687 static int print_bin_fmt(struct trace_iterator *iter)
1688 {
1689         struct trace_seq *s = &iter->seq;
1690         struct trace_entry *entry;
1691
1692         entry = iter->ent;
1693
1694         SEQ_PUT_FIELD_RET(s, entry->pid);
1695         SEQ_PUT_FIELD_RET(s, entry->cpu);
1696         SEQ_PUT_FIELD_RET(s, entry->t);
1697
1698         switch (entry->type) {
1699         case TRACE_FN:
1700                 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1701                 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1702                 break;
1703         case TRACE_CTX:
1704                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1705                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1706                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1707                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1708                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1709                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
1710                 break;
1711         case TRACE_SPECIAL:
1712         case TRACE_STACK:
1713                 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1714                 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1715                 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1716                 break;
1717         }
1718         return 1;
1719 }
1720
1721 static int trace_empty(struct trace_iterator *iter)
1722 {
1723         struct trace_array_cpu *data;
1724         int cpu;
1725
1726         for_each_tracing_cpu(cpu) {
1727                 data = iter->tr->data[cpu];
1728
1729                 if (head_page(data) && data->trace_idx &&
1730                     (data->trace_tail != data->trace_head ||
1731                      data->trace_tail_idx != data->trace_head_idx))
1732                         return 0;
1733         }
1734         return 1;
1735 }
1736
1737 static int print_trace_line(struct trace_iterator *iter)
1738 {
1739         if (iter->trace && iter->trace->print_line)
1740                 return iter->trace->print_line(iter);
1741
1742         if (trace_flags & TRACE_ITER_BIN)
1743                 return print_bin_fmt(iter);
1744
1745         if (trace_flags & TRACE_ITER_HEX)
1746                 return print_hex_fmt(iter);
1747
1748         if (trace_flags & TRACE_ITER_RAW)
1749                 return print_raw_fmt(iter);
1750
1751         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1752                 return print_lat_fmt(iter, iter->idx, iter->cpu);
1753
1754         return print_trace_fmt(iter);
1755 }
1756
1757 static int s_show(struct seq_file *m, void *v)
1758 {
1759         struct trace_iterator *iter = v;
1760
1761         if (iter->ent == NULL) {
1762                 if (iter->tr) {
1763                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1764                         seq_puts(m, "#\n");
1765                 }
1766                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1767                         /* print nothing if the buffers are empty */
1768                         if (trace_empty(iter))
1769                                 return 0;
1770                         print_trace_header(m, iter);
1771                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1772                                 print_lat_help_header(m);
1773                 } else {
1774                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1775                                 print_func_help_header(m);
1776                 }
1777         } else {
1778                 print_trace_line(iter);
1779                 trace_print_seq(m, &iter->seq);
1780         }
1781
1782         return 0;
1783 }
1784
1785 static struct seq_operations tracer_seq_ops = {
1786         .start          = s_start,
1787         .next           = s_next,
1788         .stop           = s_stop,
1789         .show           = s_show,
1790 };
1791
1792 static struct trace_iterator *
1793 __tracing_open(struct inode *inode, struct file *file, int *ret)
1794 {
1795         struct trace_iterator *iter;
1796
1797         if (tracing_disabled) {
1798                 *ret = -ENODEV;
1799                 return NULL;
1800         }
1801
1802         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1803         if (!iter) {
1804                 *ret = -ENOMEM;
1805                 goto out;
1806         }
1807
1808         mutex_lock(&trace_types_lock);
1809         if (current_trace && current_trace->print_max)
1810                 iter->tr = &max_tr;
1811         else
1812                 iter->tr = inode->i_private;
1813         iter->trace = current_trace;
1814         iter->pos = -1;
1815
1816         /* TODO stop tracer */
1817         *ret = seq_open(file, &tracer_seq_ops);
1818         if (!*ret) {
1819                 struct seq_file *m = file->private_data;
1820                 m->private = iter;
1821
1822                 /* stop the trace while dumping */
1823                 if (iter->tr->ctrl)
1824                         tracer_enabled = 0;
1825
1826                 if (iter->trace && iter->trace->open)
1827                         iter->trace->open(iter);
1828         } else {
1829                 kfree(iter);
1830                 iter = NULL;
1831         }
1832         mutex_unlock(&trace_types_lock);
1833
1834  out:
1835         return iter;
1836 }
1837
1838 int tracing_open_generic(struct inode *inode, struct file *filp)
1839 {
1840         if (tracing_disabled)
1841                 return -ENODEV;
1842
1843         filp->private_data = inode->i_private;
1844         return 0;
1845 }
1846
1847 int tracing_release(struct inode *inode, struct file *file)
1848 {
1849         struct seq_file *m = (struct seq_file *)file->private_data;
1850         struct trace_iterator *iter = m->private;
1851
1852         mutex_lock(&trace_types_lock);
1853         if (iter->trace && iter->trace->close)
1854                 iter->trace->close(iter);
1855
1856         /* reenable tracing if it was previously enabled */
1857         if (iter->tr->ctrl)
1858                 tracer_enabled = 1;
1859         mutex_unlock(&trace_types_lock);
1860
1861         seq_release(inode, file);
1862         kfree(iter);
1863         return 0;
1864 }
1865
1866 static int tracing_open(struct inode *inode, struct file *file)
1867 {
1868         int ret;
1869
1870         __tracing_open(inode, file, &ret);
1871
1872         return ret;
1873 }
1874
1875 static int tracing_lt_open(struct inode *inode, struct file *file)
1876 {
1877         struct trace_iterator *iter;
1878         int ret;
1879
1880         iter = __tracing_open(inode, file, &ret);
1881
1882         if (!ret)
1883                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1884
1885         return ret;
1886 }
1887
1888
1889 static void *
1890 t_next(struct seq_file *m, void *v, loff_t *pos)
1891 {
1892         struct tracer *t = m->private;
1893
1894         (*pos)++;
1895
1896         if (t)
1897                 t = t->next;
1898
1899         m->private = t;
1900
1901         return t;
1902 }
1903
1904 static void *t_start(struct seq_file *m, loff_t *pos)
1905 {
1906         struct tracer *t = m->private;
1907         loff_t l = 0;
1908
1909         mutex_lock(&trace_types_lock);
1910         for (; t && l < *pos; t = t_next(m, t, &l))
1911                 ;
1912
1913         return t;
1914 }
1915
1916 static void t_stop(struct seq_file *m, void *p)
1917 {
1918         mutex_unlock(&trace_types_lock);
1919 }
1920
1921 static int t_show(struct seq_file *m, void *v)
1922 {
1923         struct tracer *t = v;
1924
1925         if (!t)
1926                 return 0;
1927
1928         seq_printf(m, "%s", t->name);
1929         if (t->next)
1930                 seq_putc(m, ' ');
1931         else
1932                 seq_putc(m, '\n');
1933
1934         return 0;
1935 }
1936
1937 static struct seq_operations show_traces_seq_ops = {
1938         .start          = t_start,
1939         .next           = t_next,
1940         .stop           = t_stop,
1941         .show           = t_show,
1942 };
1943
1944 static int show_traces_open(struct inode *inode, struct file *file)
1945 {
1946         int ret;
1947
1948         if (tracing_disabled)
1949                 return -ENODEV;
1950
1951         ret = seq_open(file, &show_traces_seq_ops);
1952         if (!ret) {
1953                 struct seq_file *m = file->private_data;
1954                 m->private = trace_types;
1955         }
1956
1957         return ret;
1958 }
1959
1960 static struct file_operations tracing_fops = {
1961         .open           = tracing_open,
1962         .read           = seq_read,
1963         .llseek         = seq_lseek,
1964         .release        = tracing_release,
1965 };
1966
1967 static struct file_operations tracing_lt_fops = {
1968         .open           = tracing_lt_open,
1969         .read           = seq_read,
1970         .llseek         = seq_lseek,
1971         .release        = tracing_release,
1972 };
1973
1974 static struct file_operations show_traces_fops = {
1975         .open           = show_traces_open,
1976         .read           = seq_read,
1977         .release        = seq_release,
1978 };
1979
1980 /*
1981  * Only trace on a CPU if the bitmask is set:
1982  */
1983 static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1984
1985 /*
1986  * When tracing/tracing_cpu_mask is modified then this holds
1987  * the new bitmask we are about to install:
1988  */
1989 static cpumask_t tracing_cpumask_new;
1990
1991 /*
1992  * The tracer itself will not take this lock, but still we want
1993  * to provide a consistent cpumask to user-space:
1994  */
1995 static DEFINE_MUTEX(tracing_cpumask_update_lock);
1996
1997 /*
1998  * Temporary storage for the character representation of the
1999  * CPU bitmask (and one more byte for the newline):
2000  */
2001 static char mask_str[NR_CPUS + 1];
2002
2003 static ssize_t
2004 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2005                      size_t count, loff_t *ppos)
2006 {
2007         int len;
2008
2009         mutex_lock(&tracing_cpumask_update_lock);
2010
2011         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2012         if (count - len < 2) {
2013                 count = -EINVAL;
2014                 goto out_err;
2015         }
2016         len += sprintf(mask_str + len, "\n");
2017         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2018
2019 out_err:
2020         mutex_unlock(&tracing_cpumask_update_lock);
2021
2022         return count;
2023 }
2024
2025 static ssize_t
2026 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2027                       size_t count, loff_t *ppos)
2028 {
2029         int err, cpu;
2030
2031         mutex_lock(&tracing_cpumask_update_lock);
2032         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2033         if (err)
2034                 goto err_unlock;
2035
2036         raw_local_irq_disable();
2037         __raw_spin_lock(&ftrace_max_lock);
2038         for_each_tracing_cpu(cpu) {
2039                 /*
2040                  * Increase/decrease the disabled counter if we are
2041                  * about to flip a bit in the cpumask:
2042                  */
2043                 if (cpu_isset(cpu, tracing_cpumask) &&
2044                                 !cpu_isset(cpu, tracing_cpumask_new)) {
2045                         atomic_inc(&global_trace.data[cpu]->disabled);
2046                 }
2047                 if (!cpu_isset(cpu, tracing_cpumask) &&
2048                                 cpu_isset(cpu, tracing_cpumask_new)) {
2049                         atomic_dec(&global_trace.data[cpu]->disabled);
2050                 }
2051         }
2052         __raw_spin_unlock(&ftrace_max_lock);
2053         raw_local_irq_enable();
2054
2055         tracing_cpumask = tracing_cpumask_new;
2056
2057         mutex_unlock(&tracing_cpumask_update_lock);
2058
2059         return count;
2060
2061 err_unlock:
2062         mutex_unlock(&tracing_cpumask_update_lock);
2063
2064         return err;
2065 }
2066
2067 static struct file_operations tracing_cpumask_fops = {
2068         .open           = tracing_open_generic,
2069         .read           = tracing_cpumask_read,
2070         .write          = tracing_cpumask_write,
2071 };
2072
2073 static ssize_t
2074 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2075                        size_t cnt, loff_t *ppos)
2076 {
2077         char *buf;
2078         int r = 0;
2079         int len = 0;
2080         int i;
2081
2082         /* calulate max size */
2083         for (i = 0; trace_options[i]; i++) {
2084                 len += strlen(trace_options[i]);
2085                 len += 3; /* "no" and space */
2086         }
2087
2088         /* +2 for \n and \0 */
2089         buf = kmalloc(len + 2, GFP_KERNEL);
2090         if (!buf)
2091                 return -ENOMEM;
2092
2093         for (i = 0; trace_options[i]; i++) {
2094                 if (trace_flags & (1 << i))
2095                         r += sprintf(buf + r, "%s ", trace_options[i]);
2096                 else
2097                         r += sprintf(buf + r, "no%s ", trace_options[i]);
2098         }
2099
2100         r += sprintf(buf + r, "\n");
2101         WARN_ON(r >= len + 2);
2102
2103         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2104
2105         kfree(buf);
2106
2107         return r;
2108 }
2109
2110 static ssize_t
2111 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2112                         size_t cnt, loff_t *ppos)
2113 {
2114         char buf[64];
2115         char *cmp = buf;
2116         int neg = 0;
2117         int i;
2118
2119         if (cnt >= sizeof(buf))
2120                 return -EINVAL;
2121
2122         if (copy_from_user(&buf, ubuf, cnt))
2123                 return -EFAULT;
2124
2125         buf[cnt] = 0;
2126
2127         if (strncmp(buf, "no", 2) == 0) {
2128                 neg = 1;
2129                 cmp += 2;
2130         }
2131
2132         for (i = 0; trace_options[i]; i++) {
2133                 int len = strlen(trace_options[i]);
2134
2135                 if (strncmp(cmp, trace_options[i], len) == 0) {
2136                         if (neg)
2137                                 trace_flags &= ~(1 << i);
2138                         else
2139                                 trace_flags |= (1 << i);
2140                         break;
2141                 }
2142         }
2143         /*
2144          * If no option could be set, return an error:
2145          */
2146         if (!trace_options[i])
2147                 return -EINVAL;
2148
2149         filp->f_pos += cnt;
2150
2151         return cnt;
2152 }
2153
2154 static struct file_operations tracing_iter_fops = {
2155         .open           = tracing_open_generic,
2156         .read           = tracing_iter_ctrl_read,
2157         .write          = tracing_iter_ctrl_write,
2158 };
2159
2160 static const char readme_msg[] =
2161         "tracing mini-HOWTO:\n\n"
2162         "# mkdir /debug\n"
2163         "# mount -t debugfs nodev /debug\n\n"
2164         "# cat /debug/tracing/available_tracers\n"
2165         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2166         "# cat /debug/tracing/current_tracer\n"
2167         "none\n"
2168         "# echo sched_switch > /debug/tracing/current_tracer\n"
2169         "# cat /debug/tracing/current_tracer\n"
2170         "sched_switch\n"
2171         "# cat /debug/tracing/iter_ctrl\n"
2172         "noprint-parent nosym-offset nosym-addr noverbose\n"
2173         "# echo print-parent > /debug/tracing/iter_ctrl\n"
2174         "# echo 1 > /debug/tracing/tracing_enabled\n"
2175         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2176         "echo 0 > /debug/tracing/tracing_enabled\n"
2177 ;
2178
2179 static ssize_t
2180 tracing_readme_read(struct file *filp, char __user *ubuf,
2181                        size_t cnt, loff_t *ppos)
2182 {
2183         return simple_read_from_buffer(ubuf, cnt, ppos,
2184                                         readme_msg, strlen(readme_msg));
2185 }
2186
2187 static struct file_operations tracing_readme_fops = {
2188         .open           = tracing_open_generic,
2189         .read           = tracing_readme_read,
2190 };
2191
2192 static ssize_t
2193 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2194                   size_t cnt, loff_t *ppos)
2195 {
2196         struct trace_array *tr = filp->private_data;
2197         char buf[64];
2198         int r;
2199
2200         r = sprintf(buf, "%ld\n", tr->ctrl);
2201         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2202 }
2203
2204 static ssize_t
2205 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2206                    size_t cnt, loff_t *ppos)
2207 {
2208         struct trace_array *tr = filp->private_data;
2209         char buf[64];
2210         long val;
2211         int ret;
2212
2213         if (cnt >= sizeof(buf))
2214                 return -EINVAL;
2215
2216         if (copy_from_user(&buf, ubuf, cnt))
2217                 return -EFAULT;
2218
2219         buf[cnt] = 0;
2220
2221         ret = strict_strtoul(buf, 10, &val);
2222         if (ret < 0)
2223                 return ret;
2224
2225         val = !!val;
2226
2227         mutex_lock(&trace_types_lock);
2228         if (tr->ctrl ^ val) {
2229                 if (val)
2230                         tracer_enabled = 1;
2231                 else
2232                         tracer_enabled = 0;
2233
2234                 tr->ctrl = val;
2235
2236                 if (current_trace && current_trace->ctrl_update)
2237                         current_trace->ctrl_update(tr);
2238         }
2239         mutex_unlock(&trace_types_lock);
2240
2241         filp->f_pos += cnt;
2242
2243         return cnt;
2244 }
2245
2246 static ssize_t
2247 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2248                        size_t cnt, loff_t *ppos)
2249 {
2250         char buf[max_tracer_type_len+2];
2251         int r;
2252
2253         mutex_lock(&trace_types_lock);
2254         if (current_trace)
2255                 r = sprintf(buf, "%s\n", current_trace->name);
2256         else
2257                 r = sprintf(buf, "\n");
2258         mutex_unlock(&trace_types_lock);
2259
2260         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2261 }
2262
2263 static ssize_t
2264 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2265                         size_t cnt, loff_t *ppos)
2266 {
2267         struct trace_array *tr = &global_trace;
2268         struct tracer *t;
2269         char buf[max_tracer_type_len+1];
2270         int i;
2271
2272         if (cnt > max_tracer_type_len)
2273                 cnt = max_tracer_type_len;
2274
2275         if (copy_from_user(&buf, ubuf, cnt))
2276                 return -EFAULT;
2277
2278         buf[cnt] = 0;
2279
2280         /* strip ending whitespace. */
2281         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2282                 buf[i] = 0;
2283
2284         mutex_lock(&trace_types_lock);
2285         for (t = trace_types; t; t = t->next) {
2286                 if (strcmp(t->name, buf) == 0)
2287                         break;
2288         }
2289         if (!t || t == current_trace)
2290                 goto out;
2291
2292         if (current_trace && current_trace->reset)
2293                 current_trace->reset(tr);
2294
2295         current_trace = t;
2296         if (t->init)
2297                 t->init(tr);
2298
2299  out:
2300         mutex_unlock(&trace_types_lock);
2301
2302         filp->f_pos += cnt;
2303
2304         return cnt;
2305 }
2306
2307 static ssize_t
2308 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2309                      size_t cnt, loff_t *ppos)
2310 {
2311         unsigned long *ptr = filp->private_data;
2312         char buf[64];
2313         int r;
2314
2315         r = snprintf(buf, sizeof(buf), "%ld\n",
2316                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2317         if (r > sizeof(buf))
2318                 r = sizeof(buf);
2319         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2320 }
2321
2322 static ssize_t
2323 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2324                       size_t cnt, loff_t *ppos)
2325 {
2326         long *ptr = filp->private_data;
2327         char buf[64];
2328         long val;
2329         int ret;
2330
2331         if (cnt >= sizeof(buf))
2332                 return -EINVAL;
2333
2334         if (copy_from_user(&buf, ubuf, cnt))
2335                 return -EFAULT;
2336
2337         buf[cnt] = 0;
2338
2339         ret = strict_strtoul(buf, 10, &val);
2340         if (ret < 0)
2341                 return ret;
2342
2343         *ptr = val * 1000;
2344
2345         return cnt;
2346 }
2347
2348 static atomic_t tracing_reader;
2349
2350 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2351 {
2352         struct trace_iterator *iter;
2353
2354         if (tracing_disabled)
2355                 return -ENODEV;
2356
2357         /* We only allow for reader of the pipe */
2358         if (atomic_inc_return(&tracing_reader) != 1) {
2359                 atomic_dec(&tracing_reader);
2360                 return -EBUSY;
2361         }
2362
2363         /* create a buffer to store the information to pass to userspace */
2364         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2365         if (!iter)
2366                 return -ENOMEM;
2367
2368         mutex_lock(&trace_types_lock);
2369         iter->tr = &global_trace;
2370         iter->trace = current_trace;
2371         filp->private_data = iter;
2372
2373         if (iter->trace->pipe_open)
2374                 iter->trace->pipe_open(iter);
2375         mutex_unlock(&trace_types_lock);
2376
2377         return 0;
2378 }
2379
2380 static int tracing_release_pipe(struct inode *inode, struct file *file)
2381 {
2382         struct trace_iterator *iter = file->private_data;
2383
2384         kfree(iter);
2385         atomic_dec(&tracing_reader);
2386
2387         return 0;
2388 }
2389
2390 static unsigned int
2391 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2392 {
2393         struct trace_iterator *iter = filp->private_data;
2394
2395         if (trace_flags & TRACE_ITER_BLOCK) {
2396                 /*
2397                  * Always select as readable when in blocking mode
2398                  */
2399                 return POLLIN | POLLRDNORM;
2400         } else {
2401                 if (!trace_empty(iter))
2402                         return POLLIN | POLLRDNORM;
2403                 poll_wait(filp, &trace_wait, poll_table);
2404                 if (!trace_empty(iter))
2405                         return POLLIN | POLLRDNORM;
2406
2407                 return 0;
2408         }
2409 }
2410
2411 /*
2412  * Consumer reader.
2413  */
2414 static ssize_t
2415 tracing_read_pipe(struct file *filp, char __user *ubuf,
2416                   size_t cnt, loff_t *ppos)
2417 {
2418         struct trace_iterator *iter = filp->private_data;
2419         struct trace_array_cpu *data;
2420         static cpumask_t mask;
2421         unsigned long flags;
2422 #ifdef CONFIG_FTRACE
2423         int ftrace_save;
2424 #endif
2425         int cpu;
2426         ssize_t sret;
2427
2428         /* return any leftover data */
2429         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2430         if (sret != -EBUSY)
2431                 return sret;
2432         sret = 0;
2433
2434         trace_seq_reset(&iter->seq);
2435
2436         mutex_lock(&trace_types_lock);
2437         if (iter->trace->read) {
2438                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2439                 if (sret)
2440                         goto out;
2441         }
2442
2443         while (trace_empty(iter)) {
2444
2445                 if ((filp->f_flags & O_NONBLOCK)) {
2446                         sret = -EAGAIN;
2447                         goto out;
2448                 }
2449
2450                 /*
2451                  * This is a make-shift waitqueue. The reason we don't use
2452                  * an actual wait queue is because:
2453                  *  1) we only ever have one waiter
2454                  *  2) the tracing, traces all functions, we don't want
2455                  *     the overhead of calling wake_up and friends
2456                  *     (and tracing them too)
2457                  *     Anyway, this is really very primitive wakeup.
2458                  */
2459                 set_current_state(TASK_INTERRUPTIBLE);
2460                 iter->tr->waiter = current;
2461
2462                 mutex_unlock(&trace_types_lock);
2463
2464                 /* sleep for 100 msecs, and try again. */
2465                 schedule_timeout(HZ/10);
2466
2467                 mutex_lock(&trace_types_lock);
2468
2469                 iter->tr->waiter = NULL;
2470
2471                 if (signal_pending(current)) {
2472                         sret = -EINTR;
2473                         goto out;
2474                 }
2475
2476                 if (iter->trace != current_trace)
2477                         goto out;
2478
2479                 /*
2480                  * We block until we read something and tracing is disabled.
2481                  * We still block if tracing is disabled, but we have never
2482                  * read anything. This allows a user to cat this file, and
2483                  * then enable tracing. But after we have read something,
2484                  * we give an EOF when tracing is again disabled.
2485                  *
2486                  * iter->pos will be 0 if we haven't read anything.
2487                  */
2488                 if (!tracer_enabled && iter->pos)
2489                         break;
2490
2491                 continue;
2492         }
2493
2494         /* stop when tracing is finished */
2495         if (trace_empty(iter))
2496                 goto out;
2497
2498         if (cnt >= PAGE_SIZE)
2499                 cnt = PAGE_SIZE - 1;
2500
2501         /* reset all but tr, trace, and overruns */
2502         memset(&iter->seq, 0,
2503                sizeof(struct trace_iterator) -
2504                offsetof(struct trace_iterator, seq));
2505         iter->pos = -1;
2506
2507         /*
2508          * We need to stop all tracing on all CPUS to read the
2509          * the next buffer. This is a bit expensive, but is
2510          * not done often. We fill all what we can read,
2511          * and then release the locks again.
2512          */
2513
2514         cpus_clear(mask);
2515         local_irq_save(flags);
2516 #ifdef CONFIG_FTRACE
2517         ftrace_save = ftrace_enabled;
2518         ftrace_enabled = 0;
2519 #endif
2520         smp_wmb();
2521         for_each_tracing_cpu(cpu) {
2522                 data = iter->tr->data[cpu];
2523
2524                 if (!head_page(data) || !data->trace_idx)
2525                         continue;
2526
2527                 atomic_inc(&data->disabled);
2528                 cpu_set(cpu, mask);
2529         }
2530
2531         for_each_cpu_mask(cpu, mask) {
2532                 data = iter->tr->data[cpu];
2533                 __raw_spin_lock(&data->lock);
2534
2535                 if (data->overrun > iter->last_overrun[cpu])
2536                         iter->overrun[cpu] +=
2537                                 data->overrun - iter->last_overrun[cpu];
2538                 iter->last_overrun[cpu] = data->overrun;
2539         }
2540
2541         while (find_next_entry_inc(iter) != NULL) {
2542                 int ret;
2543                 int len = iter->seq.len;
2544
2545                 ret = print_trace_line(iter);
2546                 if (!ret) {
2547                         /* don't print partial lines */
2548                         iter->seq.len = len;
2549                         break;
2550                 }
2551
2552                 trace_consume(iter);
2553
2554                 if (iter->seq.len >= cnt)
2555                         break;
2556         }
2557
2558         for_each_cpu_mask(cpu, mask) {
2559                 data = iter->tr->data[cpu];
2560                 __raw_spin_unlock(&data->lock);
2561         }
2562
2563         for_each_cpu_mask(cpu, mask) {
2564                 data = iter->tr->data[cpu];
2565                 atomic_dec(&data->disabled);
2566         }
2567 #ifdef CONFIG_FTRACE
2568         ftrace_enabled = ftrace_save;
2569 #endif
2570         local_irq_restore(flags);
2571
2572         /* Now copy what we have to the user */
2573         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2574         if (iter->seq.readpos >= iter->seq.len)
2575                 trace_seq_reset(&iter->seq);
2576         if (sret == -EBUSY)
2577                 sret = 0;
2578
2579 out:
2580         mutex_unlock(&trace_types_lock);
2581
2582         return sret;
2583 }
2584
2585 static ssize_t
2586 tracing_entries_read(struct file *filp, char __user *ubuf,
2587                      size_t cnt, loff_t *ppos)
2588 {
2589         struct trace_array *tr = filp->private_data;
2590         char buf[64];
2591         int r;
2592
2593         r = sprintf(buf, "%lu\n", tr->entries);
2594         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2595 }
2596
2597 static ssize_t
2598 tracing_entries_write(struct file *filp, const char __user *ubuf,
2599                       size_t cnt, loff_t *ppos)
2600 {
2601         unsigned long val;
2602         char buf[64];
2603         int ret;
2604
2605         if (cnt >= sizeof(buf))
2606                 return -EINVAL;
2607
2608         if (copy_from_user(&buf, ubuf, cnt))
2609                 return -EFAULT;
2610
2611         buf[cnt] = 0;
2612
2613         ret = strict_strtoul(buf, 10, &val);
2614         if (ret < 0)
2615                 return ret;
2616
2617         /* must have at least 1 entry */
2618         if (!val)
2619                 return -EINVAL;
2620
2621         mutex_lock(&trace_types_lock);
2622
2623         if (current_trace != &no_tracer) {
2624                 cnt = -EBUSY;
2625                 pr_info("ftrace: set current_tracer to none"
2626                         " before modifying buffer size\n");
2627                 goto out;
2628         }
2629
2630         if (val > global_trace.entries) {
2631                 long pages_requested;
2632                 unsigned long freeable_pages;
2633
2634                 /* make sure we have enough memory before mapping */
2635                 pages_requested =
2636                         (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2637
2638                 /* account for each buffer (and max_tr) */
2639                 pages_requested *= tracing_nr_buffers * 2;
2640
2641                 /* Check for overflow */
2642                 if (pages_requested < 0) {
2643                         cnt = -ENOMEM;
2644                         goto out;
2645                 }
2646
2647                 freeable_pages = determine_dirtyable_memory();
2648
2649                 /* we only allow to request 1/4 of useable memory */
2650                 if (pages_requested >
2651                     ((freeable_pages + tracing_pages_allocated) / 4)) {
2652                         cnt = -ENOMEM;
2653                         goto out;
2654                 }
2655
2656                 while (global_trace.entries < val) {
2657                         if (trace_alloc_page()) {
2658                                 cnt = -ENOMEM;
2659                                 goto out;
2660                         }
2661                         /* double check that we don't go over the known pages */
2662                         if (tracing_pages_allocated > pages_requested)
2663                                 break;
2664                 }
2665
2666         } else {
2667                 /* include the number of entries in val (inc of page entries) */
2668                 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2669                         trace_free_page();
2670         }
2671
2672         filp->f_pos += cnt;
2673
2674  out:
2675         max_tr.entries = global_trace.entries;
2676         mutex_unlock(&trace_types_lock);
2677
2678         return cnt;
2679 }
2680
2681 static struct file_operations tracing_max_lat_fops = {
2682         .open           = tracing_open_generic,
2683         .read           = tracing_max_lat_read,
2684         .write          = tracing_max_lat_write,
2685 };
2686
2687 static struct file_operations tracing_ctrl_fops = {
2688         .open           = tracing_open_generic,
2689         .read           = tracing_ctrl_read,
2690         .write          = tracing_ctrl_write,
2691 };
2692
2693 static struct file_operations set_tracer_fops = {
2694         .open           = tracing_open_generic,
2695         .read           = tracing_set_trace_read,
2696         .write          = tracing_set_trace_write,
2697 };
2698
2699 static struct file_operations tracing_pipe_fops = {
2700         .open           = tracing_open_pipe,
2701         .poll           = tracing_poll_pipe,
2702         .read           = tracing_read_pipe,
2703         .release        = tracing_release_pipe,
2704 };
2705
2706 static struct file_operations tracing_entries_fops = {
2707         .open           = tracing_open_generic,
2708         .read           = tracing_entries_read,
2709         .write          = tracing_entries_write,
2710 };
2711
2712 #ifdef CONFIG_DYNAMIC_FTRACE
2713
2714 static ssize_t
2715 tracing_read_long(struct file *filp, char __user *ubuf,
2716                   size_t cnt, loff_t *ppos)
2717 {
2718         unsigned long *p = filp->private_data;
2719         char buf[64];
2720         int r;
2721
2722         r = sprintf(buf, "%ld\n", *p);
2723
2724         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2725 }
2726
2727 static struct file_operations tracing_read_long_fops = {
2728         .open           = tracing_open_generic,
2729         .read           = tracing_read_long,
2730 };
2731 #endif
2732
2733 static struct dentry *d_tracer;
2734
2735 struct dentry *tracing_init_dentry(void)
2736 {
2737         static int once;
2738
2739         if (d_tracer)
2740                 return d_tracer;
2741
2742         d_tracer = debugfs_create_dir("tracing", NULL);
2743
2744         if (!d_tracer && !once) {
2745                 once = 1;
2746                 pr_warning("Could not create debugfs directory 'tracing'\n");
2747                 return NULL;
2748         }
2749
2750         return d_tracer;
2751 }
2752
2753 #ifdef CONFIG_FTRACE_SELFTEST
2754 /* Let selftest have access to static functions in this file */
2755 #include "trace_selftest.c"
2756 #endif
2757
2758 static __init void tracer_init_debugfs(void)
2759 {
2760         struct dentry *d_tracer;
2761         struct dentry *entry;
2762
2763         d_tracer = tracing_init_dentry();
2764
2765         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2766                                     &global_trace, &tracing_ctrl_fops);
2767         if (!entry)
2768                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2769
2770         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2771                                     NULL, &tracing_iter_fops);
2772         if (!entry)
2773                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2774
2775         entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2776                                     NULL, &tracing_cpumask_fops);
2777         if (!entry)
2778                 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2779
2780         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2781                                     &global_trace, &tracing_lt_fops);
2782         if (!entry)
2783                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2784
2785         entry = debugfs_create_file("trace", 0444, d_tracer,
2786                                     &global_trace, &tracing_fops);
2787         if (!entry)
2788                 pr_warning("Could not create debugfs 'trace' entry\n");
2789
2790         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2791                                     &global_trace, &show_traces_fops);
2792         if (!entry)
2793                 pr_warning("Could not create debugfs 'trace' entry\n");
2794
2795         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2796                                     &global_trace, &set_tracer_fops);
2797         if (!entry)
2798                 pr_warning("Could not create debugfs 'trace' entry\n");
2799
2800         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2801                                     &tracing_max_latency,
2802                                     &tracing_max_lat_fops);
2803         if (!entry)
2804                 pr_warning("Could not create debugfs "
2805                            "'tracing_max_latency' entry\n");
2806
2807         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2808                                     &tracing_thresh, &tracing_max_lat_fops);
2809         if (!entry)
2810                 pr_warning("Could not create debugfs "
2811                            "'tracing_threash' entry\n");
2812         entry = debugfs_create_file("README", 0644, d_tracer,
2813                                     NULL, &tracing_readme_fops);
2814         if (!entry)
2815                 pr_warning("Could not create debugfs 'README' entry\n");
2816
2817         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2818                                     NULL, &tracing_pipe_fops);
2819         if (!entry)
2820                 pr_warning("Could not create debugfs "
2821                            "'tracing_threash' entry\n");
2822
2823         entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2824                                     &global_trace, &tracing_entries_fops);
2825         if (!entry)
2826                 pr_warning("Could not create debugfs "
2827                            "'tracing_threash' entry\n");
2828
2829 #ifdef CONFIG_DYNAMIC_FTRACE
2830         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2831                                     &ftrace_update_tot_cnt,
2832                                     &tracing_read_long_fops);
2833         if (!entry)
2834                 pr_warning("Could not create debugfs "
2835                            "'dyn_ftrace_total_info' entry\n");
2836 #endif
2837 }
2838
2839 static int trace_alloc_page(void)
2840 {
2841         struct trace_array_cpu *data;
2842         struct page *page, *tmp;
2843         LIST_HEAD(pages);
2844         void *array;
2845         unsigned pages_allocated = 0;
2846         int i;
2847
2848         /* first allocate a page for each CPU */
2849         for_each_tracing_cpu(i) {
2850                 array = (void *)__get_free_page(GFP_KERNEL);
2851                 if (array == NULL) {
2852                         printk(KERN_ERR "tracer: failed to allocate page"
2853                                "for trace buffer!\n");
2854                         goto free_pages;
2855                 }
2856
2857                 pages_allocated++;
2858                 page = virt_to_page(array);
2859                 list_add(&page->lru, &pages);
2860
2861 /* Only allocate if we are actually using the max trace */
2862 #ifdef CONFIG_TRACER_MAX_TRACE
2863                 array = (void *)__get_free_page(GFP_KERNEL);
2864                 if (array == NULL) {
2865                         printk(KERN_ERR "tracer: failed to allocate page"
2866                                "for trace buffer!\n");
2867                         goto free_pages;
2868                 }
2869                 pages_allocated++;
2870                 page = virt_to_page(array);
2871                 list_add(&page->lru, &pages);
2872 #endif
2873         }
2874
2875         /* Now that we successfully allocate a page per CPU, add them */
2876         for_each_tracing_cpu(i) {
2877                 data = global_trace.data[i];
2878                 page = list_entry(pages.next, struct page, lru);
2879                 list_del_init(&page->lru);
2880                 list_add_tail(&page->lru, &data->trace_pages);
2881                 ClearPageLRU(page);
2882
2883 #ifdef CONFIG_TRACER_MAX_TRACE
2884                 data = max_tr.data[i];
2885                 page = list_entry(pages.next, struct page, lru);
2886                 list_del_init(&page->lru);
2887                 list_add_tail(&page->lru, &data->trace_pages);
2888                 SetPageLRU(page);
2889 #endif
2890         }
2891         tracing_pages_allocated += pages_allocated;
2892         global_trace.entries += ENTRIES_PER_PAGE;
2893
2894         return 0;
2895
2896  free_pages:
2897         list_for_each_entry_safe(page, tmp, &pages, lru) {
2898                 list_del_init(&page->lru);
2899                 __free_page(page);
2900         }
2901         return -ENOMEM;
2902 }
2903
2904 static int trace_free_page(void)
2905 {
2906         struct trace_array_cpu *data;
2907         struct page *page;
2908         struct list_head *p;
2909         int i;
2910         int ret = 0;
2911
2912         /* free one page from each buffer */
2913         for_each_tracing_cpu(i) {
2914                 data = global_trace.data[i];
2915                 p = data->trace_pages.next;
2916                 if (p == &data->trace_pages) {
2917                         /* should never happen */
2918                         WARN_ON(1);
2919                         tracing_disabled = 1;
2920                         ret = -1;
2921                         break;
2922                 }
2923                 page = list_entry(p, struct page, lru);
2924                 ClearPageLRU(page);
2925                 list_del(&page->lru);
2926                 tracing_pages_allocated--;
2927                 tracing_pages_allocated--;
2928                 __free_page(page);
2929
2930                 tracing_reset(data);
2931
2932 #ifdef CONFIG_TRACER_MAX_TRACE
2933                 data = max_tr.data[i];
2934                 p = data->trace_pages.next;
2935                 if (p == &data->trace_pages) {
2936                         /* should never happen */
2937                         WARN_ON(1);
2938                         tracing_disabled = 1;
2939                         ret = -1;
2940                         break;
2941                 }
2942                 page = list_entry(p, struct page, lru);
2943                 ClearPageLRU(page);
2944                 list_del(&page->lru);
2945                 __free_page(page);
2946
2947                 tracing_reset(data);
2948 #endif
2949         }
2950         global_trace.entries -= ENTRIES_PER_PAGE;
2951
2952         return ret;
2953 }
2954
2955 __init static int tracer_alloc_buffers(void)
2956 {
2957         struct trace_array_cpu *data;
2958         void *array;
2959         struct page *page;
2960         int pages = 0;
2961         int ret = -ENOMEM;
2962         int i;
2963
2964         global_trace.ctrl = tracer_enabled;
2965
2966         /* TODO: make the number of buffers hot pluggable with CPUS */
2967         tracing_nr_buffers = num_possible_cpus();
2968         tracing_buffer_mask = cpu_possible_map;
2969
2970         /* Allocate the first page for all buffers */
2971         for_each_tracing_cpu(i) {
2972                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2973                 max_tr.data[i] = &per_cpu(max_data, i);
2974
2975                 array = (void *)__get_free_page(GFP_KERNEL);
2976                 if (array == NULL) {
2977                         printk(KERN_ERR "tracer: failed to allocate page"
2978                                "for trace buffer!\n");
2979                         goto free_buffers;
2980                 }
2981
2982                 /* set the array to the list */
2983                 INIT_LIST_HEAD(&data->trace_pages);
2984                 page = virt_to_page(array);
2985                 list_add(&page->lru, &data->trace_pages);
2986                 /* use the LRU flag to differentiate the two buffers */
2987                 ClearPageLRU(page);
2988
2989                 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2990                 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2991
2992 /* Only allocate if we are actually using the max trace */
2993 #ifdef CONFIG_TRACER_MAX_TRACE
2994                 array = (void *)__get_free_page(GFP_KERNEL);
2995                 if (array == NULL) {
2996                         printk(KERN_ERR "tracer: failed to allocate page"
2997                                "for trace buffer!\n");
2998                         goto free_buffers;
2999                 }
3000
3001                 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3002                 page = virt_to_page(array);
3003                 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3004                 SetPageLRU(page);
3005 #endif
3006         }
3007
3008         /*
3009          * Since we allocate by orders of pages, we may be able to
3010          * round up a bit.
3011          */
3012         global_trace.entries = ENTRIES_PER_PAGE;
3013         pages++;
3014
3015         while (global_trace.entries < trace_nr_entries) {
3016                 if (trace_alloc_page())
3017                         break;
3018                 pages++;
3019         }
3020         max_tr.entries = global_trace.entries;
3021
3022         pr_info("tracer: %d pages allocated for %ld",
3023                 pages, trace_nr_entries);
3024         pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3025         pr_info("   actual entries %ld\n", global_trace.entries);
3026
3027         tracer_init_debugfs();
3028
3029         trace_init_cmdlines();
3030
3031         register_tracer(&no_tracer);
3032         current_trace = &no_tracer;
3033
3034         /* All seems OK, enable tracing */
3035         tracing_disabled = 0;
3036
3037         return 0;
3038
3039  free_buffers:
3040         for (i-- ; i >= 0; i--) {
3041                 struct page *page, *tmp;
3042                 struct trace_array_cpu *data = global_trace.data[i];
3043
3044                 if (data) {
3045                         list_for_each_entry_safe(page, tmp,
3046                                                  &data->trace_pages, lru) {
3047                                 list_del_init(&page->lru);
3048                                 __free_page(page);
3049                         }
3050                 }
3051
3052 #ifdef CONFIG_TRACER_MAX_TRACE
3053                 data = max_tr.data[i];
3054                 if (data) {
3055                         list_for_each_entry_safe(page, tmp,
3056                                                  &data->trace_pages, lru) {
3057                                 list_del_init(&page->lru);
3058                                 __free_page(page);
3059                         }
3060                 }
3061 #endif
3062         }
3063         return ret;
3064 }
3065 fs_initcall(tracer_alloc_buffers);