1 // SPDX-License-Identifier: GPL-2.0
3 * trace irqs off critical timings
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 * From code in the latency_tracer, that is:
10 * Copyright (C) 2004-2006 Ingo Molnar
11 * Copyright (C) 2004 Nadia Yvette Chambers
13 #include <linux/kallsyms.h>
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/ftrace.h>
20 #include <trace/events/preemptirq.h>
22 #if defined(CONFIG_IRQSOFF_TRACER) || defined(CONFIG_PREEMPT_TRACER)
23 static struct trace_array *irqsoff_trace __read_mostly;
24 static int tracer_enabled __read_mostly;
26 static DEFINE_PER_CPU(int, tracing_cpu);
28 static DEFINE_RAW_SPINLOCK(max_trace_lock);
31 TRACER_IRQS_OFF = (1 << 1),
32 TRACER_PREEMPT_OFF = (1 << 2),
35 static int trace_type __read_mostly;
37 static int save_flags;
39 static void stop_irqsoff_tracer(struct trace_array *tr, int graph);
40 static int start_irqsoff_tracer(struct trace_array *tr, int graph);
42 #ifdef CONFIG_PREEMPT_TRACER
46 return ((trace_type & TRACER_PREEMPT_OFF) && pc);
49 # define preempt_trace(pc) (0)
52 #ifdef CONFIG_IRQSOFF_TRACER
56 return ((trace_type & TRACER_IRQS_OFF) &&
60 # define irq_trace() (0)
63 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
64 static int irqsoff_display_graph(struct trace_array *tr, int set);
65 # define is_graph(tr) ((tr)->trace_flags & TRACE_ITER_DISPLAY_GRAPH)
67 static inline int irqsoff_display_graph(struct trace_array *tr, int set)
71 # define is_graph(tr) false
75 * Sequence count - we record it when starting a measurement and
76 * skip the latency if the sequence has changed - some other section
77 * did a maximum and could disturb our measurement with serial console
78 * printouts, etc. Truly coinciding maximum latencies should be rare
79 * and what happens together happens separately as well, so this doesn't
80 * decrease the validity of the maximum found:
82 static __cacheline_aligned_in_smp unsigned long max_sequence;
84 #ifdef CONFIG_FUNCTION_TRACER
86 * Prologue for the preempt and irqs off function tracers.
88 * Returns 1 if it is OK to continue, and data->disabled is
90 * 0 if the trace is to be ignored, and data->disabled
93 * Note, this function is also used outside this ifdef but
94 * inside the #ifdef of the function graph tracer below.
95 * This is OK, since the function graph tracer is
96 * dependent on the function tracer.
98 static int func_prolog_dec(struct trace_array *tr,
99 struct trace_array_cpu **data,
100 unsigned long *flags)
106 * Does not matter if we preempt. We test the flags
107 * afterward, to see if irqs are disabled or not.
108 * If we preempt and get a false positive, the flags
111 cpu = raw_smp_processor_id();
112 if (likely(!per_cpu(tracing_cpu, cpu)))
115 local_save_flags(*flags);
117 * Slight chance to get a false positive on tracing_cpu,
118 * although I'm starting to think there isn't a chance.
119 * Leave this for now just to be paranoid.
121 if (!irqs_disabled_flags(*flags) && !preempt_count())
124 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
125 disabled = atomic_inc_return(&(*data)->disabled);
127 if (likely(disabled == 1))
130 atomic_dec(&(*data)->disabled);
136 * irqsoff uses its own tracer function to keep the overhead down:
139 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip,
140 struct ftrace_ops *op, struct pt_regs *pt_regs)
142 struct trace_array *tr = irqsoff_trace;
143 struct trace_array_cpu *data;
146 if (!func_prolog_dec(tr, &data, &flags))
149 trace_function(tr, ip, parent_ip, flags, preempt_count());
151 atomic_dec(&data->disabled);
153 #endif /* CONFIG_FUNCTION_TRACER */
155 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
156 static int irqsoff_display_graph(struct trace_array *tr, int set)
160 if (!(is_graph(tr) ^ set))
163 stop_irqsoff_tracer(irqsoff_trace, !set);
165 for_each_possible_cpu(cpu)
166 per_cpu(tracing_cpu, cpu) = 0;
169 tracing_reset_online_cpus(&irqsoff_trace->trace_buffer);
171 return start_irqsoff_tracer(irqsoff_trace, set);
174 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
176 struct trace_array *tr = irqsoff_trace;
177 struct trace_array_cpu *data;
182 if (ftrace_graph_ignore_func(trace))
185 * Do not trace a function if it's filtered by set_graph_notrace.
186 * Make the index of ret stack negative to indicate that it should
187 * ignore further functions. But it needs its own ret stack entry
188 * to recover the original index in order to continue tracing after
189 * returning from the function.
191 if (ftrace_graph_notrace_addr(trace->func))
194 if (!func_prolog_dec(tr, &data, &flags))
197 pc = preempt_count();
198 ret = __trace_graph_entry(tr, trace, flags, pc);
199 atomic_dec(&data->disabled);
204 static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
206 struct trace_array *tr = irqsoff_trace;
207 struct trace_array_cpu *data;
211 ftrace_graph_addr_finish(trace);
213 if (!func_prolog_dec(tr, &data, &flags))
216 pc = preempt_count();
217 __trace_graph_return(tr, trace, flags, pc);
218 atomic_dec(&data->disabled);
221 static struct fgraph_ops fgraph_ops = {
222 .entryfunc = &irqsoff_graph_entry,
223 .retfunc = &irqsoff_graph_return,
226 static void irqsoff_trace_open(struct trace_iterator *iter)
228 if (is_graph(iter->tr))
229 graph_trace_open(iter);
233 static void irqsoff_trace_close(struct trace_iterator *iter)
236 graph_trace_close(iter);
239 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
240 TRACE_GRAPH_PRINT_PROC | \
241 TRACE_GRAPH_PRINT_ABS_TIME | \
242 TRACE_GRAPH_PRINT_DURATION)
244 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
247 * In graph mode call the graph tracer output function,
248 * otherwise go with the TRACE_FN event handler
250 if (is_graph(iter->tr))
251 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
253 return TRACE_TYPE_UNHANDLED;
256 static void irqsoff_print_header(struct seq_file *s)
258 struct trace_array *tr = irqsoff_trace;
261 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
263 trace_default_header(s);
267 __trace_function(struct trace_array *tr,
268 unsigned long ip, unsigned long parent_ip,
269 unsigned long flags, int pc)
272 trace_graph_function(tr, ip, parent_ip, flags, pc);
274 trace_function(tr, ip, parent_ip, flags, pc);
278 #define __trace_function trace_function
280 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
282 return TRACE_TYPE_UNHANDLED;
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);
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 bool report_latency(struct trace_array *tr, u64 delta)
306 if (tracing_thresh) {
307 if (delta < tracing_thresh)
310 if (delta <= tr->max_latency)
317 check_critical_timing(struct trace_array *tr,
318 struct trace_array_cpu *data,
319 unsigned long parent_ip,
326 T0 = data->preempt_timestamp;
327 T1 = ftrace_now(cpu);
330 local_save_flags(flags);
332 pc = preempt_count();
334 if (!report_latency(tr, delta))
337 raw_spin_lock_irqsave(&max_trace_lock, flags);
339 /* check if we are still the max latency */
340 if (!report_latency(tr, delta))
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)
350 data->critical_end = parent_ip;
352 if (likely(!is_tracing_stopped())) {
353 tr->max_latency = delta;
354 update_max_tr_single(tr, current, cpu);
360 raw_spin_unlock_irqrestore(&max_trace_lock, flags);
363 data->critical_sequence = max_sequence;
364 data->preempt_timestamp = ftrace_now(cpu);
365 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
369 start_critical_timing(unsigned long ip, unsigned long parent_ip, int pc)
372 struct trace_array *tr = irqsoff_trace;
373 struct trace_array_cpu *data;
376 if (!tracer_enabled || !tracing_is_enabled())
379 cpu = raw_smp_processor_id();
381 if (per_cpu(tracing_cpu, cpu))
384 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
386 if (unlikely(!data) || atomic_read(&data->disabled))
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, pc);
399 per_cpu(tracing_cpu, cpu) = 1;
401 atomic_dec(&data->disabled);
405 stop_critical_timing(unsigned long ip, unsigned long parent_ip, int pc)
408 struct trace_array *tr = irqsoff_trace;
409 struct trace_array_cpu *data;
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;
419 if (!tracer_enabled || !tracing_is_enabled())
422 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
424 if (unlikely(!data) ||
425 !data->critical_start || atomic_read(&data->disabled))
428 atomic_inc(&data->disabled);
430 local_save_flags(flags);
431 __trace_function(tr, ip, parent_ip, flags, pc);
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 int pc = preempt_count();
442 if (preempt_trace(pc) || irq_trace())
443 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1, pc);
445 EXPORT_SYMBOL_GPL(start_critical_timings);
447 void stop_critical_timings(void)
449 int pc = preempt_count();
451 if (preempt_trace(pc) || irq_trace())
452 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1, pc);
454 EXPORT_SYMBOL_GPL(stop_critical_timings);
456 #ifdef CONFIG_FUNCTION_TRACER
457 static bool function_enabled;
459 static int register_irqsoff_function(struct trace_array *tr, int graph, int set)
463 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
464 if (function_enabled || (!set && !(tr->trace_flags & TRACE_ITER_FUNCTION)))
468 ret = register_ftrace_graph(&fgraph_ops);
470 ret = register_ftrace_function(tr->ops);
473 function_enabled = true;
478 static void unregister_irqsoff_function(struct trace_array *tr, int graph)
480 if (!function_enabled)
484 unregister_ftrace_graph(&fgraph_ops);
486 unregister_ftrace_function(tr->ops);
488 function_enabled = false;
491 static int irqsoff_function_set(struct trace_array *tr, u32 mask, int set)
493 if (!(mask & TRACE_ITER_FUNCTION))
497 register_irqsoff_function(tr, is_graph(tr), 1);
499 unregister_irqsoff_function(tr, is_graph(tr));
503 static int register_irqsoff_function(struct trace_array *tr, int graph, int set)
507 static void unregister_irqsoff_function(struct trace_array *tr, int graph) { }
508 static inline int irqsoff_function_set(struct trace_array *tr, u32 mask, int set)
512 #endif /* CONFIG_FUNCTION_TRACER */
514 static int irqsoff_flag_changed(struct trace_array *tr, u32 mask, int set)
516 struct tracer *tracer = tr->current_trace;
518 if (irqsoff_function_set(tr, mask, set))
521 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
522 if (mask & TRACE_ITER_DISPLAY_GRAPH)
523 return irqsoff_display_graph(tr, set);
526 return trace_keep_overwrite(tracer, mask, set);
529 static int start_irqsoff_tracer(struct trace_array *tr, int graph)
533 ret = register_irqsoff_function(tr, graph, 0);
535 if (!ret && tracing_is_enabled())
543 static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
547 unregister_irqsoff_function(tr, graph);
550 static bool irqsoff_busy;
552 static int __irqsoff_tracer_init(struct trace_array *tr)
557 save_flags = tr->trace_flags;
559 /* non overwrite screws up the latency tracers */
560 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
561 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
565 /* make sure that the tracer is visible */
568 ftrace_init_array_ops(tr, irqsoff_tracer_call);
570 /* Only toplevel instance supports graph tracing */
571 if (start_irqsoff_tracer(tr, (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
573 printk(KERN_ERR "failed to start irqsoff tracer\n");
579 static void __irqsoff_tracer_reset(struct trace_array *tr)
581 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
582 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
584 stop_irqsoff_tracer(tr, is_graph(tr));
586 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
587 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
588 ftrace_reset_array_ops(tr);
590 irqsoff_busy = false;
593 static void irqsoff_tracer_start(struct trace_array *tr)
598 static void irqsoff_tracer_stop(struct trace_array *tr)
603 #ifdef CONFIG_IRQSOFF_TRACER
605 * We are only interested in hardirq on/off events:
607 void tracer_hardirqs_on(unsigned long a0, unsigned long a1)
609 unsigned int pc = preempt_count();
611 if (!preempt_trace(pc) && irq_trace())
612 stop_critical_timing(a0, a1, pc);
615 void tracer_hardirqs_off(unsigned long a0, unsigned long a1)
617 unsigned int pc = preempt_count();
619 if (!preempt_trace(pc) && irq_trace())
620 start_critical_timing(a0, a1, pc);
623 static int irqsoff_tracer_init(struct trace_array *tr)
625 trace_type = TRACER_IRQS_OFF;
627 return __irqsoff_tracer_init(tr);
630 static void irqsoff_tracer_reset(struct trace_array *tr)
632 __irqsoff_tracer_reset(tr);
635 static struct tracer irqsoff_tracer __read_mostly =
638 .init = irqsoff_tracer_init,
639 .reset = irqsoff_tracer_reset,
640 .start = irqsoff_tracer_start,
641 .stop = irqsoff_tracer_stop,
643 .print_header = irqsoff_print_header,
644 .print_line = irqsoff_print_line,
645 .flag_changed = irqsoff_flag_changed,
646 #ifdef CONFIG_FTRACE_SELFTEST
647 .selftest = trace_selftest_startup_irqsoff,
649 .open = irqsoff_trace_open,
650 .close = irqsoff_trace_close,
651 .allow_instances = true,
654 #endif /* CONFIG_IRQSOFF_TRACER */
656 #ifdef CONFIG_PREEMPT_TRACER
657 void tracer_preempt_on(unsigned long a0, unsigned long a1)
659 int pc = preempt_count();
661 if (preempt_trace(pc) && !irq_trace())
662 stop_critical_timing(a0, a1, pc);
665 void tracer_preempt_off(unsigned long a0, unsigned long a1)
667 int pc = preempt_count();
669 if (preempt_trace(pc) && !irq_trace())
670 start_critical_timing(a0, a1, pc);
673 static int preemptoff_tracer_init(struct trace_array *tr)
675 trace_type = TRACER_PREEMPT_OFF;
677 return __irqsoff_tracer_init(tr);
680 static void preemptoff_tracer_reset(struct trace_array *tr)
682 __irqsoff_tracer_reset(tr);
685 static struct tracer preemptoff_tracer __read_mostly =
687 .name = "preemptoff",
688 .init = preemptoff_tracer_init,
689 .reset = preemptoff_tracer_reset,
690 .start = irqsoff_tracer_start,
691 .stop = irqsoff_tracer_stop,
693 .print_header = irqsoff_print_header,
694 .print_line = irqsoff_print_line,
695 .flag_changed = irqsoff_flag_changed,
696 #ifdef CONFIG_FTRACE_SELFTEST
697 .selftest = trace_selftest_startup_preemptoff,
699 .open = irqsoff_trace_open,
700 .close = irqsoff_trace_close,
701 .allow_instances = true,
704 #endif /* CONFIG_PREEMPT_TRACER */
706 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
708 static int preemptirqsoff_tracer_init(struct trace_array *tr)
710 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
712 return __irqsoff_tracer_init(tr);
715 static void preemptirqsoff_tracer_reset(struct trace_array *tr)
717 __irqsoff_tracer_reset(tr);
720 static struct tracer preemptirqsoff_tracer __read_mostly =
722 .name = "preemptirqsoff",
723 .init = preemptirqsoff_tracer_init,
724 .reset = preemptirqsoff_tracer_reset,
725 .start = irqsoff_tracer_start,
726 .stop = irqsoff_tracer_stop,
728 .print_header = irqsoff_print_header,
729 .print_line = irqsoff_print_line,
730 .flag_changed = irqsoff_flag_changed,
731 #ifdef CONFIG_FTRACE_SELFTEST
732 .selftest = trace_selftest_startup_preemptirqsoff,
734 .open = irqsoff_trace_open,
735 .close = irqsoff_trace_close,
736 .allow_instances = true,
741 __init static int init_irqsoff_tracer(void)
743 #ifdef CONFIG_IRQSOFF_TRACER
744 register_tracer(&irqsoff_tracer);
746 #ifdef CONFIG_PREEMPT_TRACER
747 register_tracer(&preemptoff_tracer);
749 #if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
750 register_tracer(&preemptirqsoff_tracer);
755 core_initcall(init_irqsoff_tracer);
756 #endif /* IRQSOFF_TRACER || PREEMPTOFF_TRACER */