1 // SPDX-License-Identifier: GPL-2.0
3 * trace task wakeup timings
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 * Based on code from the latency_tracer, that is:
10 * Copyright (C) 2004-2006 Ingo Molnar
11 * Copyright (C) 2004 Nadia Yvette Chambers
13 #include <linux/module.h>
14 #include <linux/kallsyms.h>
15 #include <linux/uaccess.h>
16 #include <linux/ftrace.h>
17 #include <linux/sched/rt.h>
18 #include <linux/sched/deadline.h>
19 #include <trace/events/sched.h>
22 static struct trace_array
*wakeup_trace
;
23 static int __read_mostly tracer_enabled
;
25 static struct task_struct
*wakeup_task
;
26 static int wakeup_cpu
;
27 static int wakeup_current_cpu
;
28 static unsigned wakeup_prio
= -1;
31 static int tracing_dl
= 0;
33 static arch_spinlock_t wakeup_lock
=
34 (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
36 static void wakeup_reset(struct trace_array
*tr
);
37 static void __wakeup_reset(struct trace_array
*tr
);
39 static int save_flags
;
41 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
42 static int wakeup_display_graph(struct trace_array
*tr
, int set
);
43 # define is_graph(tr) ((tr)->trace_flags & TRACE_ITER_DISPLAY_GRAPH)
45 static inline int wakeup_display_graph(struct trace_array
*tr
, int set
)
49 # define is_graph(tr) false
53 #ifdef CONFIG_FUNCTION_TRACER
55 static int wakeup_graph_entry(struct ftrace_graph_ent
*trace
);
56 static void wakeup_graph_return(struct ftrace_graph_ret
*trace
);
58 static bool function_enabled
;
61 * Prologue for the wakeup function tracers.
63 * Returns 1 if it is OK to continue, and preemption
64 * is disabled and data->disabled is incremented.
65 * 0 if the trace is to be ignored, and preemption
66 * is not disabled and data->disabled is
69 * Note, this function is also used outside this ifdef but
70 * inside the #ifdef of the function graph tracer below.
71 * This is OK, since the function graph tracer is
72 * dependent on the function tracer.
75 func_prolog_preempt_disable(struct trace_array
*tr
,
76 struct trace_array_cpu
**data
,
82 if (likely(!wakeup_task
))
85 *pc
= preempt_count();
86 preempt_disable_notrace();
88 cpu
= raw_smp_processor_id();
89 if (cpu
!= wakeup_current_cpu
)
92 *data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
93 disabled
= atomic_inc_return(&(*data
)->disabled
);
94 if (unlikely(disabled
!= 1))
100 atomic_dec(&(*data
)->disabled
);
103 preempt_enable_notrace();
108 * wakeup uses its own tracer function to keep the overhead down:
111 wakeup_tracer_call(unsigned long ip
, unsigned long parent_ip
,
112 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
114 struct trace_array
*tr
= wakeup_trace
;
115 struct trace_array_cpu
*data
;
119 if (!func_prolog_preempt_disable(tr
, &data
, &pc
))
122 local_irq_save(flags
);
123 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
124 local_irq_restore(flags
);
126 atomic_dec(&data
->disabled
);
127 preempt_enable_notrace();
130 static int register_wakeup_function(struct trace_array
*tr
, int graph
, int set
)
134 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
135 if (function_enabled
|| (!set
&& !(tr
->trace_flags
& TRACE_ITER_FUNCTION
)))
139 ret
= register_ftrace_graph(&wakeup_graph_return
,
140 &wakeup_graph_entry
);
142 ret
= register_ftrace_function(tr
->ops
);
145 function_enabled
= true;
150 static void unregister_wakeup_function(struct trace_array
*tr
, int graph
)
152 if (!function_enabled
)
156 unregister_ftrace_graph();
158 unregister_ftrace_function(tr
->ops
);
160 function_enabled
= false;
163 static int wakeup_function_set(struct trace_array
*tr
, u32 mask
, int set
)
165 if (!(mask
& TRACE_ITER_FUNCTION
))
169 register_wakeup_function(tr
, is_graph(tr
), 1);
171 unregister_wakeup_function(tr
, is_graph(tr
));
175 static int register_wakeup_function(struct trace_array
*tr
, int graph
, int set
)
179 static void unregister_wakeup_function(struct trace_array
*tr
, int graph
) { }
180 static int wakeup_function_set(struct trace_array
*tr
, u32 mask
, int set
)
184 #endif /* CONFIG_FUNCTION_TRACER */
186 static int wakeup_flag_changed(struct trace_array
*tr
, u32 mask
, int set
)
188 struct tracer
*tracer
= tr
->current_trace
;
190 if (wakeup_function_set(tr
, mask
, set
))
193 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
194 if (mask
& TRACE_ITER_DISPLAY_GRAPH
)
195 return wakeup_display_graph(tr
, set
);
198 return trace_keep_overwrite(tracer
, mask
, set
);
201 static int start_func_tracer(struct trace_array
*tr
, int graph
)
205 ret
= register_wakeup_function(tr
, graph
, 0);
207 if (!ret
&& tracing_is_enabled())
215 static void stop_func_tracer(struct trace_array
*tr
, int graph
)
219 unregister_wakeup_function(tr
, graph
);
222 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
223 static int wakeup_display_graph(struct trace_array
*tr
, int set
)
225 if (!(is_graph(tr
) ^ set
))
228 stop_func_tracer(tr
, !set
);
230 wakeup_reset(wakeup_trace
);
233 return start_func_tracer(tr
, set
);
236 static int wakeup_graph_entry(struct ftrace_graph_ent
*trace
)
238 struct trace_array
*tr
= wakeup_trace
;
239 struct trace_array_cpu
*data
;
243 if (ftrace_graph_ignore_func(trace
))
246 * Do not trace a function if it's filtered by set_graph_notrace.
247 * Make the index of ret stack negative to indicate that it should
248 * ignore further functions. But it needs its own ret stack entry
249 * to recover the original index in order to continue tracing after
250 * returning from the function.
252 if (ftrace_graph_notrace_addr(trace
->func
))
255 if (!func_prolog_preempt_disable(tr
, &data
, &pc
))
258 local_save_flags(flags
);
259 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
260 atomic_dec(&data
->disabled
);
261 preempt_enable_notrace();
266 static void wakeup_graph_return(struct ftrace_graph_ret
*trace
)
268 struct trace_array
*tr
= wakeup_trace
;
269 struct trace_array_cpu
*data
;
273 ftrace_graph_addr_finish(trace
);
275 if (!func_prolog_preempt_disable(tr
, &data
, &pc
))
278 local_save_flags(flags
);
279 __trace_graph_return(tr
, trace
, flags
, pc
);
280 atomic_dec(&data
->disabled
);
282 preempt_enable_notrace();
286 static void wakeup_trace_open(struct trace_iterator
*iter
)
288 if (is_graph(iter
->tr
))
289 graph_trace_open(iter
);
292 static void wakeup_trace_close(struct trace_iterator
*iter
)
295 graph_trace_close(iter
);
298 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
299 TRACE_GRAPH_PRINT_ABS_TIME | \
300 TRACE_GRAPH_PRINT_DURATION)
302 static enum print_line_t
wakeup_print_line(struct trace_iterator
*iter
)
305 * In graph mode call the graph tracer output function,
306 * otherwise go with the TRACE_FN event handler
308 if (is_graph(iter
->tr
))
309 return print_graph_function_flags(iter
, GRAPH_TRACER_FLAGS
);
311 return TRACE_TYPE_UNHANDLED
;
314 static void wakeup_print_header(struct seq_file
*s
)
316 if (is_graph(wakeup_trace
))
317 print_graph_headers_flags(s
, GRAPH_TRACER_FLAGS
);
319 trace_default_header(s
);
323 __trace_function(struct trace_array
*tr
,
324 unsigned long ip
, unsigned long parent_ip
,
325 unsigned long flags
, int pc
)
328 trace_graph_function(tr
, ip
, parent_ip
, flags
, pc
);
330 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
333 #define __trace_function trace_function
335 static enum print_line_t
wakeup_print_line(struct trace_iterator
*iter
)
337 return TRACE_TYPE_UNHANDLED
;
340 static void wakeup_trace_open(struct trace_iterator
*iter
) { }
341 static void wakeup_trace_close(struct trace_iterator
*iter
) { }
343 #ifdef CONFIG_FUNCTION_TRACER
344 static int wakeup_graph_entry(struct ftrace_graph_ent
*trace
)
348 static void wakeup_graph_return(struct ftrace_graph_ret
*trace
) { }
349 static void wakeup_print_header(struct seq_file
*s
)
351 trace_default_header(s
);
354 static void wakeup_print_header(struct seq_file
*s
)
356 trace_latency_header(s
);
358 #endif /* CONFIG_FUNCTION_TRACER */
359 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
362 * Should this new latency be reported/recorded?
364 static bool report_latency(struct trace_array
*tr
, u64 delta
)
366 if (tracing_thresh
) {
367 if (delta
< tracing_thresh
)
370 if (delta
<= tr
->max_latency
)
377 probe_wakeup_migrate_task(void *ignore
, struct task_struct
*task
, int cpu
)
379 if (task
!= wakeup_task
)
382 wakeup_current_cpu
= cpu
;
386 tracing_sched_switch_trace(struct trace_array
*tr
,
387 struct task_struct
*prev
,
388 struct task_struct
*next
,
389 unsigned long flags
, int pc
)
391 struct trace_event_call
*call
= &event_context_switch
;
392 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
393 struct ring_buffer_event
*event
;
394 struct ctx_switch_entry
*entry
;
396 event
= trace_buffer_lock_reserve(buffer
, TRACE_CTX
,
397 sizeof(*entry
), flags
, pc
);
400 entry
= ring_buffer_event_data(event
);
401 entry
->prev_pid
= prev
->pid
;
402 entry
->prev_prio
= prev
->prio
;
403 entry
->prev_state
= task_state_index(prev
);
404 entry
->next_pid
= next
->pid
;
405 entry
->next_prio
= next
->prio
;
406 entry
->next_state
= task_state_index(next
);
407 entry
->next_cpu
= task_cpu(next
);
409 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
410 trace_buffer_unlock_commit(tr
, buffer
, event
, flags
, pc
);
414 tracing_sched_wakeup_trace(struct trace_array
*tr
,
415 struct task_struct
*wakee
,
416 struct task_struct
*curr
,
417 unsigned long flags
, int pc
)
419 struct trace_event_call
*call
= &event_wakeup
;
420 struct ring_buffer_event
*event
;
421 struct ctx_switch_entry
*entry
;
422 struct ring_buffer
*buffer
= tr
->trace_buffer
.buffer
;
424 event
= trace_buffer_lock_reserve(buffer
, TRACE_WAKE
,
425 sizeof(*entry
), flags
, pc
);
428 entry
= ring_buffer_event_data(event
);
429 entry
->prev_pid
= curr
->pid
;
430 entry
->prev_prio
= curr
->prio
;
431 entry
->prev_state
= task_state_index(curr
);
432 entry
->next_pid
= wakee
->pid
;
433 entry
->next_prio
= wakee
->prio
;
434 entry
->next_state
= task_state_index(wakee
);
435 entry
->next_cpu
= task_cpu(wakee
);
437 if (!call_filter_check_discard(call
, entry
, buffer
, event
))
438 trace_buffer_unlock_commit(tr
, buffer
, event
, flags
, pc
);
442 probe_wakeup_sched_switch(void *ignore
, bool preempt
,
443 struct task_struct
*prev
, struct task_struct
*next
)
445 struct trace_array_cpu
*data
;
452 tracing_record_cmdline(prev
);
454 if (unlikely(!tracer_enabled
))
458 * When we start a new trace, we set wakeup_task to NULL
459 * and then set tracer_enabled = 1. We want to make sure
460 * that another CPU does not see the tracer_enabled = 1
461 * and the wakeup_task with an older task, that might
462 * actually be the same as next.
466 if (next
!= wakeup_task
)
469 pc
= preempt_count();
471 /* disable local data, not wakeup_cpu data */
472 cpu
= raw_smp_processor_id();
473 disabled
= atomic_inc_return(&per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, cpu
)->disabled
);
474 if (likely(disabled
!= 1))
477 local_irq_save(flags
);
478 arch_spin_lock(&wakeup_lock
);
480 /* We could race with grabbing wakeup_lock */
481 if (unlikely(!tracer_enabled
|| next
!= wakeup_task
))
484 /* The task we are waiting for is waking up */
485 data
= per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, wakeup_cpu
);
487 __trace_function(wakeup_trace
, CALLER_ADDR0
, CALLER_ADDR1
, flags
, pc
);
488 tracing_sched_switch_trace(wakeup_trace
, prev
, next
, flags
, pc
);
490 T0
= data
->preempt_timestamp
;
491 T1
= ftrace_now(cpu
);
494 if (!report_latency(wakeup_trace
, delta
))
497 if (likely(!is_tracing_stopped())) {
498 wakeup_trace
->max_latency
= delta
;
499 update_max_tr(wakeup_trace
, wakeup_task
, wakeup_cpu
);
503 __wakeup_reset(wakeup_trace
);
504 arch_spin_unlock(&wakeup_lock
);
505 local_irq_restore(flags
);
507 atomic_dec(&per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, cpu
)->disabled
);
510 static void __wakeup_reset(struct trace_array
*tr
)
517 put_task_struct(wakeup_task
);
522 static void wakeup_reset(struct trace_array
*tr
)
526 tracing_reset_online_cpus(&tr
->trace_buffer
);
528 local_irq_save(flags
);
529 arch_spin_lock(&wakeup_lock
);
531 arch_spin_unlock(&wakeup_lock
);
532 local_irq_restore(flags
);
536 probe_wakeup(void *ignore
, struct task_struct
*p
)
538 struct trace_array_cpu
*data
;
539 int cpu
= smp_processor_id();
544 if (likely(!tracer_enabled
))
547 tracing_record_cmdline(p
);
548 tracing_record_cmdline(current
);
551 * Semantic is like this:
552 * - wakeup tracer handles all tasks in the system, independently
553 * from their scheduling class;
554 * - wakeup_rt tracer handles tasks belonging to sched_dl and
556 * - wakeup_dl handles tasks belonging to sched_dl class only.
558 if (tracing_dl
|| (wakeup_dl
&& !dl_task(p
)) ||
559 (wakeup_rt
&& !dl_task(p
) && !rt_task(p
)) ||
560 (!dl_task(p
) && (p
->prio
>= wakeup_prio
|| p
->prio
>= current
->prio
)))
563 pc
= preempt_count();
564 disabled
= atomic_inc_return(&per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, cpu
)->disabled
);
565 if (unlikely(disabled
!= 1))
568 /* interrupts should be off from try_to_wake_up */
569 arch_spin_lock(&wakeup_lock
);
571 /* check for races. */
572 if (!tracer_enabled
|| tracing_dl
||
573 (!dl_task(p
) && p
->prio
>= wakeup_prio
))
576 /* reset the trace */
577 __wakeup_reset(wakeup_trace
);
579 wakeup_cpu
= task_cpu(p
);
580 wakeup_current_cpu
= wakeup_cpu
;
581 wakeup_prio
= p
->prio
;
584 * Once you start tracing a -deadline task, don't bother tracing
585 * another task until the first one wakes up.
593 get_task_struct(wakeup_task
);
595 local_save_flags(flags
);
597 data
= per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, wakeup_cpu
);
598 data
->preempt_timestamp
= ftrace_now(cpu
);
599 tracing_sched_wakeup_trace(wakeup_trace
, p
, current
, flags
, pc
);
602 * We must be careful in using CALLER_ADDR2. But since wake_up
603 * is not called by an assembly function (where as schedule is)
604 * it should be safe to use it here.
606 __trace_function(wakeup_trace
, CALLER_ADDR1
, CALLER_ADDR2
, flags
, pc
);
609 arch_spin_unlock(&wakeup_lock
);
611 atomic_dec(&per_cpu_ptr(wakeup_trace
->trace_buffer
.data
, cpu
)->disabled
);
614 static void start_wakeup_tracer(struct trace_array
*tr
)
618 ret
= register_trace_sched_wakeup(probe_wakeup
, NULL
);
620 pr_info("wakeup trace: Couldn't activate tracepoint"
621 " probe to kernel_sched_wakeup\n");
625 ret
= register_trace_sched_wakeup_new(probe_wakeup
, NULL
);
627 pr_info("wakeup trace: Couldn't activate tracepoint"
628 " probe to kernel_sched_wakeup_new\n");
632 ret
= register_trace_sched_switch(probe_wakeup_sched_switch
, NULL
);
634 pr_info("sched trace: Couldn't activate tracepoint"
635 " probe to kernel_sched_switch\n");
636 goto fail_deprobe_wake_new
;
639 ret
= register_trace_sched_migrate_task(probe_wakeup_migrate_task
, NULL
);
641 pr_info("wakeup trace: Couldn't activate tracepoint"
642 " probe to kernel_sched_migrate_task\n");
649 * Don't let the tracer_enabled = 1 show up before
650 * the wakeup_task is reset. This may be overkill since
651 * wakeup_reset does a spin_unlock after setting the
652 * wakeup_task to NULL, but I want to be safe.
653 * This is a slow path anyway.
657 if (start_func_tracer(tr
, is_graph(tr
)))
658 printk(KERN_ERR
"failed to start wakeup tracer\n");
661 fail_deprobe_wake_new
:
662 unregister_trace_sched_wakeup_new(probe_wakeup
, NULL
);
664 unregister_trace_sched_wakeup(probe_wakeup
, NULL
);
667 static void stop_wakeup_tracer(struct trace_array
*tr
)
670 stop_func_tracer(tr
, is_graph(tr
));
671 unregister_trace_sched_switch(probe_wakeup_sched_switch
, NULL
);
672 unregister_trace_sched_wakeup_new(probe_wakeup
, NULL
);
673 unregister_trace_sched_wakeup(probe_wakeup
, NULL
);
674 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task
, NULL
);
677 static bool wakeup_busy
;
679 static int __wakeup_tracer_init(struct trace_array
*tr
)
681 save_flags
= tr
->trace_flags
;
683 /* non overwrite screws up the latency tracers */
684 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, 1);
685 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, 1);
689 ftrace_init_array_ops(tr
, wakeup_tracer_call
);
690 start_wakeup_tracer(tr
);
696 static int wakeup_tracer_init(struct trace_array
*tr
)
703 return __wakeup_tracer_init(tr
);
706 static int wakeup_rt_tracer_init(struct trace_array
*tr
)
713 return __wakeup_tracer_init(tr
);
716 static int wakeup_dl_tracer_init(struct trace_array
*tr
)
723 return __wakeup_tracer_init(tr
);
726 static void wakeup_tracer_reset(struct trace_array
*tr
)
728 int lat_flag
= save_flags
& TRACE_ITER_LATENCY_FMT
;
729 int overwrite_flag
= save_flags
& TRACE_ITER_OVERWRITE
;
731 stop_wakeup_tracer(tr
);
732 /* make sure we put back any tasks we are tracing */
735 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, lat_flag
);
736 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, overwrite_flag
);
737 ftrace_reset_array_ops(tr
);
741 static void wakeup_tracer_start(struct trace_array
*tr
)
747 static void wakeup_tracer_stop(struct trace_array
*tr
)
752 static struct tracer wakeup_tracer __read_mostly
=
755 .init
= wakeup_tracer_init
,
756 .reset
= wakeup_tracer_reset
,
757 .start
= wakeup_tracer_start
,
758 .stop
= wakeup_tracer_stop
,
760 .print_header
= wakeup_print_header
,
761 .print_line
= wakeup_print_line
,
762 .flag_changed
= wakeup_flag_changed
,
763 #ifdef CONFIG_FTRACE_SELFTEST
764 .selftest
= trace_selftest_startup_wakeup
,
766 .open
= wakeup_trace_open
,
767 .close
= wakeup_trace_close
,
768 .allow_instances
= true,
772 static struct tracer wakeup_rt_tracer __read_mostly
=
775 .init
= wakeup_rt_tracer_init
,
776 .reset
= wakeup_tracer_reset
,
777 .start
= wakeup_tracer_start
,
778 .stop
= wakeup_tracer_stop
,
780 .print_header
= wakeup_print_header
,
781 .print_line
= wakeup_print_line
,
782 .flag_changed
= wakeup_flag_changed
,
783 #ifdef CONFIG_FTRACE_SELFTEST
784 .selftest
= trace_selftest_startup_wakeup
,
786 .open
= wakeup_trace_open
,
787 .close
= wakeup_trace_close
,
788 .allow_instances
= true,
792 static struct tracer wakeup_dl_tracer __read_mostly
=
795 .init
= wakeup_dl_tracer_init
,
796 .reset
= wakeup_tracer_reset
,
797 .start
= wakeup_tracer_start
,
798 .stop
= wakeup_tracer_stop
,
800 .print_header
= wakeup_print_header
,
801 .print_line
= wakeup_print_line
,
802 .flag_changed
= wakeup_flag_changed
,
803 #ifdef CONFIG_FTRACE_SELFTEST
804 .selftest
= trace_selftest_startup_wakeup
,
806 .open
= wakeup_trace_open
,
807 .close
= wakeup_trace_close
,
808 .allow_instances
= true,
812 __init
static int init_wakeup_tracer(void)
816 ret
= register_tracer(&wakeup_tracer
);
820 ret
= register_tracer(&wakeup_rt_tracer
);
824 ret
= register_tracer(&wakeup_dl_tracer
);
830 core_initcall(init_wakeup_tracer
);