ftrace: fast, scalable, synchronized timestamps
[linux-2.6] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
28 #include <linux/fs.h>
29
30 #include "trace.h"
31
32 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
33 unsigned long __read_mostly     tracing_thresh;
34
35 static int tracing_disabled = 1;
36
37 static long notrace
38 ns2usecs(cycle_t nsec)
39 {
40         nsec += 500;
41         do_div(nsec, 1000);
42         return nsec;
43 }
44
45 static const int time_sync_freq_max = 128;
46 static const cycle_t time_sync_thresh = 100000;
47
48 static DEFINE_PER_CPU(cycle_t, time_offset);
49 static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
50 static DEFINE_PER_CPU(int, time_sync_count);
51 static DEFINE_PER_CPU(int, time_sync_freq);
52
53 /*
54  * Global lock which we take every now and then to synchronize
55  * the CPUs time. This method is not warp-safe, but it's good
56  * enough to synchronize slowly diverging time sources and thus
57  * it's good enough for tracing:
58  */
59 static DEFINE_SPINLOCK(time_sync_lock);
60 static cycle_t prev_global_time;
61
62 static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
63 {
64         unsigned long flags;
65
66         spin_lock_irqsave(&time_sync_lock, flags);
67
68         /*
69          * Update the synchronization frequency:
70          */
71         if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
72                 per_cpu(time_sync_freq, cpu) *= 2;
73         per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
74
75         if (time < prev_global_time) {
76                 per_cpu(time_offset, cpu) += prev_global_time - time;
77                 time = prev_global_time;
78         } else {
79                 prev_global_time = time;
80         }
81
82         spin_unlock_irqrestore(&time_sync_lock, flags);
83
84         return time;
85 }
86
87 notrace cycle_t ftrace_now(int cpu)
88 {
89         cycle_t prev_cpu_time, time, delta_time;
90
91         prev_cpu_time = per_cpu(prev_cpu_time, cpu);
92         time = sched_clock() + per_cpu(time_offset, cpu);
93         delta_time = time-prev_cpu_time;
94
95         if (unlikely(delta_time > time_sync_thresh ||
96                                 --per_cpu(time_sync_count, cpu) <= 0))
97                 time = __ftrace_now_sync(time, cpu);
98
99         return time;
100 }
101
102 static atomic_t                 tracer_counter;
103 static struct trace_array       global_trace;
104
105 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
106
107 static struct trace_array       max_tr;
108
109 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
110
111 static int                      tracer_enabled;
112 static unsigned long            trace_nr_entries = 16384UL;
113
114 static struct tracer            *trace_types __read_mostly;
115 static struct tracer            *current_trace __read_mostly;
116 static int                      max_tracer_type_len;
117
118 static DEFINE_MUTEX(trace_types_lock);
119
120 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
121
122 static int __init set_nr_entries(char *str)
123 {
124         if (!str)
125                 return 0;
126         trace_nr_entries = simple_strtoul(str, &str, 0);
127         return 1;
128 }
129 __setup("trace_entries=", set_nr_entries);
130
131 unsigned long nsecs_to_usecs(unsigned long nsecs)
132 {
133         return nsecs / 1000;
134 }
135
136 enum trace_type {
137         __TRACE_FIRST_TYPE = 0,
138
139         TRACE_FN,
140         TRACE_CTX,
141
142         __TRACE_LAST_TYPE
143 };
144
145 enum trace_flag_type {
146         TRACE_FLAG_IRQS_OFF             = 0x01,
147         TRACE_FLAG_NEED_RESCHED         = 0x02,
148         TRACE_FLAG_HARDIRQ              = 0x04,
149         TRACE_FLAG_SOFTIRQ              = 0x08,
150 };
151
152 enum trace_iterator_flags {
153         TRACE_ITER_PRINT_PARENT         = 0x01,
154         TRACE_ITER_SYM_OFFSET           = 0x02,
155         TRACE_ITER_SYM_ADDR             = 0x04,
156         TRACE_ITER_VERBOSE              = 0x08,
157 };
158
159 #define TRACE_ITER_SYM_MASK \
160         (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
161
162 /* These must match the bit postions above */
163 static const char *trace_options[] = {
164         "print-parent",
165         "sym-offset",
166         "sym-addr",
167         "verbose",
168         NULL
169 };
170
171 static unsigned trace_flags;
172
173 static DEFINE_SPINLOCK(ftrace_max_lock);
174
175 /*
176  * Copy the new maximum trace into the separate maximum-trace
177  * structure. (this way the maximum trace is permanently saved,
178  * for later retrieval via /debugfs/tracing/latency_trace)
179  */
180 static notrace void
181 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
182 {
183         struct trace_array_cpu *data = tr->data[cpu];
184
185         max_tr.cpu = cpu;
186         max_tr.time_start = data->preempt_timestamp;
187
188         data = max_tr.data[cpu];
189         data->saved_latency = tracing_max_latency;
190
191         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
192         data->pid = tsk->pid;
193         data->uid = tsk->uid;
194         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
195         data->policy = tsk->policy;
196         data->rt_priority = tsk->rt_priority;
197
198         /* record this tasks comm */
199         tracing_record_cmdline(current);
200 }
201
202 void check_pages(struct trace_array_cpu *data)
203 {
204         struct page *page, *tmp;
205
206         BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
207         BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
208
209         list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
210                 BUG_ON(page->lru.next->prev != &page->lru);
211                 BUG_ON(page->lru.prev->next != &page->lru);
212         }
213 }
214
215 void *head_page(struct trace_array_cpu *data)
216 {
217         struct page *page;
218
219         check_pages(data);
220         if (list_empty(&data->trace_pages))
221                 return NULL;
222
223         page = list_entry(data->trace_pages.next, struct page, lru);
224         BUG_ON(&page->lru == &data->trace_pages);
225
226         return page_address(page);
227 }
228
229 static notrace int
230 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
231 {
232         int len = (PAGE_SIZE - 1) - s->len;
233         va_list ap;
234         int ret;
235
236         if (!len)
237                 return 0;
238
239         va_start(ap, fmt);
240         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
241         va_end(ap);
242
243         /* If we can't write it all, don't bother writing anything */
244         if (ret > len)
245                 return 0;
246
247         s->len += ret;
248
249         return len;
250 }
251
252 static notrace int
253 trace_seq_puts(struct trace_seq *s, const char *str)
254 {
255         int len = strlen(str);
256
257         if (len > ((PAGE_SIZE - 1) - s->len))
258                 return 0;
259
260         memcpy(s->buffer + s->len, str, len);
261         s->len += len;
262
263         return len;
264 }
265
266 static notrace int
267 trace_seq_putc(struct trace_seq *s, unsigned char c)
268 {
269         if (s->len >= (PAGE_SIZE - 1))
270                 return 0;
271
272         s->buffer[s->len++] = c;
273
274         return 1;
275 }
276
277 static notrace void
278 trace_seq_reset(struct trace_seq *s)
279 {
280         s->len = 0;
281 }
282
283 static notrace void
284 trace_print_seq(struct seq_file *m, struct trace_seq *s)
285 {
286         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
287
288         s->buffer[len] = 0;
289         seq_puts(m, s->buffer);
290
291         trace_seq_reset(s);
292 }
293
294 notrace static void
295 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
296 {
297         struct list_head flip_pages;
298
299         INIT_LIST_HEAD(&flip_pages);
300
301         memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
302                 sizeof(struct trace_array_cpu) -
303                 offsetof(struct trace_array_cpu, trace_head_idx));
304
305         check_pages(tr1);
306         check_pages(tr2);
307         list_splice_init(&tr1->trace_pages, &flip_pages);
308         list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
309         list_splice_init(&flip_pages, &tr2->trace_pages);
310         BUG_ON(!list_empty(&flip_pages));
311         check_pages(tr1);
312         check_pages(tr2);
313 }
314
315 notrace void
316 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
317 {
318         struct trace_array_cpu *data;
319         int i;
320
321         WARN_ON_ONCE(!irqs_disabled());
322         spin_lock(&ftrace_max_lock);
323         /* clear out all the previous traces */
324         for_each_possible_cpu(i) {
325                 data = tr->data[i];
326                 flip_trace(max_tr.data[i], data);
327                 tracing_reset(data);
328         }
329
330         __update_max_tr(tr, tsk, cpu);
331         spin_unlock(&ftrace_max_lock);
332 }
333
334 /**
335  * update_max_tr_single - only copy one trace over, and reset the rest
336  * @tr - tracer
337  * @tsk - task with the latency
338  * @cpu - the cpu of the buffer to copy.
339  */
340 notrace void
341 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
342 {
343         struct trace_array_cpu *data = tr->data[cpu];
344         int i;
345
346         WARN_ON_ONCE(!irqs_disabled());
347         spin_lock(&ftrace_max_lock);
348         for_each_possible_cpu(i)
349                 tracing_reset(max_tr.data[i]);
350
351         flip_trace(max_tr.data[cpu], data);
352         tracing_reset(data);
353
354         __update_max_tr(tr, tsk, cpu);
355         spin_unlock(&ftrace_max_lock);
356 }
357
358 int register_tracer(struct tracer *type)
359 {
360         struct tracer *t;
361         int len;
362         int ret = 0;
363
364         if (!type->name) {
365                 pr_info("Tracer must have a name\n");
366                 return -1;
367         }
368
369         mutex_lock(&trace_types_lock);
370         for (t = trace_types; t; t = t->next) {
371                 if (strcmp(type->name, t->name) == 0) {
372                         /* already found */
373                         pr_info("Trace %s already registered\n",
374                                 type->name);
375                         ret = -1;
376                         goto out;
377                 }
378         }
379
380 #ifdef CONFIG_FTRACE_STARTUP_TEST
381         if (type->selftest) {
382                 struct tracer *saved_tracer = current_trace;
383                 struct trace_array_cpu *data;
384                 struct trace_array *tr = &global_trace;
385                 int saved_ctrl = tr->ctrl;
386                 int i;
387                 /*
388                  * Run a selftest on this tracer.
389                  * Here we reset the trace buffer, and set the current
390                  * tracer to be this tracer. The tracer can then run some
391                  * internal tracing to verify that everything is in order.
392                  * If we fail, we do not register this tracer.
393                  */
394                 for_each_possible_cpu(i) {
395                         data = tr->data[i];
396                         if (!head_page(data))
397                                 continue;
398                         tracing_reset(data);
399                 }
400                 current_trace = type;
401                 tr->ctrl = 0;
402                 /* the test is responsible for initializing and enabling */
403                 pr_info("Testing tracer %s: ", type->name);
404                 ret = type->selftest(type, tr);
405                 /* the test is responsible for resetting too */
406                 current_trace = saved_tracer;
407                 tr->ctrl = saved_ctrl;
408                 if (ret) {
409                         printk(KERN_CONT "FAILED!\n");
410                         goto out;
411                 }
412                 /* Only reset on passing, to avoid touching corrupted buffers */
413                 for_each_possible_cpu(i) {
414                         data = tr->data[i];
415                         if (!head_page(data))
416                                 continue;
417                         tracing_reset(data);
418                 }
419                 printk(KERN_CONT "PASSED\n");
420         }
421 #endif
422
423         type->next = trace_types;
424         trace_types = type;
425         len = strlen(type->name);
426         if (len > max_tracer_type_len)
427                 max_tracer_type_len = len;
428
429  out:
430         mutex_unlock(&trace_types_lock);
431
432         return ret;
433 }
434
435 void unregister_tracer(struct tracer *type)
436 {
437         struct tracer **t;
438         int len;
439
440         mutex_lock(&trace_types_lock);
441         for (t = &trace_types; *t; t = &(*t)->next) {
442                 if (*t == type)
443                         goto found;
444         }
445         pr_info("Trace %s not registered\n", type->name);
446         goto out;
447
448  found:
449         *t = (*t)->next;
450         if (strlen(type->name) != max_tracer_type_len)
451                 goto out;
452
453         max_tracer_type_len = 0;
454         for (t = &trace_types; *t; t = &(*t)->next) {
455                 len = strlen((*t)->name);
456                 if (len > max_tracer_type_len)
457                         max_tracer_type_len = len;
458         }
459  out:
460         mutex_unlock(&trace_types_lock);
461 }
462
463 notrace void tracing_reset(struct trace_array_cpu *data)
464 {
465         data->trace_idx = 0;
466         data->trace_head = data->trace_tail = head_page(data);
467         data->trace_head_idx = 0;
468         data->trace_tail_idx = 0;
469 }
470
471 #ifdef CONFIG_FTRACE
472 static notrace void
473 function_trace_call(unsigned long ip, unsigned long parent_ip)
474 {
475         struct trace_array *tr = &global_trace;
476         struct trace_array_cpu *data;
477         unsigned long flags;
478         long disabled;
479         int cpu;
480
481         if (unlikely(!tracer_enabled))
482                 return;
483
484         local_irq_save(flags);
485         cpu = raw_smp_processor_id();
486         data = tr->data[cpu];
487         disabled = atomic_inc_return(&data->disabled);
488
489         if (likely(disabled == 1))
490                 ftrace(tr, data, ip, parent_ip, flags);
491
492         atomic_dec(&data->disabled);
493         local_irq_restore(flags);
494 }
495
496 static struct ftrace_ops trace_ops __read_mostly =
497 {
498         .func = function_trace_call,
499 };
500 #endif
501
502 notrace void tracing_start_function_trace(void)
503 {
504         register_ftrace_function(&trace_ops);
505 }
506
507 notrace void tracing_stop_function_trace(void)
508 {
509         unregister_ftrace_function(&trace_ops);
510 }
511
512 #define SAVED_CMDLINES 128
513 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
514 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
515 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
516 static int cmdline_idx;
517 static DEFINE_SPINLOCK(trace_cmdline_lock);
518 atomic_t trace_record_cmdline_disabled;
519
520 static void trace_init_cmdlines(void)
521 {
522         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
523         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
524         cmdline_idx = 0;
525 }
526
527 notrace void trace_stop_cmdline_recording(void);
528
529 static notrace void trace_save_cmdline(struct task_struct *tsk)
530 {
531         unsigned map;
532         unsigned idx;
533
534         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
535                 return;
536
537         /*
538          * It's not the end of the world if we don't get
539          * the lock, but we also don't want to spin
540          * nor do we want to disable interrupts,
541          * so if we miss here, then better luck next time.
542          */
543         if (!spin_trylock(&trace_cmdline_lock))
544                 return;
545
546         idx = map_pid_to_cmdline[tsk->pid];
547         if (idx >= SAVED_CMDLINES) {
548                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
549
550                 map = map_cmdline_to_pid[idx];
551                 if (map <= PID_MAX_DEFAULT)
552                         map_pid_to_cmdline[map] = (unsigned)-1;
553
554                 map_pid_to_cmdline[tsk->pid] = idx;
555
556                 cmdline_idx = idx;
557         }
558
559         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
560
561         spin_unlock(&trace_cmdline_lock);
562 }
563
564 static notrace char *trace_find_cmdline(int pid)
565 {
566         char *cmdline = "<...>";
567         unsigned map;
568
569         if (!pid)
570                 return "<idle>";
571
572         if (pid > PID_MAX_DEFAULT)
573                 goto out;
574
575         map = map_pid_to_cmdline[pid];
576         if (map >= SAVED_CMDLINES)
577                 goto out;
578
579         cmdline = saved_cmdlines[map];
580
581  out:
582         return cmdline;
583 }
584
585 notrace void tracing_record_cmdline(struct task_struct *tsk)
586 {
587         if (atomic_read(&trace_record_cmdline_disabled))
588                 return;
589
590         trace_save_cmdline(tsk);
591 }
592
593 static inline notrace struct list_head *
594 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
595 {
596         /*
597          * Roundrobin - but skip the head (which is not a real page):
598          */
599         next = next->next;
600         if (unlikely(next == &data->trace_pages))
601                 next = next->next;
602         BUG_ON(next == &data->trace_pages);
603
604         return next;
605 }
606
607 static inline notrace void *
608 trace_next_page(struct trace_array_cpu *data, void *addr)
609 {
610         struct list_head *next;
611         struct page *page;
612
613         page = virt_to_page(addr);
614
615         next = trace_next_list(data, &page->lru);
616         page = list_entry(next, struct page, lru);
617
618         return page_address(page);
619 }
620
621 static inline notrace struct trace_entry *
622 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
623 {
624         unsigned long idx, idx_next;
625         struct trace_entry *entry;
626
627         data->trace_idx++;
628         idx = data->trace_head_idx;
629         idx_next = idx + 1;
630
631         BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
632
633         entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
634
635         if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
636                 data->trace_head = trace_next_page(data, data->trace_head);
637                 idx_next = 0;
638         }
639
640         if (data->trace_head == data->trace_tail &&
641             idx_next == data->trace_tail_idx) {
642                 /* overrun */
643                 data->trace_tail_idx++;
644                 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
645                         data->trace_tail =
646                                 trace_next_page(data, data->trace_tail);
647                         data->trace_tail_idx = 0;
648                 }
649         }
650
651         data->trace_head_idx = idx_next;
652
653         return entry;
654 }
655
656 static inline notrace void
657 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
658 {
659         struct task_struct *tsk = current;
660         unsigned long pc;
661
662         pc = preempt_count();
663
664         entry->idx              = atomic_inc_return(&tracer_counter);
665         entry->preempt_count    = pc & 0xff;
666         entry->pid              = tsk->pid;
667         entry->t                = ftrace_now(raw_smp_processor_id());
668         entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
669                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
670                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
671                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
672 }
673
674 notrace void
675 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
676        unsigned long ip, unsigned long parent_ip, unsigned long flags)
677 {
678         struct trace_entry *entry;
679
680         spin_lock(&data->lock);
681         entry                   = tracing_get_trace_entry(tr, data);
682         tracing_generic_entry_update(entry, flags);
683         entry->type             = TRACE_FN;
684         entry->fn.ip            = ip;
685         entry->fn.parent_ip     = parent_ip;
686         spin_unlock(&data->lock);
687 }
688
689 notrace void
690 tracing_sched_switch_trace(struct trace_array *tr,
691                            struct trace_array_cpu *data,
692                            struct task_struct *prev, struct task_struct *next,
693                            unsigned long flags)
694 {
695         struct trace_entry *entry;
696
697         spin_lock(&data->lock);
698         entry                   = tracing_get_trace_entry(tr, data);
699         tracing_generic_entry_update(entry, flags);
700         entry->type             = TRACE_CTX;
701         entry->ctx.prev_pid     = prev->pid;
702         entry->ctx.prev_prio    = prev->prio;
703         entry->ctx.prev_state   = prev->state;
704         entry->ctx.next_pid     = next->pid;
705         entry->ctx.next_prio    = next->prio;
706         spin_unlock(&data->lock);
707 }
708
709 enum trace_file_type {
710         TRACE_FILE_LAT_FMT      = 1,
711 };
712
713 static struct trace_entry *
714 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
715                 struct trace_iterator *iter, int cpu)
716 {
717         struct page *page;
718         struct trace_entry *array;
719
720         if (iter->next_idx[cpu] >= tr->entries ||
721             iter->next_idx[cpu] >= data->trace_idx ||
722             (data->trace_head == data->trace_tail &&
723              data->trace_head_idx == data->trace_tail_idx))
724                 return NULL;
725
726         if (!iter->next_page[cpu]) {
727                 /* Initialize the iterator for this cpu trace buffer */
728                 WARN_ON(!data->trace_tail);
729                 page = virt_to_page(data->trace_tail);
730                 iter->next_page[cpu] = &page->lru;
731                 iter->next_page_idx[cpu] = data->trace_tail_idx;
732         }
733
734         page = list_entry(iter->next_page[cpu], struct page, lru);
735         BUG_ON(&data->trace_pages == &page->lru);
736
737         array = page_address(page);
738
739         /* Still possible to catch up to the tail */
740         if (iter->next_idx[cpu] && array == data->trace_tail &&
741             iter->next_page_idx[cpu] == data->trace_tail_idx)
742                 return NULL;
743
744         WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
745         return &array[iter->next_page_idx[cpu]];
746 }
747
748 static struct notrace trace_entry *
749 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
750 {
751         struct trace_array *tr = iter->tr;
752         struct trace_entry *ent, *next = NULL;
753         int next_cpu = -1;
754         int cpu;
755
756         for_each_possible_cpu(cpu) {
757                 if (!head_page(tr->data[cpu]))
758                         continue;
759                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
760                 if (ent &&
761                     (!next || (long)(next->idx - ent->idx) > 0)) {
762                         next = ent;
763                         next_cpu = cpu;
764                 }
765         }
766
767         if (ent_cpu)
768                 *ent_cpu = next_cpu;
769
770         return next;
771 }
772
773 static notrace void
774 trace_iterator_increment(struct trace_iterator *iter)
775 {
776         iter->idx++;
777         iter->next_idx[iter->cpu]++;
778         iter->next_page_idx[iter->cpu]++;
779         if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
780                 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
781
782                 iter->next_page_idx[iter->cpu] = 0;
783                 iter->next_page[iter->cpu] =
784                         trace_next_list(data, iter->next_page[iter->cpu]);
785         }
786 }
787
788 static notrace void
789 trace_consume(struct trace_iterator *iter)
790 {
791         struct trace_array_cpu *data = iter->tr->data[iter->cpu];
792
793         data->trace_tail_idx++;
794         if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
795                 data->trace_tail = trace_next_page(data, data->trace_tail);
796                 data->trace_tail_idx = 0;
797         }
798
799         /* Check if we empty it, then reset the index */
800         if (data->trace_head == data->trace_tail &&
801             data->trace_head_idx == data->trace_tail_idx)
802                 data->trace_idx = 0;
803
804         trace_iterator_increment(iter);
805 }
806
807 static notrace void *
808 find_next_entry_inc(struct trace_iterator *iter)
809 {
810         struct trace_entry *next;
811         int next_cpu = -1;
812
813         next = find_next_entry(iter, &next_cpu);
814
815         iter->prev_ent = iter->ent;
816         iter->prev_cpu = iter->cpu;
817
818         iter->ent = next;
819         iter->cpu = next_cpu;
820
821         if (next)
822                 trace_iterator_increment(iter);
823
824         return next ? iter : NULL;
825 }
826
827 static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
828 {
829         struct trace_iterator *iter = m->private;
830         void *last_ent = iter->ent;
831         int i = (int)*pos;
832         void *ent;
833
834         (*pos)++;
835
836         /* can't go backwards */
837         if (iter->idx > i)
838                 return NULL;
839
840         if (iter->idx < 0)
841                 ent = find_next_entry_inc(iter);
842         else
843                 ent = iter;
844
845         while (ent && iter->idx < i)
846                 ent = find_next_entry_inc(iter);
847
848         iter->pos = *pos;
849
850         if (last_ent && !ent)
851                 seq_puts(m, "\n\nvim:ft=help\n");
852
853         return ent;
854 }
855
856 static void *s_start(struct seq_file *m, loff_t *pos)
857 {
858         struct trace_iterator *iter = m->private;
859         void *p = NULL;
860         loff_t l = 0;
861         int i;
862
863         mutex_lock(&trace_types_lock);
864
865         if (!current_trace || current_trace != iter->trace)
866                 return NULL;
867
868         atomic_inc(&trace_record_cmdline_disabled);
869
870         /* let the tracer grab locks here if needed */
871         if (current_trace->start)
872                 current_trace->start(iter);
873
874         if (*pos != iter->pos) {
875                 iter->ent = NULL;
876                 iter->cpu = 0;
877                 iter->idx = -1;
878                 iter->prev_ent = NULL;
879                 iter->prev_cpu = -1;
880
881                 for_each_possible_cpu(i) {
882                         iter->next_idx[i] = 0;
883                         iter->next_page[i] = NULL;
884                 }
885
886                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
887                         ;
888
889         } else {
890                 l = *pos - 1;
891                 p = s_next(m, p, &l);
892         }
893
894         return p;
895 }
896
897 static void s_stop(struct seq_file *m, void *p)
898 {
899         struct trace_iterator *iter = m->private;
900
901         atomic_dec(&trace_record_cmdline_disabled);
902
903         /* let the tracer release locks here if needed */
904         if (current_trace && current_trace == iter->trace && iter->trace->stop)
905                 iter->trace->stop(iter);
906
907         mutex_unlock(&trace_types_lock);
908 }
909
910 static int
911 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
912 {
913 #ifdef CONFIG_KALLSYMS
914         char str[KSYM_SYMBOL_LEN];
915
916         kallsyms_lookup(address, NULL, NULL, NULL, str);
917
918         return trace_seq_printf(s, fmt, str);
919 #endif
920         return 1;
921 }
922
923 static int
924 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
925                      unsigned long address)
926 {
927 #ifdef CONFIG_KALLSYMS
928         char str[KSYM_SYMBOL_LEN];
929
930         sprint_symbol(str, address);
931         return trace_seq_printf(s, fmt, str);
932 #endif
933         return 1;
934 }
935
936 #ifndef CONFIG_64BIT
937 # define IP_FMT "%08lx"
938 #else
939 # define IP_FMT "%016lx"
940 #endif
941
942 static notrace int
943 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
944 {
945         int ret;
946
947         if (!ip)
948                 return trace_seq_printf(s, "0");
949
950         if (sym_flags & TRACE_ITER_SYM_OFFSET)
951                 ret = seq_print_sym_offset(s, "%s", ip);
952         else
953                 ret = seq_print_sym_short(s, "%s", ip);
954
955         if (!ret)
956                 return 0;
957
958         if (sym_flags & TRACE_ITER_SYM_ADDR)
959                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
960         return ret;
961 }
962
963 static notrace void print_lat_help_header(struct seq_file *m)
964 {
965         seq_puts(m, "#                _------=> CPU#            \n");
966         seq_puts(m, "#               / _-----=> irqs-off        \n");
967         seq_puts(m, "#              | / _----=> need-resched    \n");
968         seq_puts(m, "#              || / _---=> hardirq/softirq \n");
969         seq_puts(m, "#              ||| / _--=> preempt-depth   \n");
970         seq_puts(m, "#              |||| /                      \n");
971         seq_puts(m, "#              |||||     delay             \n");
972         seq_puts(m, "#  cmd     pid ||||| time  |   caller      \n");
973         seq_puts(m, "#     \\   /    |||||   \\   |   /           \n");
974 }
975
976 static notrace void print_func_help_header(struct seq_file *m)
977 {
978         seq_puts(m, "#           TASK-PID   CPU#    TIMESTAMP  FUNCTION\n");
979         seq_puts(m, "#              | |      |          |         |\n");
980 }
981
982
983 static notrace void
984 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
985 {
986         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
987         struct trace_array *tr = iter->tr;
988         struct trace_array_cpu *data = tr->data[tr->cpu];
989         struct tracer *type = current_trace;
990         unsigned long total   = 0;
991         unsigned long entries = 0;
992         int cpu;
993         const char *name = "preemption";
994
995         if (type)
996                 name = type->name;
997
998         for_each_possible_cpu(cpu) {
999                 if (head_page(tr->data[cpu])) {
1000                         total += tr->data[cpu]->trace_idx;
1001                         if (tr->data[cpu]->trace_idx > tr->entries)
1002                                 entries += tr->entries;
1003                         else
1004                                 entries += tr->data[cpu]->trace_idx;
1005                 }
1006         }
1007
1008         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1009                    name, UTS_RELEASE);
1010         seq_puts(m, "-----------------------------------"
1011                  "---------------------------------\n");
1012         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1013                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1014                    nsecs_to_usecs(data->saved_latency),
1015                    entries,
1016                    total,
1017                    tr->cpu,
1018 #if defined(CONFIG_PREEMPT_NONE)
1019                    "server",
1020 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1021                    "desktop",
1022 #elif defined(CONFIG_PREEMPT_DESKTOP)
1023                    "preempt",
1024 #else
1025                    "unknown",
1026 #endif
1027                    /* These are reserved for later use */
1028                    0, 0, 0, 0);
1029 #ifdef CONFIG_SMP
1030         seq_printf(m, " #P:%d)\n", num_online_cpus());
1031 #else
1032         seq_puts(m, ")\n");
1033 #endif
1034         seq_puts(m, "    -----------------\n");
1035         seq_printf(m, "    | task: %.16s-%d "
1036                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1037                    data->comm, data->pid, data->uid, data->nice,
1038                    data->policy, data->rt_priority);
1039         seq_puts(m, "    -----------------\n");
1040
1041         if (data->critical_start) {
1042                 seq_puts(m, " => started at: ");
1043                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1044                 trace_print_seq(m, &iter->seq);
1045                 seq_puts(m, "\n => ended at:   ");
1046                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1047                 trace_print_seq(m, &iter->seq);
1048                 seq_puts(m, "\n");
1049         }
1050
1051         seq_puts(m, "\n");
1052 }
1053
1054 static notrace void
1055 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1056 {
1057         int hardirq, softirq;
1058         char *comm;
1059
1060         comm = trace_find_cmdline(entry->pid);
1061
1062         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1063         trace_seq_printf(s, "%d", cpu);
1064         trace_seq_printf(s, "%c%c",
1065                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1066                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1067
1068         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1069         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1070         if (hardirq && softirq)
1071                 trace_seq_putc(s, 'H');
1072         else {
1073                 if (hardirq)
1074                         trace_seq_putc(s, 'h');
1075                 else {
1076                         if (softirq)
1077                                 trace_seq_putc(s, 's');
1078                         else
1079                                 trace_seq_putc(s, '.');
1080                 }
1081         }
1082
1083         if (entry->preempt_count)
1084                 trace_seq_printf(s, "%x", entry->preempt_count);
1085         else
1086                 trace_seq_puts(s, ".");
1087 }
1088
1089 unsigned long preempt_mark_thresh = 100;
1090
1091 static notrace void
1092 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1093                     unsigned long rel_usecs)
1094 {
1095         trace_seq_printf(s, " %4lldus", abs_usecs);
1096         if (rel_usecs > preempt_mark_thresh)
1097                 trace_seq_puts(s, "!: ");
1098         else if (rel_usecs > 1)
1099                 trace_seq_puts(s, "+: ");
1100         else
1101                 trace_seq_puts(s, " : ");
1102 }
1103
1104 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1105
1106 static notrace void
1107 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1108 {
1109         struct trace_seq *s = &iter->seq;
1110         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1111         struct trace_entry *next_entry = find_next_entry(iter, NULL);
1112         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1113         struct trace_entry *entry = iter->ent;
1114         unsigned long abs_usecs;
1115         unsigned long rel_usecs;
1116         char *comm;
1117         int S;
1118
1119         if (!next_entry)
1120                 next_entry = entry;
1121         rel_usecs = ns2usecs(next_entry->t - entry->t);
1122         abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1123
1124         if (verbose) {
1125                 comm = trace_find_cmdline(entry->pid);
1126                 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1127                                  " %ld.%03ldms (+%ld.%03ldms): ",
1128                                  comm,
1129                                  entry->pid, cpu, entry->flags,
1130                                  entry->preempt_count, trace_idx,
1131                                  ns2usecs(entry->t),
1132                                  abs_usecs/1000,
1133                                  abs_usecs % 1000, rel_usecs/1000,
1134                                  rel_usecs % 1000);
1135         } else {
1136                 lat_print_generic(s, entry, cpu);
1137                 lat_print_timestamp(s, abs_usecs, rel_usecs);
1138         }
1139         switch (entry->type) {
1140         case TRACE_FN:
1141                 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1142                 trace_seq_puts(s, " (");
1143                 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1144                 trace_seq_puts(s, ")\n");
1145                 break;
1146         case TRACE_CTX:
1147                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1148                         state_to_char[entry->ctx.prev_state] : 'X';
1149                 comm = trace_find_cmdline(entry->ctx.next_pid);
1150                 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1151                                  entry->ctx.prev_pid,
1152                                  entry->ctx.prev_prio,
1153                                  S,
1154                                  entry->ctx.next_pid,
1155                                  entry->ctx.next_prio,
1156                                  comm);
1157                 break;
1158         default:
1159                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1160         }
1161 }
1162
1163 static notrace void sync_time_offset(struct trace_iterator *iter)
1164 {
1165         struct trace_array_cpu *prev_array, *array;
1166         struct trace_entry *prev_entry, *entry;
1167         cycle_t prev_t, t;
1168
1169         entry = iter->ent;
1170         prev_entry = iter->prev_ent;
1171         if (!prev_entry)
1172                 return;
1173
1174         prev_array = iter->tr->data[iter->prev_cpu];
1175         array = iter->tr->data[iter->cpu];
1176
1177         prev_t = prev_entry->t + prev_array->time_offset;
1178         t = entry->t + array->time_offset;
1179
1180         /*
1181          * If time goes backwards we increase the offset of
1182          * the current array, to not have observable time warps.
1183          * This will quickly synchronize the time offsets of
1184          * multiple CPUs:
1185          */
1186         if (t < prev_t)
1187                 array->time_offset += prev_t - t;
1188 }
1189
1190 static notrace int
1191 print_trace_fmt(struct trace_iterator *iter)
1192 {
1193         struct trace_seq *s = &iter->seq;
1194         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1195         struct trace_entry *entry;
1196         unsigned long usec_rem;
1197         unsigned long long t;
1198         unsigned long secs;
1199         char *comm;
1200         int S;
1201         int ret;
1202
1203         sync_time_offset(iter);
1204         entry = iter->ent;
1205
1206         comm = trace_find_cmdline(iter->ent->pid);
1207
1208         t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
1209         usec_rem = do_div(t, 1000000ULL);
1210         secs = (unsigned long)t;
1211
1212         ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1213         if (!ret)
1214                 return 0;
1215         ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1216         if (!ret)
1217                 return 0;
1218         ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1219         if (!ret)
1220                 return 0;
1221
1222         switch (entry->type) {
1223         case TRACE_FN:
1224                 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1225                 if (!ret)
1226                         return 0;
1227                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1228                                                 entry->fn.parent_ip) {
1229                         ret = trace_seq_printf(s, " <-");
1230                         if (!ret)
1231                                 return 0;
1232                         ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1233                                                sym_flags);
1234                         if (!ret)
1235                                 return 0;
1236                 }
1237                 ret = trace_seq_printf(s, "\n");
1238                 if (!ret)
1239                         return 0;
1240                 break;
1241         case TRACE_CTX:
1242                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1243                         state_to_char[entry->ctx.prev_state] : 'X';
1244                 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1245                                        entry->ctx.prev_pid,
1246                                        entry->ctx.prev_prio,
1247                                        S,
1248                                        entry->ctx.next_pid,
1249                                        entry->ctx.next_prio);
1250                 if (!ret)
1251                         return 0;
1252                 break;
1253         }
1254         return 1;
1255 }
1256
1257 static int trace_empty(struct trace_iterator *iter)
1258 {
1259         struct trace_array_cpu *data;
1260         int cpu;
1261
1262         for_each_possible_cpu(cpu) {
1263                 data = iter->tr->data[cpu];
1264
1265                 if (head_page(data) && data->trace_idx &&
1266                     (data->trace_tail != data->trace_head ||
1267                      data->trace_tail_idx != data->trace_head_idx))
1268                         return 0;
1269         }
1270         return 1;
1271 }
1272
1273 static int s_show(struct seq_file *m, void *v)
1274 {
1275         struct trace_iterator *iter = v;
1276
1277         if (iter->ent == NULL) {
1278                 if (iter->tr) {
1279                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1280                         seq_puts(m, "#\n");
1281                 }
1282                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1283                         /* print nothing if the buffers are empty */
1284                         if (trace_empty(iter))
1285                                 return 0;
1286                         print_trace_header(m, iter);
1287                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1288                                 print_lat_help_header(m);
1289                 } else {
1290                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1291                                 print_func_help_header(m);
1292                 }
1293         } else {
1294                 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1295                         print_lat_fmt(iter, iter->idx, iter->cpu);
1296                 else
1297                         print_trace_fmt(iter);
1298                 trace_print_seq(m, &iter->seq);
1299         }
1300
1301         return 0;
1302 }
1303
1304 static struct seq_operations tracer_seq_ops = {
1305         .start          = s_start,
1306         .next           = s_next,
1307         .stop           = s_stop,
1308         .show           = s_show,
1309 };
1310
1311 static struct trace_iterator notrace *
1312 __tracing_open(struct inode *inode, struct file *file, int *ret)
1313 {
1314         struct trace_iterator *iter;
1315
1316         if (tracing_disabled) {
1317                 *ret = -ENODEV;
1318                 return NULL;
1319         }
1320
1321         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1322         if (!iter) {
1323                 *ret = -ENOMEM;
1324                 goto out;
1325         }
1326
1327         mutex_lock(&trace_types_lock);
1328         if (current_trace && current_trace->print_max)
1329                 iter->tr = &max_tr;
1330         else
1331                 iter->tr = inode->i_private;
1332         iter->trace = current_trace;
1333         iter->pos = -1;
1334
1335         /* TODO stop tracer */
1336         *ret = seq_open(file, &tracer_seq_ops);
1337         if (!*ret) {
1338                 struct seq_file *m = file->private_data;
1339                 m->private = iter;
1340
1341                 /* stop the trace while dumping */
1342                 if (iter->tr->ctrl)
1343                         tracer_enabled = 0;
1344
1345                 if (iter->trace && iter->trace->open)
1346                         iter->trace->open(iter);
1347         } else {
1348                 kfree(iter);
1349                 iter = NULL;
1350         }
1351         mutex_unlock(&trace_types_lock);
1352
1353  out:
1354         return iter;
1355 }
1356
1357 int tracing_open_generic(struct inode *inode, struct file *filp)
1358 {
1359         if (tracing_disabled)
1360                 return -ENODEV;
1361
1362         filp->private_data = inode->i_private;
1363         return 0;
1364 }
1365
1366 int tracing_release(struct inode *inode, struct file *file)
1367 {
1368         struct seq_file *m = (struct seq_file *)file->private_data;
1369         struct trace_iterator *iter = m->private;
1370
1371         mutex_lock(&trace_types_lock);
1372         if (iter->trace && iter->trace->close)
1373                 iter->trace->close(iter);
1374
1375         /* reenable tracing if it was previously enabled */
1376         if (iter->tr->ctrl)
1377                 tracer_enabled = 1;
1378         mutex_unlock(&trace_types_lock);
1379
1380         seq_release(inode, file);
1381         kfree(iter);
1382         return 0;
1383 }
1384
1385 static int tracing_open(struct inode *inode, struct file *file)
1386 {
1387         int ret;
1388
1389         __tracing_open(inode, file, &ret);
1390
1391         return ret;
1392 }
1393
1394 static int tracing_lt_open(struct inode *inode, struct file *file)
1395 {
1396         struct trace_iterator *iter;
1397         int ret;
1398
1399         iter = __tracing_open(inode, file, &ret);
1400
1401         if (!ret)
1402                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1403
1404         return ret;
1405 }
1406
1407
1408 static notrace void *
1409 t_next(struct seq_file *m, void *v, loff_t *pos)
1410 {
1411         struct tracer *t = m->private;
1412
1413         (*pos)++;
1414
1415         if (t)
1416                 t = t->next;
1417
1418         m->private = t;
1419
1420         return t;
1421 }
1422
1423 static void *t_start(struct seq_file *m, loff_t *pos)
1424 {
1425         struct tracer *t = m->private;
1426         loff_t l = 0;
1427
1428         mutex_lock(&trace_types_lock);
1429         for (; t && l < *pos; t = t_next(m, t, &l))
1430                 ;
1431
1432         return t;
1433 }
1434
1435 static void t_stop(struct seq_file *m, void *p)
1436 {
1437         mutex_unlock(&trace_types_lock);
1438 }
1439
1440 static int t_show(struct seq_file *m, void *v)
1441 {
1442         struct tracer *t = v;
1443
1444         if (!t)
1445                 return 0;
1446
1447         seq_printf(m, "%s", t->name);
1448         if (t->next)
1449                 seq_putc(m, ' ');
1450         else
1451                 seq_putc(m, '\n');
1452
1453         return 0;
1454 }
1455
1456 static struct seq_operations show_traces_seq_ops = {
1457         .start          = t_start,
1458         .next           = t_next,
1459         .stop           = t_stop,
1460         .show           = t_show,
1461 };
1462
1463 static int show_traces_open(struct inode *inode, struct file *file)
1464 {
1465         int ret;
1466
1467         if (tracing_disabled)
1468                 return -ENODEV;
1469
1470         ret = seq_open(file, &show_traces_seq_ops);
1471         if (!ret) {
1472                 struct seq_file *m = file->private_data;
1473                 m->private = trace_types;
1474         }
1475
1476         return ret;
1477 }
1478
1479 static struct file_operations tracing_fops = {
1480         .open           = tracing_open,
1481         .read           = seq_read,
1482         .llseek         = seq_lseek,
1483         .release        = tracing_release,
1484 };
1485
1486 static struct file_operations tracing_lt_fops = {
1487         .open           = tracing_lt_open,
1488         .read           = seq_read,
1489         .llseek         = seq_lseek,
1490         .release        = tracing_release,
1491 };
1492
1493 static struct file_operations show_traces_fops = {
1494         .open = show_traces_open,
1495         .read = seq_read,
1496         .release = seq_release,
1497 };
1498
1499 static ssize_t
1500 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1501                        size_t cnt, loff_t *ppos)
1502 {
1503         char *buf;
1504         int r = 0;
1505         int len = 0;
1506         int i;
1507
1508         /* calulate max size */
1509         for (i = 0; trace_options[i]; i++) {
1510                 len += strlen(trace_options[i]);
1511                 len += 3; /* "no" and space */
1512         }
1513
1514         /* +2 for \n and \0 */
1515         buf = kmalloc(len + 2, GFP_KERNEL);
1516         if (!buf)
1517                 return -ENOMEM;
1518
1519         for (i = 0; trace_options[i]; i++) {
1520                 if (trace_flags & (1 << i))
1521                         r += sprintf(buf + r, "%s ", trace_options[i]);
1522                 else
1523                         r += sprintf(buf + r, "no%s ", trace_options[i]);
1524         }
1525
1526         r += sprintf(buf + r, "\n");
1527         WARN_ON(r >= len + 2);
1528
1529         r = simple_read_from_buffer(ubuf, cnt, ppos,
1530                                     buf, r);
1531
1532         kfree(buf);
1533
1534         return r;
1535 }
1536
1537 static ssize_t
1538 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1539                         size_t cnt, loff_t *ppos)
1540 {
1541         char buf[64];
1542         char *cmp = buf;
1543         int neg = 0;
1544         int i;
1545
1546         if (cnt > 63)
1547                 cnt = 63;
1548
1549         if (copy_from_user(&buf, ubuf, cnt))
1550                 return -EFAULT;
1551
1552         buf[cnt] = 0;
1553
1554         if (strncmp(buf, "no", 2) == 0) {
1555                 neg = 1;
1556                 cmp += 2;
1557         }
1558
1559         for (i = 0; trace_options[i]; i++) {
1560                 int len = strlen(trace_options[i]);
1561
1562                 if (strncmp(cmp, trace_options[i], len) == 0) {
1563                         if (neg)
1564                                 trace_flags &= ~(1 << i);
1565                         else
1566                                 trace_flags |= (1 << i);
1567                         break;
1568                 }
1569         }
1570
1571         filp->f_pos += cnt;
1572
1573         return cnt;
1574 }
1575
1576 static struct file_operations tracing_iter_fops = {
1577         .open = tracing_open_generic,
1578         .read = tracing_iter_ctrl_read,
1579         .write = tracing_iter_ctrl_write,
1580 };
1581
1582 static const char readme_msg[] =
1583         "tracing mini-HOWTO:\n\n"
1584         "# mkdir /debug\n"
1585         "# mount -t debugfs nodev /debug\n\n"
1586         "# cat /debug/tracing/available_tracers\n"
1587         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1588         "# cat /debug/tracing/current_tracer\n"
1589         "none\n"
1590         "# echo sched_switch > /debug/tracing/current_tracer\n"
1591         "# cat /debug/tracing/current_tracer\n"
1592         "sched_switch\n"
1593         "# cat /debug/tracing/iter_ctrl\n"
1594         "noprint-parent nosym-offset nosym-addr noverbose\n"
1595         "# echo print-parent > /debug/tracing/iter_ctrl\n"
1596         "# echo 1 > /debug/tracing/tracing_enabled\n"
1597         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1598         "echo 0 > /debug/tracing/tracing_enabled\n"
1599 ;
1600
1601 static ssize_t
1602 tracing_readme_read(struct file *filp, char __user *ubuf,
1603                        size_t cnt, loff_t *ppos)
1604 {
1605         return simple_read_from_buffer(ubuf, cnt, ppos,
1606                                         readme_msg, strlen(readme_msg));
1607 }
1608
1609 static struct file_operations tracing_readme_fops = {
1610         .open = tracing_open_generic,
1611         .read = tracing_readme_read,
1612 };
1613
1614
1615 static ssize_t
1616 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1617                   size_t cnt, loff_t *ppos)
1618 {
1619         struct trace_array *tr = filp->private_data;
1620         char buf[64];
1621         int r;
1622
1623         r = sprintf(buf, "%ld\n", tr->ctrl);
1624         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1625 }
1626
1627 static ssize_t
1628 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1629                    size_t cnt, loff_t *ppos)
1630 {
1631         struct trace_array *tr = filp->private_data;
1632         long val;
1633         char buf[64];
1634
1635         if (cnt > 63)
1636                 cnt = 63;
1637
1638         if (copy_from_user(&buf, ubuf, cnt))
1639                 return -EFAULT;
1640
1641         buf[cnt] = 0;
1642
1643         val = simple_strtoul(buf, NULL, 10);
1644
1645         val = !!val;
1646
1647         mutex_lock(&trace_types_lock);
1648         if (tr->ctrl ^ val) {
1649                 if (val)
1650                         tracer_enabled = 1;
1651                 else
1652                         tracer_enabled = 0;
1653
1654                 tr->ctrl = val;
1655
1656                 if (current_trace && current_trace->ctrl_update)
1657                         current_trace->ctrl_update(tr);
1658         }
1659         mutex_unlock(&trace_types_lock);
1660
1661         filp->f_pos += cnt;
1662
1663         return cnt;
1664 }
1665
1666 static ssize_t
1667 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1668                        size_t cnt, loff_t *ppos)
1669 {
1670         char buf[max_tracer_type_len+2];
1671         int r;
1672
1673         mutex_lock(&trace_types_lock);
1674         if (current_trace)
1675                 r = sprintf(buf, "%s\n", current_trace->name);
1676         else
1677                 r = sprintf(buf, "\n");
1678         mutex_unlock(&trace_types_lock);
1679
1680         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1681 }
1682
1683 static ssize_t
1684 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1685                         size_t cnt, loff_t *ppos)
1686 {
1687         struct trace_array *tr = &global_trace;
1688         struct tracer *t;
1689         char buf[max_tracer_type_len+1];
1690         int i;
1691
1692         if (cnt > max_tracer_type_len)
1693                 cnt = max_tracer_type_len;
1694
1695         if (copy_from_user(&buf, ubuf, cnt))
1696                 return -EFAULT;
1697
1698         buf[cnt] = 0;
1699
1700         /* strip ending whitespace. */
1701         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1702                 buf[i] = 0;
1703
1704         mutex_lock(&trace_types_lock);
1705         for (t = trace_types; t; t = t->next) {
1706                 if (strcmp(t->name, buf) == 0)
1707                         break;
1708         }
1709         if (!t || t == current_trace)
1710                 goto out;
1711
1712         if (current_trace && current_trace->reset)
1713                 current_trace->reset(tr);
1714
1715         current_trace = t;
1716         if (t->init)
1717                 t->init(tr);
1718
1719  out:
1720         mutex_unlock(&trace_types_lock);
1721
1722         filp->f_pos += cnt;
1723
1724         return cnt;
1725 }
1726
1727 static ssize_t
1728 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1729                      size_t cnt, loff_t *ppos)
1730 {
1731         unsigned long *ptr = filp->private_data;
1732         char buf[64];
1733         int r;
1734
1735         r = snprintf(buf, 64, "%ld\n",
1736                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1737         if (r > 64)
1738                 r = 64;
1739         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1740 }
1741
1742 static ssize_t
1743 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1744                       size_t cnt, loff_t *ppos)
1745 {
1746         long *ptr = filp->private_data;
1747         long val;
1748         char buf[64];
1749
1750         if (cnt > 63)
1751                 cnt = 63;
1752
1753         if (copy_from_user(&buf, ubuf, cnt))
1754                 return -EFAULT;
1755
1756         buf[cnt] = 0;
1757
1758         val = simple_strtoul(buf, NULL, 10);
1759
1760         *ptr = val * 1000;
1761
1762         return cnt;
1763 }
1764
1765 static atomic_t tracing_reader;
1766
1767 static int tracing_open_pipe(struct inode *inode, struct file *filp)
1768 {
1769         struct trace_iterator *iter;
1770
1771         if (tracing_disabled)
1772                 return -ENODEV;
1773
1774         /* We only allow for reader of the pipe */
1775         if (atomic_inc_return(&tracing_reader) != 1) {
1776                 atomic_dec(&tracing_reader);
1777                 return -EBUSY;
1778         }
1779
1780         /* create a buffer to store the information to pass to userspace */
1781         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1782         if (!iter)
1783                 return -ENOMEM;
1784
1785         iter->tr = &global_trace;
1786
1787         filp->private_data = iter;
1788
1789         return 0;
1790 }
1791
1792 static int tracing_release_pipe(struct inode *inode, struct file *file)
1793 {
1794         struct trace_iterator *iter = file->private_data;
1795
1796         kfree(iter);
1797         atomic_dec(&tracing_reader);
1798
1799         return 0;
1800 }
1801
1802 /*
1803  * Consumer reader.
1804  */
1805 static ssize_t
1806 tracing_read_pipe(struct file *filp, char __user *ubuf,
1807                   size_t cnt, loff_t *ppos)
1808 {
1809         struct trace_iterator *iter = filp->private_data;
1810         struct trace_array_cpu *data;
1811         static cpumask_t mask;
1812         struct trace_entry *entry;
1813         static int start;
1814         unsigned long flags;
1815         int read = 0;
1816         int cpu;
1817         int len;
1818         int ret;
1819
1820         /* return any leftover data */
1821         if (iter->seq.len > start) {
1822                 len = iter->seq.len - start;
1823                 if (cnt > len)
1824                         cnt = len;
1825                 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1826                 if (ret)
1827                         cnt = -EFAULT;
1828
1829                 start += len;
1830
1831                 return cnt;
1832         }
1833
1834         trace_seq_reset(&iter->seq);
1835         start = 0;
1836
1837         while (trace_empty(iter)) {
1838                 /*
1839                  * This is a make-shift waitqueue. The reason we don't use
1840                  * an actual wait queue is because:
1841                  *  1) we only ever have one waiter
1842                  *  2) the tracing, traces all functions, we don't want
1843                  *     the overhead of calling wake_up and friends
1844                  *     (and tracing them too)
1845                  *     Anyway, this is really very primitive wakeup.
1846                  */
1847                 set_current_state(TASK_INTERRUPTIBLE);
1848                 iter->tr->waiter = current;
1849
1850                 /* sleep for one second, and try again. */
1851                 schedule_timeout(HZ);
1852
1853                 iter->tr->waiter = NULL;
1854
1855                 if (signal_pending(current))
1856                         return -EINTR;
1857
1858                 /*
1859                  * We block until we read something and tracing is disabled.
1860                  * We still block if tracing is disabled, but we have never
1861                  * read anything. This allows a user to cat this file, and
1862                  * then enable tracing. But after we have read something,
1863                  * we give an EOF when tracing is again disabled.
1864                  *
1865                  * iter->pos will be 0 if we haven't read anything.
1866                  */
1867                 if (!tracer_enabled && iter->pos)
1868                         break;
1869
1870                 continue;
1871         }
1872
1873         /* stop when tracing is finished */
1874         if (trace_empty(iter))
1875                 return 0;
1876
1877         if (cnt >= PAGE_SIZE)
1878                 cnt = PAGE_SIZE - 1;
1879
1880         memset(iter, 0, sizeof(*iter));
1881         iter->tr = &global_trace;
1882         iter->pos = -1;
1883
1884         /*
1885          * We need to stop all tracing on all CPUS to read the
1886          * the next buffer. This is a bit expensive, but is
1887          * not done often. We fill all what we can read,
1888          * and then release the locks again.
1889          */
1890
1891         cpus_clear(mask);
1892         local_irq_save(flags);
1893         for_each_possible_cpu(cpu) {
1894                 data = iter->tr->data[cpu];
1895
1896                 if (!head_page(data) || !data->trace_idx)
1897                         continue;
1898
1899                 atomic_inc(&data->disabled);
1900                 spin_lock(&data->lock);
1901                 cpu_set(cpu, mask);
1902         }
1903
1904         while ((entry = find_next_entry(iter, &cpu))) {
1905
1906                 if (!entry)
1907                         break;
1908
1909                 iter->ent = entry;
1910                 iter->cpu = cpu;
1911
1912                 ret = print_trace_fmt(iter);
1913                 if (!ret)
1914                         break;
1915
1916                 trace_consume(iter);
1917
1918                 if (iter->seq.len >= cnt)
1919                         break;
1920
1921         }
1922
1923         for_each_cpu_mask(cpu, mask) {
1924                 data = iter->tr->data[cpu];
1925                 spin_unlock(&data->lock);
1926                 atomic_dec(&data->disabled);
1927         }
1928         local_irq_restore(flags);
1929
1930         /* Now copy what we have to the user */
1931         read = iter->seq.len;
1932         if (read > cnt)
1933                 read = cnt;
1934
1935         ret = copy_to_user(ubuf, iter->seq.buffer, read);
1936
1937         if (read < iter->seq.len)
1938                 start = read;
1939         else
1940                 trace_seq_reset(&iter->seq);
1941
1942         if (ret)
1943                 read = -EFAULT;
1944
1945         return read;
1946 }
1947
1948 static struct file_operations tracing_max_lat_fops = {
1949         .open           = tracing_open_generic,
1950         .read           = tracing_max_lat_read,
1951         .write          = tracing_max_lat_write,
1952 };
1953
1954 static struct file_operations tracing_ctrl_fops = {
1955         .open           = tracing_open_generic,
1956         .read           = tracing_ctrl_read,
1957         .write          = tracing_ctrl_write,
1958 };
1959
1960 static struct file_operations set_tracer_fops = {
1961         .open           = tracing_open_generic,
1962         .read           = tracing_set_trace_read,
1963         .write          = tracing_set_trace_write,
1964 };
1965
1966 static struct file_operations tracing_pipe_fops = {
1967         .open           = tracing_open_pipe,
1968         .read           = tracing_read_pipe,
1969         .release        = tracing_release_pipe,
1970 };
1971
1972 #ifdef CONFIG_DYNAMIC_FTRACE
1973
1974 static ssize_t
1975 tracing_read_long(struct file *filp, char __user *ubuf,
1976                   size_t cnt, loff_t *ppos)
1977 {
1978         unsigned long *p = filp->private_data;
1979         char buf[64];
1980         int r;
1981
1982         r = sprintf(buf, "%ld\n", *p);
1983
1984         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1985 }
1986
1987 static struct file_operations tracing_read_long_fops = {
1988         .open           = tracing_open_generic,
1989         .read           = tracing_read_long,
1990 };
1991 #endif
1992
1993 static struct dentry *d_tracer;
1994
1995 struct dentry *tracing_init_dentry(void)
1996 {
1997         static int once;
1998
1999         if (d_tracer)
2000                 return d_tracer;
2001
2002         d_tracer = debugfs_create_dir("tracing", NULL);
2003
2004         if (!d_tracer && !once) {
2005                 once = 1;
2006                 pr_warning("Could not create debugfs directory 'tracing'\n");
2007                 return NULL;
2008         }
2009
2010         return d_tracer;
2011 }
2012
2013 #ifdef CONFIG_FTRACE_SELFTEST
2014 /* Let selftest have access to static functions in this file */
2015 #include "trace_selftest.c"
2016 #endif
2017
2018 static __init void tracer_init_debugfs(void)
2019 {
2020         struct dentry *d_tracer;
2021         struct dentry *entry;
2022
2023         d_tracer = tracing_init_dentry();
2024
2025         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2026                                     &global_trace, &tracing_ctrl_fops);
2027         if (!entry)
2028                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2029
2030         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2031                                     NULL, &tracing_iter_fops);
2032         if (!entry)
2033                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2034
2035         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2036                                     &global_trace, &tracing_lt_fops);
2037         if (!entry)
2038                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2039
2040         entry = debugfs_create_file("trace", 0444, d_tracer,
2041                                     &global_trace, &tracing_fops);
2042         if (!entry)
2043                 pr_warning("Could not create debugfs 'trace' entry\n");
2044
2045         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2046                                     &global_trace, &show_traces_fops);
2047         if (!entry)
2048                 pr_warning("Could not create debugfs 'trace' entry\n");
2049
2050         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2051                                     &global_trace, &set_tracer_fops);
2052         if (!entry)
2053                 pr_warning("Could not create debugfs 'trace' entry\n");
2054
2055         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2056                                     &tracing_max_latency,
2057                                     &tracing_max_lat_fops);
2058         if (!entry)
2059                 pr_warning("Could not create debugfs "
2060                            "'tracing_max_latency' entry\n");
2061
2062         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2063                                     &tracing_thresh, &tracing_max_lat_fops);
2064         if (!entry)
2065                 pr_warning("Could not create debugfs "
2066                            "'tracing_threash' entry\n");
2067         entry = debugfs_create_file("README", 0644, d_tracer,
2068                                     NULL, &tracing_readme_fops);
2069         if (!entry)
2070                 pr_warning("Could not create debugfs 'README' entry\n");
2071
2072         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2073                                     NULL, &tracing_pipe_fops);
2074         if (!entry)
2075                 pr_warning("Could not create debugfs "
2076                            "'tracing_threash' entry\n");
2077
2078 #ifdef CONFIG_DYNAMIC_FTRACE
2079         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2080                                     &ftrace_update_tot_cnt,
2081                                     &tracing_read_long_fops);
2082         if (!entry)
2083                 pr_warning("Could not create debugfs "
2084                            "'dyn_ftrace_total_info' entry\n");
2085 #endif
2086 }
2087
2088 /* dummy trace to disable tracing */
2089 static struct tracer no_tracer __read_mostly =
2090 {
2091         .name           = "none",
2092 };
2093
2094 static int trace_alloc_page(void)
2095 {
2096         struct trace_array_cpu *data;
2097         struct page *page, *tmp;
2098         LIST_HEAD(pages);
2099         void *array;
2100         int i;
2101
2102         /* first allocate a page for each CPU */
2103         for_each_possible_cpu(i) {
2104                 array = (void *)__get_free_page(GFP_KERNEL);
2105                 if (array == NULL) {
2106                         printk(KERN_ERR "tracer: failed to allocate page"
2107                                "for trace buffer!\n");
2108                         goto free_pages;
2109                 }
2110
2111                 page = virt_to_page(array);
2112                 list_add(&page->lru, &pages);
2113
2114 /* Only allocate if we are actually using the max trace */
2115 #ifdef CONFIG_TRACER_MAX_TRACE
2116                 array = (void *)__get_free_page(GFP_KERNEL);
2117                 if (array == NULL) {
2118                         printk(KERN_ERR "tracer: failed to allocate page"
2119                                "for trace buffer!\n");
2120                         goto free_pages;
2121                 }
2122                 page = virt_to_page(array);
2123                 list_add(&page->lru, &pages);
2124 #endif
2125         }
2126
2127         /* Now that we successfully allocate a page per CPU, add them */
2128         for_each_possible_cpu(i) {
2129                 data = global_trace.data[i];
2130                 spin_lock_init(&data->lock);
2131                 lockdep_set_class(&data->lock, &data->lock_key);
2132                 page = list_entry(pages.next, struct page, lru);
2133                 list_del_init(&page->lru);
2134                 list_add_tail(&page->lru, &data->trace_pages);
2135                 ClearPageLRU(page);
2136
2137 #ifdef CONFIG_TRACER_MAX_TRACE
2138                 data = max_tr.data[i];
2139                 spin_lock_init(&data->lock);
2140                 lockdep_set_class(&data->lock, &data->lock_key);
2141                 page = list_entry(pages.next, struct page, lru);
2142                 list_del_init(&page->lru);
2143                 list_add_tail(&page->lru, &data->trace_pages);
2144                 SetPageLRU(page);
2145 #endif
2146         }
2147         global_trace.entries += ENTRIES_PER_PAGE;
2148
2149         return 0;
2150
2151  free_pages:
2152         list_for_each_entry_safe(page, tmp, &pages, lru) {
2153                 list_del_init(&page->lru);
2154                 __free_page(page);
2155         }
2156         return -ENOMEM;
2157 }
2158
2159 __init static int tracer_alloc_buffers(void)
2160 {
2161         struct trace_array_cpu *data;
2162         void *array;
2163         struct page *page;
2164         int pages = 0;
2165         int ret = -ENOMEM;
2166         int i;
2167
2168         /* Allocate the first page for all buffers */
2169         for_each_possible_cpu(i) {
2170                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2171                 max_tr.data[i] = &per_cpu(max_data, i);
2172
2173                 array = (void *)__get_free_page(GFP_KERNEL);
2174                 if (array == NULL) {
2175                         printk(KERN_ERR "tracer: failed to allocate page"
2176                                "for trace buffer!\n");
2177                         goto free_buffers;
2178                 }
2179
2180                 /* set the array to the list */
2181                 INIT_LIST_HEAD(&data->trace_pages);
2182                 page = virt_to_page(array);
2183                 list_add(&page->lru, &data->trace_pages);
2184                 /* use the LRU flag to differentiate the two buffers */
2185                 ClearPageLRU(page);
2186
2187 /* Only allocate if we are actually using the max trace */
2188 #ifdef CONFIG_TRACER_MAX_TRACE
2189                 array = (void *)__get_free_page(GFP_KERNEL);
2190                 if (array == NULL) {
2191                         printk(KERN_ERR "tracer: failed to allocate page"
2192                                "for trace buffer!\n");
2193                         goto free_buffers;
2194                 }
2195
2196                 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2197                 page = virt_to_page(array);
2198                 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2199                 SetPageLRU(page);
2200 #endif
2201         }
2202
2203         /*
2204          * Since we allocate by orders of pages, we may be able to
2205          * round up a bit.
2206          */
2207         global_trace.entries = ENTRIES_PER_PAGE;
2208         pages++;
2209
2210         while (global_trace.entries < trace_nr_entries) {
2211                 if (trace_alloc_page())
2212                         break;
2213                 pages++;
2214         }
2215         max_tr.entries = global_trace.entries;
2216
2217         pr_info("tracer: %d pages allocated for %ld",
2218                 pages, trace_nr_entries);
2219         pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2220         pr_info("   actual entries %ld\n", global_trace.entries);
2221
2222         tracer_init_debugfs();
2223
2224         trace_init_cmdlines();
2225
2226         register_tracer(&no_tracer);
2227         current_trace = &no_tracer;
2228
2229         /* All seems OK, enable tracing */
2230         tracing_disabled = 0;
2231
2232         return 0;
2233
2234  free_buffers:
2235         for (i-- ; i >= 0; i--) {
2236                 struct page *page, *tmp;
2237                 struct trace_array_cpu *data = global_trace.data[i];
2238
2239                 if (data) {
2240                         list_for_each_entry_safe(page, tmp,
2241                                                  &data->trace_pages, lru) {
2242                                 list_del_init(&page->lru);
2243                                 __free_page(page);
2244                         }
2245                 }
2246
2247 #ifdef CONFIG_TRACER_MAX_TRACE
2248                 data = max_tr.data[i];
2249                 if (data) {
2250                         list_for_each_entry_safe(page, tmp,
2251                                                  &data->trace_pages, lru) {
2252                                 list_del_init(&page->lru);
2253                                 __free_page(page);
2254                         }
2255                 }
2256 #endif
2257         }
2258         return ret;
2259 }
2260 fs_initcall(tracer_alloc_buffers);