ftrace: preempt disable over interrupt disable
[deliverable/linux.git] / kernel / trace / trace_sched_wakeup.c
CommitLineData
352ad25a
SR
1/*
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
10 * Copyright (C) 2004 William Lee Irwin III
11 */
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>
b07c3f19 18#include <trace/sched.h>
352ad25a
SR
19
20#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;
27static unsigned wakeup_prio = -1;
28
e59494f4
SR
29static raw_spinlock_t wakeup_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
352ad25a 31
e309b41d 32static void __wakeup_reset(struct trace_array *tr);
352ad25a 33
7e18d8e7
SR
34#ifdef CONFIG_FTRACE
35/*
36 * irqsoff uses its own tracer function to keep the overhead down:
37 */
38static void
39wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
40{
41 struct trace_array *tr = wakeup_trace;
42 struct trace_array_cpu *data;
43 unsigned long flags;
44 long disabled;
45 int resched;
46 int cpu;
38697053 47 int pc;
7e18d8e7
SR
48
49 if (likely(!wakeup_task))
50 return;
51
38697053 52 pc = preempt_count();
7e18d8e7
SR
53 resched = need_resched();
54 preempt_disable_notrace();
55
56 cpu = raw_smp_processor_id();
57 data = tr->data[cpu];
58 disabled = atomic_inc_return(&data->disabled);
59 if (unlikely(disabled != 1))
60 goto out;
61
e59494f4
SR
62 local_irq_save(flags);
63 __raw_spin_lock(&wakeup_lock);
7e18d8e7
SR
64
65 if (unlikely(!wakeup_task))
66 goto unlock;
67
68 /*
69 * The task can't disappear because it needs to
70 * wake up first, and we have the wakeup_lock.
71 */
72 if (task_cpu(wakeup_task) != cpu)
73 goto unlock;
74
38697053 75 trace_function(tr, data, ip, parent_ip, flags, pc);
7e18d8e7
SR
76
77 unlock:
e59494f4
SR
78 __raw_spin_unlock(&wakeup_lock);
79 local_irq_restore(flags);
7e18d8e7
SR
80
81 out:
82 atomic_dec(&data->disabled);
83
84 /*
85 * To prevent recursion from the scheduler, if the
86 * resched flag was set before we entered, then
87 * don't reschedule.
88 */
89 if (resched)
90 preempt_enable_no_resched_notrace();
91 else
92 preempt_enable_notrace();
93}
94
95static struct ftrace_ops trace_ops __read_mostly =
96{
97 .func = wakeup_tracer_call,
98};
99#endif /* CONFIG_FTRACE */
100
352ad25a
SR
101/*
102 * Should this new latency be reported/recorded?
103 */
e309b41d 104static int report_latency(cycle_t delta)
352ad25a
SR
105{
106 if (tracing_thresh) {
107 if (delta < tracing_thresh)
108 return 0;
109 } else {
110 if (delta <= tracing_max_latency)
111 return 0;
112 }
113 return 1;
114}
115
5b82a1b0 116static void notrace
b07c3f19 117probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
5b82a1b0 118 struct task_struct *next)
352ad25a
SR
119{
120 unsigned long latency = 0, t0 = 0, t1 = 0;
352ad25a
SR
121 struct trace_array_cpu *data;
122 cycle_t T0, T1, delta;
123 unsigned long flags;
124 long disabled;
125 int cpu;
38697053 126 int pc;
352ad25a 127
b07c3f19
MD
128 tracing_record_cmdline(prev);
129
352ad25a
SR
130 if (unlikely(!tracer_enabled))
131 return;
132
133 /*
134 * When we start a new trace, we set wakeup_task to NULL
135 * and then set tracer_enabled = 1. We want to make sure
136 * that another CPU does not see the tracer_enabled = 1
137 * and the wakeup_task with an older task, that might
138 * actually be the same as next.
139 */
140 smp_rmb();
141
142 if (next != wakeup_task)
143 return;
144
38697053
SR
145 pc = preempt_count();
146
7e18d8e7 147 /* The task we are waiting for is waking up */
b07c3f19 148 data = wakeup_trace->data[wakeup_cpu];
352ad25a
SR
149
150 /* disable local data, not wakeup_cpu data */
151 cpu = raw_smp_processor_id();
b07c3f19 152 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
153 if (likely(disabled != 1))
154 goto out;
155
e59494f4
SR
156 local_irq_save(flags);
157 __raw_spin_lock(&wakeup_lock);
352ad25a
SR
158
159 /* We could race with grabbing wakeup_lock */
160 if (unlikely(!tracer_enabled || next != wakeup_task))
161 goto out_unlock;
162
38697053 163 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
352ad25a
SR
164
165 /*
166 * usecs conversion is slow so we try to delay the conversion
167 * as long as possible:
168 */
169 T0 = data->preempt_timestamp;
750ed1a4 170 T1 = ftrace_now(cpu);
352ad25a
SR
171 delta = T1-T0;
172
173 if (!report_latency(delta))
174 goto out_unlock;
175
176 latency = nsecs_to_usecs(delta);
177
178 tracing_max_latency = delta;
179 t0 = nsecs_to_usecs(T0);
180 t1 = nsecs_to_usecs(T1);
181
b07c3f19 182 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
352ad25a 183
352ad25a 184out_unlock:
b07c3f19 185 __wakeup_reset(wakeup_trace);
e59494f4
SR
186 __raw_spin_unlock(&wakeup_lock);
187 local_irq_restore(flags);
352ad25a 188out:
b07c3f19 189 atomic_dec(&wakeup_trace->data[cpu]->disabled);
5b82a1b0
MD
190}
191
e309b41d 192static void __wakeup_reset(struct trace_array *tr)
352ad25a
SR
193{
194 struct trace_array_cpu *data;
195 int cpu;
196
352ad25a
SR
197 for_each_possible_cpu(cpu) {
198 data = tr->data[cpu];
3928a8a2 199 tracing_reset(tr, cpu);
352ad25a
SR
200 }
201
202 wakeup_cpu = -1;
203 wakeup_prio = -1;
204
205 if (wakeup_task)
206 put_task_struct(wakeup_task);
207
208 wakeup_task = NULL;
209}
210
e309b41d 211static void wakeup_reset(struct trace_array *tr)
352ad25a
SR
212{
213 unsigned long flags;
214
e59494f4
SR
215 local_irq_save(flags);
216 __raw_spin_lock(&wakeup_lock);
352ad25a 217 __wakeup_reset(tr);
e59494f4
SR
218 __raw_spin_unlock(&wakeup_lock);
219 local_irq_restore(flags);
352ad25a
SR
220}
221
e309b41d 222static void
b07c3f19 223probe_wakeup(struct rq *rq, struct task_struct *p)
352ad25a
SR
224{
225 int cpu = smp_processor_id();
226 unsigned long flags;
227 long disabled;
38697053 228 int pc;
352ad25a 229
b07c3f19
MD
230 if (likely(!tracer_enabled))
231 return;
232
233 tracing_record_cmdline(p);
234 tracing_record_cmdline(current);
235
352ad25a
SR
236 if (likely(!rt_task(p)) ||
237 p->prio >= wakeup_prio ||
b07c3f19 238 p->prio >= current->prio)
352ad25a
SR
239 return;
240
38697053 241 pc = preempt_count();
b07c3f19 242 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
243 if (unlikely(disabled != 1))
244 goto out;
245
246 /* interrupts should be off from try_to_wake_up */
e59494f4 247 __raw_spin_lock(&wakeup_lock);
352ad25a
SR
248
249 /* check for races. */
250 if (!tracer_enabled || p->prio >= wakeup_prio)
251 goto out_locked;
252
253 /* reset the trace */
b07c3f19 254 __wakeup_reset(wakeup_trace);
352ad25a
SR
255
256 wakeup_cpu = task_cpu(p);
257 wakeup_prio = p->prio;
258
259 wakeup_task = p;
260 get_task_struct(wakeup_task);
261
262 local_save_flags(flags);
263
b07c3f19
MD
264 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
265 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
38697053 266 CALLER_ADDR1, CALLER_ADDR2, flags, pc);
352ad25a
SR
267
268out_locked:
e59494f4 269 __raw_spin_unlock(&wakeup_lock);
352ad25a 270out:
b07c3f19 271 atomic_dec(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
272}
273
e309b41d 274static void start_wakeup_tracer(struct trace_array *tr)
352ad25a 275{
5b82a1b0
MD
276 int ret;
277
b07c3f19 278 ret = register_trace_sched_wakeup(probe_wakeup);
5b82a1b0 279 if (ret) {
b07c3f19 280 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
281 " probe to kernel_sched_wakeup\n");
282 return;
283 }
284
b07c3f19 285 ret = register_trace_sched_wakeup_new(probe_wakeup);
5b82a1b0 286 if (ret) {
b07c3f19 287 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
288 " probe to kernel_sched_wakeup_new\n");
289 goto fail_deprobe;
290 }
291
b07c3f19 292 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
5b82a1b0 293 if (ret) {
b07c3f19 294 pr_info("sched trace: Couldn't activate tracepoint"
5b82a1b0
MD
295 " probe to kernel_sched_schedule\n");
296 goto fail_deprobe_wake_new;
297 }
298
352ad25a
SR
299 wakeup_reset(tr);
300
301 /*
302 * Don't let the tracer_enabled = 1 show up before
303 * the wakeup_task is reset. This may be overkill since
304 * wakeup_reset does a spin_unlock after setting the
305 * wakeup_task to NULL, but I want to be safe.
306 * This is a slow path anyway.
307 */
308 smp_wmb();
309
7e18d8e7 310 register_ftrace_function(&trace_ops);
352ad25a 311
ad591240
SR
312 tracer_enabled = 1;
313
352ad25a 314 return;
5b82a1b0 315fail_deprobe_wake_new:
b07c3f19 316 unregister_trace_sched_wakeup_new(probe_wakeup);
5b82a1b0 317fail_deprobe:
b07c3f19 318 unregister_trace_sched_wakeup(probe_wakeup);
352ad25a
SR
319}
320
e309b41d 321static void stop_wakeup_tracer(struct trace_array *tr)
352ad25a
SR
322{
323 tracer_enabled = 0;
7e18d8e7 324 unregister_ftrace_function(&trace_ops);
b07c3f19
MD
325 unregister_trace_sched_switch(probe_wakeup_sched_switch);
326 unregister_trace_sched_wakeup_new(probe_wakeup);
327 unregister_trace_sched_wakeup(probe_wakeup);
352ad25a
SR
328}
329
e309b41d 330static void wakeup_tracer_init(struct trace_array *tr)
352ad25a
SR
331{
332 wakeup_trace = tr;
333
334 if (tr->ctrl)
335 start_wakeup_tracer(tr);
336}
337
e309b41d 338static void wakeup_tracer_reset(struct trace_array *tr)
352ad25a
SR
339{
340 if (tr->ctrl) {
341 stop_wakeup_tracer(tr);
342 /* make sure we put back any tasks we are tracing */
343 wakeup_reset(tr);
344 }
345}
346
347static void wakeup_tracer_ctrl_update(struct trace_array *tr)
348{
349 if (tr->ctrl)
350 start_wakeup_tracer(tr);
351 else
352 stop_wakeup_tracer(tr);
353}
354
e309b41d 355static void wakeup_tracer_open(struct trace_iterator *iter)
352ad25a
SR
356{
357 /* stop the trace while dumping */
358 if (iter->tr->ctrl)
359 stop_wakeup_tracer(iter->tr);
360}
361
e309b41d 362static void wakeup_tracer_close(struct trace_iterator *iter)
352ad25a
SR
363{
364 /* forget about any processes we were recording */
365 if (iter->tr->ctrl)
366 start_wakeup_tracer(iter->tr);
367}
368
369static struct tracer wakeup_tracer __read_mostly =
370{
371 .name = "wakeup",
372 .init = wakeup_tracer_init,
373 .reset = wakeup_tracer_reset,
374 .open = wakeup_tracer_open,
375 .close = wakeup_tracer_close,
376 .ctrl_update = wakeup_tracer_ctrl_update,
377 .print_max = 1,
60a11774
SR
378#ifdef CONFIG_FTRACE_SELFTEST
379 .selftest = trace_selftest_startup_wakeup,
380#endif
352ad25a
SR
381};
382
383__init static int init_wakeup_tracer(void)
384{
385 int ret;
386
387 ret = register_tracer(&wakeup_tracer);
388 if (ret)
389 return ret;
390
391 return 0;
392}
393device_initcall(init_wakeup_tracer);
This page took 0.067471 seconds and 5 git commands to generate.