blob: fee77e15d8154e5a9a7936824b14b6d805c0dc13 [file] [log] [blame]
Steven Rostedt352ad252008-05-12 21:20:42 +02001/*
2 * trace task wakeup timings
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Based on code from 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 Rostedt352ad252008-05-12 21:20:42 +020011 */
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/debugfs.h>
15#include <linux/kallsyms.h>
16#include <linux/uaccess.h>
17#include <linux/ftrace.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060018#include <linux/sched/rt.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040019#include <trace/events/sched.h>
Steven Rostedt352ad252008-05-12 21:20:42 +020020#include "trace.h"
21
22static struct trace_array *wakeup_trace;
23static int __read_mostly tracer_enabled;
24
25static struct task_struct *wakeup_task;
26static int wakeup_cpu;
Steven Rostedt478142c32009-09-09 10:36:01 -040027static int wakeup_current_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +020028static unsigned wakeup_prio = -1;
Steven Rostedt32443512009-01-21 16:24:46 -050029static int wakeup_rt;
Steven Rostedt352ad252008-05-12 21:20:42 +020030
Thomas Gleixner445c8952009-12-02 19:49:50 +010031static arch_spinlock_t wakeup_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010032 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt352ad252008-05-12 21:20:42 +020033
Jiri Olsa7495a5b2010-09-23 14:00:53 +020034static void wakeup_reset(struct trace_array *tr);
Ingo Molnare309b412008-05-12 21:20:51 +020035static void __wakeup_reset(struct trace_array *tr);
Jiri Olsa7495a5b2010-09-23 14:00:53 +020036static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
37static void wakeup_graph_return(struct ftrace_graph_ret *trace);
Steven Rostedt352ad252008-05-12 21:20:42 +020038
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -040039static int save_flags;
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -040040static bool function_enabled;
Steven Rostedte9d25fe2009-03-04 22:15:30 -050041
Jiri Olsa7495a5b2010-09-23 14:00:53 +020042#define TRACE_DISPLAY_GRAPH 1
43
44static struct tracer_opt trace_opts[] = {
45#ifdef CONFIG_FUNCTION_GRAPH_TRACER
46 /* display latency trace as call graph */
47 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
48#endif
49 { } /* Empty entry */
50};
51
52static struct tracer_flags tracer_flags = {
53 .val = 0,
54 .opts = trace_opts,
55};
56
57#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
58
Steven Rostedt606576c2008-10-06 19:06:12 -040059#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt542181d2010-10-05 16:38:49 -040060
61/*
62 * Prologue for the wakeup function tracers.
63 *
64 * Returns 1 if it is OK to continue, and preemption
65 * is disabled and data->disabled is incremented.
66 * 0 if the trace is to be ignored, and preemption
67 * is not disabled and data->disabled is
68 * kept the same.
69 *
70 * Note, this function is also used outside this ifdef but
71 * inside the #ifdef of the function graph tracer below.
72 * This is OK, since the function graph tracer is
73 * dependent on the function tracer.
74 */
75static int
76func_prolog_preempt_disable(struct trace_array *tr,
77 struct trace_array_cpu **data,
78 int *pc)
79{
80 long disabled;
81 int cpu;
82
83 if (likely(!wakeup_task))
84 return 0;
85
86 *pc = preempt_count();
87 preempt_disable_notrace();
88
89 cpu = raw_smp_processor_id();
90 if (cpu != wakeup_current_cpu)
91 goto out_enable;
92
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050093 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt542181d2010-10-05 16:38:49 -040094 disabled = atomic_inc_return(&(*data)->disabled);
95 if (unlikely(disabled != 1))
96 goto out;
97
98 return 1;
99
100out:
101 atomic_dec(&(*data)->disabled);
102
103out_enable:
104 preempt_enable_notrace();
105 return 0;
106}
107
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400108/*
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200109 * wakeup uses its own tracer function to keep the overhead down:
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400110 */
111static void
Steven Rostedta1e2e312011-08-09 12:50:46 -0400112wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
113 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400114{
115 struct trace_array *tr = wakeup_trace;
116 struct trace_array_cpu *data;
117 unsigned long flags;
Steven Rostedt38697052008-10-01 13:14:09 -0400118 int pc;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400119
Steven Rostedt542181d2010-10-05 16:38:49 -0400120 if (!func_prolog_preempt_disable(tr, &data, &pc))
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400121 return;
122
Steven Rostedte59494f2008-07-16 00:13:45 -0400123 local_irq_save(flags);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500124 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedte59494f2008-07-16 00:13:45 -0400125 local_irq_restore(flags);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400126
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400127 atomic_dec(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -0400128 preempt_enable_notrace();
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400129}
130
131static struct ftrace_ops trace_ops __read_mostly =
132{
133 .func = wakeup_tracer_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400134 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400135};
Steven Rostedt7e407982010-10-19 10:56:19 -0400136#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200137
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400138static int register_wakeup_function(int graph, int set)
139{
140 int ret;
141
142 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
143 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
144 return 0;
145
146 if (graph)
147 ret = register_ftrace_graph(&wakeup_graph_return,
148 &wakeup_graph_entry);
149 else
150 ret = register_ftrace_function(&trace_ops);
151
152 if (!ret)
153 function_enabled = true;
154
155 return ret;
156}
157
158static void unregister_wakeup_function(int graph)
159{
160 if (!function_enabled)
161 return;
162
163 if (graph)
164 unregister_ftrace_graph();
165 else
166 unregister_ftrace_function(&trace_ops);
167
168 function_enabled = false;
169}
170
171static void wakeup_function_set(int set)
172{
173 if (set)
174 register_wakeup_function(is_graph(), 1);
175 else
176 unregister_wakeup_function(is_graph());
177}
178
179static int wakeup_flag_changed(struct tracer *tracer, u32 mask, int set)
180{
181 if (mask & TRACE_ITER_FUNCTION)
182 wakeup_function_set(set);
183
184 return trace_keep_overwrite(tracer, mask, set);
185}
186
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200187static int start_func_tracer(int graph)
188{
189 int ret;
190
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400191 ret = register_wakeup_function(graph, 0);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200192
193 if (!ret && tracing_is_enabled())
194 tracer_enabled = 1;
195 else
196 tracer_enabled = 0;
197
198 return ret;
199}
200
201static void stop_func_tracer(int graph)
202{
203 tracer_enabled = 0;
204
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400205 unregister_wakeup_function(graph);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200206}
207
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200208#ifdef CONFIG_FUNCTION_GRAPH_TRACER
209static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
210{
211
212 if (!(bit & TRACE_DISPLAY_GRAPH))
213 return -EINVAL;
214
215 if (!(is_graph() ^ set))
216 return 0;
217
218 stop_func_tracer(!set);
219
220 wakeup_reset(wakeup_trace);
221 tracing_max_latency = 0;
222
223 return start_func_tracer(set);
224}
225
226static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
227{
228 struct trace_array *tr = wakeup_trace;
229 struct trace_array_cpu *data;
230 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400231 int pc, ret = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200232
Steven Rostedt542181d2010-10-05 16:38:49 -0400233 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200234 return 0;
235
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200236 local_save_flags(flags);
237 ret = __trace_graph_entry(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200238 atomic_dec(&data->disabled);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200239 preempt_enable_notrace();
Steven Rostedt542181d2010-10-05 16:38:49 -0400240
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200241 return ret;
242}
243
244static void wakeup_graph_return(struct ftrace_graph_ret *trace)
245{
246 struct trace_array *tr = wakeup_trace;
247 struct trace_array_cpu *data;
248 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400249 int pc;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200250
Steven Rostedt542181d2010-10-05 16:38:49 -0400251 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200252 return;
253
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200254 local_save_flags(flags);
255 __trace_graph_return(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200256 atomic_dec(&data->disabled);
257
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200258 preempt_enable_notrace();
259 return;
260}
261
262static void wakeup_trace_open(struct trace_iterator *iter)
263{
264 if (is_graph())
265 graph_trace_open(iter);
266}
267
268static void wakeup_trace_close(struct trace_iterator *iter)
269{
270 if (iter->private)
271 graph_trace_close(iter);
272}
273
Jiri Olsa321e68b2011-06-03 16:58:47 +0200274#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
275 TRACE_GRAPH_PRINT_ABS_TIME | \
276 TRACE_GRAPH_PRINT_DURATION)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200277
278static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
279{
280 /*
281 * In graph mode call the graph tracer output function,
282 * otherwise go with the TRACE_FN event handler
283 */
284 if (is_graph())
285 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
286
287 return TRACE_TYPE_UNHANDLED;
288}
289
290static void wakeup_print_header(struct seq_file *s)
291{
292 if (is_graph())
293 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
294 else
295 trace_default_header(s);
296}
297
298static void
299__trace_function(struct trace_array *tr,
300 unsigned long ip, unsigned long parent_ip,
301 unsigned long flags, int pc)
302{
303 if (is_graph())
304 trace_graph_function(tr, ip, parent_ip, flags, pc);
305 else
306 trace_function(tr, ip, parent_ip, flags, pc);
307}
308#else
309#define __trace_function trace_function
310
311static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
312{
313 return -EINVAL;
314}
315
316static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
317{
318 return -1;
319}
320
321static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
322{
323 return TRACE_TYPE_UNHANDLED;
324}
325
326static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200327static void wakeup_trace_open(struct trace_iterator *iter) { }
328static void wakeup_trace_close(struct trace_iterator *iter) { }
Jiri Olsa7e9a49e2011-11-07 16:08:49 +0100329
330#ifdef CONFIG_FUNCTION_TRACER
331static void wakeup_print_header(struct seq_file *s)
332{
333 trace_default_header(s);
334}
335#else
336static void wakeup_print_header(struct seq_file *s)
337{
338 trace_latency_header(s);
339}
340#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200341#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
342
Steven Rostedt352ad252008-05-12 21:20:42 +0200343/*
344 * Should this new latency be reported/recorded?
345 */
Ingo Molnare309b412008-05-12 21:20:51 +0200346static int report_latency(cycle_t delta)
Steven Rostedt352ad252008-05-12 21:20:42 +0200347{
348 if (tracing_thresh) {
349 if (delta < tracing_thresh)
350 return 0;
351 } else {
352 if (delta <= tracing_max_latency)
353 return 0;
354 }
355 return 1;
356}
357
Steven Rostedt38516ab2010-04-20 17:04:50 -0400358static void
359probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
Steven Rostedt478142c32009-09-09 10:36:01 -0400360{
361 if (task != wakeup_task)
362 return;
363
364 wakeup_current_cpu = cpu;
365}
366
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200367static void notrace
Steven Rostedt38516ab2010-04-20 17:04:50 -0400368probe_wakeup_sched_switch(void *ignore,
369 struct task_struct *prev, struct task_struct *next)
Steven Rostedt352ad252008-05-12 21:20:42 +0200370{
Steven Rostedt352ad252008-05-12 21:20:42 +0200371 struct trace_array_cpu *data;
372 cycle_t T0, T1, delta;
373 unsigned long flags;
374 long disabled;
375 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400376 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200377
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400378 tracing_record_cmdline(prev);
379
Steven Rostedt352ad252008-05-12 21:20:42 +0200380 if (unlikely(!tracer_enabled))
381 return;
382
383 /*
384 * When we start a new trace, we set wakeup_task to NULL
385 * and then set tracer_enabled = 1. We want to make sure
386 * that another CPU does not see the tracer_enabled = 1
387 * and the wakeup_task with an older task, that might
388 * actually be the same as next.
389 */
390 smp_rmb();
391
392 if (next != wakeup_task)
393 return;
394
Steven Rostedt38697052008-10-01 13:14:09 -0400395 pc = preempt_count();
396
Steven Rostedt352ad252008-05-12 21:20:42 +0200397 /* disable local data, not wakeup_cpu data */
398 cpu = raw_smp_processor_id();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500399 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200400 if (likely(disabled != 1))
401 goto out;
402
Steven Rostedte59494f2008-07-16 00:13:45 -0400403 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100404 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200405
406 /* We could race with grabbing wakeup_lock */
407 if (unlikely(!tracer_enabled || next != wakeup_task))
408 goto out_unlock;
409
Steven Rostedt9be24412009-03-26 10:25:24 -0400410 /* The task we are waiting for is waking up */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500411 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedt9be24412009-03-26 10:25:24 -0400412
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200413 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500414 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200415
Steven Rostedt352ad252008-05-12 21:20:42 +0200416 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200417 T1 = ftrace_now(cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200418 delta = T1-T0;
419
420 if (!report_latency(delta))
421 goto out_unlock;
422
Carsten Emdeb5130b12009-09-13 01:43:07 +0200423 if (likely(!is_tracing_stopped())) {
424 tracing_max_latency = delta;
425 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
426 }
Steven Rostedt352ad252008-05-12 21:20:42 +0200427
Steven Rostedt352ad252008-05-12 21:20:42 +0200428out_unlock:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400429 __wakeup_reset(wakeup_trace);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100430 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400431 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200432out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500433 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200434}
435
Ingo Molnare309b412008-05-12 21:20:51 +0200436static void __wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200437{
Steven Rostedt352ad252008-05-12 21:20:42 +0200438 wakeup_cpu = -1;
439 wakeup_prio = -1;
440
441 if (wakeup_task)
442 put_task_struct(wakeup_task);
443
444 wakeup_task = NULL;
445}
446
Ingo Molnare309b412008-05-12 21:20:51 +0200447static void wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200448{
449 unsigned long flags;
450
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500451 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -0400452
Steven Rostedte59494f2008-07-16 00:13:45 -0400453 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100454 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200455 __wakeup_reset(tr);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100456 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400457 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200458}
459
Ingo Molnare309b412008-05-12 21:20:51 +0200460static void
Steven Rostedt38516ab2010-04-20 17:04:50 -0400461probe_wakeup(void *ignore, struct task_struct *p, int success)
Steven Rostedt352ad252008-05-12 21:20:42 +0200462{
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500463 struct trace_array_cpu *data;
Steven Rostedt352ad252008-05-12 21:20:42 +0200464 int cpu = smp_processor_id();
465 unsigned long flags;
466 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400467 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200468
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400469 if (likely(!tracer_enabled))
Steven Rostedt352ad252008-05-12 21:20:42 +0200470 return;
471
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400472 tracing_record_cmdline(p);
473 tracing_record_cmdline(current);
474
Steven Rostedt32443512009-01-21 16:24:46 -0500475 if ((wakeup_rt && !rt_task(p)) ||
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400476 p->prio >= wakeup_prio ||
477 p->prio >= current->prio)
478 return;
479
Steven Rostedt38697052008-10-01 13:14:09 -0400480 pc = preempt_count();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500481 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200482 if (unlikely(disabled != 1))
483 goto out;
484
485 /* interrupts should be off from try_to_wake_up */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100486 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200487
488 /* check for races. */
489 if (!tracer_enabled || p->prio >= wakeup_prio)
490 goto out_locked;
491
492 /* reset the trace */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400493 __wakeup_reset(wakeup_trace);
Steven Rostedt352ad252008-05-12 21:20:42 +0200494
495 wakeup_cpu = task_cpu(p);
Steven Rostedt478142c32009-09-09 10:36:01 -0400496 wakeup_current_cpu = wakeup_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +0200497 wakeup_prio = p->prio;
498
499 wakeup_task = p;
500 get_task_struct(wakeup_task);
501
502 local_save_flags(flags);
503
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500504 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500505 data->preempt_timestamp = ftrace_now(cpu);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500506 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
Steven Rostedt301fd742009-04-03 11:12:23 -0400507
508 /*
509 * We must be careful in using CALLER_ADDR2. But since wake_up
510 * is not called by an assembly function (where as schedule is)
511 * it should be safe to use it here.
512 */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200513 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200514
515out_locked:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100516 arch_spin_unlock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200517out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500518 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200519}
520
Ingo Molnare309b412008-05-12 21:20:51 +0200521static void start_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200522{
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200523 int ret;
524
Steven Rostedt38516ab2010-04-20 17:04:50 -0400525 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200526 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400527 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200528 " probe to kernel_sched_wakeup\n");
529 return;
530 }
531
Steven Rostedt38516ab2010-04-20 17:04:50 -0400532 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200533 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400534 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200535 " probe to kernel_sched_wakeup_new\n");
536 goto fail_deprobe;
537 }
538
Steven Rostedt38516ab2010-04-20 17:04:50 -0400539 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200540 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400541 pr_info("sched trace: Couldn't activate tracepoint"
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500542 " probe to kernel_sched_switch\n");
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200543 goto fail_deprobe_wake_new;
544 }
545
Steven Rostedt38516ab2010-04-20 17:04:50 -0400546 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt478142c32009-09-09 10:36:01 -0400547 if (ret) {
548 pr_info("wakeup trace: Couldn't activate tracepoint"
549 " probe to kernel_sched_migrate_task\n");
550 return;
551 }
552
Steven Rostedt352ad252008-05-12 21:20:42 +0200553 wakeup_reset(tr);
554
555 /*
556 * Don't let the tracer_enabled = 1 show up before
557 * the wakeup_task is reset. This may be overkill since
558 * wakeup_reset does a spin_unlock after setting the
559 * wakeup_task to NULL, but I want to be safe.
560 * This is a slow path anyway.
561 */
562 smp_wmb();
563
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200564 if (start_func_tracer(is_graph()))
565 printk(KERN_ERR "failed to start wakeup tracer\n");
Steven Rostedtad591242008-07-10 20:58:13 -0400566
Steven Rostedt352ad252008-05-12 21:20:42 +0200567 return;
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200568fail_deprobe_wake_new:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400569 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200570fail_deprobe:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400571 unregister_trace_sched_wakeup(probe_wakeup, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200572}
573
Ingo Molnare309b412008-05-12 21:20:51 +0200574static void stop_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200575{
576 tracer_enabled = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200577 stop_func_tracer(is_graph());
Steven Rostedt38516ab2010-04-20 17:04:50 -0400578 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
579 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
580 unregister_trace_sched_wakeup(probe_wakeup, NULL);
581 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200582}
583
Steven Rostedt32443512009-01-21 16:24:46 -0500584static int __wakeup_tracer_init(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200585{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400586 save_flags = trace_flags;
587
588 /* non overwrite screws up the latency tracers */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400589 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
590 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500591
Steven Rostedt745b1622009-01-15 23:40:11 -0500592 tracing_max_latency = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200593 wakeup_trace = tr;
Steven Rostedtc76f0692008-11-07 22:36:02 -0500594 start_wakeup_tracer(tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100595 return 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200596}
597
Steven Rostedt32443512009-01-21 16:24:46 -0500598static int wakeup_tracer_init(struct trace_array *tr)
599{
600 wakeup_rt = 0;
601 return __wakeup_tracer_init(tr);
602}
603
604static int wakeup_rt_tracer_init(struct trace_array *tr)
605{
606 wakeup_rt = 1;
607 return __wakeup_tracer_init(tr);
608}
609
Ingo Molnare309b412008-05-12 21:20:51 +0200610static void wakeup_tracer_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200611{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400612 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
613 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
614
Steven Rostedtc76f0692008-11-07 22:36:02 -0500615 stop_wakeup_tracer(tr);
616 /* make sure we put back any tasks we are tracing */
617 wakeup_reset(tr);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500618
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400619 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
620 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
Steven Rostedt352ad252008-05-12 21:20:42 +0200621}
622
Steven Rostedt90369902008-11-05 16:05:44 -0500623static void wakeup_tracer_start(struct trace_array *tr)
624{
625 wakeup_reset(tr);
626 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -0500627}
628
629static void wakeup_tracer_stop(struct trace_array *tr)
630{
631 tracer_enabled = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200632}
633
634static struct tracer wakeup_tracer __read_mostly =
635{
636 .name = "wakeup",
637 .init = wakeup_tracer_init,
638 .reset = wakeup_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500639 .start = wakeup_tracer_start,
640 .stop = wakeup_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900641 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200642 .print_header = wakeup_print_header,
643 .print_line = wakeup_print_line,
644 .flags = &tracer_flags,
645 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400646 .flag_changed = wakeup_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200647#ifdef CONFIG_FTRACE_SELFTEST
648 .selftest = trace_selftest_startup_wakeup,
649#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200650 .open = wakeup_trace_open,
651 .close = wakeup_trace_close,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900652 .use_max_tr = true,
Steven Rostedt352ad252008-05-12 21:20:42 +0200653};
654
Steven Rostedt32443512009-01-21 16:24:46 -0500655static struct tracer wakeup_rt_tracer __read_mostly =
656{
657 .name = "wakeup_rt",
658 .init = wakeup_rt_tracer_init,
659 .reset = wakeup_tracer_reset,
660 .start = wakeup_tracer_start,
661 .stop = wakeup_tracer_stop,
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100662 .wait_pipe = poll_wait_pipe,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900663 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200664 .print_header = wakeup_print_header,
665 .print_line = wakeup_print_line,
666 .flags = &tracer_flags,
667 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400668 .flag_changed = wakeup_flag_changed,
Steven Rostedt32443512009-01-21 16:24:46 -0500669#ifdef CONFIG_FTRACE_SELFTEST
670 .selftest = trace_selftest_startup_wakeup,
671#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200672 .open = wakeup_trace_open,
673 .close = wakeup_trace_close,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900674 .use_max_tr = true,
Steven Rostedt32443512009-01-21 16:24:46 -0500675};
676
Steven Rostedt352ad252008-05-12 21:20:42 +0200677__init static int init_wakeup_tracer(void)
678{
679 int ret;
680
681 ret = register_tracer(&wakeup_tracer);
682 if (ret)
683 return ret;
684
Steven Rostedt32443512009-01-21 16:24:46 -0500685 ret = register_tracer(&wakeup_rt_tracer);
686 if (ret)
687 return ret;
688
Steven Rostedt352ad252008-05-12 21:20:42 +0200689 return 0;
690}
Steven Rostedt6f415672012-10-05 12:13:07 -0400691core_initcall(init_wakeup_tracer);