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