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");
643 goto fail_deprobe_sched_switch;
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_sched_switch:
662 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
663 fail_deprobe_wake_new:
664 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
666 unregister_trace_sched_wakeup(probe_wakeup, NULL);
669 static void stop_wakeup_tracer(struct trace_array *tr)
672 stop_func_tracer(tr, is_graph(tr));
673 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
674 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
675 unregister_trace_sched_wakeup(probe_wakeup, NULL);
676 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
679 static bool wakeup_busy;
681 static int __wakeup_tracer_init(struct trace_array *tr)
683 save_flags = tr->trace_flags;
685 /* non overwrite screws up the latency tracers */
686 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
687 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
691 ftrace_init_array_ops(tr, wakeup_tracer_call);
692 start_wakeup_tracer(tr);
698 static int wakeup_tracer_init(struct trace_array *tr)
705 return __wakeup_tracer_init(tr);
708 static int wakeup_rt_tracer_init(struct trace_array *tr)
715 return __wakeup_tracer_init(tr);
718 static int wakeup_dl_tracer_init(struct trace_array *tr)
725 return __wakeup_tracer_init(tr);
728 static void wakeup_tracer_reset(struct trace_array *tr)
730 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
731 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
733 stop_wakeup_tracer(tr);
734 /* make sure we put back any tasks we are tracing */
737 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
738 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
739 ftrace_reset_array_ops(tr);
743 static void wakeup_tracer_start(struct trace_array *tr)
749 static void wakeup_tracer_stop(struct trace_array *tr)
754 static struct tracer wakeup_tracer __read_mostly =
757 .init = wakeup_tracer_init,
758 .reset = wakeup_tracer_reset,
759 .start = wakeup_tracer_start,
760 .stop = wakeup_tracer_stop,
762 .print_header = wakeup_print_header,
763 .print_line = wakeup_print_line,
764 .flag_changed = wakeup_flag_changed,
765 #ifdef CONFIG_FTRACE_SELFTEST
766 .selftest = trace_selftest_startup_wakeup,
768 .open = wakeup_trace_open,
769 .close = wakeup_trace_close,
770 .allow_instances = true,
774 static struct tracer wakeup_rt_tracer __read_mostly =
777 .init = wakeup_rt_tracer_init,
778 .reset = wakeup_tracer_reset,
779 .start = wakeup_tracer_start,
780 .stop = wakeup_tracer_stop,
782 .print_header = wakeup_print_header,
783 .print_line = wakeup_print_line,
784 .flag_changed = wakeup_flag_changed,
785 #ifdef CONFIG_FTRACE_SELFTEST
786 .selftest = trace_selftest_startup_wakeup,
788 .open = wakeup_trace_open,
789 .close = wakeup_trace_close,
790 .allow_instances = true,
794 static struct tracer wakeup_dl_tracer __read_mostly =
797 .init = wakeup_dl_tracer_init,
798 .reset = wakeup_tracer_reset,
799 .start = wakeup_tracer_start,
800 .stop = wakeup_tracer_stop,
802 .print_header = wakeup_print_header,
803 .print_line = wakeup_print_line,
804 .flag_changed = wakeup_flag_changed,
805 #ifdef CONFIG_FTRACE_SELFTEST
806 .selftest = trace_selftest_startup_wakeup,
808 .open = wakeup_trace_open,
809 .close = wakeup_trace_close,
810 .allow_instances = true,
814 __init static int init_wakeup_tracer(void)
818 ret = register_tracer(&wakeup_tracer);
822 ret = register_tracer(&wakeup_rt_tracer);
826 ret = register_tracer(&wakeup_dl_tracer);
832 core_initcall(init_wakeup_tracer);