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