1
2
3
4
5
6
7
8
9
10
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
22static struct trace_array *wakeup_trace;
23static int __read_mostly tracer_enabled;
24
25static struct task_struct *wakeup_task;
26static int wakeup_cpu;
27static int wakeup_current_cpu;
28static unsigned wakeup_prio = -1;
29static int wakeup_rt;
30
31static arch_spinlock_t wakeup_lock =
32 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
33
34static void wakeup_reset(struct trace_array *tr);
35static void __wakeup_reset(struct trace_array *tr);
36static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
37static void wakeup_graph_return(struct ftrace_graph_ret *trace);
38
39static int save_lat_flag;
40
41#define TRACE_DISPLAY_GRAPH 1
42
43static struct tracer_opt trace_opts[] = {
44#ifdef CONFIG_FUNCTION_GRAPH_TRACER
45
46 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
47#endif
48 { }
49};
50
51static struct tracer_flags tracer_flags = {
52 .val = 0,
53 .opts = trace_opts,
54};
55
56#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
57
58#ifdef CONFIG_FUNCTION_TRACER
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74static int
75func_prolog_preempt_disable(struct trace_array *tr,
76 struct trace_array_cpu **data,
77 int *pc)
78{
79 long disabled;
80 int cpu;
81
82 if (likely(!wakeup_task))
83 return 0;
84
85 *pc = preempt_count();
86 preempt_disable_notrace();
87
88 cpu = raw_smp_processor_id();
89 if (cpu != wakeup_current_cpu)
90 goto out_enable;
91
92 *data = tr->data[cpu];
93 disabled = atomic_inc_return(&(*data)->disabled);
94 if (unlikely(disabled != 1))
95 goto out;
96
97 return 1;
98
99out:
100 atomic_dec(&(*data)->disabled);
101
102out_enable:
103 preempt_enable_notrace();
104 return 0;
105}
106
107
108
109
110static void
111wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
112 struct ftrace_ops *op, struct pt_regs *pt_regs)
113{
114 struct trace_array *tr = wakeup_trace;
115 struct trace_array_cpu *data;
116 unsigned long flags;
117 int pc;
118
119 if (!func_prolog_preempt_disable(tr, &data, &pc))
120 return;
121
122 local_irq_save(flags);
123 trace_function(tr, ip, parent_ip, flags, pc);
124 local_irq_restore(flags);
125
126 atomic_dec(&data->disabled);
127 preempt_enable_notrace();
128}
129
130static struct ftrace_ops trace_ops __read_mostly =
131{
132 .func = wakeup_tracer_call,
133 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
134};
135#endif
136
137static int start_func_tracer(int graph)
138{
139 int ret;
140
141 if (!graph)
142 ret = register_ftrace_function(&trace_ops);
143 else
144 ret = register_ftrace_graph(&wakeup_graph_return,
145 &wakeup_graph_entry);
146
147 if (!ret && tracing_is_enabled())
148 tracer_enabled = 1;
149 else
150 tracer_enabled = 0;
151
152 return ret;
153}
154
155static void stop_func_tracer(int graph)
156{
157 tracer_enabled = 0;
158
159 if (!graph)
160 unregister_ftrace_function(&trace_ops);
161 else
162 unregister_ftrace_graph();
163}
164
165#ifdef CONFIG_FUNCTION_GRAPH_TRACER
166static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
167{
168
169 if (!(bit & TRACE_DISPLAY_GRAPH))
170 return -EINVAL;
171
172 if (!(is_graph() ^ set))
173 return 0;
174
175 stop_func_tracer(!set);
176
177 wakeup_reset(wakeup_trace);
178 tracing_max_latency = 0;
179
180 return start_func_tracer(set);
181}
182
183static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
184{
185 struct trace_array *tr = wakeup_trace;
186 struct trace_array_cpu *data;
187 unsigned long flags;
188 int pc, ret = 0;
189
190 if (!func_prolog_preempt_disable(tr, &data, &pc))
191 return 0;
192
193 local_save_flags(flags);
194 ret = __trace_graph_entry(tr, trace, flags, pc);
195 atomic_dec(&data->disabled);
196 preempt_enable_notrace();
197
198 return ret;
199}
200
201static void wakeup_graph_return(struct ftrace_graph_ret *trace)
202{
203 struct trace_array *tr = wakeup_trace;
204 struct trace_array_cpu *data;
205 unsigned long flags;
206 int pc;
207
208 if (!func_prolog_preempt_disable(tr, &data, &pc))
209 return;
210
211 local_save_flags(flags);
212 __trace_graph_return(tr, trace, flags, pc);
213 atomic_dec(&data->disabled);
214
215 preempt_enable_notrace();
216 return;
217}
218
219static void wakeup_trace_open(struct trace_iterator *iter)
220{
221 if (is_graph())
222 graph_trace_open(iter);
223}
224
225static void wakeup_trace_close(struct trace_iterator *iter)
226{
227 if (iter->private)
228 graph_trace_close(iter);
229}
230
231#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
232 TRACE_GRAPH_PRINT_ABS_TIME | \
233 TRACE_GRAPH_PRINT_DURATION)
234
235static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
236{
237
238
239
240
241 if (is_graph())
242 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
243
244 return TRACE_TYPE_UNHANDLED;
245}
246
247static void wakeup_print_header(struct seq_file *s)
248{
249 if (is_graph())
250 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
251 else
252 trace_default_header(s);
253}
254
255static void
256__trace_function(struct trace_array *tr,
257 unsigned long ip, unsigned long parent_ip,
258 unsigned long flags, int pc)
259{
260 if (is_graph())
261 trace_graph_function(tr, ip, parent_ip, flags, pc);
262 else
263 trace_function(tr, ip, parent_ip, flags, pc);
264}
265#else
266#define __trace_function trace_function
267
268static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
269{
270 return -EINVAL;
271}
272
273static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
274{
275 return -1;
276}
277
278static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
279{
280 return TRACE_TYPE_UNHANDLED;
281}
282
283static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
284static void wakeup_trace_open(struct trace_iterator *iter) { }
285static void wakeup_trace_close(struct trace_iterator *iter) { }
286
287#ifdef CONFIG_FUNCTION_TRACER
288static void wakeup_print_header(struct seq_file *s)
289{
290 trace_default_header(s);
291}
292#else
293static void wakeup_print_header(struct seq_file *s)
294{
295 trace_latency_header(s);
296}
297#endif
298#endif
299
300
301
302
303static int report_latency(cycle_t delta)
304{
305 if (tracing_thresh) {
306 if (delta < tracing_thresh)
307 return 0;
308 } else {
309 if (delta <= tracing_max_latency)
310 return 0;
311 }
312 return 1;
313}
314
315static void
316probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
317{
318 if (task != wakeup_task)
319 return;
320
321 wakeup_current_cpu = cpu;
322}
323
324static void notrace
325probe_wakeup_sched_switch(void *ignore,
326 struct task_struct *prev, struct task_struct *next)
327{
328 struct trace_array_cpu *data;
329 cycle_t T0, T1, delta;
330 unsigned long flags;
331 long disabled;
332 int cpu;
333 int pc;
334
335 tracing_record_cmdline(prev);
336
337 if (unlikely(!tracer_enabled))
338 return;
339
340
341
342
343
344
345
346
347 smp_rmb();
348
349 if (next != wakeup_task)
350 return;
351
352 pc = preempt_count();
353
354
355 cpu = raw_smp_processor_id();
356 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
357 if (likely(disabled != 1))
358 goto out;
359
360 local_irq_save(flags);
361 arch_spin_lock(&wakeup_lock);
362
363
364 if (unlikely(!tracer_enabled || next != wakeup_task))
365 goto out_unlock;
366
367
368 data = wakeup_trace->data[wakeup_cpu];
369
370 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
371 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
372
373 T0 = data->preempt_timestamp;
374 T1 = ftrace_now(cpu);
375 delta = T1-T0;
376
377 if (!report_latency(delta))
378 goto out_unlock;
379
380 if (likely(!is_tracing_stopped())) {
381 tracing_max_latency = delta;
382 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
383 }
384
385out_unlock:
386 __wakeup_reset(wakeup_trace);
387 arch_spin_unlock(&wakeup_lock);
388 local_irq_restore(flags);
389out:
390 atomic_dec(&wakeup_trace->data[cpu]->disabled);
391}
392
393static void __wakeup_reset(struct trace_array *tr)
394{
395 wakeup_cpu = -1;
396 wakeup_prio = -1;
397
398 if (wakeup_task)
399 put_task_struct(wakeup_task);
400
401 wakeup_task = NULL;
402}
403
404static void wakeup_reset(struct trace_array *tr)
405{
406 unsigned long flags;
407
408 tracing_reset_online_cpus(tr);
409
410 local_irq_save(flags);
411 arch_spin_lock(&wakeup_lock);
412 __wakeup_reset(tr);
413 arch_spin_unlock(&wakeup_lock);
414 local_irq_restore(flags);
415}
416
417static void
418probe_wakeup(void *ignore, struct task_struct *p, int success)
419{
420 struct trace_array_cpu *data;
421 int cpu = smp_processor_id();
422 unsigned long flags;
423 long disabled;
424 int pc;
425
426 if (likely(!tracer_enabled))
427 return;
428
429 tracing_record_cmdline(p);
430 tracing_record_cmdline(current);
431
432 if ((wakeup_rt && !rt_task(p)) ||
433 p->prio >= wakeup_prio ||
434 p->prio >= current->prio)
435 return;
436
437 pc = preempt_count();
438 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
439 if (unlikely(disabled != 1))
440 goto out;
441
442
443 arch_spin_lock(&wakeup_lock);
444
445
446 if (!tracer_enabled || p->prio >= wakeup_prio)
447 goto out_locked;
448
449
450 __wakeup_reset(wakeup_trace);
451
452 wakeup_cpu = task_cpu(p);
453 wakeup_current_cpu = wakeup_cpu;
454 wakeup_prio = p->prio;
455
456 wakeup_task = p;
457 get_task_struct(wakeup_task);
458
459 local_save_flags(flags);
460
461 data = wakeup_trace->data[wakeup_cpu];
462 data->preempt_timestamp = ftrace_now(cpu);
463 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
464
465
466
467
468
469
470 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
471
472out_locked:
473 arch_spin_unlock(&wakeup_lock);
474out:
475 atomic_dec(&wakeup_trace->data[cpu]->disabled);
476}
477
478static void start_wakeup_tracer(struct trace_array *tr)
479{
480 int ret;
481
482 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
483 if (ret) {
484 pr_info("wakeup trace: Couldn't activate tracepoint"
485 " probe to kernel_sched_wakeup\n");
486 return;
487 }
488
489 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
490 if (ret) {
491 pr_info("wakeup trace: Couldn't activate tracepoint"
492 " probe to kernel_sched_wakeup_new\n");
493 goto fail_deprobe;
494 }
495
496 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
497 if (ret) {
498 pr_info("sched trace: Couldn't activate tracepoint"
499 " probe to kernel_sched_switch\n");
500 goto fail_deprobe_wake_new;
501 }
502
503 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
504 if (ret) {
505 pr_info("wakeup trace: Couldn't activate tracepoint"
506 " probe to kernel_sched_migrate_task\n");
507 return;
508 }
509
510 wakeup_reset(tr);
511
512
513
514
515
516
517
518
519 smp_wmb();
520
521 if (start_func_tracer(is_graph()))
522 printk(KERN_ERR "failed to start wakeup tracer\n");
523
524 return;
525fail_deprobe_wake_new:
526 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
527fail_deprobe:
528 unregister_trace_sched_wakeup(probe_wakeup, NULL);
529}
530
531static void stop_wakeup_tracer(struct trace_array *tr)
532{
533 tracer_enabled = 0;
534 stop_func_tracer(is_graph());
535 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
536 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
537 unregister_trace_sched_wakeup(probe_wakeup, NULL);
538 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
539}
540
541static int __wakeup_tracer_init(struct trace_array *tr)
542{
543 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
544 trace_flags |= TRACE_ITER_LATENCY_FMT;
545
546 tracing_max_latency = 0;
547 wakeup_trace = tr;
548 start_wakeup_tracer(tr);
549 return 0;
550}
551
552static int wakeup_tracer_init(struct trace_array *tr)
553{
554 wakeup_rt = 0;
555 return __wakeup_tracer_init(tr);
556}
557
558static int wakeup_rt_tracer_init(struct trace_array *tr)
559{
560 wakeup_rt = 1;
561 return __wakeup_tracer_init(tr);
562}
563
564static void wakeup_tracer_reset(struct trace_array *tr)
565{
566 stop_wakeup_tracer(tr);
567
568 wakeup_reset(tr);
569
570 if (!save_lat_flag)
571 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
572}
573
574static void wakeup_tracer_start(struct trace_array *tr)
575{
576 wakeup_reset(tr);
577 tracer_enabled = 1;
578}
579
580static void wakeup_tracer_stop(struct trace_array *tr)
581{
582 tracer_enabled = 0;
583}
584
585static struct tracer wakeup_tracer __read_mostly =
586{
587 .name = "wakeup",
588 .init = wakeup_tracer_init,
589 .reset = wakeup_tracer_reset,
590 .start = wakeup_tracer_start,
591 .stop = wakeup_tracer_stop,
592 .print_max = 1,
593 .print_header = wakeup_print_header,
594 .print_line = wakeup_print_line,
595 .flags = &tracer_flags,
596 .set_flag = wakeup_set_flag,
597#ifdef CONFIG_FTRACE_SELFTEST
598 .selftest = trace_selftest_startup_wakeup,
599#endif
600 .open = wakeup_trace_open,
601 .close = wakeup_trace_close,
602 .use_max_tr = 1,
603};
604
605static struct tracer wakeup_rt_tracer __read_mostly =
606{
607 .name = "wakeup_rt",
608 .init = wakeup_rt_tracer_init,
609 .reset = wakeup_tracer_reset,
610 .start = wakeup_tracer_start,
611 .stop = wakeup_tracer_stop,
612 .wait_pipe = poll_wait_pipe,
613 .print_max = 1,
614 .print_header = wakeup_print_header,
615 .print_line = wakeup_print_line,
616 .flags = &tracer_flags,
617 .set_flag = wakeup_set_flag,
618#ifdef CONFIG_FTRACE_SELFTEST
619 .selftest = trace_selftest_startup_wakeup,
620#endif
621 .open = wakeup_trace_open,
622 .close = wakeup_trace_close,
623 .use_max_tr = 1,
624};
625
626__init static int init_wakeup_tracer(void)
627{
628 int ret;
629
630 ret = register_tracer(&wakeup_tracer);
631 if (ret)
632 return ret;
633
634 ret = register_tracer(&wakeup_rt_tracer);
635 if (ret)
636 return ret;
637
638 return 0;
639}
640device_initcall(init_wakeup_tracer);
641