xref: /linux/kernel/trace/trace_sched_wakeup.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace task wakeup timings
4  *
5  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7  *
8  * Based on code from the latency_tracer, that is:
9  *
10  *  Copyright (C) 2004-2006 Ingo Molnar
11  *  Copyright (C) 2004 Nadia Yvette Chambers
12  */
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>
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 bool			wakeup_rt;
30 static bool			wakeup_dl;
31 static bool			tracing_dl;
32 
33 static arch_spinlock_t wakeup_lock =
34 	(arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
35 
36 static void wakeup_reset(struct trace_array *tr);
37 static void __wakeup_reset(struct trace_array *tr);
38 static int start_func_tracer(struct trace_array *tr, int graph);
39 static void stop_func_tracer(struct trace_array *tr, int graph);
40 
41 static int save_flags;
42 
43 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
44 # define is_graph(tr) ((tr)->trace_flags & TRACE_ITER_DISPLAY_GRAPH)
45 #else
46 # define is_graph(tr) false
47 #endif
48 
49 #ifdef CONFIG_FUNCTION_TRACER
50 
51 static bool function_enabled;
52 
53 /*
54  * Prologue for the wakeup function tracers.
55  *
56  * Returns 1 if it is OK to continue, and preemption
57  *            is disabled and data->disabled is incremented.
58  *         0 if the trace is to be ignored, and preemption
59  *            is not disabled and data->disabled is
60  *            kept the same.
61  *
62  * Note, this function is also used outside this ifdef but
63  *  inside the #ifdef of the function graph tracer below.
64  *  This is OK, since the function graph tracer is
65  *  dependent on the function tracer.
66  */
67 static int
func_prolog_preempt_disable(struct trace_array * tr,struct trace_array_cpu ** data,unsigned int * trace_ctx)68 func_prolog_preempt_disable(struct trace_array *tr,
69 			    struct trace_array_cpu **data,
70 			    unsigned int *trace_ctx)
71 {
72 	long disabled;
73 	int cpu;
74 
75 	if (likely(!wakeup_task))
76 		return 0;
77 
78 	*trace_ctx = tracing_gen_ctx();
79 	preempt_disable_notrace();
80 
81 	cpu = raw_smp_processor_id();
82 	if (cpu != wakeup_current_cpu)
83 		goto out_enable;
84 
85 	*data = per_cpu_ptr(tr->array_buffer.data, cpu);
86 	disabled = atomic_inc_return(&(*data)->disabled);
87 	if (unlikely(disabled != 1))
88 		goto out;
89 
90 	return 1;
91 
92 out:
93 	atomic_dec(&(*data)->disabled);
94 
95 out_enable:
96 	preempt_enable_notrace();
97 	return 0;
98 }
99 
100 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
101 
wakeup_display_graph(struct trace_array * tr,int set)102 static int wakeup_display_graph(struct trace_array *tr, int set)
103 {
104 	if (!(is_graph(tr) ^ set))
105 		return 0;
106 
107 	stop_func_tracer(tr, !set);
108 
109 	wakeup_reset(wakeup_trace);
110 	tr->max_latency = 0;
111 
112 	return start_func_tracer(tr, set);
113 }
114 
wakeup_graph_entry(struct ftrace_graph_ent * trace,struct fgraph_ops * gops)115 static int wakeup_graph_entry(struct ftrace_graph_ent *trace,
116 			      struct fgraph_ops *gops)
117 {
118 	struct trace_array *tr = wakeup_trace;
119 	struct trace_array_cpu *data;
120 	unsigned int trace_ctx;
121 	int ret = 0;
122 
123 	if (ftrace_graph_ignore_func(gops, trace))
124 		return 0;
125 	/*
126 	 * Do not trace a function if it's filtered by set_graph_notrace.
127 	 * Make the index of ret stack negative to indicate that it should
128 	 * ignore further functions.  But it needs its own ret stack entry
129 	 * to recover the original index in order to continue tracing after
130 	 * returning from the function.
131 	 */
132 	if (ftrace_graph_notrace_addr(trace->func))
133 		return 1;
134 
135 	if (!func_prolog_preempt_disable(tr, &data, &trace_ctx))
136 		return 0;
137 
138 	ret = __trace_graph_entry(tr, trace, trace_ctx);
139 	atomic_dec(&data->disabled);
140 	preempt_enable_notrace();
141 
142 	return ret;
143 }
144 
wakeup_graph_return(struct ftrace_graph_ret * trace,struct fgraph_ops * gops)145 static void wakeup_graph_return(struct ftrace_graph_ret *trace,
146 				struct fgraph_ops *gops)
147 {
148 	struct trace_array *tr = wakeup_trace;
149 	struct trace_array_cpu *data;
150 	unsigned int trace_ctx;
151 
152 	ftrace_graph_addr_finish(gops, trace);
153 
154 	if (!func_prolog_preempt_disable(tr, &data, &trace_ctx))
155 		return;
156 
157 	__trace_graph_return(tr, trace, trace_ctx);
158 	atomic_dec(&data->disabled);
159 
160 	preempt_enable_notrace();
161 	return;
162 }
163 
164 static struct fgraph_ops fgraph_wakeup_ops = {
165 	.entryfunc = &wakeup_graph_entry,
166 	.retfunc = &wakeup_graph_return,
167 };
168 
wakeup_trace_open(struct trace_iterator * iter)169 static void wakeup_trace_open(struct trace_iterator *iter)
170 {
171 	if (is_graph(iter->tr))
172 		graph_trace_open(iter);
173 	else
174 		iter->private = NULL;
175 }
176 
wakeup_trace_close(struct trace_iterator * iter)177 static void wakeup_trace_close(struct trace_iterator *iter)
178 {
179 	if (iter->private)
180 		graph_trace_close(iter);
181 }
182 
183 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
184 			    TRACE_GRAPH_PRINT_CPU |  \
185 			    TRACE_GRAPH_PRINT_REL_TIME | \
186 			    TRACE_GRAPH_PRINT_DURATION | \
187 			    TRACE_GRAPH_PRINT_OVERHEAD | \
188 			    TRACE_GRAPH_PRINT_IRQS)
189 
wakeup_print_line(struct trace_iterator * iter)190 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
191 {
192 	/*
193 	 * In graph mode call the graph tracer output function,
194 	 * otherwise go with the TRACE_FN event handler
195 	 */
196 	if (is_graph(iter->tr))
197 		return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
198 
199 	return TRACE_TYPE_UNHANDLED;
200 }
201 
wakeup_print_header(struct seq_file * s)202 static void wakeup_print_header(struct seq_file *s)
203 {
204 	if (is_graph(wakeup_trace))
205 		print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
206 	else
207 		trace_default_header(s);
208 }
209 #endif /* else CONFIG_FUNCTION_GRAPH_TRACER */
210 
211 /*
212  * wakeup uses its own tracer function to keep the overhead down:
213  */
214 static void
wakeup_tracer_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct ftrace_regs * fregs)215 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
216 		   struct ftrace_ops *op, struct ftrace_regs *fregs)
217 {
218 	struct trace_array *tr = wakeup_trace;
219 	struct trace_array_cpu *data;
220 	unsigned long flags;
221 	unsigned int trace_ctx;
222 
223 	if (!func_prolog_preempt_disable(tr, &data, &trace_ctx))
224 		return;
225 
226 	local_irq_save(flags);
227 	trace_function(tr, ip, parent_ip, trace_ctx);
228 	local_irq_restore(flags);
229 
230 	atomic_dec(&data->disabled);
231 	preempt_enable_notrace();
232 }
233 
register_wakeup_function(struct trace_array * tr,int graph,int set)234 static int register_wakeup_function(struct trace_array *tr, int graph, int set)
235 {
236 	int ret;
237 
238 	/* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
239 	if (function_enabled || (!set && !(tr->trace_flags & TRACE_ITER_FUNCTION)))
240 		return 0;
241 
242 	if (graph)
243 		ret = register_ftrace_graph(&fgraph_wakeup_ops);
244 	else
245 		ret = register_ftrace_function(tr->ops);
246 
247 	if (!ret)
248 		function_enabled = true;
249 
250 	return ret;
251 }
252 
unregister_wakeup_function(struct trace_array * tr,int graph)253 static void unregister_wakeup_function(struct trace_array *tr, int graph)
254 {
255 	if (!function_enabled)
256 		return;
257 
258 	if (graph)
259 		unregister_ftrace_graph(&fgraph_wakeup_ops);
260 	else
261 		unregister_ftrace_function(tr->ops);
262 
263 	function_enabled = false;
264 }
265 
wakeup_function_set(struct trace_array * tr,u32 mask,int set)266 static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
267 {
268 	if (!(mask & TRACE_ITER_FUNCTION))
269 		return 0;
270 
271 	if (set)
272 		register_wakeup_function(tr, is_graph(tr), 1);
273 	else
274 		unregister_wakeup_function(tr, is_graph(tr));
275 	return 1;
276 }
277 #else /* CONFIG_FUNCTION_TRACER */
register_wakeup_function(struct trace_array * tr,int graph,int set)278 static int register_wakeup_function(struct trace_array *tr, int graph, int set)
279 {
280 	return 0;
281 }
unregister_wakeup_function(struct trace_array * tr,int graph)282 static void unregister_wakeup_function(struct trace_array *tr, int graph) { }
wakeup_function_set(struct trace_array * tr,u32 mask,int set)283 static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
284 {
285 	return 0;
286 }
287 #endif /* else CONFIG_FUNCTION_TRACER */
288 
289 #ifndef CONFIG_FUNCTION_GRAPH_TRACER
wakeup_print_line(struct trace_iterator * iter)290 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
291 {
292 	return TRACE_TYPE_UNHANDLED;
293 }
294 
wakeup_trace_open(struct trace_iterator * iter)295 static void wakeup_trace_open(struct trace_iterator *iter) { }
wakeup_trace_close(struct trace_iterator * iter)296 static void wakeup_trace_close(struct trace_iterator *iter) { }
297 
wakeup_print_header(struct seq_file * s)298 static void wakeup_print_header(struct seq_file *s)
299 {
300 	trace_default_header(s);
301 }
302 #endif /* !CONFIG_FUNCTION_GRAPH_TRACER */
303 
304 static void
__trace_function(struct trace_array * tr,unsigned long ip,unsigned long parent_ip,unsigned int trace_ctx)305 __trace_function(struct trace_array *tr,
306 		 unsigned long ip, unsigned long parent_ip,
307 		 unsigned int trace_ctx)
308 {
309 	if (is_graph(tr))
310 		trace_graph_function(tr, ip, parent_ip, trace_ctx);
311 	else
312 		trace_function(tr, ip, parent_ip, trace_ctx);
313 }
314 
wakeup_flag_changed(struct trace_array * tr,u32 mask,int set)315 static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
316 {
317 	struct tracer *tracer = tr->current_trace;
318 
319 	if (wakeup_function_set(tr, mask, set))
320 		return 0;
321 
322 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
323 	if (mask & TRACE_ITER_DISPLAY_GRAPH)
324 		return wakeup_display_graph(tr, set);
325 #endif
326 
327 	return trace_keep_overwrite(tracer, mask, set);
328 }
329 
start_func_tracer(struct trace_array * tr,int graph)330 static int start_func_tracer(struct trace_array *tr, int graph)
331 {
332 	int ret;
333 
334 	ret = register_wakeup_function(tr, graph, 0);
335 
336 	if (!ret && tracing_is_enabled())
337 		tracer_enabled = 1;
338 	else
339 		tracer_enabled = 0;
340 
341 	return ret;
342 }
343 
stop_func_tracer(struct trace_array * tr,int graph)344 static void stop_func_tracer(struct trace_array *tr, int graph)
345 {
346 	tracer_enabled = 0;
347 
348 	unregister_wakeup_function(tr, graph);
349 }
350 
351 /*
352  * Should this new latency be reported/recorded?
353  */
report_latency(struct trace_array * tr,u64 delta)354 static bool report_latency(struct trace_array *tr, u64 delta)
355 {
356 	if (tracing_thresh) {
357 		if (delta < tracing_thresh)
358 			return false;
359 	} else {
360 		if (delta <= tr->max_latency)
361 			return false;
362 	}
363 	return true;
364 }
365 
366 static void
probe_wakeup_migrate_task(void * ignore,struct task_struct * task,int cpu)367 probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
368 {
369 	if (task != wakeup_task)
370 		return;
371 
372 	wakeup_current_cpu = cpu;
373 }
374 
375 static void
tracing_sched_switch_trace(struct trace_array * tr,struct task_struct * prev,struct task_struct * next,unsigned int trace_ctx)376 tracing_sched_switch_trace(struct trace_array *tr,
377 			   struct task_struct *prev,
378 			   struct task_struct *next,
379 			   unsigned int trace_ctx)
380 {
381 	struct trace_event_call *call = &event_context_switch;
382 	struct trace_buffer *buffer = tr->array_buffer.buffer;
383 	struct ring_buffer_event *event;
384 	struct ctx_switch_entry *entry;
385 
386 	event = trace_buffer_lock_reserve(buffer, TRACE_CTX,
387 					  sizeof(*entry), trace_ctx);
388 	if (!event)
389 		return;
390 	entry	= ring_buffer_event_data(event);
391 	entry->prev_pid			= prev->pid;
392 	entry->prev_prio		= prev->prio;
393 	entry->prev_state		= task_state_index(prev);
394 	entry->next_pid			= next->pid;
395 	entry->next_prio		= next->prio;
396 	entry->next_state		= task_state_index(next);
397 	entry->next_cpu	= task_cpu(next);
398 
399 	if (!call_filter_check_discard(call, entry, buffer, event))
400 		trace_buffer_unlock_commit(tr, buffer, event, trace_ctx);
401 }
402 
403 static void
tracing_sched_wakeup_trace(struct trace_array * tr,struct task_struct * wakee,struct task_struct * curr,unsigned int trace_ctx)404 tracing_sched_wakeup_trace(struct trace_array *tr,
405 			   struct task_struct *wakee,
406 			   struct task_struct *curr,
407 			   unsigned int trace_ctx)
408 {
409 	struct trace_event_call *call = &event_wakeup;
410 	struct ring_buffer_event *event;
411 	struct ctx_switch_entry *entry;
412 	struct trace_buffer *buffer = tr->array_buffer.buffer;
413 
414 	event = trace_buffer_lock_reserve(buffer, TRACE_WAKE,
415 					  sizeof(*entry), trace_ctx);
416 	if (!event)
417 		return;
418 	entry	= ring_buffer_event_data(event);
419 	entry->prev_pid			= curr->pid;
420 	entry->prev_prio		= curr->prio;
421 	entry->prev_state		= task_state_index(curr);
422 	entry->next_pid			= wakee->pid;
423 	entry->next_prio		= wakee->prio;
424 	entry->next_state		= task_state_index(wakee);
425 	entry->next_cpu			= task_cpu(wakee);
426 
427 	if (!call_filter_check_discard(call, entry, buffer, event))
428 		trace_buffer_unlock_commit(tr, buffer, event, trace_ctx);
429 }
430 
431 static void notrace
probe_wakeup_sched_switch(void * ignore,bool preempt,struct task_struct * prev,struct task_struct * next,unsigned int prev_state)432 probe_wakeup_sched_switch(void *ignore, bool preempt,
433 			  struct task_struct *prev, struct task_struct *next,
434 			  unsigned int prev_state)
435 {
436 	struct trace_array_cpu *data;
437 	u64 T0, T1, delta;
438 	unsigned long flags;
439 	long disabled;
440 	int cpu;
441 	unsigned int trace_ctx;
442 
443 	tracing_record_cmdline(prev);
444 
445 	if (unlikely(!tracer_enabled))
446 		return;
447 
448 	/*
449 	 * When we start a new trace, we set wakeup_task to NULL
450 	 * and then set tracer_enabled = 1. We want to make sure
451 	 * that another CPU does not see the tracer_enabled = 1
452 	 * and the wakeup_task with an older task, that might
453 	 * actually be the same as next.
454 	 */
455 	smp_rmb();
456 
457 	if (next != wakeup_task)
458 		return;
459 
460 	/* disable local data, not wakeup_cpu data */
461 	cpu = raw_smp_processor_id();
462 	disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
463 	if (likely(disabled != 1))
464 		goto out;
465 
466 	local_irq_save(flags);
467 	trace_ctx = tracing_gen_ctx_flags(flags);
468 
469 	arch_spin_lock(&wakeup_lock);
470 
471 	/* We could race with grabbing wakeup_lock */
472 	if (unlikely(!tracer_enabled || next != wakeup_task))
473 		goto out_unlock;
474 
475 	/* The task we are waiting for is waking up */
476 	data = per_cpu_ptr(wakeup_trace->array_buffer.data, wakeup_cpu);
477 
478 	__trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, trace_ctx);
479 	tracing_sched_switch_trace(wakeup_trace, prev, next, trace_ctx);
480 	__trace_stack(wakeup_trace, trace_ctx, 0);
481 
482 	T0 = data->preempt_timestamp;
483 	T1 = ftrace_now(cpu);
484 	delta = T1-T0;
485 
486 	if (!report_latency(wakeup_trace, delta))
487 		goto out_unlock;
488 
489 	if (likely(!is_tracing_stopped())) {
490 		wakeup_trace->max_latency = delta;
491 		update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu, NULL);
492 	}
493 
494 out_unlock:
495 	__wakeup_reset(wakeup_trace);
496 	arch_spin_unlock(&wakeup_lock);
497 	local_irq_restore(flags);
498 out:
499 	atomic_dec(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
500 }
501 
__wakeup_reset(struct trace_array * tr)502 static void __wakeup_reset(struct trace_array *tr)
503 {
504 	wakeup_cpu = -1;
505 	wakeup_prio = -1;
506 	tracing_dl = false;
507 
508 	if (wakeup_task)
509 		put_task_struct(wakeup_task);
510 
511 	wakeup_task = NULL;
512 }
513 
wakeup_reset(struct trace_array * tr)514 static void wakeup_reset(struct trace_array *tr)
515 {
516 	unsigned long flags;
517 
518 	tracing_reset_online_cpus(&tr->array_buffer);
519 
520 	local_irq_save(flags);
521 	arch_spin_lock(&wakeup_lock);
522 	__wakeup_reset(tr);
523 	arch_spin_unlock(&wakeup_lock);
524 	local_irq_restore(flags);
525 }
526 
527 static void
probe_wakeup(void * ignore,struct task_struct * p)528 probe_wakeup(void *ignore, struct task_struct *p)
529 {
530 	struct trace_array_cpu *data;
531 	int cpu = smp_processor_id();
532 	long disabled;
533 	unsigned int trace_ctx;
534 
535 	if (likely(!tracer_enabled))
536 		return;
537 
538 	tracing_record_cmdline(p);
539 	tracing_record_cmdline(current);
540 
541 	/*
542 	 * Semantic is like this:
543 	 *  - wakeup tracer handles all tasks in the system, independently
544 	 *    from their scheduling class;
545 	 *  - wakeup_rt tracer handles tasks belonging to sched_dl and
546 	 *    sched_rt class;
547 	 *  - wakeup_dl handles tasks belonging to sched_dl class only.
548 	 */
549 	if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
550 	    (wakeup_rt && !rt_or_dl_task(p)) ||
551 	    (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
552 		return;
553 
554 	disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
555 	if (unlikely(disabled != 1))
556 		goto out;
557 
558 	trace_ctx = tracing_gen_ctx();
559 
560 	/* interrupts should be off from try_to_wake_up */
561 	arch_spin_lock(&wakeup_lock);
562 
563 	/* check for races. */
564 	if (!tracer_enabled || tracing_dl ||
565 	    (!dl_task(p) && p->prio >= wakeup_prio))
566 		goto out_locked;
567 
568 	/* reset the trace */
569 	__wakeup_reset(wakeup_trace);
570 
571 	wakeup_cpu = task_cpu(p);
572 	wakeup_current_cpu = wakeup_cpu;
573 	wakeup_prio = p->prio;
574 
575 	/*
576 	 * Once you start tracing a -deadline task, don't bother tracing
577 	 * another task until the first one wakes up.
578 	 */
579 	if (dl_task(p))
580 		tracing_dl = true;
581 	else
582 		tracing_dl = false;
583 
584 	wakeup_task = get_task_struct(p);
585 
586 	data = per_cpu_ptr(wakeup_trace->array_buffer.data, wakeup_cpu);
587 	data->preempt_timestamp = ftrace_now(cpu);
588 	tracing_sched_wakeup_trace(wakeup_trace, p, current, trace_ctx);
589 	__trace_stack(wakeup_trace, trace_ctx, 0);
590 
591 	/*
592 	 * We must be careful in using CALLER_ADDR2. But since wake_up
593 	 * is not called by an assembly function  (where as schedule is)
594 	 * it should be safe to use it here.
595 	 */
596 	__trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, trace_ctx);
597 
598 out_locked:
599 	arch_spin_unlock(&wakeup_lock);
600 out:
601 	atomic_dec(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
602 }
603 
start_wakeup_tracer(struct trace_array * tr)604 static void start_wakeup_tracer(struct trace_array *tr)
605 {
606 	int ret;
607 
608 	ret = register_trace_sched_wakeup(probe_wakeup, NULL);
609 	if (ret) {
610 		pr_info("wakeup trace: Couldn't activate tracepoint"
611 			" probe to kernel_sched_wakeup\n");
612 		return;
613 	}
614 
615 	ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
616 	if (ret) {
617 		pr_info("wakeup trace: Couldn't activate tracepoint"
618 			" probe to kernel_sched_wakeup_new\n");
619 		goto fail_deprobe;
620 	}
621 
622 	ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
623 	if (ret) {
624 		pr_info("sched trace: Couldn't activate tracepoint"
625 			" probe to kernel_sched_switch\n");
626 		goto fail_deprobe_wake_new;
627 	}
628 
629 	ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
630 	if (ret) {
631 		pr_info("wakeup trace: Couldn't activate tracepoint"
632 			" probe to kernel_sched_migrate_task\n");
633 		goto fail_deprobe_sched_switch;
634 	}
635 
636 	wakeup_reset(tr);
637 
638 	/*
639 	 * Don't let the tracer_enabled = 1 show up before
640 	 * the wakeup_task is reset. This may be overkill since
641 	 * wakeup_reset does a spin_unlock after setting the
642 	 * wakeup_task to NULL, but I want to be safe.
643 	 * This is a slow path anyway.
644 	 */
645 	smp_wmb();
646 
647 	if (start_func_tracer(tr, is_graph(tr)))
648 		printk(KERN_ERR "failed to start wakeup tracer\n");
649 
650 	return;
651 fail_deprobe_sched_switch:
652 	unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
653 fail_deprobe_wake_new:
654 	unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
655 fail_deprobe:
656 	unregister_trace_sched_wakeup(probe_wakeup, NULL);
657 }
658 
stop_wakeup_tracer(struct trace_array * tr)659 static void stop_wakeup_tracer(struct trace_array *tr)
660 {
661 	tracer_enabled = 0;
662 	stop_func_tracer(tr, is_graph(tr));
663 	unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
664 	unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
665 	unregister_trace_sched_wakeup(probe_wakeup, NULL);
666 	unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
667 }
668 
669 static bool wakeup_busy;
670 
__wakeup_tracer_init(struct trace_array * tr)671 static int __wakeup_tracer_init(struct trace_array *tr)
672 {
673 	save_flags = tr->trace_flags;
674 
675 	/* non overwrite screws up the latency tracers */
676 	set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
677 	set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
678 
679 	tr->max_latency = 0;
680 	wakeup_trace = tr;
681 	ftrace_init_array_ops(tr, wakeup_tracer_call);
682 	start_wakeup_tracer(tr);
683 
684 	wakeup_busy = true;
685 	return 0;
686 }
687 
wakeup_tracer_init(struct trace_array * tr)688 static int wakeup_tracer_init(struct trace_array *tr)
689 {
690 	if (wakeup_busy)
691 		return -EBUSY;
692 
693 	wakeup_dl = false;
694 	wakeup_rt = false;
695 	return __wakeup_tracer_init(tr);
696 }
697 
wakeup_rt_tracer_init(struct trace_array * tr)698 static int wakeup_rt_tracer_init(struct trace_array *tr)
699 {
700 	if (wakeup_busy)
701 		return -EBUSY;
702 
703 	wakeup_dl = false;
704 	wakeup_rt = true;
705 	return __wakeup_tracer_init(tr);
706 }
707 
wakeup_dl_tracer_init(struct trace_array * tr)708 static int wakeup_dl_tracer_init(struct trace_array *tr)
709 {
710 	if (wakeup_busy)
711 		return -EBUSY;
712 
713 	wakeup_dl = true;
714 	wakeup_rt = false;
715 	return __wakeup_tracer_init(tr);
716 }
717 
wakeup_tracer_reset(struct trace_array * tr)718 static void wakeup_tracer_reset(struct trace_array *tr)
719 {
720 	int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
721 	int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
722 
723 	stop_wakeup_tracer(tr);
724 	/* make sure we put back any tasks we are tracing */
725 	wakeup_reset(tr);
726 
727 	set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
728 	set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
729 	ftrace_reset_array_ops(tr);
730 	wakeup_busy = false;
731 }
732 
wakeup_tracer_start(struct trace_array * tr)733 static void wakeup_tracer_start(struct trace_array *tr)
734 {
735 	wakeup_reset(tr);
736 	tracer_enabled = 1;
737 }
738 
wakeup_tracer_stop(struct trace_array * tr)739 static void wakeup_tracer_stop(struct trace_array *tr)
740 {
741 	tracer_enabled = 0;
742 }
743 
744 static struct tracer wakeup_tracer __read_mostly =
745 {
746 	.name		= "wakeup",
747 	.init		= wakeup_tracer_init,
748 	.reset		= wakeup_tracer_reset,
749 	.start		= wakeup_tracer_start,
750 	.stop		= wakeup_tracer_stop,
751 	.print_max	= true,
752 	.print_header	= wakeup_print_header,
753 	.print_line	= wakeup_print_line,
754 	.flag_changed	= wakeup_flag_changed,
755 #ifdef CONFIG_FTRACE_SELFTEST
756 	.selftest    = trace_selftest_startup_wakeup,
757 #endif
758 	.open		= wakeup_trace_open,
759 	.close		= wakeup_trace_close,
760 	.allow_instances = true,
761 	.use_max_tr	= true,
762 };
763 
764 static struct tracer wakeup_rt_tracer __read_mostly =
765 {
766 	.name		= "wakeup_rt",
767 	.init		= wakeup_rt_tracer_init,
768 	.reset		= wakeup_tracer_reset,
769 	.start		= wakeup_tracer_start,
770 	.stop		= wakeup_tracer_stop,
771 	.print_max	= true,
772 	.print_header	= wakeup_print_header,
773 	.print_line	= wakeup_print_line,
774 	.flag_changed	= wakeup_flag_changed,
775 #ifdef CONFIG_FTRACE_SELFTEST
776 	.selftest    = trace_selftest_startup_wakeup,
777 #endif
778 	.open		= wakeup_trace_open,
779 	.close		= wakeup_trace_close,
780 	.allow_instances = true,
781 	.use_max_tr	= true,
782 };
783 
784 static struct tracer wakeup_dl_tracer __read_mostly =
785 {
786 	.name		= "wakeup_dl",
787 	.init		= wakeup_dl_tracer_init,
788 	.reset		= wakeup_tracer_reset,
789 	.start		= wakeup_tracer_start,
790 	.stop		= wakeup_tracer_stop,
791 	.print_max	= true,
792 	.print_header	= wakeup_print_header,
793 	.print_line	= wakeup_print_line,
794 	.flag_changed	= wakeup_flag_changed,
795 #ifdef CONFIG_FTRACE_SELFTEST
796 	.selftest    = trace_selftest_startup_wakeup,
797 #endif
798 	.open		= wakeup_trace_open,
799 	.close		= wakeup_trace_close,
800 	.allow_instances = true,
801 	.use_max_tr	= true,
802 };
803 
init_wakeup_tracer(void)804 __init static int init_wakeup_tracer(void)
805 {
806 	int ret;
807 
808 	ret = register_tracer(&wakeup_tracer);
809 	if (ret)
810 		return ret;
811 
812 	ret = register_tracer(&wakeup_rt_tracer);
813 	if (ret)
814 		return ret;
815 
816 	ret = register_tracer(&wakeup_dl_tracer);
817 	if (ret)
818 		return ret;
819 
820 	return 0;
821 }
822 core_initcall(init_wakeup_tracer);
823