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