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