tracing: Have preempt(irqs)off trace preempt disabled functions
[pandora-kernel.git] / kernel / trace / trace_irqsoff.c
1 /*
2  * trace irqs off critical timings
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * From code in the latency_tracer, that is:
8  *
9  *  Copyright (C) 2004-2006 Ingo Molnar
10  *  Copyright (C) 2004 William Lee Irwin III
11  */
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>
18
19 #include "trace.h"
20
21 static struct trace_array               *irqsoff_trace __read_mostly;
22 static int                              tracer_enabled __read_mostly;
23
24 static DEFINE_PER_CPU(int, tracing_cpu);
25
26 static DEFINE_RAW_SPINLOCK(max_trace_lock);
27
28 enum {
29         TRACER_IRQS_OFF         = (1 << 1),
30         TRACER_PREEMPT_OFF      = (1 << 2),
31 };
32
33 static int trace_type __read_mostly;
34
35 static int save_flags;
36
37 static void stop_irqsoff_tracer(struct trace_array *tr, int graph);
38 static int start_irqsoff_tracer(struct trace_array *tr, int graph);
39
40 #ifdef CONFIG_PREEMPT_TRACER
41 static inline int
42 preempt_trace(void)
43 {
44         return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
45 }
46 #else
47 # define preempt_trace() (0)
48 #endif
49
50 #ifdef CONFIG_IRQSOFF_TRACER
51 static inline int
52 irq_trace(void)
53 {
54         return ((trace_type & TRACER_IRQS_OFF) &&
55                 irqs_disabled());
56 }
57 #else
58 # define irq_trace() (0)
59 #endif
60
61 #define TRACE_DISPLAY_GRAPH     1
62
63 static struct tracer_opt trace_opts[] = {
64 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
65         /* display latency trace as call graph */
66         { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
67 #endif
68         { } /* Empty entry */
69 };
70
71 static struct tracer_flags tracer_flags = {
72         .val  = 0,
73         .opts = trace_opts,
74 };
75
76 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
77
78 /*
79  * Sequence count - we record it when starting a measurement and
80  * skip the latency if the sequence has changed - some other section
81  * did a maximum and could disturb our measurement with serial console
82  * printouts, etc. Truly coinciding maximum latencies should be rare
83  * and what happens together happens separately as well, so this doesn't
84  * decrease the validity of the maximum found:
85  */
86 static __cacheline_aligned_in_smp       unsigned long max_sequence;
87
88 #ifdef CONFIG_FUNCTION_TRACER
89 /*
90  * Prologue for the preempt and irqs off function tracers.
91  *
92  * Returns 1 if it is OK to continue, and data->disabled is
93  *            incremented.
94  *         0 if the trace is to be ignored, and data->disabled
95  *            is kept the same.
96  *
97  * Note, this function is also used outside this ifdef but
98  *  inside the #ifdef of the function graph tracer below.
99  *  This is OK, since the function graph tracer is
100  *  dependent on the function tracer.
101  */
102 static int func_prolog_dec(struct trace_array *tr,
103                            struct trace_array_cpu **data,
104                            unsigned long *flags)
105 {
106         long disabled;
107         int cpu;
108
109         /*
110          * Does not matter if we preempt. We test the flags
111          * afterward, to see if irqs are disabled or not.
112          * If we preempt and get a false positive, the flags
113          * test will fail.
114          */
115         cpu = raw_smp_processor_id();
116         if (likely(!per_cpu(tracing_cpu, cpu)))
117                 return 0;
118
119         local_save_flags(*flags);
120         /*
121          * Slight chance to get a false positive on tracing_cpu,
122          * although I'm starting to think there isn't a chance.
123          * Leave this for now just to be paranoid.
124          */
125         if (!irqs_disabled_flags(*flags) && !preempt_count())
126                 return 0;
127
128         *data = tr->data[cpu];
129         disabled = atomic_inc_return(&(*data)->disabled);
130
131         if (likely(disabled == 1))
132                 return 1;
133
134         atomic_dec(&(*data)->disabled);
135
136         return 0;
137 }
138
139 /*
140  * irqsoff uses its own tracer function to keep the overhead down:
141  */
142 static void
143 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
144 {
145         struct trace_array *tr = irqsoff_trace;
146         struct trace_array_cpu *data;
147         unsigned long flags;
148
149         if (!func_prolog_dec(tr, &data, &flags))
150                 return;
151
152         trace_function(tr, ip, parent_ip, flags, preempt_count());
153
154         atomic_dec(&data->disabled);
155 }
156
157 static struct ftrace_ops trace_ops __read_mostly =
158 {
159         .func = irqsoff_tracer_call,
160         .flags = FTRACE_OPS_FL_GLOBAL,
161 };
162 #endif /* CONFIG_FUNCTION_TRACER */
163
164 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
165 static int irqsoff_set_flag(u32 old_flags, u32 bit, int set)
166 {
167         int cpu;
168
169         if (!(bit & TRACE_DISPLAY_GRAPH))
170                 return -EINVAL;
171
172         if (!(is_graph() ^ set))
173                 return 0;
174
175         stop_irqsoff_tracer(irqsoff_trace, !set);
176
177         for_each_possible_cpu(cpu)
178                 per_cpu(tracing_cpu, cpu) = 0;
179
180         tracing_max_latency = 0;
181         tracing_reset_online_cpus(irqsoff_trace);
182
183         return start_irqsoff_tracer(irqsoff_trace, set);
184 }
185
186 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
187 {
188         struct trace_array *tr = irqsoff_trace;
189         struct trace_array_cpu *data;
190         unsigned long flags;
191         int ret;
192         int pc;
193
194         if (!func_prolog_dec(tr, &data, &flags))
195                 return 0;
196
197         pc = preempt_count();
198         ret = __trace_graph_entry(tr, trace, flags, pc);
199         atomic_dec(&data->disabled);
200
201         return ret;
202 }
203
204 static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
205 {
206         struct trace_array *tr = irqsoff_trace;
207         struct trace_array_cpu *data;
208         unsigned long flags;
209         int pc;
210
211         if (!func_prolog_dec(tr, &data, &flags))
212                 return;
213
214         pc = preempt_count();
215         __trace_graph_return(tr, trace, flags, pc);
216         atomic_dec(&data->disabled);
217 }
218
219 static void irqsoff_trace_open(struct trace_iterator *iter)
220 {
221         if (is_graph())
222                 graph_trace_open(iter);
223
224 }
225
226 static void irqsoff_trace_close(struct trace_iterator *iter)
227 {
228         if (iter->private)
229                 graph_trace_close(iter);
230 }
231
232 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
233                             TRACE_GRAPH_PRINT_PROC | \
234                             TRACE_GRAPH_PRINT_ABS_TIME | \
235                             TRACE_GRAPH_PRINT_DURATION)
236
237 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
238 {
239         /*
240          * In graph mode call the graph tracer output function,
241          * otherwise go with the TRACE_FN event handler
242          */
243         if (is_graph())
244                 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
245
246         return TRACE_TYPE_UNHANDLED;
247 }
248
249 static void irqsoff_print_header(struct seq_file *s)
250 {
251         if (is_graph())
252                 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
253         else
254                 trace_default_header(s);
255 }
256
257 static void
258 __trace_function(struct trace_array *tr,
259                  unsigned long ip, unsigned long parent_ip,
260                  unsigned long flags, int pc)
261 {
262         if (is_graph())
263                 trace_graph_function(tr, ip, parent_ip, flags, pc);
264         else
265                 trace_function(tr, ip, parent_ip, flags, pc);
266 }
267
268 #else
269 #define __trace_function trace_function
270
271 static int irqsoff_set_flag(u32 old_flags, u32 bit, int set)
272 {
273         return -EINVAL;
274 }
275
276 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
277 {
278         return -1;
279 }
280
281 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
282 {
283         return TRACE_TYPE_UNHANDLED;
284 }
285
286 static void irqsoff_graph_return(struct ftrace_graph_ret *trace) { }
287 static void irqsoff_print_header(struct seq_file *s) { }
288 static void irqsoff_trace_open(struct trace_iterator *iter) { }
289 static void irqsoff_trace_close(struct trace_iterator *iter) { }
290 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
291
292 /*
293  * Should this new latency be reported/recorded?
294  */
295 static int report_latency(cycle_t delta)
296 {
297         if (tracing_thresh) {
298                 if (delta < tracing_thresh)
299                         return 0;
300         } else {
301                 if (delta <= tracing_max_latency)
302                         return 0;
303         }
304         return 1;
305 }
306
307 static void
308 check_critical_timing(struct trace_array *tr,
309                       struct trace_array_cpu *data,
310                       unsigned long parent_ip,
311                       int cpu)
312 {
313         cycle_t T0, T1, delta;
314         unsigned long flags;
315         int pc;
316
317         T0 = data->preempt_timestamp;
318         T1 = ftrace_now(cpu);
319         delta = T1-T0;
320
321         local_save_flags(flags);
322
323         pc = preempt_count();
324
325         if (!report_latency(delta))
326                 goto out;
327
328         raw_spin_lock_irqsave(&max_trace_lock, flags);
329
330         /* check if we are still the max latency */
331         if (!report_latency(delta))
332                 goto out_unlock;
333
334         __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
335         /* Skip 5 functions to get to the irq/preempt enable function */
336         __trace_stack(tr, flags, 5, pc);
337
338         if (data->critical_sequence != max_sequence)
339                 goto out_unlock;
340
341         data->critical_end = parent_ip;
342
343         if (likely(!is_tracing_stopped())) {
344                 tracing_max_latency = delta;
345                 update_max_tr_single(tr, current, cpu);
346         }
347
348         max_sequence++;
349
350 out_unlock:
351         raw_spin_unlock_irqrestore(&max_trace_lock, flags);
352
353 out:
354         data->critical_sequence = max_sequence;
355         data->preempt_timestamp = ftrace_now(cpu);
356         __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
357 }
358
359 static inline void
360 start_critical_timing(unsigned long ip, unsigned long parent_ip)
361 {
362         int cpu;
363         struct trace_array *tr = irqsoff_trace;
364         struct trace_array_cpu *data;
365         unsigned long flags;
366
367         if (likely(!tracer_enabled))
368                 return;
369
370         cpu = raw_smp_processor_id();
371
372         if (per_cpu(tracing_cpu, cpu))
373                 return;
374
375         data = tr->data[cpu];
376
377         if (unlikely(!data) || atomic_read(&data->disabled))
378                 return;
379
380         atomic_inc(&data->disabled);
381
382         data->critical_sequence = max_sequence;
383         data->preempt_timestamp = ftrace_now(cpu);
384         data->critical_start = parent_ip ? : ip;
385
386         local_save_flags(flags);
387
388         __trace_function(tr, ip, parent_ip, flags, preempt_count());
389
390         per_cpu(tracing_cpu, cpu) = 1;
391
392         atomic_dec(&data->disabled);
393 }
394
395 static inline void
396 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
397 {
398         int cpu;
399         struct trace_array *tr = irqsoff_trace;
400         struct trace_array_cpu *data;
401         unsigned long flags;
402
403         cpu = raw_smp_processor_id();
404         /* Always clear the tracing cpu on stopping the trace */
405         if (unlikely(per_cpu(tracing_cpu, cpu)))
406                 per_cpu(tracing_cpu, cpu) = 0;
407         else
408                 return;
409
410         if (!tracer_enabled)
411                 return;
412
413         data = tr->data[cpu];
414
415         if (unlikely(!data) ||
416             !data->critical_start || atomic_read(&data->disabled))
417                 return;
418
419         atomic_inc(&data->disabled);
420
421         local_save_flags(flags);
422         __trace_function(tr, ip, parent_ip, flags, preempt_count());
423         check_critical_timing(tr, data, parent_ip ? : ip, cpu);
424         data->critical_start = 0;
425         atomic_dec(&data->disabled);
426 }
427
428 /* start and stop critical timings used to for stoppage (in idle) */
429 void start_critical_timings(void)
430 {
431         if (preempt_trace() || irq_trace())
432                 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
433 }
434 EXPORT_SYMBOL_GPL(start_critical_timings);
435
436 void stop_critical_timings(void)
437 {
438         if (preempt_trace() || irq_trace())
439                 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
440 }
441 EXPORT_SYMBOL_GPL(stop_critical_timings);
442
443 #ifdef CONFIG_IRQSOFF_TRACER
444 #ifdef CONFIG_PROVE_LOCKING
445 void time_hardirqs_on(unsigned long a0, unsigned long a1)
446 {
447         if (!preempt_trace() && irq_trace())
448                 stop_critical_timing(a0, a1);
449 }
450
451 void time_hardirqs_off(unsigned long a0, unsigned long a1)
452 {
453         if (!preempt_trace() && irq_trace())
454                 start_critical_timing(a0, a1);
455 }
456
457 #else /* !CONFIG_PROVE_LOCKING */
458
459 /*
460  * Stubs:
461  */
462
463 void trace_softirqs_on(unsigned long ip)
464 {
465 }
466
467 void trace_softirqs_off(unsigned long ip)
468 {
469 }
470
471 inline void print_irqtrace_events(struct task_struct *curr)
472 {
473 }
474
475 /*
476  * We are only interested in hardirq on/off events:
477  */
478 void trace_hardirqs_on(void)
479 {
480         if (!preempt_trace() && irq_trace())
481                 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
482 }
483 EXPORT_SYMBOL(trace_hardirqs_on);
484
485 void trace_hardirqs_off(void)
486 {
487         if (!preempt_trace() && irq_trace())
488                 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
489 }
490 EXPORT_SYMBOL(trace_hardirqs_off);
491
492 void trace_hardirqs_on_caller(unsigned long caller_addr)
493 {
494         if (!preempt_trace() && irq_trace())
495                 stop_critical_timing(CALLER_ADDR0, caller_addr);
496 }
497 EXPORT_SYMBOL(trace_hardirqs_on_caller);
498
499 void trace_hardirqs_off_caller(unsigned long caller_addr)
500 {
501         if (!preempt_trace() && irq_trace())
502                 start_critical_timing(CALLER_ADDR0, caller_addr);
503 }
504 EXPORT_SYMBOL(trace_hardirqs_off_caller);
505
506 #endif /* CONFIG_PROVE_LOCKING */
507 #endif /*  CONFIG_IRQSOFF_TRACER */
508
509 #ifdef CONFIG_PREEMPT_TRACER
510 void trace_preempt_on(unsigned long a0, unsigned long a1)
511 {
512         if (preempt_trace() && !irq_trace())
513                 stop_critical_timing(a0, a1);
514 }
515
516 void trace_preempt_off(unsigned long a0, unsigned long a1)
517 {
518         if (preempt_trace() && !irq_trace())
519                 start_critical_timing(a0, a1);
520 }
521 #endif /* CONFIG_PREEMPT_TRACER */
522
523 static int start_irqsoff_tracer(struct trace_array *tr, int graph)
524 {
525         int ret = 0;
526
527         if (!graph)
528                 ret = register_ftrace_function(&trace_ops);
529         else
530                 ret = register_ftrace_graph(&irqsoff_graph_return,
531                                             &irqsoff_graph_entry);
532
533         if (!ret && tracing_is_enabled())
534                 tracer_enabled = 1;
535         else
536                 tracer_enabled = 0;
537
538         return ret;
539 }
540
541 static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
542 {
543         tracer_enabled = 0;
544
545         if (!graph)
546                 unregister_ftrace_function(&trace_ops);
547         else
548                 unregister_ftrace_graph();
549 }
550
551 static void __irqsoff_tracer_init(struct trace_array *tr)
552 {
553         save_flags = trace_flags;
554
555         /* non overwrite screws up the latency tracers */
556         set_tracer_flag(TRACE_ITER_OVERWRITE, 1);
557         set_tracer_flag(TRACE_ITER_LATENCY_FMT, 1);
558
559         tracing_max_latency = 0;
560         irqsoff_trace = tr;
561         /* make sure that the tracer is visible */
562         smp_wmb();
563         tracing_reset_online_cpus(tr);
564
565         if (start_irqsoff_tracer(tr, is_graph()))
566                 printk(KERN_ERR "failed to start irqsoff tracer\n");
567 }
568
569 static void irqsoff_tracer_reset(struct trace_array *tr)
570 {
571         int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
572         int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
573
574         stop_irqsoff_tracer(tr, is_graph());
575
576         set_tracer_flag(TRACE_ITER_LATENCY_FMT, lat_flag);
577         set_tracer_flag(TRACE_ITER_OVERWRITE, overwrite_flag);
578 }
579
580 static void irqsoff_tracer_start(struct trace_array *tr)
581 {
582         tracer_enabled = 1;
583 }
584
585 static void irqsoff_tracer_stop(struct trace_array *tr)
586 {
587         tracer_enabled = 0;
588 }
589
590 #ifdef CONFIG_IRQSOFF_TRACER
591 static int irqsoff_tracer_init(struct trace_array *tr)
592 {
593         trace_type = TRACER_IRQS_OFF;
594
595         __irqsoff_tracer_init(tr);
596         return 0;
597 }
598 static struct tracer irqsoff_tracer __read_mostly =
599 {
600         .name           = "irqsoff",
601         .init           = irqsoff_tracer_init,
602         .reset          = irqsoff_tracer_reset,
603         .start          = irqsoff_tracer_start,
604         .stop           = irqsoff_tracer_stop,
605         .print_max      = 1,
606         .print_header   = irqsoff_print_header,
607         .print_line     = irqsoff_print_line,
608         .flags          = &tracer_flags,
609         .set_flag       = irqsoff_set_flag,
610         .flag_changed   = trace_keep_overwrite,
611 #ifdef CONFIG_FTRACE_SELFTEST
612         .selftest    = trace_selftest_startup_irqsoff,
613 #endif
614         .open           = irqsoff_trace_open,
615         .close          = irqsoff_trace_close,
616         .use_max_tr     = 1,
617 };
618 # define register_irqsoff(trace) register_tracer(&trace)
619 #else
620 # define register_irqsoff(trace) do { } while (0)
621 #endif
622
623 #ifdef CONFIG_PREEMPT_TRACER
624 static int preemptoff_tracer_init(struct trace_array *tr)
625 {
626         trace_type = TRACER_PREEMPT_OFF;
627
628         __irqsoff_tracer_init(tr);
629         return 0;
630 }
631
632 static struct tracer preemptoff_tracer __read_mostly =
633 {
634         .name           = "preemptoff",
635         .init           = preemptoff_tracer_init,
636         .reset          = irqsoff_tracer_reset,
637         .start          = irqsoff_tracer_start,
638         .stop           = irqsoff_tracer_stop,
639         .print_max      = 1,
640         .print_header   = irqsoff_print_header,
641         .print_line     = irqsoff_print_line,
642         .flags          = &tracer_flags,
643         .set_flag       = irqsoff_set_flag,
644         .flag_changed   = trace_keep_overwrite,
645 #ifdef CONFIG_FTRACE_SELFTEST
646         .selftest    = trace_selftest_startup_preemptoff,
647 #endif
648         .open           = irqsoff_trace_open,
649         .close          = irqsoff_trace_close,
650         .use_max_tr     = 1,
651 };
652 # define register_preemptoff(trace) register_tracer(&trace)
653 #else
654 # define register_preemptoff(trace) do { } while (0)
655 #endif
656
657 #if defined(CONFIG_IRQSOFF_TRACER) && \
658         defined(CONFIG_PREEMPT_TRACER)
659
660 static int preemptirqsoff_tracer_init(struct trace_array *tr)
661 {
662         trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
663
664         __irqsoff_tracer_init(tr);
665         return 0;
666 }
667
668 static struct tracer preemptirqsoff_tracer __read_mostly =
669 {
670         .name           = "preemptirqsoff",
671         .init           = preemptirqsoff_tracer_init,
672         .reset          = irqsoff_tracer_reset,
673         .start          = irqsoff_tracer_start,
674         .stop           = irqsoff_tracer_stop,
675         .print_max      = 1,
676         .print_header   = irqsoff_print_header,
677         .print_line     = irqsoff_print_line,
678         .flags          = &tracer_flags,
679         .set_flag       = irqsoff_set_flag,
680         .flag_changed   = trace_keep_overwrite,
681 #ifdef CONFIG_FTRACE_SELFTEST
682         .selftest    = trace_selftest_startup_preemptirqsoff,
683 #endif
684         .open           = irqsoff_trace_open,
685         .close          = irqsoff_trace_close,
686         .use_max_tr     = 1,
687 };
688
689 # define register_preemptirqsoff(trace) register_tracer(&trace)
690 #else
691 # define register_preemptirqsoff(trace) do { } while (0)
692 #endif
693
694 __init static int init_irqsoff_tracer(void)
695 {
696         register_irqsoff(irqsoff_tracer);
697         register_preemptoff(preemptoff_tracer);
698         register_preemptirqsoff(preemptirqsoff_tracer);
699
700         return 0;
701 }
702 device_initcall(init_irqsoff_tracer);