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