blob: 9b33dd117f3f5b63cf8380417293e70beb9e0c0b [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>
Steven Rostedt352ad252008-05-12 21:20:42 +020013#include <linux/kallsyms.h>
14#include <linux/uaccess.h>
15#include <linux/ftrace.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060016#include <linux/sched/rt.h>
Dario Faggioli2d3d8912013-11-07 14:43:44 +010017#include <linux/sched/deadline.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040018#include <trace/events/sched.h>
Steven Rostedt352ad252008-05-12 21:20:42 +020019#include "trace.h"
20
21static struct trace_array *wakeup_trace;
22static int __read_mostly tracer_enabled;
23
24static struct task_struct *wakeup_task;
25static int wakeup_cpu;
Steven Rostedt478142c32009-09-09 10:36:01 -040026static int wakeup_current_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +020027static unsigned wakeup_prio = -1;
Steven Rostedt32443512009-01-21 16:24:46 -050028static int wakeup_rt;
Dario Faggioliaf6ace72013-11-07 14:43:42 +010029static int wakeup_dl;
30static int tracing_dl = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +020031
Thomas Gleixner445c8952009-12-02 19:49:50 +010032static arch_spinlock_t wakeup_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010033 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt352ad252008-05-12 21:20:42 +020034
Jiri Olsa7495a5b2010-09-23 14:00:53 +020035static void wakeup_reset(struct trace_array *tr);
Ingo Molnare309b412008-05-12 21:20:51 +020036static void __wakeup_reset(struct trace_array *tr);
Jiri Olsa7495a5b2010-09-23 14:00:53 +020037static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
38static void wakeup_graph_return(struct ftrace_graph_ret *trace);
Steven Rostedt352ad252008-05-12 21:20:42 +020039
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -040040static int save_flags;
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -040041static bool function_enabled;
Steven Rostedte9d25fe2009-03-04 22:15:30 -050042
Jiri Olsa7495a5b2010-09-23 14:00:53 +020043#define TRACE_DISPLAY_GRAPH 1
44
45static struct tracer_opt trace_opts[] = {
46#ifdef CONFIG_FUNCTION_GRAPH_TRACER
47 /* display latency trace as call graph */
48 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
49#endif
50 { } /* Empty entry */
51};
52
53static struct tracer_flags tracer_flags = {
54 .val = 0,
55 .opts = trace_opts,
56};
57
58#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
59
Steven Rostedt606576c2008-10-06 19:06:12 -040060#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt542181d2010-10-05 16:38:49 -040061
62/*
63 * Prologue for the wakeup function tracers.
64 *
65 * Returns 1 if it is OK to continue, and preemption
66 * is disabled and data->disabled is incremented.
67 * 0 if the trace is to be ignored, and preemption
68 * is not disabled and data->disabled is
69 * kept the same.
70 *
71 * Note, this function is also used outside this ifdef but
72 * inside the #ifdef of the function graph tracer below.
73 * This is OK, since the function graph tracer is
74 * dependent on the function tracer.
75 */
76static int
77func_prolog_preempt_disable(struct trace_array *tr,
78 struct trace_array_cpu **data,
79 int *pc)
80{
81 long disabled;
82 int cpu;
83
84 if (likely(!wakeup_task))
85 return 0;
86
87 *pc = preempt_count();
88 preempt_disable_notrace();
89
90 cpu = raw_smp_processor_id();
91 if (cpu != wakeup_current_cpu)
92 goto out_enable;
93
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050094 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt542181d2010-10-05 16:38:49 -040095 disabled = atomic_inc_return(&(*data)->disabled);
96 if (unlikely(disabled != 1))
97 goto out;
98
99 return 1;
100
101out:
102 atomic_dec(&(*data)->disabled);
103
104out_enable:
105 preempt_enable_notrace();
106 return 0;
107}
108
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400109/*
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200110 * wakeup uses its own tracer function to keep the overhead down:
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400111 */
112static void
Steven Rostedta1e2e312011-08-09 12:50:46 -0400113wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
114 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400115{
116 struct trace_array *tr = wakeup_trace;
117 struct trace_array_cpu *data;
118 unsigned long flags;
Steven Rostedt38697052008-10-01 13:14:09 -0400119 int pc;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400120
Steven Rostedt542181d2010-10-05 16:38:49 -0400121 if (!func_prolog_preempt_disable(tr, &data, &pc))
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400122 return;
123
Steven Rostedte59494f2008-07-16 00:13:45 -0400124 local_irq_save(flags);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500125 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedte59494f2008-07-16 00:13:45 -0400126 local_irq_restore(flags);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400127
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400128 atomic_dec(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -0400129 preempt_enable_notrace();
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400130}
Steven Rostedt7e407982010-10-19 10:56:19 -0400131#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200132
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500133static int register_wakeup_function(struct trace_array *tr, int graph, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400134{
135 int ret;
136
137 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
138 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
139 return 0;
140
141 if (graph)
142 ret = register_ftrace_graph(&wakeup_graph_return,
143 &wakeup_graph_entry);
144 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500145 ret = register_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400146
147 if (!ret)
148 function_enabled = true;
149
150 return ret;
151}
152
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500153static void unregister_wakeup_function(struct trace_array *tr, int graph)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400154{
155 if (!function_enabled)
156 return;
157
158 if (graph)
159 unregister_ftrace_graph();
160 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500161 unregister_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400162
163 function_enabled = false;
164}
165
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500166static void wakeup_function_set(struct trace_array *tr, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400167{
168 if (set)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500169 register_wakeup_function(tr, is_graph(), 1);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400170 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500171 unregister_wakeup_function(tr, is_graph());
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400172}
173
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500174static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400175{
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500176 struct tracer *tracer = tr->current_trace;
177
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400178 if (mask & TRACE_ITER_FUNCTION)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500179 wakeup_function_set(tr, set);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400180
181 return trace_keep_overwrite(tracer, mask, set);
182}
183
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500184static int start_func_tracer(struct trace_array *tr, int graph)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200185{
186 int ret;
187
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500188 ret = register_wakeup_function(tr, graph, 0);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200189
190 if (!ret && tracing_is_enabled())
191 tracer_enabled = 1;
192 else
193 tracer_enabled = 0;
194
195 return ret;
196}
197
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500198static void stop_func_tracer(struct trace_array *tr, int graph)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200199{
200 tracer_enabled = 0;
201
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500202 unregister_wakeup_function(tr, graph);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200203}
204
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200205#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500206static int
207wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200208{
209
210 if (!(bit & TRACE_DISPLAY_GRAPH))
211 return -EINVAL;
212
213 if (!(is_graph() ^ set))
214 return 0;
215
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500216 stop_func_tracer(tr, !set);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200217
218 wakeup_reset(wakeup_trace);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500219 tr->max_latency = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200220
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500221 return start_func_tracer(tr, set);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200222}
223
224static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
225{
226 struct trace_array *tr = wakeup_trace;
227 struct trace_array_cpu *data;
228 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400229 int pc, ret = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200230
Steven Rostedt542181d2010-10-05 16:38:49 -0400231 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200232 return 0;
233
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200234 local_save_flags(flags);
235 ret = __trace_graph_entry(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200236 atomic_dec(&data->disabled);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200237 preempt_enable_notrace();
Steven Rostedt542181d2010-10-05 16:38:49 -0400238
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200239 return ret;
240}
241
242static void wakeup_graph_return(struct ftrace_graph_ret *trace)
243{
244 struct trace_array *tr = wakeup_trace;
245 struct trace_array_cpu *data;
246 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400247 int pc;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200248
Steven Rostedt542181d2010-10-05 16:38:49 -0400249 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200250 return;
251
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200252 local_save_flags(flags);
253 __trace_graph_return(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200254 atomic_dec(&data->disabled);
255
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200256 preempt_enable_notrace();
257 return;
258}
259
260static void wakeup_trace_open(struct trace_iterator *iter)
261{
262 if (is_graph())
263 graph_trace_open(iter);
264}
265
266static void wakeup_trace_close(struct trace_iterator *iter)
267{
268 if (iter->private)
269 graph_trace_close(iter);
270}
271
Jiri Olsa321e68b2011-06-03 16:58:47 +0200272#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
273 TRACE_GRAPH_PRINT_ABS_TIME | \
274 TRACE_GRAPH_PRINT_DURATION)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200275
276static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
277{
278 /*
279 * In graph mode call the graph tracer output function,
280 * otherwise go with the TRACE_FN event handler
281 */
282 if (is_graph())
283 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
284
285 return TRACE_TYPE_UNHANDLED;
286}
287
288static void wakeup_print_header(struct seq_file *s)
289{
290 if (is_graph())
291 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
292 else
293 trace_default_header(s);
294}
295
296static void
297__trace_function(struct trace_array *tr,
298 unsigned long ip, unsigned long parent_ip,
299 unsigned long flags, int pc)
300{
301 if (is_graph())
302 trace_graph_function(tr, ip, parent_ip, flags, pc);
303 else
304 trace_function(tr, ip, parent_ip, flags, pc);
305}
306#else
307#define __trace_function trace_function
308
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500309static int
310wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200311{
312 return -EINVAL;
313}
314
315static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
316{
317 return -1;
318}
319
320static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
321{
322 return TRACE_TYPE_UNHANDLED;
323}
324
325static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200326static void wakeup_trace_open(struct trace_iterator *iter) { }
327static void wakeup_trace_close(struct trace_iterator *iter) { }
Jiri Olsa7e9a49e2011-11-07 16:08:49 +0100328
329#ifdef CONFIG_FUNCTION_TRACER
330static void wakeup_print_header(struct seq_file *s)
331{
332 trace_default_header(s);
333}
334#else
335static void wakeup_print_header(struct seq_file *s)
336{
337 trace_latency_header(s);
338}
339#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200340#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
341
Steven Rostedt352ad252008-05-12 21:20:42 +0200342/*
343 * Should this new latency be reported/recorded?
344 */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500345static int report_latency(struct trace_array *tr, cycle_t delta)
Steven Rostedt352ad252008-05-12 21:20:42 +0200346{
347 if (tracing_thresh) {
348 if (delta < tracing_thresh)
349 return 0;
350 } else {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500351 if (delta <= tr->max_latency)
Steven Rostedt352ad252008-05-12 21:20:42 +0200352 return 0;
353 }
354 return 1;
355}
356
Steven Rostedt38516ab2010-04-20 17:04:50 -0400357static void
358probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
Steven Rostedt478142c32009-09-09 10:36:01 -0400359{
360 if (task != wakeup_task)
361 return;
362
363 wakeup_current_cpu = cpu;
364}
365
Steven Rostedt (Red Hat)243f7612014-10-30 20:44:53 -0400366static void
367tracing_sched_switch_trace(struct trace_array *tr,
368 struct task_struct *prev,
369 struct task_struct *next,
370 unsigned long flags, int pc)
371{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400372 struct trace_event_call *call = &event_context_switch;
Steven Rostedt (Red Hat)243f7612014-10-30 20:44:53 -0400373 struct ring_buffer *buffer = tr->trace_buffer.buffer;
374 struct ring_buffer_event *event;
375 struct ctx_switch_entry *entry;
376
377 event = trace_buffer_lock_reserve(buffer, TRACE_CTX,
378 sizeof(*entry), flags, pc);
379 if (!event)
380 return;
381 entry = ring_buffer_event_data(event);
382 entry->prev_pid = prev->pid;
383 entry->prev_prio = prev->prio;
384 entry->prev_state = prev->state;
385 entry->next_pid = next->pid;
386 entry->next_prio = next->prio;
387 entry->next_state = next->state;
388 entry->next_cpu = task_cpu(next);
389
390 if (!call_filter_check_discard(call, entry, buffer, event))
391 trace_buffer_unlock_commit(buffer, event, flags, pc);
392}
393
394static void
395tracing_sched_wakeup_trace(struct trace_array *tr,
396 struct task_struct *wakee,
397 struct task_struct *curr,
398 unsigned long flags, int pc)
399{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400400 struct trace_event_call *call = &event_wakeup;
Steven Rostedt (Red Hat)243f7612014-10-30 20:44:53 -0400401 struct ring_buffer_event *event;
402 struct ctx_switch_entry *entry;
403 struct ring_buffer *buffer = tr->trace_buffer.buffer;
404
405 event = trace_buffer_lock_reserve(buffer, TRACE_WAKE,
406 sizeof(*entry), flags, pc);
407 if (!event)
408 return;
409 entry = ring_buffer_event_data(event);
410 entry->prev_pid = curr->pid;
411 entry->prev_prio = curr->prio;
412 entry->prev_state = curr->state;
413 entry->next_pid = wakee->pid;
414 entry->next_prio = wakee->prio;
415 entry->next_state = wakee->state;
416 entry->next_cpu = task_cpu(wakee);
417
418 if (!call_filter_check_discard(call, entry, buffer, event))
419 trace_buffer_unlock_commit(buffer, event, flags, pc);
420}
421
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200422static void notrace
Steven Rostedt38516ab2010-04-20 17:04:50 -0400423probe_wakeup_sched_switch(void *ignore,
424 struct task_struct *prev, struct task_struct *next)
Steven Rostedt352ad252008-05-12 21:20:42 +0200425{
Steven Rostedt352ad252008-05-12 21:20:42 +0200426 struct trace_array_cpu *data;
427 cycle_t T0, T1, delta;
428 unsigned long flags;
429 long disabled;
430 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400431 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200432
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400433 tracing_record_cmdline(prev);
434
Steven Rostedt352ad252008-05-12 21:20:42 +0200435 if (unlikely(!tracer_enabled))
436 return;
437
438 /*
439 * When we start a new trace, we set wakeup_task to NULL
440 * and then set tracer_enabled = 1. We want to make sure
441 * that another CPU does not see the tracer_enabled = 1
442 * and the wakeup_task with an older task, that might
443 * actually be the same as next.
444 */
445 smp_rmb();
446
447 if (next != wakeup_task)
448 return;
449
Steven Rostedt38697052008-10-01 13:14:09 -0400450 pc = preempt_count();
451
Steven Rostedt352ad252008-05-12 21:20:42 +0200452 /* disable local data, not wakeup_cpu data */
453 cpu = raw_smp_processor_id();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500454 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200455 if (likely(disabled != 1))
456 goto out;
457
Steven Rostedte59494f2008-07-16 00:13:45 -0400458 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100459 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200460
461 /* We could race with grabbing wakeup_lock */
462 if (unlikely(!tracer_enabled || next != wakeup_task))
463 goto out_unlock;
464
Steven Rostedt9be24412009-03-26 10:25:24 -0400465 /* The task we are waiting for is waking up */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500466 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedt9be24412009-03-26 10:25:24 -0400467
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200468 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500469 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200470
Steven Rostedt352ad252008-05-12 21:20:42 +0200471 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200472 T1 = ftrace_now(cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200473 delta = T1-T0;
474
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500475 if (!report_latency(wakeup_trace, delta))
Steven Rostedt352ad252008-05-12 21:20:42 +0200476 goto out_unlock;
477
Carsten Emdeb5130b12009-09-13 01:43:07 +0200478 if (likely(!is_tracing_stopped())) {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500479 wakeup_trace->max_latency = delta;
Carsten Emdeb5130b12009-09-13 01:43:07 +0200480 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
481 }
Steven Rostedt352ad252008-05-12 21:20:42 +0200482
Steven Rostedt352ad252008-05-12 21:20:42 +0200483out_unlock:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400484 __wakeup_reset(wakeup_trace);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100485 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400486 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200487out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500488 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200489}
490
Ingo Molnare309b412008-05-12 21:20:51 +0200491static void __wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200492{
Steven Rostedt352ad252008-05-12 21:20:42 +0200493 wakeup_cpu = -1;
494 wakeup_prio = -1;
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100495 tracing_dl = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200496
497 if (wakeup_task)
498 put_task_struct(wakeup_task);
499
500 wakeup_task = NULL;
501}
502
Ingo Molnare309b412008-05-12 21:20:51 +0200503static void wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200504{
505 unsigned long flags;
506
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500507 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -0400508
Steven Rostedte59494f2008-07-16 00:13:45 -0400509 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100510 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200511 __wakeup_reset(tr);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100512 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400513 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200514}
515
Ingo Molnare309b412008-05-12 21:20:51 +0200516static void
Steven Rostedt38516ab2010-04-20 17:04:50 -0400517probe_wakeup(void *ignore, struct task_struct *p, int success)
Steven Rostedt352ad252008-05-12 21:20:42 +0200518{
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500519 struct trace_array_cpu *data;
Steven Rostedt352ad252008-05-12 21:20:42 +0200520 int cpu = smp_processor_id();
521 unsigned long flags;
522 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400523 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200524
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400525 if (likely(!tracer_enabled))
Steven Rostedt352ad252008-05-12 21:20:42 +0200526 return;
527
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400528 tracing_record_cmdline(p);
529 tracing_record_cmdline(current);
530
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100531 /*
532 * Semantic is like this:
533 * - wakeup tracer handles all tasks in the system, independently
534 * from their scheduling class;
535 * - wakeup_rt tracer handles tasks belonging to sched_dl and
536 * sched_rt class;
537 * - wakeup_dl handles tasks belonging to sched_dl class only.
538 */
539 if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
540 (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
541 (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400542 return;
543
Steven Rostedt38697052008-10-01 13:14:09 -0400544 pc = preempt_count();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500545 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200546 if (unlikely(disabled != 1))
547 goto out;
548
549 /* interrupts should be off from try_to_wake_up */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100550 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200551
552 /* check for races. */
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100553 if (!tracer_enabled || tracing_dl ||
554 (!dl_task(p) && p->prio >= wakeup_prio))
Steven Rostedt352ad252008-05-12 21:20:42 +0200555 goto out_locked;
556
557 /* reset the trace */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400558 __wakeup_reset(wakeup_trace);
Steven Rostedt352ad252008-05-12 21:20:42 +0200559
560 wakeup_cpu = task_cpu(p);
Steven Rostedt478142c32009-09-09 10:36:01 -0400561 wakeup_current_cpu = wakeup_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +0200562 wakeup_prio = p->prio;
563
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100564 /*
565 * Once you start tracing a -deadline task, don't bother tracing
566 * another task until the first one wakes up.
567 */
568 if (dl_task(p))
569 tracing_dl = 1;
570 else
571 tracing_dl = 0;
572
Steven Rostedt352ad252008-05-12 21:20:42 +0200573 wakeup_task = p;
574 get_task_struct(wakeup_task);
575
576 local_save_flags(flags);
577
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500578 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500579 data->preempt_timestamp = ftrace_now(cpu);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500580 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
Steven Rostedt301fd742009-04-03 11:12:23 -0400581
582 /*
583 * We must be careful in using CALLER_ADDR2. But since wake_up
584 * is not called by an assembly function (where as schedule is)
585 * it should be safe to use it here.
586 */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200587 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200588
589out_locked:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100590 arch_spin_unlock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200591out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500592 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200593}
594
Ingo Molnare309b412008-05-12 21:20:51 +0200595static void start_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200596{
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200597 int ret;
598
Steven Rostedt38516ab2010-04-20 17:04:50 -0400599 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200600 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400601 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200602 " probe to kernel_sched_wakeup\n");
603 return;
604 }
605
Steven Rostedt38516ab2010-04-20 17:04:50 -0400606 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200607 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400608 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200609 " probe to kernel_sched_wakeup_new\n");
610 goto fail_deprobe;
611 }
612
Steven Rostedt38516ab2010-04-20 17:04:50 -0400613 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200614 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400615 pr_info("sched trace: Couldn't activate tracepoint"
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500616 " probe to kernel_sched_switch\n");
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200617 goto fail_deprobe_wake_new;
618 }
619
Steven Rostedt38516ab2010-04-20 17:04:50 -0400620 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt478142c32009-09-09 10:36:01 -0400621 if (ret) {
622 pr_info("wakeup trace: Couldn't activate tracepoint"
623 " probe to kernel_sched_migrate_task\n");
624 return;
625 }
626
Steven Rostedt352ad252008-05-12 21:20:42 +0200627 wakeup_reset(tr);
628
629 /*
630 * Don't let the tracer_enabled = 1 show up before
631 * the wakeup_task is reset. This may be overkill since
632 * wakeup_reset does a spin_unlock after setting the
633 * wakeup_task to NULL, but I want to be safe.
634 * This is a slow path anyway.
635 */
636 smp_wmb();
637
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500638 if (start_func_tracer(tr, is_graph()))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200639 printk(KERN_ERR "failed to start wakeup tracer\n");
Steven Rostedtad591242008-07-10 20:58:13 -0400640
Steven Rostedt352ad252008-05-12 21:20:42 +0200641 return;
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200642fail_deprobe_wake_new:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400643 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200644fail_deprobe:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400645 unregister_trace_sched_wakeup(probe_wakeup, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200646}
647
Ingo Molnare309b412008-05-12 21:20:51 +0200648static void stop_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200649{
650 tracer_enabled = 0;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500651 stop_func_tracer(tr, is_graph());
Steven Rostedt38516ab2010-04-20 17:04:50 -0400652 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
653 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
654 unregister_trace_sched_wakeup(probe_wakeup, NULL);
655 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200656}
657
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500658static bool wakeup_busy;
659
Steven Rostedt32443512009-01-21 16:24:46 -0500660static int __wakeup_tracer_init(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200661{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400662 save_flags = trace_flags;
663
664 /* non overwrite screws up the latency tracers */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400665 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
666 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500667
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500668 tr->max_latency = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200669 wakeup_trace = tr;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500670 ftrace_init_array_ops(tr, wakeup_tracer_call);
Steven Rostedtc76f0692008-11-07 22:36:02 -0500671 start_wakeup_tracer(tr);
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500672
673 wakeup_busy = true;
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100674 return 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200675}
676
Steven Rostedt32443512009-01-21 16:24:46 -0500677static int wakeup_tracer_init(struct trace_array *tr)
678{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500679 if (wakeup_busy)
680 return -EBUSY;
681
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100682 wakeup_dl = 0;
Steven Rostedt32443512009-01-21 16:24:46 -0500683 wakeup_rt = 0;
684 return __wakeup_tracer_init(tr);
685}
686
687static int wakeup_rt_tracer_init(struct trace_array *tr)
688{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500689 if (wakeup_busy)
690 return -EBUSY;
691
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100692 wakeup_dl = 0;
Steven Rostedt32443512009-01-21 16:24:46 -0500693 wakeup_rt = 1;
694 return __wakeup_tracer_init(tr);
695}
696
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100697static int wakeup_dl_tracer_init(struct trace_array *tr)
698{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500699 if (wakeup_busy)
700 return -EBUSY;
701
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100702 wakeup_dl = 1;
703 wakeup_rt = 0;
704 return __wakeup_tracer_init(tr);
705}
706
Ingo Molnare309b412008-05-12 21:20:51 +0200707static void wakeup_tracer_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200708{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400709 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
710 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
711
Steven Rostedtc76f0692008-11-07 22:36:02 -0500712 stop_wakeup_tracer(tr);
713 /* make sure we put back any tasks we are tracing */
714 wakeup_reset(tr);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500715
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400716 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
717 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500718 ftrace_reset_array_ops(tr);
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500719 wakeup_busy = false;
Steven Rostedt352ad252008-05-12 21:20:42 +0200720}
721
Steven Rostedt90369902008-11-05 16:05:44 -0500722static void wakeup_tracer_start(struct trace_array *tr)
723{
724 wakeup_reset(tr);
725 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -0500726}
727
728static void wakeup_tracer_stop(struct trace_array *tr)
729{
730 tracer_enabled = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200731}
732
733static struct tracer wakeup_tracer __read_mostly =
734{
735 .name = "wakeup",
736 .init = wakeup_tracer_init,
737 .reset = wakeup_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500738 .start = wakeup_tracer_start,
739 .stop = wakeup_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900740 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200741 .print_header = wakeup_print_header,
742 .print_line = wakeup_print_line,
743 .flags = &tracer_flags,
744 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400745 .flag_changed = wakeup_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200746#ifdef CONFIG_FTRACE_SELFTEST
747 .selftest = trace_selftest_startup_wakeup,
748#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200749 .open = wakeup_trace_open,
750 .close = wakeup_trace_close,
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500751 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900752 .use_max_tr = true,
Steven Rostedt352ad252008-05-12 21:20:42 +0200753};
754
Steven Rostedt32443512009-01-21 16:24:46 -0500755static struct tracer wakeup_rt_tracer __read_mostly =
756{
757 .name = "wakeup_rt",
758 .init = wakeup_rt_tracer_init,
759 .reset = wakeup_tracer_reset,
760 .start = wakeup_tracer_start,
761 .stop = wakeup_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900762 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200763 .print_header = wakeup_print_header,
764 .print_line = wakeup_print_line,
765 .flags = &tracer_flags,
766 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400767 .flag_changed = wakeup_flag_changed,
Steven Rostedt32443512009-01-21 16:24:46 -0500768#ifdef CONFIG_FTRACE_SELFTEST
769 .selftest = trace_selftest_startup_wakeup,
770#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200771 .open = wakeup_trace_open,
772 .close = wakeup_trace_close,
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500773 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900774 .use_max_tr = true,
Steven Rostedt32443512009-01-21 16:24:46 -0500775};
776
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100777static struct tracer wakeup_dl_tracer __read_mostly =
778{
779 .name = "wakeup_dl",
780 .init = wakeup_dl_tracer_init,
781 .reset = wakeup_tracer_reset,
782 .start = wakeup_tracer_start,
783 .stop = wakeup_tracer_stop,
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100784 .print_max = true,
785 .print_header = wakeup_print_header,
786 .print_line = wakeup_print_line,
787 .flags = &tracer_flags,
788 .set_flag = wakeup_set_flag,
789 .flag_changed = wakeup_flag_changed,
790#ifdef CONFIG_FTRACE_SELFTEST
791 .selftest = trace_selftest_startup_wakeup,
792#endif
793 .open = wakeup_trace_open,
794 .close = wakeup_trace_close,
795 .use_max_tr = true,
796};
797
Steven Rostedt352ad252008-05-12 21:20:42 +0200798__init static int init_wakeup_tracer(void)
799{
800 int ret;
801
802 ret = register_tracer(&wakeup_tracer);
803 if (ret)
804 return ret;
805
Steven Rostedt32443512009-01-21 16:24:46 -0500806 ret = register_tracer(&wakeup_rt_tracer);
807 if (ret)
808 return ret;
809
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100810 ret = register_tracer(&wakeup_dl_tracer);
811 if (ret)
812 return ret;
813
Steven Rostedt352ad252008-05-12 21:20:42 +0200814 return 0;
815}
Steven Rostedt6f415672012-10-05 12:13:07 -0400816core_initcall(init_wakeup_tracer);