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