tracing: do not grab lock in wakeup latency function tracing
[safe/jmp/linux-2.6] / kernel / trace / trace_sched_wakeup.c
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>
18 #include <trace/events/sched.h>
19
20 #include "trace.h"
21
22 static struct trace_array       *wakeup_trace;
23 static int __read_mostly        tracer_enabled;
24
25 static struct task_struct       *wakeup_task;
26 static int                      wakeup_cpu;
27 static int                      wakeup_current_cpu;
28 static unsigned                 wakeup_prio = -1;
29 static int                      wakeup_rt;
30
31 static raw_spinlock_t wakeup_lock =
32         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
33
34 static void __wakeup_reset(struct trace_array *tr);
35
36 static int save_lat_flag;
37
38 #ifdef CONFIG_FUNCTION_TRACER
39 /*
40  * irqsoff uses its own tracer function to keep the overhead down:
41  */
42 static void
43 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
44 {
45         struct trace_array *tr = wakeup_trace;
46         struct trace_array_cpu *data;
47         unsigned long flags;
48         long disabled;
49         int resched;
50         int cpu;
51         int pc;
52
53         if (likely(!wakeup_task))
54                 return;
55
56         pc = preempt_count();
57         resched = ftrace_preempt_disable();
58
59         cpu = raw_smp_processor_id();
60         if (cpu != wakeup_current_cpu)
61                 goto out_enable;
62
63         data = tr->data[cpu];
64         disabled = atomic_inc_return(&data->disabled);
65         if (unlikely(disabled != 1))
66                 goto out;
67
68         local_irq_save(flags);
69
70         trace_function(tr, ip, parent_ip, flags, pc);
71
72         local_irq_restore(flags);
73
74  out:
75         atomic_dec(&data->disabled);
76  out_enable:
77         ftrace_preempt_enable(resched);
78 }
79
80 static struct ftrace_ops trace_ops __read_mostly =
81 {
82         .func = wakeup_tracer_call,
83 };
84 #endif /* CONFIG_FUNCTION_TRACER */
85
86 /*
87  * Should this new latency be reported/recorded?
88  */
89 static int report_latency(cycle_t delta)
90 {
91         if (tracing_thresh) {
92                 if (delta < tracing_thresh)
93                         return 0;
94         } else {
95                 if (delta <= tracing_max_latency)
96                         return 0;
97         }
98         return 1;
99 }
100
101 static void probe_wakeup_migrate_task(struct task_struct *task, int cpu)
102 {
103         if (task != wakeup_task)
104                 return;
105
106         wakeup_current_cpu = cpu;
107 }
108
109 static void notrace
110 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
111         struct task_struct *next)
112 {
113         unsigned long latency = 0, t0 = 0, t1 = 0;
114         struct trace_array_cpu *data;
115         cycle_t T0, T1, delta;
116         unsigned long flags;
117         long disabled;
118         int cpu;
119         int pc;
120
121         tracing_record_cmdline(prev);
122
123         if (unlikely(!tracer_enabled))
124                 return;
125
126         /*
127          * When we start a new trace, we set wakeup_task to NULL
128          * and then set tracer_enabled = 1. We want to make sure
129          * that another CPU does not see the tracer_enabled = 1
130          * and the wakeup_task with an older task, that might
131          * actually be the same as next.
132          */
133         smp_rmb();
134
135         if (next != wakeup_task)
136                 return;
137
138         pc = preempt_count();
139
140         /* disable local data, not wakeup_cpu data */
141         cpu = raw_smp_processor_id();
142         disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
143         if (likely(disabled != 1))
144                 goto out;
145
146         local_irq_save(flags);
147         __raw_spin_lock(&wakeup_lock);
148
149         /* We could race with grabbing wakeup_lock */
150         if (unlikely(!tracer_enabled || next != wakeup_task))
151                 goto out_unlock;
152
153         /* The task we are waiting for is waking up */
154         data = wakeup_trace->data[wakeup_cpu];
155
156         trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
157         tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
158
159         /*
160          * usecs conversion is slow so we try to delay the conversion
161          * as long as possible:
162          */
163         T0 = data->preempt_timestamp;
164         T1 = ftrace_now(cpu);
165         delta = T1-T0;
166
167         if (!report_latency(delta))
168                 goto out_unlock;
169
170         latency = nsecs_to_usecs(delta);
171
172         tracing_max_latency = delta;
173         t0 = nsecs_to_usecs(T0);
174         t1 = nsecs_to_usecs(T1);
175
176         update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
177
178 out_unlock:
179         __wakeup_reset(wakeup_trace);
180         __raw_spin_unlock(&wakeup_lock);
181         local_irq_restore(flags);
182 out:
183         atomic_dec(&wakeup_trace->data[cpu]->disabled);
184 }
185
186 static void __wakeup_reset(struct trace_array *tr)
187 {
188         wakeup_cpu = -1;
189         wakeup_prio = -1;
190
191         if (wakeup_task)
192                 put_task_struct(wakeup_task);
193
194         wakeup_task = NULL;
195 }
196
197 static void wakeup_reset(struct trace_array *tr)
198 {
199         unsigned long flags;
200
201         tracing_reset_online_cpus(tr);
202
203         local_irq_save(flags);
204         __raw_spin_lock(&wakeup_lock);
205         __wakeup_reset(tr);
206         __raw_spin_unlock(&wakeup_lock);
207         local_irq_restore(flags);
208 }
209
210 static void
211 probe_wakeup(struct rq *rq, struct task_struct *p, int success)
212 {
213         struct trace_array_cpu *data;
214         int cpu = smp_processor_id();
215         unsigned long flags;
216         long disabled;
217         int pc;
218
219         if (likely(!tracer_enabled))
220                 return;
221
222         tracing_record_cmdline(p);
223         tracing_record_cmdline(current);
224
225         if ((wakeup_rt && !rt_task(p)) ||
226                         p->prio >= wakeup_prio ||
227                         p->prio >= current->prio)
228                 return;
229
230         pc = preempt_count();
231         disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
232         if (unlikely(disabled != 1))
233                 goto out;
234
235         /* interrupts should be off from try_to_wake_up */
236         __raw_spin_lock(&wakeup_lock);
237
238         /* check for races. */
239         if (!tracer_enabled || p->prio >= wakeup_prio)
240                 goto out_locked;
241
242         /* reset the trace */
243         __wakeup_reset(wakeup_trace);
244
245         wakeup_cpu = task_cpu(p);
246         wakeup_current_cpu = wakeup_cpu;
247         wakeup_prio = p->prio;
248
249         wakeup_task = p;
250         get_task_struct(wakeup_task);
251
252         local_save_flags(flags);
253
254         data = wakeup_trace->data[wakeup_cpu];
255         data->preempt_timestamp = ftrace_now(cpu);
256         tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
257
258         /*
259          * We must be careful in using CALLER_ADDR2. But since wake_up
260          * is not called by an assembly function  (where as schedule is)
261          * it should be safe to use it here.
262          */
263         trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
264
265 out_locked:
266         __raw_spin_unlock(&wakeup_lock);
267 out:
268         atomic_dec(&wakeup_trace->data[cpu]->disabled);
269 }
270
271 static void start_wakeup_tracer(struct trace_array *tr)
272 {
273         int ret;
274
275         ret = register_trace_sched_wakeup(probe_wakeup);
276         if (ret) {
277                 pr_info("wakeup trace: Couldn't activate tracepoint"
278                         " probe to kernel_sched_wakeup\n");
279                 return;
280         }
281
282         ret = register_trace_sched_wakeup_new(probe_wakeup);
283         if (ret) {
284                 pr_info("wakeup trace: Couldn't activate tracepoint"
285                         " probe to kernel_sched_wakeup_new\n");
286                 goto fail_deprobe;
287         }
288
289         ret = register_trace_sched_switch(probe_wakeup_sched_switch);
290         if (ret) {
291                 pr_info("sched trace: Couldn't activate tracepoint"
292                         " probe to kernel_sched_switch\n");
293                 goto fail_deprobe_wake_new;
294         }
295
296         ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task);
297         if (ret) {
298                 pr_info("wakeup trace: Couldn't activate tracepoint"
299                         " probe to kernel_sched_migrate_task\n");
300                 return;
301         }
302
303         wakeup_reset(tr);
304
305         /*
306          * Don't let the tracer_enabled = 1 show up before
307          * the wakeup_task is reset. This may be overkill since
308          * wakeup_reset does a spin_unlock after setting the
309          * wakeup_task to NULL, but I want to be safe.
310          * This is a slow path anyway.
311          */
312         smp_wmb();
313
314         register_ftrace_function(&trace_ops);
315
316         if (tracing_is_enabled())
317                 tracer_enabled = 1;
318         else
319                 tracer_enabled = 0;
320
321         return;
322 fail_deprobe_wake_new:
323         unregister_trace_sched_wakeup_new(probe_wakeup);
324 fail_deprobe:
325         unregister_trace_sched_wakeup(probe_wakeup);
326 }
327
328 static void stop_wakeup_tracer(struct trace_array *tr)
329 {
330         tracer_enabled = 0;
331         unregister_ftrace_function(&trace_ops);
332         unregister_trace_sched_switch(probe_wakeup_sched_switch);
333         unregister_trace_sched_wakeup_new(probe_wakeup);
334         unregister_trace_sched_wakeup(probe_wakeup);
335         unregister_trace_sched_migrate_task(probe_wakeup_migrate_task);
336 }
337
338 static int __wakeup_tracer_init(struct trace_array *tr)
339 {
340         save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
341         trace_flags |= TRACE_ITER_LATENCY_FMT;
342
343         tracing_max_latency = 0;
344         wakeup_trace = tr;
345         start_wakeup_tracer(tr);
346         return 0;
347 }
348
349 static int wakeup_tracer_init(struct trace_array *tr)
350 {
351         wakeup_rt = 0;
352         return __wakeup_tracer_init(tr);
353 }
354
355 static int wakeup_rt_tracer_init(struct trace_array *tr)
356 {
357         wakeup_rt = 1;
358         return __wakeup_tracer_init(tr);
359 }
360
361 static void wakeup_tracer_reset(struct trace_array *tr)
362 {
363         stop_wakeup_tracer(tr);
364         /* make sure we put back any tasks we are tracing */
365         wakeup_reset(tr);
366
367         if (!save_lat_flag)
368                 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
369 }
370
371 static void wakeup_tracer_start(struct trace_array *tr)
372 {
373         wakeup_reset(tr);
374         tracer_enabled = 1;
375 }
376
377 static void wakeup_tracer_stop(struct trace_array *tr)
378 {
379         tracer_enabled = 0;
380 }
381
382 static struct tracer wakeup_tracer __read_mostly =
383 {
384         .name           = "wakeup",
385         .init           = wakeup_tracer_init,
386         .reset          = wakeup_tracer_reset,
387         .start          = wakeup_tracer_start,
388         .stop           = wakeup_tracer_stop,
389         .print_max      = 1,
390 #ifdef CONFIG_FTRACE_SELFTEST
391         .selftest    = trace_selftest_startup_wakeup,
392 #endif
393 };
394
395 static struct tracer wakeup_rt_tracer __read_mostly =
396 {
397         .name           = "wakeup_rt",
398         .init           = wakeup_rt_tracer_init,
399         .reset          = wakeup_tracer_reset,
400         .start          = wakeup_tracer_start,
401         .stop           = wakeup_tracer_stop,
402         .wait_pipe      = poll_wait_pipe,
403         .print_max      = 1,
404 #ifdef CONFIG_FTRACE_SELFTEST
405         .selftest    = trace_selftest_startup_wakeup,
406 #endif
407 };
408
409 __init static int init_wakeup_tracer(void)
410 {
411         int ret;
412
413         ret = register_tracer(&wakeup_tracer);
414         if (ret)
415                 return ret;
416
417         ret = register_tracer(&wakeup_rt_tracer);
418         if (ret)
419                 return ret;
420
421         return 0;
422 }
423 device_initcall(init_wakeup_tracer);