[media] media: vb2: Share code between vb2_prepare_buf and vb2_qbuf
[linux-2.6.git] / kernel / trace / trace_irqsoff.c
blob2aefbee93a6d574a0ea082632788a1d5c7791474
1 /*
2 * trace irqs off critical timings
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * From code in the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 Nadia Yvette Chambers
12 #include <linux/kallsyms.h>
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/ftrace.h>
17 #include <linux/fs.h>
19 #include "trace.h"
21 static struct trace_array *irqsoff_trace __read_mostly;
22 static int tracer_enabled __read_mostly;
24 static DEFINE_PER_CPU(int, tracing_cpu);
26 static DEFINE_RAW_SPINLOCK(max_trace_lock);
28 enum {
29 TRACER_IRQS_OFF = (1 << 1),
30 TRACER_PREEMPT_OFF = (1 << 2),
33 static int trace_type __read_mostly;
35 static int save_flags;
36 static bool function_enabled;
38 static void stop_irqsoff_tracer(struct trace_array *tr, int graph);
39 static int start_irqsoff_tracer(struct trace_array *tr, int graph);
41 #ifdef CONFIG_PREEMPT_TRACER
42 static inline int
43 preempt_trace(void)
45 return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
47 #else
48 # define preempt_trace() (0)
49 #endif
51 #ifdef CONFIG_IRQSOFF_TRACER
52 static inline int
53 irq_trace(void)
55 return ((trace_type & TRACER_IRQS_OFF) &&
56 irqs_disabled());
58 #else
59 # define irq_trace() (0)
60 #endif
62 #define TRACE_DISPLAY_GRAPH 1
64 static struct tracer_opt trace_opts[] = {
65 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
66 /* display latency trace as call graph */
67 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
68 #endif
69 { } /* Empty entry */
72 static struct tracer_flags tracer_flags = {
73 .val = 0,
74 .opts = trace_opts,
77 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
80 * Sequence count - we record it when starting a measurement and
81 * skip the latency if the sequence has changed - some other section
82 * did a maximum and could disturb our measurement with serial console
83 * printouts, etc. Truly coinciding maximum latencies should be rare
84 * and what happens together happens separately as well, so this doesn't
85 * decrease the validity of the maximum found:
87 static __cacheline_aligned_in_smp unsigned long max_sequence;
89 #ifdef CONFIG_FUNCTION_TRACER
91 * Prologue for the preempt and irqs off function tracers.
93 * Returns 1 if it is OK to continue, and data->disabled is
94 * incremented.
95 * 0 if the trace is to be ignored, and data->disabled
96 * is kept the same.
98 * Note, this function is also used outside this ifdef but
99 * inside the #ifdef of the function graph tracer below.
100 * This is OK, since the function graph tracer is
101 * dependent on the function tracer.
103 static int func_prolog_dec(struct trace_array *tr,
104 struct trace_array_cpu **data,
105 unsigned long *flags)
107 long disabled;
108 int cpu;
111 * Does not matter if we preempt. We test the flags
112 * afterward, to see if irqs are disabled or not.
113 * If we preempt and get a false positive, the flags
114 * test will fail.
116 cpu = raw_smp_processor_id();
117 if (likely(!per_cpu(tracing_cpu, cpu)))
118 return 0;
120 local_save_flags(*flags);
121 /* slight chance to get a false positive on tracing_cpu */
122 if (!irqs_disabled_flags(*flags))
123 return 0;
125 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
126 disabled = atomic_inc_return(&(*data)->disabled);
128 if (likely(disabled == 1))
129 return 1;
131 atomic_dec(&(*data)->disabled);
133 return 0;
137 * irqsoff uses its own tracer function to keep the overhead down:
139 static void
140 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip,
141 struct ftrace_ops *op, struct pt_regs *pt_regs)
143 struct trace_array *tr = irqsoff_trace;
144 struct trace_array_cpu *data;
145 unsigned long flags;
147 if (!func_prolog_dec(tr, &data, &flags))
148 return;
150 trace_function(tr, ip, parent_ip, flags, preempt_count());
152 atomic_dec(&data->disabled);
155 static struct ftrace_ops trace_ops __read_mostly =
157 .func = irqsoff_tracer_call,
158 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
160 #endif /* CONFIG_FUNCTION_TRACER */
162 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
163 static int irqsoff_set_flag(u32 old_flags, u32 bit, int set)
165 int cpu;
167 if (!(bit & TRACE_DISPLAY_GRAPH))
168 return -EINVAL;
170 if (!(is_graph() ^ set))
171 return 0;
173 stop_irqsoff_tracer(irqsoff_trace, !set);
175 for_each_possible_cpu(cpu)
176 per_cpu(tracing_cpu, cpu) = 0;
178 tracing_max_latency = 0;
179 tracing_reset_online_cpus(&irqsoff_trace->trace_buffer);
181 return start_irqsoff_tracer(irqsoff_trace, set);
184 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
186 struct trace_array *tr = irqsoff_trace;
187 struct trace_array_cpu *data;
188 unsigned long flags;
189 int ret;
190 int pc;
192 if (!func_prolog_dec(tr, &data, &flags))
193 return 0;
195 pc = preempt_count();
196 ret = __trace_graph_entry(tr, trace, flags, pc);
197 atomic_dec(&data->disabled);
199 return ret;
202 static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
204 struct trace_array *tr = irqsoff_trace;
205 struct trace_array_cpu *data;
206 unsigned long flags;
207 int pc;
209 if (!func_prolog_dec(tr, &data, &flags))
210 return;
212 pc = preempt_count();
213 __trace_graph_return(tr, trace, flags, pc);
214 atomic_dec(&data->disabled);
217 static void irqsoff_trace_open(struct trace_iterator *iter)
219 if (is_graph())
220 graph_trace_open(iter);
224 static void irqsoff_trace_close(struct trace_iterator *iter)
226 if (iter->private)
227 graph_trace_close(iter);
230 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
231 TRACE_GRAPH_PRINT_PROC | \
232 TRACE_GRAPH_PRINT_ABS_TIME | \
233 TRACE_GRAPH_PRINT_DURATION)
235 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
238 * In graph mode call the graph tracer output function,
239 * otherwise go with the TRACE_FN event handler
241 if (is_graph())
242 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
244 return TRACE_TYPE_UNHANDLED;
247 static void irqsoff_print_header(struct seq_file *s)
249 if (is_graph())
250 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
251 else
252 trace_default_header(s);
255 static void
256 __trace_function(struct trace_array *tr,
257 unsigned long ip, unsigned long parent_ip,
258 unsigned long flags, int pc)
260 if (is_graph())
261 trace_graph_function(tr, ip, parent_ip, flags, pc);
262 else
263 trace_function(tr, ip, parent_ip, flags, pc);
266 #else
267 #define __trace_function trace_function
269 static int irqsoff_set_flag(u32 old_flags, u32 bit, int set)
271 return -EINVAL;
274 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
276 return -1;
279 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
281 return TRACE_TYPE_UNHANDLED;
284 static void irqsoff_graph_return(struct ftrace_graph_ret *trace) { }
285 static void irqsoff_trace_open(struct trace_iterator *iter) { }
286 static void irqsoff_trace_close(struct trace_iterator *iter) { }
288 #ifdef CONFIG_FUNCTION_TRACER
289 static void irqsoff_print_header(struct seq_file *s)
291 trace_default_header(s);
293 #else
294 static void irqsoff_print_header(struct seq_file *s)
296 trace_latency_header(s);
298 #endif /* CONFIG_FUNCTION_TRACER */
299 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
302 * Should this new latency be reported/recorded?
304 static int report_latency(cycle_t delta)
306 if (tracing_thresh) {
307 if (delta < tracing_thresh)
308 return 0;
309 } else {
310 if (delta <= tracing_max_latency)
311 return 0;
313 return 1;
316 static void
317 check_critical_timing(struct trace_array *tr,
318 struct trace_array_cpu *data,
319 unsigned long parent_ip,
320 int cpu)
322 cycle_t T0, T1, delta;
323 unsigned long flags;
324 int pc;
326 T0 = data->preempt_timestamp;
327 T1 = ftrace_now(cpu);
328 delta = T1-T0;
330 local_save_flags(flags);
332 pc = preempt_count();
334 if (!report_latency(delta))
335 goto out;
337 raw_spin_lock_irqsave(&max_trace_lock, flags);
339 /* check if we are still the max latency */
340 if (!report_latency(delta))
341 goto out_unlock;
343 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
344 /* Skip 5 functions to get to the irq/preempt enable function */
345 __trace_stack(tr, flags, 5, pc);
347 if (data->critical_sequence != max_sequence)
348 goto out_unlock;
350 data->critical_end = parent_ip;
352 if (likely(!is_tracing_stopped())) {
353 tracing_max_latency = delta;
354 update_max_tr_single(tr, current, cpu);
357 max_sequence++;
359 out_unlock:
360 raw_spin_unlock_irqrestore(&max_trace_lock, flags);
362 out:
363 data->critical_sequence = max_sequence;
364 data->preempt_timestamp = ftrace_now(cpu);
365 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
368 static inline void
369 start_critical_timing(unsigned long ip, unsigned long parent_ip)
371 int cpu;
372 struct trace_array *tr = irqsoff_trace;
373 struct trace_array_cpu *data;
374 unsigned long flags;
376 if (!tracer_enabled || !tracing_is_enabled())
377 return;
379 cpu = raw_smp_processor_id();
381 if (per_cpu(tracing_cpu, cpu))
382 return;
384 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
386 if (unlikely(!data) || atomic_read(&data->disabled))
387 return;
389 atomic_inc(&data->disabled);
391 data->critical_sequence = max_sequence;
392 data->preempt_timestamp = ftrace_now(cpu);
393 data->critical_start = parent_ip ? : ip;
395 local_save_flags(flags);
397 __trace_function(tr, ip, parent_ip, flags, preempt_count());
399 per_cpu(tracing_cpu, cpu) = 1;
401 atomic_dec(&data->disabled);
404 static inline void
405 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
407 int cpu;
408 struct trace_array *tr = irqsoff_trace;
409 struct trace_array_cpu *data;
410 unsigned long flags;
412 cpu = raw_smp_processor_id();
413 /* Always clear the tracing cpu on stopping the trace */
414 if (unlikely(per_cpu(tracing_cpu, cpu)))
415 per_cpu(tracing_cpu, cpu) = 0;
416 else
417 return;
419 if (!tracer_enabled || !tracing_is_enabled())
420 return;
422 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
424 if (unlikely(!data) ||
425 !data->critical_start || atomic_read(&data->disabled))
426 return;
428 atomic_inc(&data->disabled);
430 local_save_flags(flags);
431 __trace_function(tr, ip, parent_ip, flags, preempt_count());
432 check_critical_timing(tr, data, parent_ip ? : ip, cpu);
433 data->critical_start = 0;
434 atomic_dec(&data->disabled);
437 /* start and stop critical timings used to for stoppage (in idle) */
438 void start_critical_timings(void)
440 if (preempt_trace() || irq_trace())
441 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
443 EXPORT_SYMBOL_GPL(start_critical_timings);
445 void stop_critical_timings(void)
447 if (preempt_trace() || irq_trace())
448 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
450 EXPORT_SYMBOL_GPL(stop_critical_timings);
452 #ifdef CONFIG_IRQSOFF_TRACER
453 #ifdef CONFIG_PROVE_LOCKING
454 void time_hardirqs_on(unsigned long a0, unsigned long a1)
456 if (!preempt_trace() && irq_trace())
457 stop_critical_timing(a0, a1);
460 void time_hardirqs_off(unsigned long a0, unsigned long a1)
462 if (!preempt_trace() && irq_trace())
463 start_critical_timing(a0, a1);
466 #else /* !CONFIG_PROVE_LOCKING */
469 * Stubs:
472 void trace_softirqs_on(unsigned long ip)
476 void trace_softirqs_off(unsigned long ip)
480 inline void print_irqtrace_events(struct task_struct *curr)
485 * We are only interested in hardirq on/off events:
487 void trace_hardirqs_on(void)
489 if (!preempt_trace() && irq_trace())
490 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
492 EXPORT_SYMBOL(trace_hardirqs_on);
494 void trace_hardirqs_off(void)
496 if (!preempt_trace() && irq_trace())
497 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
499 EXPORT_SYMBOL(trace_hardirqs_off);
501 void trace_hardirqs_on_caller(unsigned long caller_addr)
503 if (!preempt_trace() && irq_trace())
504 stop_critical_timing(CALLER_ADDR0, caller_addr);
506 EXPORT_SYMBOL(trace_hardirqs_on_caller);
508 void trace_hardirqs_off_caller(unsigned long caller_addr)
510 if (!preempt_trace() && irq_trace())
511 start_critical_timing(CALLER_ADDR0, caller_addr);
513 EXPORT_SYMBOL(trace_hardirqs_off_caller);
515 #endif /* CONFIG_PROVE_LOCKING */
516 #endif /* CONFIG_IRQSOFF_TRACER */
518 #ifdef CONFIG_PREEMPT_TRACER
519 void trace_preempt_on(unsigned long a0, unsigned long a1)
521 if (preempt_trace() && !irq_trace())
522 stop_critical_timing(a0, a1);
525 void trace_preempt_off(unsigned long a0, unsigned long a1)
527 if (preempt_trace() && !irq_trace())
528 start_critical_timing(a0, a1);
530 #endif /* CONFIG_PREEMPT_TRACER */
532 static int register_irqsoff_function(int graph, int set)
534 int ret;
536 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
537 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
538 return 0;
540 if (graph)
541 ret = register_ftrace_graph(&irqsoff_graph_return,
542 &irqsoff_graph_entry);
543 else
544 ret = register_ftrace_function(&trace_ops);
546 if (!ret)
547 function_enabled = true;
549 return ret;
552 static void unregister_irqsoff_function(int graph)
554 if (!function_enabled)
555 return;
557 if (graph)
558 unregister_ftrace_graph();
559 else
560 unregister_ftrace_function(&trace_ops);
562 function_enabled = false;
565 static void irqsoff_function_set(int set)
567 if (set)
568 register_irqsoff_function(is_graph(), 1);
569 else
570 unregister_irqsoff_function(is_graph());
573 static int irqsoff_flag_changed(struct tracer *tracer, u32 mask, int set)
575 if (mask & TRACE_ITER_FUNCTION)
576 irqsoff_function_set(set);
578 return trace_keep_overwrite(tracer, mask, set);
581 static int start_irqsoff_tracer(struct trace_array *tr, int graph)
583 int ret;
585 ret = register_irqsoff_function(graph, 0);
587 if (!ret && tracing_is_enabled())
588 tracer_enabled = 1;
589 else
590 tracer_enabled = 0;
592 return ret;
595 static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
597 tracer_enabled = 0;
599 unregister_irqsoff_function(graph);
602 static void __irqsoff_tracer_init(struct trace_array *tr)
604 save_flags = trace_flags;
606 /* non overwrite screws up the latency tracers */
607 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
608 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
610 tracing_max_latency = 0;
611 irqsoff_trace = tr;
612 /* make sure that the tracer is visible */
613 smp_wmb();
614 tracing_reset_online_cpus(&tr->trace_buffer);
616 if (start_irqsoff_tracer(tr, is_graph()))
617 printk(KERN_ERR "failed to start irqsoff tracer\n");
620 static void irqsoff_tracer_reset(struct trace_array *tr)
622 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
623 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
625 stop_irqsoff_tracer(tr, is_graph());
627 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
628 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
631 static void irqsoff_tracer_start(struct trace_array *tr)
633 tracer_enabled = 1;
636 static void irqsoff_tracer_stop(struct trace_array *tr)
638 tracer_enabled = 0;
641 #ifdef CONFIG_IRQSOFF_TRACER
642 static int irqsoff_tracer_init(struct trace_array *tr)
644 trace_type = TRACER_IRQS_OFF;
646 __irqsoff_tracer_init(tr);
647 return 0;
649 static struct tracer irqsoff_tracer __read_mostly =
651 .name = "irqsoff",
652 .init = irqsoff_tracer_init,
653 .reset = irqsoff_tracer_reset,
654 .start = irqsoff_tracer_start,
655 .stop = irqsoff_tracer_stop,
656 .print_max = true,
657 .print_header = irqsoff_print_header,
658 .print_line = irqsoff_print_line,
659 .flags = &tracer_flags,
660 .set_flag = irqsoff_set_flag,
661 .flag_changed = irqsoff_flag_changed,
662 #ifdef CONFIG_FTRACE_SELFTEST
663 .selftest = trace_selftest_startup_irqsoff,
664 #endif
665 .open = irqsoff_trace_open,
666 .close = irqsoff_trace_close,
667 .use_max_tr = true,
669 # define register_irqsoff(trace) register_tracer(&trace)
670 #else
671 # define register_irqsoff(trace) do { } while (0)
672 #endif
674 #ifdef CONFIG_PREEMPT_TRACER
675 static int preemptoff_tracer_init(struct trace_array *tr)
677 trace_type = TRACER_PREEMPT_OFF;
679 __irqsoff_tracer_init(tr);
680 return 0;
683 static struct tracer preemptoff_tracer __read_mostly =
685 .name = "preemptoff",
686 .init = preemptoff_tracer_init,
687 .reset = irqsoff_tracer_reset,
688 .start = irqsoff_tracer_start,
689 .stop = irqsoff_tracer_stop,
690 .print_max = true,
691 .print_header = irqsoff_print_header,
692 .print_line = irqsoff_print_line,
693 .flags = &tracer_flags,
694 .set_flag = irqsoff_set_flag,
695 .flag_changed = irqsoff_flag_changed,
696 #ifdef CONFIG_FTRACE_SELFTEST
697 .selftest = trace_selftest_startup_preemptoff,
698 #endif
699 .open = irqsoff_trace_open,
700 .close = irqsoff_trace_close,
701 .use_max_tr = true,
703 # define register_preemptoff(trace) register_tracer(&trace)
704 #else
705 # define register_preemptoff(trace) do { } while (0)
706 #endif
708 #if defined(CONFIG_IRQSOFF_TRACER) && \
709 defined(CONFIG_PREEMPT_TRACER)
711 static int preemptirqsoff_tracer_init(struct trace_array *tr)
713 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
715 __irqsoff_tracer_init(tr);
716 return 0;
719 static struct tracer preemptirqsoff_tracer __read_mostly =
721 .name = "preemptirqsoff",
722 .init = preemptirqsoff_tracer_init,
723 .reset = irqsoff_tracer_reset,
724 .start = irqsoff_tracer_start,
725 .stop = irqsoff_tracer_stop,
726 .print_max = true,
727 .print_header = irqsoff_print_header,
728 .print_line = irqsoff_print_line,
729 .flags = &tracer_flags,
730 .set_flag = irqsoff_set_flag,
731 .flag_changed = irqsoff_flag_changed,
732 #ifdef CONFIG_FTRACE_SELFTEST
733 .selftest = trace_selftest_startup_preemptirqsoff,
734 #endif
735 .open = irqsoff_trace_open,
736 .close = irqsoff_trace_close,
737 .use_max_tr = true,
740 # define register_preemptirqsoff(trace) register_tracer(&trace)
741 #else
742 # define register_preemptirqsoff(trace) do { } while (0)
743 #endif
745 __init static int init_irqsoff_tracer(void)
747 register_irqsoff(irqsoff_tracer);
748 register_preemptoff(preemptoff_tracer);
749 register_preemptirqsoff(preemptirqsoff_tracer);
751 return 0;
753 core_initcall(init_irqsoff_tracer);