blob: 8523ea345f2b1a3849d53f2d6731ed41fb5d94f4 [file] [log] [blame]
Steven Rostedt81d68a92008-05-12 21:20:42 +02001/*
Wenji Huang73d8b8b2009-02-17 01:10:02 -05002 * trace irqs off critical timings
Steven Rostedt81d68a92008-05-12 21:20:42 +02003 *
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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010010 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedt81d68a92008-05-12 21:20:42 +020011 */
12#include <linux/kallsyms.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020013#include <linux/uaccess.h>
14#include <linux/module.h>
15#include <linux/ftrace.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020016
17#include "trace.h"
18
19static struct trace_array *irqsoff_trace __read_mostly;
20static int tracer_enabled __read_mostly;
21
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020022static DEFINE_PER_CPU(int, tracing_cpu);
23
Thomas Gleixner5389f6f2009-07-25 17:13:33 +020024static DEFINE_RAW_SPINLOCK(max_trace_lock);
Steven Rostedt89b2f972008-05-12 21:20:44 +020025
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020026enum {
27 TRACER_IRQS_OFF = (1 << 1),
28 TRACER_PREEMPT_OFF = (1 << 2),
29};
30
31static int trace_type __read_mostly;
32
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -040033static int save_flags;
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -040034static bool function_enabled;
Steven Rostedte9d25fe2009-03-04 22:15:30 -050035
Jiri Olsa62b915f2010-04-02 19:01:22 +020036static void stop_irqsoff_tracer(struct trace_array *tr, int graph);
37static int start_irqsoff_tracer(struct trace_array *tr, int graph);
38
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020039#ifdef CONFIG_PREEMPT_TRACER
Ingo Molnare309b412008-05-12 21:20:51 +020040static inline int
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020041preempt_trace(void)
42{
43 return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
44}
45#else
46# define preempt_trace() (0)
47#endif
48
49#ifdef CONFIG_IRQSOFF_TRACER
Ingo Molnare309b412008-05-12 21:20:51 +020050static inline int
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020051irq_trace(void)
52{
53 return ((trace_type & TRACER_IRQS_OFF) &&
54 irqs_disabled());
55}
56#else
57# define irq_trace() (0)
58#endif
59
Jiri Olsa62b915f2010-04-02 19:01:22 +020060#define TRACE_DISPLAY_GRAPH 1
61
62static struct tracer_opt trace_opts[] = {
63#ifdef CONFIG_FUNCTION_GRAPH_TRACER
64 /* display latency trace as call graph */
65 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
66#endif
67 { } /* Empty entry */
68};
69
70static struct tracer_flags tracer_flags = {
71 .val = 0,
72 .opts = trace_opts,
73};
74
75#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
76
Steven Rostedt81d68a92008-05-12 21:20:42 +020077/*
78 * Sequence count - we record it when starting a measurement and
79 * skip the latency if the sequence has changed - some other section
80 * did a maximum and could disturb our measurement with serial console
81 * printouts, etc. Truly coinciding maximum latencies should be rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -030082 * and what happens together happens separately as well, so this doesn't
Steven Rostedt81d68a92008-05-12 21:20:42 +020083 * decrease the validity of the maximum found:
84 */
85static __cacheline_aligned_in_smp unsigned long max_sequence;
86
Steven Rostedt606576c2008-10-06 19:06:12 -040087#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt81d68a92008-05-12 21:20:42 +020088/*
Steven Rostedt5e6d2b92010-10-05 19:41:43 -040089 * Prologue for the preempt and irqs off function tracers.
90 *
91 * Returns 1 if it is OK to continue, and data->disabled is
92 * incremented.
93 * 0 if the trace is to be ignored, and data->disabled
94 * is kept the same.
95 *
96 * Note, this function is also used outside this ifdef but
97 * inside the #ifdef of the function graph tracer below.
98 * This is OK, since the function graph tracer is
99 * dependent on the function tracer.
Steven Rostedt81d68a92008-05-12 21:20:42 +0200100 */
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400101static int func_prolog_dec(struct trace_array *tr,
102 struct trace_array_cpu **data,
103 unsigned long *flags)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200104{
Steven Rostedt81d68a92008-05-12 21:20:42 +0200105 long disabled;
106 int cpu;
107
Steven Rostedt361943a2008-05-12 21:20:44 +0200108 /*
109 * Does not matter if we preempt. We test the flags
110 * afterward, to see if irqs are disabled or not.
111 * If we preempt and get a false positive, the flags
112 * test will fail.
113 */
114 cpu = raw_smp_processor_id();
115 if (likely(!per_cpu(tracing_cpu, cpu)))
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400116 return 0;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200117
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400118 local_save_flags(*flags);
Steven Rostedt361943a2008-05-12 21:20:44 +0200119 /* slight chance to get a false positive on tracing_cpu */
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400120 if (!irqs_disabled_flags(*flags))
121 return 0;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200122
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500123 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400124 disabled = atomic_inc_return(&(*data)->disabled);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200125
126 if (likely(disabled == 1))
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400127 return 1;
128
129 atomic_dec(&(*data)->disabled);
130
131 return 0;
132}
133
134/*
135 * irqsoff uses its own tracer function to keep the overhead down:
136 */
137static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400138irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400139 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400140{
141 struct trace_array *tr = irqsoff_trace;
142 struct trace_array_cpu *data;
143 unsigned long flags;
144
145 if (!func_prolog_dec(tr, &data, &flags))
146 return;
147
148 trace_function(tr, ip, parent_ip, flags, preempt_count());
Steven Rostedt81d68a92008-05-12 21:20:42 +0200149
150 atomic_dec(&data->disabled);
151}
Steven Rostedt606576c2008-10-06 19:06:12 -0400152#endif /* CONFIG_FUNCTION_TRACER */
Steven Rostedt81d68a92008-05-12 21:20:42 +0200153
Jiri Olsa62b915f2010-04-02 19:01:22 +0200154#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500155static int
156irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa62b915f2010-04-02 19:01:22 +0200157{
158 int cpu;
159
160 if (!(bit & TRACE_DISPLAY_GRAPH))
161 return -EINVAL;
162
163 if (!(is_graph() ^ set))
164 return 0;
165
166 stop_irqsoff_tracer(irqsoff_trace, !set);
167
168 for_each_possible_cpu(cpu)
169 per_cpu(tracing_cpu, cpu) = 0;
170
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500171 tr->max_latency = 0;
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500172 tracing_reset_online_cpus(&irqsoff_trace->trace_buffer);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200173
174 return start_irqsoff_tracer(irqsoff_trace, set);
175}
176
177static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
178{
179 struct trace_array *tr = irqsoff_trace;
180 struct trace_array_cpu *data;
181 unsigned long flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200182 int ret;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200183 int pc;
184
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400185 if (!func_prolog_dec(tr, &data, &flags))
Jiri Olsa62b915f2010-04-02 19:01:22 +0200186 return 0;
187
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400188 pc = preempt_count();
189 ret = __trace_graph_entry(tr, trace, flags, pc);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200190 atomic_dec(&data->disabled);
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400191
Jiri Olsa62b915f2010-04-02 19:01:22 +0200192 return ret;
193}
194
195static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
196{
197 struct trace_array *tr = irqsoff_trace;
198 struct trace_array_cpu *data;
199 unsigned long flags;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200200 int pc;
201
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400202 if (!func_prolog_dec(tr, &data, &flags))
Jiri Olsa62b915f2010-04-02 19:01:22 +0200203 return;
204
Steven Rostedt5e6d2b92010-10-05 19:41:43 -0400205 pc = preempt_count();
206 __trace_graph_return(tr, trace, flags, pc);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200207 atomic_dec(&data->disabled);
208}
209
210static void irqsoff_trace_open(struct trace_iterator *iter)
211{
212 if (is_graph())
213 graph_trace_open(iter);
214
215}
216
217static void irqsoff_trace_close(struct trace_iterator *iter)
218{
219 if (iter->private)
220 graph_trace_close(iter);
221}
222
223#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
Jiri Olsa321e68b02011-06-03 16:58:47 +0200224 TRACE_GRAPH_PRINT_PROC | \
225 TRACE_GRAPH_PRINT_ABS_TIME | \
226 TRACE_GRAPH_PRINT_DURATION)
Jiri Olsa62b915f2010-04-02 19:01:22 +0200227
228static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
229{
Jiri Olsa62b915f2010-04-02 19:01:22 +0200230 /*
231 * In graph mode call the graph tracer output function,
232 * otherwise go with the TRACE_FN event handler
233 */
234 if (is_graph())
Jiri Olsa0a772622010-09-23 14:00:52 +0200235 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200236
237 return TRACE_TYPE_UNHANDLED;
238}
239
240static void irqsoff_print_header(struct seq_file *s)
241{
Jiri Olsa0a772622010-09-23 14:00:52 +0200242 if (is_graph())
243 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
244 else
Jiri Olsa62b915f2010-04-02 19:01:22 +0200245 trace_default_header(s);
246}
247
248static void
Jiri Olsa62b915f2010-04-02 19:01:22 +0200249__trace_function(struct trace_array *tr,
250 unsigned long ip, unsigned long parent_ip,
251 unsigned long flags, int pc)
252{
Jiri Olsa0a772622010-09-23 14:00:52 +0200253 if (is_graph())
254 trace_graph_function(tr, ip, parent_ip, flags, pc);
255 else
Jiri Olsa62b915f2010-04-02 19:01:22 +0200256 trace_function(tr, ip, parent_ip, flags, pc);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200257}
258
259#else
260#define __trace_function trace_function
261
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500262static int
263irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa62b915f2010-04-02 19:01:22 +0200264{
265 return -EINVAL;
266}
267
268static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
269{
270 return -1;
271}
272
273static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
274{
275 return TRACE_TYPE_UNHANDLED;
276}
277
278static void irqsoff_graph_return(struct ftrace_graph_ret *trace) { }
Jiri Olsa62b915f2010-04-02 19:01:22 +0200279static void irqsoff_trace_open(struct trace_iterator *iter) { }
280static void irqsoff_trace_close(struct trace_iterator *iter) { }
Jiri Olsa7e9a49e2011-11-07 16:08:49 +0100281
282#ifdef CONFIG_FUNCTION_TRACER
283static void irqsoff_print_header(struct seq_file *s)
284{
285 trace_default_header(s);
286}
287#else
288static void irqsoff_print_header(struct seq_file *s)
289{
290 trace_latency_header(s);
291}
292#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa62b915f2010-04-02 19:01:22 +0200293#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
294
Steven Rostedt81d68a92008-05-12 21:20:42 +0200295/*
296 * Should this new latency be reported/recorded?
297 */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500298static int report_latency(struct trace_array *tr, cycle_t delta)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200299{
300 if (tracing_thresh) {
301 if (delta < tracing_thresh)
302 return 0;
303 } else {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500304 if (delta <= tr->max_latency)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200305 return 0;
306 }
307 return 1;
308}
309
Ingo Molnare309b412008-05-12 21:20:51 +0200310static void
Steven Rostedt81d68a92008-05-12 21:20:42 +0200311check_critical_timing(struct trace_array *tr,
312 struct trace_array_cpu *data,
313 unsigned long parent_ip,
314 int cpu)
315{
Steven Rostedt89b2f972008-05-12 21:20:44 +0200316 cycle_t T0, T1, delta;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200317 unsigned long flags;
Steven Rostedt38697052008-10-01 13:14:09 -0400318 int pc;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200319
Steven Rostedt81d68a92008-05-12 21:20:42 +0200320 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200321 T1 = ftrace_now(cpu);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200322 delta = T1-T0;
323
324 local_save_flags(flags);
325
Steven Rostedt6450c1d2008-10-02 19:23:04 -0400326 pc = preempt_count();
327
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500328 if (!report_latency(tr, delta))
Steven Rostedt81d68a92008-05-12 21:20:42 +0200329 goto out;
330
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200331 raw_spin_lock_irqsave(&max_trace_lock, flags);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200332
Steven Rostedt89b2f972008-05-12 21:20:44 +0200333 /* check if we are still the max latency */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500334 if (!report_latency(tr, delta))
Steven Rostedt89b2f972008-05-12 21:20:44 +0200335 goto out_unlock;
336
Jiri Olsa62b915f2010-04-02 19:01:22 +0200337 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
Steven Rostedtcc51a0f2009-12-11 11:54:51 -0500338 /* Skip 5 functions to get to the irq/preempt enable function */
339 __trace_stack(tr, flags, 5, pc);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200340
Steven Rostedt81d68a92008-05-12 21:20:42 +0200341 if (data->critical_sequence != max_sequence)
Steven Rostedt89b2f972008-05-12 21:20:44 +0200342 goto out_unlock;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200343
Steven Rostedt81d68a92008-05-12 21:20:42 +0200344 data->critical_end = parent_ip;
345
Carsten Emdeb5130b12009-09-13 01:43:07 +0200346 if (likely(!is_tracing_stopped())) {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500347 tr->max_latency = delta;
Carsten Emdeb5130b12009-09-13 01:43:07 +0200348 update_max_tr_single(tr, current, cpu);
349 }
Steven Rostedt81d68a92008-05-12 21:20:42 +0200350
Steven Rostedt81d68a92008-05-12 21:20:42 +0200351 max_sequence++;
352
Steven Rostedt89b2f972008-05-12 21:20:44 +0200353out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200354 raw_spin_unlock_irqrestore(&max_trace_lock, flags);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200355
Steven Rostedt81d68a92008-05-12 21:20:42 +0200356out:
357 data->critical_sequence = max_sequence;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200358 data->preempt_timestamp = ftrace_now(cpu);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200359 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200360}
361
Ingo Molnare309b412008-05-12 21:20:51 +0200362static inline void
Steven Rostedt81d68a92008-05-12 21:20:42 +0200363start_critical_timing(unsigned long ip, unsigned long parent_ip)
364{
365 int cpu;
366 struct trace_array *tr = irqsoff_trace;
367 struct trace_array_cpu *data;
368 unsigned long flags;
369
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400370 if (!tracer_enabled || !tracing_is_enabled())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200371 return;
372
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200373 cpu = raw_smp_processor_id();
374
375 if (per_cpu(tracing_cpu, cpu))
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200376 return;
377
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500378 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200379
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200380 if (unlikely(!data) || atomic_read(&data->disabled))
Steven Rostedt81d68a92008-05-12 21:20:42 +0200381 return;
382
383 atomic_inc(&data->disabled);
384
385 data->critical_sequence = max_sequence;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200386 data->preempt_timestamp = ftrace_now(cpu);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200387 data->critical_start = parent_ip ? : ip;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200388
389 local_save_flags(flags);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200390
Jiri Olsa62b915f2010-04-02 19:01:22 +0200391 __trace_function(tr, ip, parent_ip, flags, preempt_count());
Steven Rostedt81d68a92008-05-12 21:20:42 +0200392
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200393 per_cpu(tracing_cpu, cpu) = 1;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200394
Steven Rostedt81d68a92008-05-12 21:20:42 +0200395 atomic_dec(&data->disabled);
396}
397
Ingo Molnare309b412008-05-12 21:20:51 +0200398static inline void
Steven Rostedt81d68a92008-05-12 21:20:42 +0200399stop_critical_timing(unsigned long ip, unsigned long parent_ip)
400{
401 int cpu;
402 struct trace_array *tr = irqsoff_trace;
403 struct trace_array_cpu *data;
404 unsigned long flags;
405
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200406 cpu = raw_smp_processor_id();
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200407 /* Always clear the tracing cpu on stopping the trace */
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200408 if (unlikely(per_cpu(tracing_cpu, cpu)))
409 per_cpu(tracing_cpu, cpu) = 0;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200410 else
411 return;
412
Steven Rostedt (Red Hat)10246fa2013-07-01 15:58:24 -0400413 if (!tracer_enabled || !tracing_is_enabled())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200414 return;
415
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500416 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200417
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400418 if (unlikely(!data) ||
Steven Rostedt81d68a92008-05-12 21:20:42 +0200419 !data->critical_start || atomic_read(&data->disabled))
420 return;
421
422 atomic_inc(&data->disabled);
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200423
Steven Rostedt81d68a92008-05-12 21:20:42 +0200424 local_save_flags(flags);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200425 __trace_function(tr, ip, parent_ip, flags, preempt_count());
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200426 check_critical_timing(tr, data, parent_ip ? : ip, cpu);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200427 data->critical_start = 0;
428 atomic_dec(&data->disabled);
429}
430
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200431/* start and stop critical timings used to for stoppage (in idle) */
Ingo Molnare309b412008-05-12 21:20:51 +0200432void start_critical_timings(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200433{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200434 if (preempt_trace() || irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200435 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
436}
Ingo Molnar1fe37102008-07-26 15:09:47 +0200437EXPORT_SYMBOL_GPL(start_critical_timings);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200438
Ingo Molnare309b412008-05-12 21:20:51 +0200439void stop_critical_timings(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200440{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200441 if (preempt_trace() || irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200442 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
443}
Ingo Molnar1fe37102008-07-26 15:09:47 +0200444EXPORT_SYMBOL_GPL(stop_critical_timings);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200445
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200446#ifdef CONFIG_IRQSOFF_TRACER
Steven Rostedt81d68a92008-05-12 21:20:42 +0200447#ifdef CONFIG_PROVE_LOCKING
Ingo Molnare309b412008-05-12 21:20:51 +0200448void time_hardirqs_on(unsigned long a0, unsigned long a1)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200449{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200450 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200451 stop_critical_timing(a0, a1);
452}
453
Ingo Molnare309b412008-05-12 21:20:51 +0200454void time_hardirqs_off(unsigned long a0, unsigned long a1)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200455{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200456 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200457 start_critical_timing(a0, a1);
458}
459
460#else /* !CONFIG_PROVE_LOCKING */
461
462/*
463 * Stubs:
464 */
465
Steven Rostedt81d68a92008-05-12 21:20:42 +0200466void trace_softirqs_on(unsigned long ip)
467{
468}
469
470void trace_softirqs_off(unsigned long ip)
471{
472}
473
Ingo Molnare309b412008-05-12 21:20:51 +0200474inline void print_irqtrace_events(struct task_struct *curr)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200475{
476}
477
478/*
479 * We are only interested in hardirq on/off events:
480 */
Ingo Molnare309b412008-05-12 21:20:51 +0200481void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200482{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200483 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200484 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
485}
486EXPORT_SYMBOL(trace_hardirqs_on);
487
Ingo Molnare309b412008-05-12 21:20:51 +0200488void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200489{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200490 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200491 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
492}
493EXPORT_SYMBOL(trace_hardirqs_off);
494
Andi Kleen285c00a2014-02-08 08:52:08 +0100495__visible void trace_hardirqs_on_caller(unsigned long caller_addr)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200496{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200497 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200498 stop_critical_timing(CALLER_ADDR0, caller_addr);
499}
500EXPORT_SYMBOL(trace_hardirqs_on_caller);
501
Andi Kleen285c00a2014-02-08 08:52:08 +0100502__visible void trace_hardirqs_off_caller(unsigned long caller_addr)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200503{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200504 if (!preempt_trace() && irq_trace())
Steven Rostedt81d68a92008-05-12 21:20:42 +0200505 start_critical_timing(CALLER_ADDR0, caller_addr);
506}
507EXPORT_SYMBOL(trace_hardirqs_off_caller);
508
509#endif /* CONFIG_PROVE_LOCKING */
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200510#endif /* CONFIG_IRQSOFF_TRACER */
511
512#ifdef CONFIG_PREEMPT_TRACER
Ingo Molnare309b412008-05-12 21:20:51 +0200513void trace_preempt_on(unsigned long a0, unsigned long a1)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200514{
Steven Rostedte36de1d2011-09-22 11:11:51 -0400515 if (preempt_trace() && !irq_trace())
Steven Rostedt1e01cb02008-07-15 09:53:37 -0400516 stop_critical_timing(a0, a1);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200517}
518
Ingo Molnare309b412008-05-12 21:20:51 +0200519void trace_preempt_off(unsigned long a0, unsigned long a1)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200520{
Steven Rostedte36de1d2011-09-22 11:11:51 -0400521 if (preempt_trace() && !irq_trace())
Steven Rostedt1e01cb02008-07-15 09:53:37 -0400522 start_critical_timing(a0, a1);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200523}
524#endif /* CONFIG_PREEMPT_TRACER */
Steven Rostedt81d68a92008-05-12 21:20:42 +0200525
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500526static int register_irqsoff_function(struct trace_array *tr, int graph, int set)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200527{
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400528 int ret;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200529
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400530 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
531 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
532 return 0;
533
534 if (graph)
Jiri Olsa62b915f2010-04-02 19:01:22 +0200535 ret = register_ftrace_graph(&irqsoff_graph_return,
536 &irqsoff_graph_entry);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400537 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500538 ret = register_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400539
540 if (!ret)
541 function_enabled = true;
542
543 return ret;
544}
545
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500546static void unregister_irqsoff_function(struct trace_array *tr, int graph)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400547{
548 if (!function_enabled)
549 return;
550
551 if (graph)
552 unregister_ftrace_graph();
553 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500554 unregister_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400555
556 function_enabled = false;
557}
558
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500559static void irqsoff_function_set(struct trace_array *tr, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400560{
561 if (set)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500562 register_irqsoff_function(tr, is_graph(), 1);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400563 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500564 unregister_irqsoff_function(tr, is_graph());
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400565}
566
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500567static int irqsoff_flag_changed(struct trace_array *tr, u32 mask, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400568{
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500569 struct tracer *tracer = tr->current_trace;
570
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400571 if (mask & TRACE_ITER_FUNCTION)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500572 irqsoff_function_set(tr, set);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400573
574 return trace_keep_overwrite(tracer, mask, set);
575}
576
577static int start_irqsoff_tracer(struct trace_array *tr, int graph)
578{
579 int ret;
580
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500581 ret = register_irqsoff_function(tr, graph, 0);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200582
583 if (!ret && tracing_is_enabled())
Steven Rostedt90369902008-11-05 16:05:44 -0500584 tracer_enabled = 1;
Steven Rostedt94523e82009-01-22 11:18:06 -0500585 else
Steven Rostedt90369902008-11-05 16:05:44 -0500586 tracer_enabled = 0;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200587
588 return ret;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200589}
590
Jiri Olsa62b915f2010-04-02 19:01:22 +0200591static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200592{
Steven Rostedt81d68a92008-05-12 21:20:42 +0200593 tracer_enabled = 0;
Jiri Olsa62b915f2010-04-02 19:01:22 +0200594
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500595 unregister_irqsoff_function(tr, graph);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200596}
597
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500598static bool irqsoff_busy;
599
600static int __irqsoff_tracer_init(struct trace_array *tr)
Steven Rostedt81d68a92008-05-12 21:20:42 +0200601{
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500602 if (irqsoff_busy)
603 return -EBUSY;
604
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400605 save_flags = trace_flags;
606
607 /* non overwrite screws up the latency tracers */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400608 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
609 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500610
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500611 tr->max_latency = 0;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200612 irqsoff_trace = tr;
Steven Rostedtc5f888c2008-05-12 21:20:55 +0200613 /* make sure that the tracer is visible */
Steven Rostedt81d68a92008-05-12 21:20:42 +0200614 smp_wmb();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500615 tracing_reset_online_cpus(&tr->trace_buffer);
Jiri Olsa62b915f2010-04-02 19:01:22 +0200616
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500617 ftrace_init_array_ops(tr, irqsoff_tracer_call);
618
619 /* Only toplevel instance supports graph tracing */
620 if (start_irqsoff_tracer(tr, (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
621 is_graph())))
Jiri Olsa62b915f2010-04-02 19:01:22 +0200622 printk(KERN_ERR "failed to start irqsoff tracer\n");
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500623
624 irqsoff_busy = true;
625 return 0;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200626}
627
628static void irqsoff_tracer_reset(struct trace_array *tr)
629{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400630 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
631 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
632
Jiri Olsa62b915f2010-04-02 19:01:22 +0200633 stop_irqsoff_tracer(tr, is_graph());
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500634
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400635 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
636 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500637 ftrace_reset_array_ops(tr);
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500638
639 irqsoff_busy = false;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200640}
641
Steven Rostedt90369902008-11-05 16:05:44 -0500642static void irqsoff_tracer_start(struct trace_array *tr)
643{
Steven Rostedt90369902008-11-05 16:05:44 -0500644 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -0500645}
646
647static void irqsoff_tracer_stop(struct trace_array *tr)
648{
649 tracer_enabled = 0;
Steven Rostedt81d68a92008-05-12 21:20:42 +0200650}
651
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200652#ifdef CONFIG_IRQSOFF_TRACER
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100653static int irqsoff_tracer_init(struct trace_array *tr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200654{
655 trace_type = TRACER_IRQS_OFF;
656
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500657 return __irqsoff_tracer_init(tr);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200658}
Steven Rostedt81d68a92008-05-12 21:20:42 +0200659static struct tracer irqsoff_tracer __read_mostly =
660{
661 .name = "irqsoff",
662 .init = irqsoff_tracer_init,
663 .reset = irqsoff_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500664 .start = irqsoff_tracer_start,
665 .stop = irqsoff_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900666 .print_max = true,
Jiri Olsa62b915f2010-04-02 19:01:22 +0200667 .print_header = irqsoff_print_header,
668 .print_line = irqsoff_print_line,
669 .flags = &tracer_flags,
670 .set_flag = irqsoff_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400671 .flag_changed = irqsoff_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200672#ifdef CONFIG_FTRACE_SELFTEST
673 .selftest = trace_selftest_startup_irqsoff,
674#endif
Jiri Olsa62b915f2010-04-02 19:01:22 +0200675 .open = irqsoff_trace_open,
676 .close = irqsoff_trace_close,
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500677 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900678 .use_max_tr = true,
Steven Rostedt81d68a92008-05-12 21:20:42 +0200679};
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200680# define register_irqsoff(trace) register_tracer(&trace)
681#else
682# define register_irqsoff(trace) do { } while (0)
683#endif
684
685#ifdef CONFIG_PREEMPT_TRACER
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100686static int preemptoff_tracer_init(struct trace_array *tr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200687{
688 trace_type = TRACER_PREEMPT_OFF;
689
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500690 return __irqsoff_tracer_init(tr);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200691}
692
693static struct tracer preemptoff_tracer __read_mostly =
694{
695 .name = "preemptoff",
696 .init = preemptoff_tracer_init,
697 .reset = irqsoff_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500698 .start = irqsoff_tracer_start,
699 .stop = irqsoff_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900700 .print_max = true,
Jiri Olsa62b915f2010-04-02 19:01:22 +0200701 .print_header = irqsoff_print_header,
702 .print_line = irqsoff_print_line,
703 .flags = &tracer_flags,
704 .set_flag = irqsoff_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400705 .flag_changed = irqsoff_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200706#ifdef CONFIG_FTRACE_SELFTEST
707 .selftest = trace_selftest_startup_preemptoff,
708#endif
Jiri Olsa62b915f2010-04-02 19:01:22 +0200709 .open = irqsoff_trace_open,
710 .close = irqsoff_trace_close,
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500711 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900712 .use_max_tr = true,
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200713};
714# define register_preemptoff(trace) register_tracer(&trace)
715#else
716# define register_preemptoff(trace) do { } while (0)
717#endif
718
719#if defined(CONFIG_IRQSOFF_TRACER) && \
720 defined(CONFIG_PREEMPT_TRACER)
721
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100722static int preemptirqsoff_tracer_init(struct trace_array *tr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200723{
724 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
725
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500726 return __irqsoff_tracer_init(tr);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200727}
728
729static struct tracer preemptirqsoff_tracer __read_mostly =
730{
731 .name = "preemptirqsoff",
732 .init = preemptirqsoff_tracer_init,
733 .reset = irqsoff_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500734 .start = irqsoff_tracer_start,
735 .stop = irqsoff_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900736 .print_max = true,
Jiri Olsa62b915f2010-04-02 19:01:22 +0200737 .print_header = irqsoff_print_header,
738 .print_line = irqsoff_print_line,
739 .flags = &tracer_flags,
740 .set_flag = irqsoff_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400741 .flag_changed = irqsoff_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200742#ifdef CONFIG_FTRACE_SELFTEST
743 .selftest = trace_selftest_startup_preemptirqsoff,
744#endif
Jiri Olsa62b915f2010-04-02 19:01:22 +0200745 .open = irqsoff_trace_open,
746 .close = irqsoff_trace_close,
Steven Rostedt (Red Hat)02f2f762014-01-14 09:32:58 -0500747 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900748 .use_max_tr = true,
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200749};
750
751# define register_preemptirqsoff(trace) register_tracer(&trace)
752#else
753# define register_preemptirqsoff(trace) do { } while (0)
754#endif
Steven Rostedt81d68a92008-05-12 21:20:42 +0200755
756__init static int init_irqsoff_tracer(void)
757{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200758 register_irqsoff(irqsoff_tracer);
759 register_preemptoff(preemptoff_tracer);
760 register_preemptirqsoff(preemptirqsoff_tracer);
Steven Rostedt81d68a92008-05-12 21:20:42 +0200761
762 return 0;
763}
Steven Rostedt6f415672012-10-05 12:13:07 -0400764core_initcall(init_irqsoff_tracer);