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