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