ftrace: remove-idx-sync
[deliverable/linux.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
b3806b43
SR
773static notrace void
774trace_iterator_increment(struct trace_iterator *iter)
bc0c38d1 775{
b3806b43
SR
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];
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
b3806b43
SR
788static notrace void
789trace_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 }
4e3c3333 798
b3806b43
SR
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;
b3806b43
SR
803}
804
805static notrace void *
806find_next_entry_inc(struct trace_iterator *iter)
807{
808 struct trace_entry *next;
809 int next_cpu = -1;
810
811 next = find_next_entry(iter, &next_cpu);
93a588f4 812
4e3c3333
IM
813 iter->prev_ent = iter->ent;
814 iter->prev_cpu = iter->cpu;
815
bc0c38d1
SR
816 iter->ent = next;
817 iter->cpu = next_cpu;
818
b3806b43
SR
819 if (next)
820 trace_iterator_increment(iter);
821
bc0c38d1
SR
822 return next ? iter : NULL;
823}
824
4e3c3333 825static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
826{
827 struct trace_iterator *iter = m->private;
bc0c38d1
SR
828 void *last_ent = iter->ent;
829 int i = (int)*pos;
4e3c3333 830 void *ent;
bc0c38d1
SR
831
832 (*pos)++;
833
834 /* can't go backwards */
835 if (iter->idx > i)
836 return NULL;
837
838 if (iter->idx < 0)
839 ent = find_next_entry_inc(iter);
840 else
841 ent = iter;
842
843 while (ent && iter->idx < i)
844 ent = find_next_entry_inc(iter);
845
846 iter->pos = *pos;
847
848 if (last_ent && !ent)
849 seq_puts(m, "\n\nvim:ft=help\n");
850
851 return ent;
852}
853
854static void *s_start(struct seq_file *m, loff_t *pos)
855{
856 struct trace_iterator *iter = m->private;
857 void *p = NULL;
858 loff_t l = 0;
859 int i;
860
861 mutex_lock(&trace_types_lock);
862
863 if (!current_trace || current_trace != iter->trace)
864 return NULL;
865
866 atomic_inc(&trace_record_cmdline_disabled);
867
868 /* let the tracer grab locks here if needed */
869 if (current_trace->start)
870 current_trace->start(iter);
871
872 if (*pos != iter->pos) {
873 iter->ent = NULL;
874 iter->cpu = 0;
875 iter->idx = -1;
4e3c3333
IM
876 iter->prev_ent = NULL;
877 iter->prev_cpu = -1;
bc0c38d1 878
4c11d7ae 879 for_each_possible_cpu(i) {
bc0c38d1 880 iter->next_idx[i] = 0;
4c11d7ae
SR
881 iter->next_page[i] = NULL;
882 }
bc0c38d1
SR
883
884 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
885 ;
886
887 } else {
4c11d7ae 888 l = *pos - 1;
bc0c38d1
SR
889 p = s_next(m, p, &l);
890 }
891
892 return p;
893}
894
895static void s_stop(struct seq_file *m, void *p)
896{
897 struct trace_iterator *iter = m->private;
898
899 atomic_dec(&trace_record_cmdline_disabled);
900
901 /* let the tracer release locks here if needed */
902 if (current_trace && current_trace == iter->trace && iter->trace->stop)
903 iter->trace->stop(iter);
904
905 mutex_unlock(&trace_types_lock);
906}
907
b3806b43 908static int
214023c3 909seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
bc0c38d1
SR
910{
911#ifdef CONFIG_KALLSYMS
912 char str[KSYM_SYMBOL_LEN];
913
914 kallsyms_lookup(address, NULL, NULL, NULL, str);
915
b3806b43 916 return trace_seq_printf(s, fmt, str);
bc0c38d1 917#endif
b3806b43 918 return 1;
bc0c38d1
SR
919}
920
b3806b43 921static int
214023c3
SR
922seq_print_sym_offset(struct trace_seq *s, const char *fmt,
923 unsigned long address)
bc0c38d1
SR
924{
925#ifdef CONFIG_KALLSYMS
926 char str[KSYM_SYMBOL_LEN];
927
928 sprint_symbol(str, address);
b3806b43 929 return trace_seq_printf(s, fmt, str);
bc0c38d1 930#endif
b3806b43 931 return 1;
bc0c38d1
SR
932}
933
934#ifndef CONFIG_64BIT
935# define IP_FMT "%08lx"
936#else
937# define IP_FMT "%016lx"
938#endif
939
b3806b43 940static notrace int
214023c3 941seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
bc0c38d1 942{
b3806b43
SR
943 int ret;
944
945 if (!ip)
946 return trace_seq_printf(s, "0");
bc0c38d1
SR
947
948 if (sym_flags & TRACE_ITER_SYM_OFFSET)
b3806b43 949 ret = seq_print_sym_offset(s, "%s", ip);
bc0c38d1 950 else
b3806b43
SR
951 ret = seq_print_sym_short(s, "%s", ip);
952
953 if (!ret)
954 return 0;
bc0c38d1
SR
955
956 if (sym_flags & TRACE_ITER_SYM_ADDR)
b3806b43
SR
957 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
958 return ret;
bc0c38d1
SR
959}
960
4e3c3333 961static notrace void print_lat_help_header(struct seq_file *m)
bc0c38d1
SR
962{
963 seq_puts(m, "# _------=> CPU# \n");
964 seq_puts(m, "# / _-----=> irqs-off \n");
965 seq_puts(m, "# | / _----=> need-resched \n");
966 seq_puts(m, "# || / _---=> hardirq/softirq \n");
967 seq_puts(m, "# ||| / _--=> preempt-depth \n");
968 seq_puts(m, "# |||| / \n");
969 seq_puts(m, "# ||||| delay \n");
970 seq_puts(m, "# cmd pid ||||| time | caller \n");
971 seq_puts(m, "# \\ / ||||| \\ | / \n");
972}
973
4e3c3333 974static notrace void print_func_help_header(struct seq_file *m)
bc0c38d1
SR
975{
976 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
977 seq_puts(m, "# | | | | |\n");
978}
979
980
4e3c3333 981static notrace void
bc0c38d1
SR
982print_trace_header(struct seq_file *m, struct trace_iterator *iter)
983{
984 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
985 struct trace_array *tr = iter->tr;
986 struct trace_array_cpu *data = tr->data[tr->cpu];
987 struct tracer *type = current_trace;
4c11d7ae
SR
988 unsigned long total = 0;
989 unsigned long entries = 0;
bc0c38d1
SR
990 int cpu;
991 const char *name = "preemption";
992
993 if (type)
994 name = type->name;
995
996 for_each_possible_cpu(cpu) {
c7aafc54 997 if (head_page(tr->data[cpu])) {
4c11d7ae
SR
998 total += tr->data[cpu]->trace_idx;
999 if (tr->data[cpu]->trace_idx > tr->entries)
bc0c38d1 1000 entries += tr->entries;
4c11d7ae 1001 else
bc0c38d1
SR
1002 entries += tr->data[cpu]->trace_idx;
1003 }
1004 }
1005
1006 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1007 name, UTS_RELEASE);
1008 seq_puts(m, "-----------------------------------"
1009 "---------------------------------\n");
1010 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1011 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 1012 nsecs_to_usecs(data->saved_latency),
bc0c38d1 1013 entries,
4c11d7ae 1014 total,
bc0c38d1
SR
1015 tr->cpu,
1016#if defined(CONFIG_PREEMPT_NONE)
1017 "server",
1018#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1019 "desktop",
1020#elif defined(CONFIG_PREEMPT_DESKTOP)
1021 "preempt",
1022#else
1023 "unknown",
1024#endif
1025 /* These are reserved for later use */
1026 0, 0, 0, 0);
1027#ifdef CONFIG_SMP
1028 seq_printf(m, " #P:%d)\n", num_online_cpus());
1029#else
1030 seq_puts(m, ")\n");
1031#endif
1032 seq_puts(m, " -----------------\n");
1033 seq_printf(m, " | task: %.16s-%d "
1034 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1035 data->comm, data->pid, data->uid, data->nice,
1036 data->policy, data->rt_priority);
1037 seq_puts(m, " -----------------\n");
1038
1039 if (data->critical_start) {
1040 seq_puts(m, " => started at: ");
214023c3
SR
1041 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1042 trace_print_seq(m, &iter->seq);
bc0c38d1 1043 seq_puts(m, "\n => ended at: ");
214023c3
SR
1044 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1045 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1046 seq_puts(m, "\n");
1047 }
1048
1049 seq_puts(m, "\n");
1050}
1051
4e3c3333 1052static notrace void
214023c3 1053lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
bc0c38d1
SR
1054{
1055 int hardirq, softirq;
1056 char *comm;
1057
1058 comm = trace_find_cmdline(entry->pid);
1059
214023c3
SR
1060 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1061 trace_seq_printf(s, "%d", cpu);
1062 trace_seq_printf(s, "%c%c",
1063 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1064 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
bc0c38d1
SR
1065
1066 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1067 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1068 if (hardirq && softirq)
214023c3 1069 trace_seq_putc(s, 'H');
bc0c38d1
SR
1070 else {
1071 if (hardirq)
214023c3 1072 trace_seq_putc(s, 'h');
bc0c38d1
SR
1073 else {
1074 if (softirq)
214023c3 1075 trace_seq_putc(s, 's');
bc0c38d1 1076 else
214023c3 1077 trace_seq_putc(s, '.');
bc0c38d1
SR
1078 }
1079 }
1080
1081 if (entry->preempt_count)
214023c3 1082 trace_seq_printf(s, "%x", entry->preempt_count);
bc0c38d1 1083 else
214023c3 1084 trace_seq_puts(s, ".");
bc0c38d1
SR
1085}
1086
1087unsigned long preempt_mark_thresh = 100;
1088
4e3c3333 1089static notrace void
214023c3 1090lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
bc0c38d1
SR
1091 unsigned long rel_usecs)
1092{
214023c3 1093 trace_seq_printf(s, " %4lldus", abs_usecs);
bc0c38d1 1094 if (rel_usecs > preempt_mark_thresh)
214023c3 1095 trace_seq_puts(s, "!: ");
bc0c38d1 1096 else if (rel_usecs > 1)
214023c3 1097 trace_seq_puts(s, "+: ");
bc0c38d1 1098 else
214023c3 1099 trace_seq_puts(s, " : ");
bc0c38d1
SR
1100}
1101
1102static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1103
4e3c3333 1104static notrace void
214023c3 1105print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
bc0c38d1 1106{
214023c3 1107 struct trace_seq *s = &iter->seq;
bc0c38d1
SR
1108 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1109 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1110 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1111 struct trace_entry *entry = iter->ent;
1112 unsigned long abs_usecs;
1113 unsigned long rel_usecs;
1114 char *comm;
1115 int S;
1116
1117 if (!next_entry)
1118 next_entry = entry;
1119 rel_usecs = ns2usecs(next_entry->t - entry->t);
1120 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1121
1122 if (verbose) {
1123 comm = trace_find_cmdline(entry->pid);
214023c3
SR
1124 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1125 " %ld.%03ldms (+%ld.%03ldms): ",
1126 comm,
1127 entry->pid, cpu, entry->flags,
1128 entry->preempt_count, trace_idx,
1129 ns2usecs(entry->t),
1130 abs_usecs/1000,
1131 abs_usecs % 1000, rel_usecs/1000,
1132 rel_usecs % 1000);
bc0c38d1 1133 } else {
214023c3
SR
1134 lat_print_generic(s, entry, cpu);
1135 lat_print_timestamp(s, abs_usecs, rel_usecs);
bc0c38d1
SR
1136 }
1137 switch (entry->type) {
1138 case TRACE_FN:
214023c3
SR
1139 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1140 trace_seq_puts(s, " (");
1141 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1142 trace_seq_puts(s, ")\n");
bc0c38d1
SR
1143 break;
1144 case TRACE_CTX:
1145 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1146 state_to_char[entry->ctx.prev_state] : 'X';
1147 comm = trace_find_cmdline(entry->ctx.next_pid);
214023c3
SR
1148 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1149 entry->ctx.prev_pid,
1150 entry->ctx.prev_prio,
1151 S,
1152 entry->ctx.next_pid,
1153 entry->ctx.next_prio,
1154 comm);
bc0c38d1 1155 break;
89b2f978 1156 default:
214023c3 1157 trace_seq_printf(s, "Unknown type %d\n", entry->type);
bc0c38d1
SR
1158 }
1159}
1160
b3806b43 1161static notrace int
214023c3 1162print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 1163{
214023c3 1164 struct trace_seq *s = &iter->seq;
bc0c38d1 1165 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 1166 struct trace_entry *entry;
bc0c38d1
SR
1167 unsigned long usec_rem;
1168 unsigned long long t;
1169 unsigned long secs;
1170 char *comm;
1171 int S;
b3806b43 1172 int ret;
bc0c38d1 1173
4e3c3333
IM
1174 entry = iter->ent;
1175
bc0c38d1
SR
1176 comm = trace_find_cmdline(iter->ent->pid);
1177
cdd31cd2 1178 t = ns2usecs(entry->t);
bc0c38d1
SR
1179 usec_rem = do_div(t, 1000000ULL);
1180 secs = (unsigned long)t;
1181
b3806b43
SR
1182 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1183 if (!ret)
1184 return 0;
1185 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1186 if (!ret)
1187 return 0;
1188 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1189 if (!ret)
1190 return 0;
bc0c38d1
SR
1191
1192 switch (entry->type) {
1193 case TRACE_FN:
b3806b43
SR
1194 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1195 if (!ret)
1196 return 0;
bc0c38d1
SR
1197 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1198 entry->fn.parent_ip) {
b3806b43
SR
1199 ret = trace_seq_printf(s, " <-");
1200 if (!ret)
1201 return 0;
1202 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1203 sym_flags);
1204 if (!ret)
1205 return 0;
bc0c38d1 1206 }
b3806b43
SR
1207 ret = trace_seq_printf(s, "\n");
1208 if (!ret)
1209 return 0;
bc0c38d1
SR
1210 break;
1211 case TRACE_CTX:
1212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
b3806b43
SR
1214 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1215 entry->ctx.prev_pid,
1216 entry->ctx.prev_prio,
1217 S,
1218 entry->ctx.next_pid,
1219 entry->ctx.next_prio);
1220 if (!ret)
1221 return 0;
bc0c38d1
SR
1222 break;
1223 }
b3806b43 1224 return 1;
bc0c38d1
SR
1225}
1226
1227static int trace_empty(struct trace_iterator *iter)
1228{
1229 struct trace_array_cpu *data;
1230 int cpu;
1231
1232 for_each_possible_cpu(cpu) {
1233 data = iter->tr->data[cpu];
1234
b3806b43
SR
1235 if (head_page(data) && data->trace_idx &&
1236 (data->trace_tail != data->trace_head ||
1237 data->trace_tail_idx != data->trace_head_idx))
bc0c38d1
SR
1238 return 0;
1239 }
1240 return 1;
1241}
1242
1243static int s_show(struct seq_file *m, void *v)
1244{
1245 struct trace_iterator *iter = v;
1246
1247 if (iter->ent == NULL) {
1248 if (iter->tr) {
1249 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1250 seq_puts(m, "#\n");
1251 }
1252 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1253 /* print nothing if the buffers are empty */
1254 if (trace_empty(iter))
1255 return 0;
1256 print_trace_header(m, iter);
1257 if (!(trace_flags & TRACE_ITER_VERBOSE))
1258 print_lat_help_header(m);
1259 } else {
1260 if (!(trace_flags & TRACE_ITER_VERBOSE))
1261 print_func_help_header(m);
1262 }
1263 } else {
1264 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
214023c3 1265 print_lat_fmt(iter, iter->idx, iter->cpu);
bc0c38d1 1266 else
214023c3
SR
1267 print_trace_fmt(iter);
1268 trace_print_seq(m, &iter->seq);
bc0c38d1
SR
1269 }
1270
1271 return 0;
1272}
1273
1274static struct seq_operations tracer_seq_ops = {
4bf39a94
IM
1275 .start = s_start,
1276 .next = s_next,
1277 .stop = s_stop,
1278 .show = s_show,
bc0c38d1
SR
1279};
1280
1281static struct trace_iterator notrace *
1282__tracing_open(struct inode *inode, struct file *file, int *ret)
1283{
1284 struct trace_iterator *iter;
1285
60a11774
SR
1286 if (tracing_disabled) {
1287 *ret = -ENODEV;
1288 return NULL;
1289 }
1290
bc0c38d1
SR
1291 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1292 if (!iter) {
1293 *ret = -ENOMEM;
1294 goto out;
1295 }
1296
1297 mutex_lock(&trace_types_lock);
1298 if (current_trace && current_trace->print_max)
1299 iter->tr = &max_tr;
1300 else
1301 iter->tr = inode->i_private;
1302 iter->trace = current_trace;
1303 iter->pos = -1;
1304
1305 /* TODO stop tracer */
1306 *ret = seq_open(file, &tracer_seq_ops);
1307 if (!*ret) {
1308 struct seq_file *m = file->private_data;
1309 m->private = iter;
1310
1311 /* stop the trace while dumping */
1312 if (iter->tr->ctrl)
1313 tracer_enabled = 0;
1314
1315 if (iter->trace && iter->trace->open)
1316 iter->trace->open(iter);
1317 } else {
1318 kfree(iter);
1319 iter = NULL;
1320 }
1321 mutex_unlock(&trace_types_lock);
1322
1323 out:
1324 return iter;
1325}
1326
1327int tracing_open_generic(struct inode *inode, struct file *filp)
1328{
60a11774
SR
1329 if (tracing_disabled)
1330 return -ENODEV;
1331
bc0c38d1
SR
1332 filp->private_data = inode->i_private;
1333 return 0;
1334}
1335
1336int tracing_release(struct inode *inode, struct file *file)
1337{
1338 struct seq_file *m = (struct seq_file *)file->private_data;
1339 struct trace_iterator *iter = m->private;
1340
1341 mutex_lock(&trace_types_lock);
1342 if (iter->trace && iter->trace->close)
1343 iter->trace->close(iter);
1344
1345 /* reenable tracing if it was previously enabled */
1346 if (iter->tr->ctrl)
1347 tracer_enabled = 1;
1348 mutex_unlock(&trace_types_lock);
1349
1350 seq_release(inode, file);
1351 kfree(iter);
1352 return 0;
1353}
1354
1355static int tracing_open(struct inode *inode, struct file *file)
1356{
1357 int ret;
1358
1359 __tracing_open(inode, file, &ret);
1360
1361 return ret;
1362}
1363
1364static int tracing_lt_open(struct inode *inode, struct file *file)
1365{
1366 struct trace_iterator *iter;
1367 int ret;
1368
1369 iter = __tracing_open(inode, file, &ret);
1370
1371 if (!ret)
1372 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1373
1374 return ret;
1375}
1376
1377
4e3c3333 1378static notrace void *
bc0c38d1
SR
1379t_next(struct seq_file *m, void *v, loff_t *pos)
1380{
1381 struct tracer *t = m->private;
1382
1383 (*pos)++;
1384
1385 if (t)
1386 t = t->next;
1387
1388 m->private = t;
1389
1390 return t;
1391}
1392
1393static void *t_start(struct seq_file *m, loff_t *pos)
1394{
1395 struct tracer *t = m->private;
1396 loff_t l = 0;
1397
1398 mutex_lock(&trace_types_lock);
1399 for (; t && l < *pos; t = t_next(m, t, &l))
1400 ;
1401
1402 return t;
1403}
1404
1405static void t_stop(struct seq_file *m, void *p)
1406{
1407 mutex_unlock(&trace_types_lock);
1408}
1409
1410static int t_show(struct seq_file *m, void *v)
1411{
1412 struct tracer *t = v;
1413
1414 if (!t)
1415 return 0;
1416
1417 seq_printf(m, "%s", t->name);
1418 if (t->next)
1419 seq_putc(m, ' ');
1420 else
1421 seq_putc(m, '\n');
1422
1423 return 0;
1424}
1425
1426static struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
1427 .start = t_start,
1428 .next = t_next,
1429 .stop = t_stop,
1430 .show = t_show,
bc0c38d1
SR
1431};
1432
1433static int show_traces_open(struct inode *inode, struct file *file)
1434{
1435 int ret;
1436
60a11774
SR
1437 if (tracing_disabled)
1438 return -ENODEV;
1439
bc0c38d1
SR
1440 ret = seq_open(file, &show_traces_seq_ops);
1441 if (!ret) {
1442 struct seq_file *m = file->private_data;
1443 m->private = trace_types;
1444 }
1445
1446 return ret;
1447}
1448
1449static struct file_operations tracing_fops = {
4bf39a94
IM
1450 .open = tracing_open,
1451 .read = seq_read,
1452 .llseek = seq_lseek,
1453 .release = tracing_release,
bc0c38d1
SR
1454};
1455
1456static struct file_operations tracing_lt_fops = {
4bf39a94
IM
1457 .open = tracing_lt_open,
1458 .read = seq_read,
1459 .llseek = seq_lseek,
1460 .release = tracing_release,
bc0c38d1
SR
1461};
1462
1463static struct file_operations show_traces_fops = {
1464 .open = show_traces_open,
1465 .read = seq_read,
1466 .release = seq_release,
1467};
1468
1469static ssize_t
1470tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1471 size_t cnt, loff_t *ppos)
1472{
1473 char *buf;
1474 int r = 0;
1475 int len = 0;
1476 int i;
1477
1478 /* calulate max size */
1479 for (i = 0; trace_options[i]; i++) {
1480 len += strlen(trace_options[i]);
1481 len += 3; /* "no" and space */
1482 }
1483
1484 /* +2 for \n and \0 */
1485 buf = kmalloc(len + 2, GFP_KERNEL);
1486 if (!buf)
1487 return -ENOMEM;
1488
1489 for (i = 0; trace_options[i]; i++) {
1490 if (trace_flags & (1 << i))
1491 r += sprintf(buf + r, "%s ", trace_options[i]);
1492 else
1493 r += sprintf(buf + r, "no%s ", trace_options[i]);
1494 }
1495
1496 r += sprintf(buf + r, "\n");
1497 WARN_ON(r >= len + 2);
1498
1499 r = simple_read_from_buffer(ubuf, cnt, ppos,
1500 buf, r);
1501
1502 kfree(buf);
1503
1504 return r;
1505}
1506
1507static ssize_t
1508tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1509 size_t cnt, loff_t *ppos)
1510{
1511 char buf[64];
1512 char *cmp = buf;
1513 int neg = 0;
1514 int i;
1515
1516 if (cnt > 63)
1517 cnt = 63;
1518
1519 if (copy_from_user(&buf, ubuf, cnt))
1520 return -EFAULT;
1521
1522 buf[cnt] = 0;
1523
1524 if (strncmp(buf, "no", 2) == 0) {
1525 neg = 1;
1526 cmp += 2;
1527 }
1528
1529 for (i = 0; trace_options[i]; i++) {
1530 int len = strlen(trace_options[i]);
1531
1532 if (strncmp(cmp, trace_options[i], len) == 0) {
1533 if (neg)
1534 trace_flags &= ~(1 << i);
1535 else
1536 trace_flags |= (1 << i);
1537 break;
1538 }
1539 }
1540
1541 filp->f_pos += cnt;
1542
1543 return cnt;
1544}
1545
1546static struct file_operations tracing_iter_fops = {
1547 .open = tracing_open_generic,
1548 .read = tracing_iter_ctrl_read,
1549 .write = tracing_iter_ctrl_write,
1550};
1551
7bd2f24c
IM
1552static const char readme_msg[] =
1553 "tracing mini-HOWTO:\n\n"
1554 "# mkdir /debug\n"
1555 "# mount -t debugfs nodev /debug\n\n"
1556 "# cat /debug/tracing/available_tracers\n"
1557 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1558 "# cat /debug/tracing/current_tracer\n"
1559 "none\n"
1560 "# echo sched_switch > /debug/tracing/current_tracer\n"
1561 "# cat /debug/tracing/current_tracer\n"
1562 "sched_switch\n"
1563 "# cat /debug/tracing/iter_ctrl\n"
1564 "noprint-parent nosym-offset nosym-addr noverbose\n"
1565 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1566 "# echo 1 > /debug/tracing/tracing_enabled\n"
1567 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1568 "echo 0 > /debug/tracing/tracing_enabled\n"
1569;
1570
1571static ssize_t
1572tracing_readme_read(struct file *filp, char __user *ubuf,
1573 size_t cnt, loff_t *ppos)
1574{
1575 return simple_read_from_buffer(ubuf, cnt, ppos,
1576 readme_msg, strlen(readme_msg));
1577}
1578
1579static struct file_operations tracing_readme_fops = {
1580 .open = tracing_open_generic,
1581 .read = tracing_readme_read,
1582};
1583
1584
bc0c38d1
SR
1585static ssize_t
1586tracing_ctrl_read(struct file *filp, char __user *ubuf,
1587 size_t cnt, loff_t *ppos)
1588{
1589 struct trace_array *tr = filp->private_data;
1590 char buf[64];
1591 int r;
1592
1593 r = sprintf(buf, "%ld\n", tr->ctrl);
4e3c3333 1594 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1595}
1596
1597static ssize_t
1598tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1599 size_t cnt, loff_t *ppos)
1600{
1601 struct trace_array *tr = filp->private_data;
1602 long val;
1603 char buf[64];
1604
1605 if (cnt > 63)
1606 cnt = 63;
1607
1608 if (copy_from_user(&buf, ubuf, cnt))
1609 return -EFAULT;
1610
1611 buf[cnt] = 0;
1612
1613 val = simple_strtoul(buf, NULL, 10);
1614
1615 val = !!val;
1616
1617 mutex_lock(&trace_types_lock);
1618 if (tr->ctrl ^ val) {
1619 if (val)
1620 tracer_enabled = 1;
1621 else
1622 tracer_enabled = 0;
1623
1624 tr->ctrl = val;
1625
1626 if (current_trace && current_trace->ctrl_update)
1627 current_trace->ctrl_update(tr);
1628 }
1629 mutex_unlock(&trace_types_lock);
1630
1631 filp->f_pos += cnt;
1632
1633 return cnt;
1634}
1635
1636static ssize_t
1637tracing_set_trace_read(struct file *filp, char __user *ubuf,
1638 size_t cnt, loff_t *ppos)
1639{
1640 char buf[max_tracer_type_len+2];
1641 int r;
1642
1643 mutex_lock(&trace_types_lock);
1644 if (current_trace)
1645 r = sprintf(buf, "%s\n", current_trace->name);
1646 else
1647 r = sprintf(buf, "\n");
1648 mutex_unlock(&trace_types_lock);
1649
4bf39a94 1650 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1651}
1652
1653static ssize_t
1654tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1655 size_t cnt, loff_t *ppos)
1656{
1657 struct trace_array *tr = &global_trace;
1658 struct tracer *t;
1659 char buf[max_tracer_type_len+1];
1660 int i;
1661
1662 if (cnt > max_tracer_type_len)
1663 cnt = max_tracer_type_len;
1664
1665 if (copy_from_user(&buf, ubuf, cnt))
1666 return -EFAULT;
1667
1668 buf[cnt] = 0;
1669
1670 /* strip ending whitespace. */
1671 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1672 buf[i] = 0;
1673
1674 mutex_lock(&trace_types_lock);
1675 for (t = trace_types; t; t = t->next) {
1676 if (strcmp(t->name, buf) == 0)
1677 break;
1678 }
1679 if (!t || t == current_trace)
1680 goto out;
1681
1682 if (current_trace && current_trace->reset)
1683 current_trace->reset(tr);
1684
1685 current_trace = t;
1686 if (t->init)
1687 t->init(tr);
1688
1689 out:
1690 mutex_unlock(&trace_types_lock);
1691
1692 filp->f_pos += cnt;
1693
1694 return cnt;
1695}
1696
1697static ssize_t
1698tracing_max_lat_read(struct file *filp, char __user *ubuf,
1699 size_t cnt, loff_t *ppos)
1700{
1701 unsigned long *ptr = filp->private_data;
1702 char buf[64];
1703 int r;
1704
1705 r = snprintf(buf, 64, "%ld\n",
1706 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1707 if (r > 64)
1708 r = 64;
4bf39a94 1709 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1710}
1711
1712static ssize_t
1713tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1714 size_t cnt, loff_t *ppos)
1715{
1716 long *ptr = filp->private_data;
1717 long val;
1718 char buf[64];
1719
1720 if (cnt > 63)
1721 cnt = 63;
1722
1723 if (copy_from_user(&buf, ubuf, cnt))
1724 return -EFAULT;
1725
1726 buf[cnt] = 0;
1727
1728 val = simple_strtoul(buf, NULL, 10);
1729
1730 *ptr = val * 1000;
1731
1732 return cnt;
1733}
1734
b3806b43
SR
1735static atomic_t tracing_reader;
1736
1737static int tracing_open_pipe(struct inode *inode, struct file *filp)
1738{
1739 struct trace_iterator *iter;
1740
1741 if (tracing_disabled)
1742 return -ENODEV;
1743
1744 /* We only allow for reader of the pipe */
1745 if (atomic_inc_return(&tracing_reader) != 1) {
1746 atomic_dec(&tracing_reader);
1747 return -EBUSY;
1748 }
1749
1750 /* create a buffer to store the information to pass to userspace */
1751 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1752 if (!iter)
1753 return -ENOMEM;
1754
1755 iter->tr = &global_trace;
1756
1757 filp->private_data = iter;
1758
1759 return 0;
1760}
1761
1762static int tracing_release_pipe(struct inode *inode, struct file *file)
1763{
1764 struct trace_iterator *iter = file->private_data;
1765
1766 kfree(iter);
1767 atomic_dec(&tracing_reader);
1768
1769 return 0;
1770}
1771
1772/*
1773 * Consumer reader.
1774 */
1775static ssize_t
1776tracing_read_pipe(struct file *filp, char __user *ubuf,
1777 size_t cnt, loff_t *ppos)
1778{
1779 struct trace_iterator *iter = filp->private_data;
1780 struct trace_array_cpu *data;
1781 static cpumask_t mask;
1782 struct trace_entry *entry;
1783 static int start;
1784 unsigned long flags;
1785 int read = 0;
1786 int cpu;
1787 int len;
1788 int ret;
1789
1790 /* return any leftover data */
1791 if (iter->seq.len > start) {
1792 len = iter->seq.len - start;
1793 if (cnt > len)
1794 cnt = len;
1795 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1796 if (ret)
1797 cnt = -EFAULT;
1798
1799 start += len;
1800
1801 return cnt;
1802 }
1803
1804 trace_seq_reset(&iter->seq);
1805 start = 0;
1806
1807 while (trace_empty(iter)) {
1808 /*
1809 * This is a make-shift waitqueue. The reason we don't use
1810 * an actual wait queue is because:
1811 * 1) we only ever have one waiter
1812 * 2) the tracing, traces all functions, we don't want
1813 * the overhead of calling wake_up and friends
1814 * (and tracing them too)
1815 * Anyway, this is really very primitive wakeup.
1816 */
1817 set_current_state(TASK_INTERRUPTIBLE);
1818 iter->tr->waiter = current;
1819
1820 /* sleep for one second, and try again. */
1821 schedule_timeout(HZ);
1822
1823 iter->tr->waiter = NULL;
1824
1825 if (signal_pending(current))
1826 return -EINTR;
1827
1828 /*
1829 * We block until we read something and tracing is disabled.
1830 * We still block if tracing is disabled, but we have never
1831 * read anything. This allows a user to cat this file, and
1832 * then enable tracing. But after we have read something,
1833 * we give an EOF when tracing is again disabled.
1834 *
1835 * iter->pos will be 0 if we haven't read anything.
1836 */
1837 if (!tracer_enabled && iter->pos)
1838 break;
1839
1840 continue;
1841 }
1842
1843 /* stop when tracing is finished */
1844 if (trace_empty(iter))
1845 return 0;
1846
1847 if (cnt >= PAGE_SIZE)
1848 cnt = PAGE_SIZE - 1;
1849
1850 memset(iter, 0, sizeof(*iter));
1851 iter->tr = &global_trace;
1852 iter->pos = -1;
1853
1854 /*
1855 * We need to stop all tracing on all CPUS to read the
1856 * the next buffer. This is a bit expensive, but is
1857 * not done often. We fill all what we can read,
1858 * and then release the locks again.
1859 */
1860
1861 cpus_clear(mask);
1862 local_irq_save(flags);
1863 for_each_possible_cpu(cpu) {
1864 data = iter->tr->data[cpu];
1865
1866 if (!head_page(data) || !data->trace_idx)
1867 continue;
1868
1869 atomic_inc(&data->disabled);
1870 spin_lock(&data->lock);
1871 cpu_set(cpu, mask);
1872 }
1873
1874 while ((entry = find_next_entry(iter, &cpu))) {
1875
1876 if (!entry)
1877 break;
1878
1879 iter->ent = entry;
1880 iter->cpu = cpu;
1881
1882 ret = print_trace_fmt(iter);
1883 if (!ret)
1884 break;
1885
1886 trace_consume(iter);
1887
1888 if (iter->seq.len >= cnt)
1889 break;
1890
1891 }
1892
d4c5a2f5 1893 for_each_cpu_mask(cpu, mask) {
b3806b43 1894 data = iter->tr->data[cpu];
b3806b43
SR
1895 spin_unlock(&data->lock);
1896 atomic_dec(&data->disabled);
1897 }
1898 local_irq_restore(flags);
1899
1900 /* Now copy what we have to the user */
1901 read = iter->seq.len;
1902 if (read > cnt)
1903 read = cnt;
1904
1905 ret = copy_to_user(ubuf, iter->seq.buffer, read);
1906
1907 if (read < iter->seq.len)
1908 start = read;
1909 else
1910 trace_seq_reset(&iter->seq);
1911
1912 if (ret)
1913 read = -EFAULT;
1914
1915 return read;
1916}
1917
bc0c38d1 1918static struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
1919 .open = tracing_open_generic,
1920 .read = tracing_max_lat_read,
1921 .write = tracing_max_lat_write,
bc0c38d1
SR
1922};
1923
1924static struct file_operations tracing_ctrl_fops = {
4bf39a94
IM
1925 .open = tracing_open_generic,
1926 .read = tracing_ctrl_read,
1927 .write = tracing_ctrl_write,
bc0c38d1
SR
1928};
1929
1930static struct file_operations set_tracer_fops = {
4bf39a94
IM
1931 .open = tracing_open_generic,
1932 .read = tracing_set_trace_read,
1933 .write = tracing_set_trace_write,
bc0c38d1
SR
1934};
1935
b3806b43 1936static struct file_operations tracing_pipe_fops = {
4bf39a94
IM
1937 .open = tracing_open_pipe,
1938 .read = tracing_read_pipe,
1939 .release = tracing_release_pipe,
b3806b43
SR
1940};
1941
bc0c38d1
SR
1942#ifdef CONFIG_DYNAMIC_FTRACE
1943
1944static ssize_t
1945tracing_read_long(struct file *filp, char __user *ubuf,
1946 size_t cnt, loff_t *ppos)
1947{
1948 unsigned long *p = filp->private_data;
1949 char buf[64];
1950 int r;
1951
1952 r = sprintf(buf, "%ld\n", *p);
4bf39a94
IM
1953
1954 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
1955}
1956
1957static struct file_operations tracing_read_long_fops = {
4bf39a94
IM
1958 .open = tracing_open_generic,
1959 .read = tracing_read_long,
bc0c38d1
SR
1960};
1961#endif
1962
1963static struct dentry *d_tracer;
1964
1965struct dentry *tracing_init_dentry(void)
1966{
1967 static int once;
1968
1969 if (d_tracer)
1970 return d_tracer;
1971
1972 d_tracer = debugfs_create_dir("tracing", NULL);
1973
1974 if (!d_tracer && !once) {
1975 once = 1;
1976 pr_warning("Could not create debugfs directory 'tracing'\n");
1977 return NULL;
1978 }
1979
1980 return d_tracer;
1981}
1982
60a11774
SR
1983#ifdef CONFIG_FTRACE_SELFTEST
1984/* Let selftest have access to static functions in this file */
1985#include "trace_selftest.c"
1986#endif
1987
bc0c38d1
SR
1988static __init void tracer_init_debugfs(void)
1989{
1990 struct dentry *d_tracer;
1991 struct dentry *entry;
1992
1993 d_tracer = tracing_init_dentry();
1994
1995 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
1996 &global_trace, &tracing_ctrl_fops);
1997 if (!entry)
1998 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
1999
2000 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2001 NULL, &tracing_iter_fops);
2002 if (!entry)
2003 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2004
2005 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2006 &global_trace, &tracing_lt_fops);
2007 if (!entry)
2008 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2009
2010 entry = debugfs_create_file("trace", 0444, d_tracer,
2011 &global_trace, &tracing_fops);
2012 if (!entry)
2013 pr_warning("Could not create debugfs 'trace' entry\n");
2014
2015 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2016 &global_trace, &show_traces_fops);
2017 if (!entry)
2018 pr_warning("Could not create debugfs 'trace' entry\n");
2019
2020 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2021 &global_trace, &set_tracer_fops);
2022 if (!entry)
2023 pr_warning("Could not create debugfs 'trace' entry\n");
2024
2025 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2026 &tracing_max_latency,
2027 &tracing_max_lat_fops);
2028 if (!entry)
2029 pr_warning("Could not create debugfs "
2030 "'tracing_max_latency' entry\n");
2031
2032 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2033 &tracing_thresh, &tracing_max_lat_fops);
2034 if (!entry)
2035 pr_warning("Could not create debugfs "
2036 "'tracing_threash' entry\n");
7bd2f24c
IM
2037 entry = debugfs_create_file("README", 0644, d_tracer,
2038 NULL, &tracing_readme_fops);
2039 if (!entry)
2040 pr_warning("Could not create debugfs 'README' entry\n");
2041
b3806b43
SR
2042 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2043 NULL, &tracing_pipe_fops);
2044 if (!entry)
2045 pr_warning("Could not create debugfs "
2046 "'tracing_threash' entry\n");
bc0c38d1
SR
2047
2048#ifdef CONFIG_DYNAMIC_FTRACE
2049 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2050 &ftrace_update_tot_cnt,
2051 &tracing_read_long_fops);
2052 if (!entry)
2053 pr_warning("Could not create debugfs "
2054 "'dyn_ftrace_total_info' entry\n");
2055#endif
2056}
2057
2058/* dummy trace to disable tracing */
2059static struct tracer no_tracer __read_mostly =
2060{
4bf39a94 2061 .name = "none",
bc0c38d1
SR
2062};
2063
4c11d7ae 2064static int trace_alloc_page(void)
bc0c38d1 2065{
4c11d7ae 2066 struct trace_array_cpu *data;
4c11d7ae
SR
2067 struct page *page, *tmp;
2068 LIST_HEAD(pages);
c7aafc54 2069 void *array;
4c11d7ae
SR
2070 int i;
2071
2072 /* first allocate a page for each CPU */
2073 for_each_possible_cpu(i) {
2074 array = (void *)__get_free_page(GFP_KERNEL);
2075 if (array == NULL) {
2076 printk(KERN_ERR "tracer: failed to allocate page"
2077 "for trace buffer!\n");
2078 goto free_pages;
2079 }
2080
2081 page = virt_to_page(array);
2082 list_add(&page->lru, &pages);
2083
2084/* Only allocate if we are actually using the max trace */
2085#ifdef CONFIG_TRACER_MAX_TRACE
2086 array = (void *)__get_free_page(GFP_KERNEL);
2087 if (array == NULL) {
2088 printk(KERN_ERR "tracer: failed to allocate page"
2089 "for trace buffer!\n");
2090 goto free_pages;
2091 }
2092 page = virt_to_page(array);
2093 list_add(&page->lru, &pages);
2094#endif
2095 }
2096
2097 /* Now that we successfully allocate a page per CPU, add them */
2098 for_each_possible_cpu(i) {
2099 data = global_trace.data[i];
b3806b43 2100 spin_lock_init(&data->lock);
d4c5a2f5 2101 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2102 page = list_entry(pages.next, struct page, lru);
c7aafc54 2103 list_del_init(&page->lru);
4c11d7ae
SR
2104 list_add_tail(&page->lru, &data->trace_pages);
2105 ClearPageLRU(page);
2106
2107#ifdef CONFIG_TRACER_MAX_TRACE
2108 data = max_tr.data[i];
b3806b43 2109 spin_lock_init(&data->lock);
d4c5a2f5 2110 lockdep_set_class(&data->lock, &data->lock_key);
4c11d7ae 2111 page = list_entry(pages.next, struct page, lru);
c7aafc54 2112 list_del_init(&page->lru);
4c11d7ae
SR
2113 list_add_tail(&page->lru, &data->trace_pages);
2114 SetPageLRU(page);
2115#endif
2116 }
2117 global_trace.entries += ENTRIES_PER_PAGE;
2118
2119 return 0;
2120
2121 free_pages:
2122 list_for_each_entry_safe(page, tmp, &pages, lru) {
c7aafc54 2123 list_del_init(&page->lru);
4c11d7ae
SR
2124 __free_page(page);
2125 }
2126 return -ENOMEM;
bc0c38d1
SR
2127}
2128
2129__init static int tracer_alloc_buffers(void)
2130{
4c11d7ae
SR
2131 struct trace_array_cpu *data;
2132 void *array;
2133 struct page *page;
2134 int pages = 0;
60a11774 2135 int ret = -ENOMEM;
bc0c38d1
SR
2136 int i;
2137
4c11d7ae 2138 /* Allocate the first page for all buffers */
bc0c38d1 2139 for_each_possible_cpu(i) {
4c11d7ae 2140 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
bc0c38d1
SR
2141 max_tr.data[i] = &per_cpu(max_data, i);
2142
4c11d7ae 2143 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2144 if (array == NULL) {
4c11d7ae
SR
2145 printk(KERN_ERR "tracer: failed to allocate page"
2146 "for trace buffer!\n");
bc0c38d1
SR
2147 goto free_buffers;
2148 }
4c11d7ae
SR
2149
2150 /* set the array to the list */
2151 INIT_LIST_HEAD(&data->trace_pages);
2152 page = virt_to_page(array);
2153 list_add(&page->lru, &data->trace_pages);
2154 /* use the LRU flag to differentiate the two buffers */
2155 ClearPageLRU(page);
bc0c38d1
SR
2156
2157/* Only allocate if we are actually using the max trace */
2158#ifdef CONFIG_TRACER_MAX_TRACE
4c11d7ae 2159 array = (void *)__get_free_page(GFP_KERNEL);
bc0c38d1 2160 if (array == NULL) {
4c11d7ae
SR
2161 printk(KERN_ERR "tracer: failed to allocate page"
2162 "for trace buffer!\n");
bc0c38d1
SR
2163 goto free_buffers;
2164 }
4c11d7ae
SR
2165
2166 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2167 page = virt_to_page(array);
2168 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2169 SetPageLRU(page);
bc0c38d1
SR
2170#endif
2171 }
2172
2173 /*
2174 * Since we allocate by orders of pages, we may be able to
2175 * round up a bit.
2176 */
4c11d7ae 2177 global_trace.entries = ENTRIES_PER_PAGE;
4c11d7ae
SR
2178 pages++;
2179
2180 while (global_trace.entries < trace_nr_entries) {
2181 if (trace_alloc_page())
2182 break;
2183 pages++;
2184 }
89b2f978 2185 max_tr.entries = global_trace.entries;
bc0c38d1 2186
4c11d7ae
SR
2187 pr_info("tracer: %d pages allocated for %ld",
2188 pages, trace_nr_entries);
bc0c38d1
SR
2189 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2190 pr_info(" actual entries %ld\n", global_trace.entries);
2191
2192 tracer_init_debugfs();
2193
2194 trace_init_cmdlines();
2195
2196 register_tracer(&no_tracer);
2197 current_trace = &no_tracer;
2198
60a11774
SR
2199 /* All seems OK, enable tracing */
2200 tracing_disabled = 0;
2201
bc0c38d1
SR
2202 return 0;
2203
2204 free_buffers:
2205 for (i-- ; i >= 0; i--) {
4c11d7ae 2206 struct page *page, *tmp;
bc0c38d1
SR
2207 struct trace_array_cpu *data = global_trace.data[i];
2208
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
2217#ifdef CONFIG_TRACER_MAX_TRACE
2218 data = max_tr.data[i];
c7aafc54 2219 if (data) {
4c11d7ae
SR
2220 list_for_each_entry_safe(page, tmp,
2221 &data->trace_pages, lru) {
c7aafc54 2222 list_del_init(&page->lru);
4c11d7ae
SR
2223 __free_page(page);
2224 }
bc0c38d1
SR
2225 }
2226#endif
2227 }
60a11774 2228 return ret;
bc0c38d1 2229}
60a11774 2230fs_initcall(tracer_alloc_buffers);
This page took 0.120552 seconds and 5 git commands to generate.