• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int			wakeup_rt;
30 static int			wakeup_dl;
31 static int			tracing_dl = 0;
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,int * pc)68 func_prolog_preempt_disable(struct trace_array *tr,
69 			    struct trace_array_cpu **data,
70 			    int *pc)
71 {
72 	long disabled;
73 	int cpu;
74 
75 	if (likely(!wakeup_task))
76 		return 0;
77 
78 	*pc = preempt_count();
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)115 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
116 {
117 	struct trace_array *tr = wakeup_trace;
118 	struct trace_array_cpu *data;
119 	unsigned long flags;
120 	int pc, ret = 0;
121 
122 	if (ftrace_graph_ignore_func(trace))
123 		return 0;
124 	/*
125 	 * Do not trace a function if it's filtered by set_graph_notrace.
126 	 * Make the index of ret stack negative to indicate that it should
127 	 * ignore further functions.  But it needs its own ret stack entry
128 	 * to recover the original index in order to continue tracing after
129 	 * returning from the function.
130 	 */
131 	if (ftrace_graph_notrace_addr(trace->func))
132 		return 1;
133 
134 	if (!func_prolog_preempt_disable(tr, &data, &pc))
135 		return 0;
136 
137 	local_save_flags(flags);
138 	ret = __trace_graph_entry(tr, trace, flags, pc);
139 	atomic_dec(&data->disabled);
140 	preempt_enable_notrace();
141 
142 	return ret;
143 }
144 
wakeup_graph_return(struct ftrace_graph_ret * trace)145 static void wakeup_graph_return(struct ftrace_graph_ret *trace)
146 {
147 	struct trace_array *tr = wakeup_trace;
148 	struct trace_array_cpu *data;
149 	unsigned long flags;
150 	int pc;
151 
152 	ftrace_graph_addr_finish(trace);
153 
154 	if (!func_prolog_preempt_disable(tr, &data, &pc))
155 		return;
156 
157 	local_save_flags(flags);
158 	__trace_graph_return(tr, trace, flags, pc);
159 	atomic_dec(&data->disabled);
160 
161 	preempt_enable_notrace();
162 	return;
163 }
164 
165 static struct fgraph_ops fgraph_wakeup_ops = {
166 	.entryfunc = &wakeup_graph_entry,
167 	.retfunc = &wakeup_graph_return,
168 };
169 
wakeup_trace_open(struct trace_iterator * iter)170 static void wakeup_trace_open(struct trace_iterator *iter)
171 {
172 	if (is_graph(iter->tr))
173 		graph_trace_open(iter);
174 	else
175 		iter->private = NULL;
176 }
177 
wakeup_trace_close(struct trace_iterator * iter)178 static void wakeup_trace_close(struct trace_iterator *iter)
179 {
180 	if (iter->private)
181 		graph_trace_close(iter);
182 }
183 
184 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
185 			    TRACE_GRAPH_PRINT_CPU |  \
186 			    TRACE_GRAPH_PRINT_REL_TIME | \
187 			    TRACE_GRAPH_PRINT_DURATION | \
188 			    TRACE_GRAPH_PRINT_OVERHEAD | \
189 			    TRACE_GRAPH_PRINT_IRQS)
190 
wakeup_print_line(struct trace_iterator * iter)191 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
192 {
193 	/*
194 	 * In graph mode call the graph tracer output function,
195 	 * otherwise go with the TRACE_FN event handler
196 	 */
197 	if (is_graph(iter->tr))
198 		return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
199 
200 	return TRACE_TYPE_UNHANDLED;
201 }
202 
wakeup_print_header(struct seq_file * s)203 static void wakeup_print_header(struct seq_file *s)
204 {
205 	if (is_graph(wakeup_trace))
206 		print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
207 	else
208 		trace_default_header(s);
209 }
210 #endif /* else CONFIG_FUNCTION_GRAPH_TRACER */
211 
212 /*
213  * wakeup uses its own tracer function to keep the overhead down:
214  */
215 static void
wakeup_tracer_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * pt_regs)216 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
217 		   struct ftrace_ops *op, struct pt_regs *pt_regs)
218 {
219 	struct trace_array *tr = wakeup_trace;
220 	struct trace_array_cpu *data;
221 	unsigned long flags;
222 	int pc;
223 
224 	if (!func_prolog_preempt_disable(tr, &data, &pc))
225 		return;
226 
227 	local_irq_save(flags);
228 	trace_function(tr, ip, parent_ip, flags, pc);
229 	local_irq_restore(flags);
230 
231 	atomic_dec(&data->disabled);
232 	preempt_enable_notrace();
233 }
234 
register_wakeup_function(struct trace_array * tr,int graph,int set)235 static int register_wakeup_function(struct trace_array *tr, int graph, int set)
236 {
237 	int ret;
238 
239 	/* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
240 	if (function_enabled || (!set && !(tr->trace_flags & TRACE_ITER_FUNCTION)))
241 		return 0;
242 
243 	if (graph)
244 		ret = register_ftrace_graph(&fgraph_wakeup_ops);
245 	else
246 		ret = register_ftrace_function(tr->ops);
247 
248 	if (!ret)
249 		function_enabled = true;
250 
251 	return ret;
252 }
253 
unregister_wakeup_function(struct trace_array * tr,int graph)254 static void unregister_wakeup_function(struct trace_array *tr, int graph)
255 {
256 	if (!function_enabled)
257 		return;
258 
259 	if (graph)
260 		unregister_ftrace_graph(&fgraph_wakeup_ops);
261 	else
262 		unregister_ftrace_function(tr->ops);
263 
264 	function_enabled = false;
265 }
266 
wakeup_function_set(struct trace_array * tr,u32 mask,int set)267 static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
268 {
269 	if (!(mask & TRACE_ITER_FUNCTION))
270 		return 0;
271 
272 	if (set)
273 		register_wakeup_function(tr, is_graph(tr), 1);
274 	else
275 		unregister_wakeup_function(tr, is_graph(tr));
276 	return 1;
277 }
278 #else /* CONFIG_FUNCTION_TRACER */
register_wakeup_function(struct trace_array * tr,int graph,int set)279 static int register_wakeup_function(struct trace_array *tr, int graph, int set)
280 {
281 	return 0;
282 }
unregister_wakeup_function(struct trace_array * tr,int graph)283 static void unregister_wakeup_function(struct trace_array *tr, int graph) { }
wakeup_function_set(struct trace_array * tr,u32 mask,int set)284 static int wakeup_function_set(struct trace_array *tr, u32 mask, int set)
285 {
286 	return 0;
287 }
288 #endif /* else CONFIG_FUNCTION_TRACER */
289 
290 #ifndef CONFIG_FUNCTION_GRAPH_TRACER
wakeup_print_line(struct trace_iterator * iter)291 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
292 {
293 	return TRACE_TYPE_UNHANDLED;
294 }
295 
wakeup_trace_open(struct trace_iterator * iter)296 static void wakeup_trace_open(struct trace_iterator *iter) { }
wakeup_trace_close(struct trace_iterator * iter)297 static void wakeup_trace_close(struct trace_iterator *iter) { }
298 
wakeup_print_header(struct seq_file * s)299 static void wakeup_print_header(struct seq_file *s)
300 {
301 	trace_default_header(s);
302 }
303 #endif /* !CONFIG_FUNCTION_GRAPH_TRACER */
304 
305 static void
__trace_function(struct trace_array * tr,unsigned long ip,unsigned long parent_ip,unsigned long flags,int pc)306 __trace_function(struct trace_array *tr,
307 		 unsigned long ip, unsigned long parent_ip,
308 		 unsigned long flags, int pc)
309 {
310 	if (is_graph(tr))
311 		trace_graph_function(tr, ip, parent_ip, flags, pc);
312 	else
313 		trace_function(tr, ip, parent_ip, flags, pc);
314 }
315 
wakeup_flag_changed(struct trace_array * tr,u32 mask,int set)316 static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
317 {
318 	struct tracer *tracer = tr->current_trace;
319 
320 	if (wakeup_function_set(tr, mask, set))
321 		return 0;
322 
323 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
324 	if (mask & TRACE_ITER_DISPLAY_GRAPH)
325 		return wakeup_display_graph(tr, set);
326 #endif
327 
328 	return trace_keep_overwrite(tracer, mask, set);
329 }
330 
start_func_tracer(struct trace_array * tr,int graph)331 static int start_func_tracer(struct trace_array *tr, int graph)
332 {
333 	int ret;
334 
335 	ret = register_wakeup_function(tr, graph, 0);
336 
337 	if (!ret && tracing_is_enabled())
338 		tracer_enabled = 1;
339 	else
340 		tracer_enabled = 0;
341 
342 	return ret;
343 }
344 
stop_func_tracer(struct trace_array * tr,int graph)345 static void stop_func_tracer(struct trace_array *tr, int graph)
346 {
347 	tracer_enabled = 0;
348 
349 	unregister_wakeup_function(tr, graph);
350 }
351 
352 /*
353  * Should this new latency be reported/recorded?
354  */
report_latency(struct trace_array * tr,u64 delta)355 static bool report_latency(struct trace_array *tr, u64 delta)
356 {
357 	if (tracing_thresh) {
358 		if (delta < tracing_thresh)
359 			return false;
360 	} else {
361 		if (delta <= tr->max_latency)
362 			return false;
363 	}
364 	return true;
365 }
366 
367 static void
probe_wakeup_migrate_task(void * ignore,struct task_struct * task,int cpu)368 probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
369 {
370 	if (task != wakeup_task)
371 		return;
372 
373 	wakeup_current_cpu = cpu;
374 }
375 
376 static void
tracing_sched_switch_trace(struct trace_array * tr,struct task_struct * prev,struct task_struct * next,unsigned long flags,int pc)377 tracing_sched_switch_trace(struct trace_array *tr,
378 			   struct task_struct *prev,
379 			   struct task_struct *next,
380 			   unsigned long flags, int pc)
381 {
382 	struct trace_event_call *call = &event_context_switch;
383 	struct trace_buffer *buffer = tr->array_buffer.buffer;
384 	struct ring_buffer_event *event;
385 	struct ctx_switch_entry *entry;
386 
387 	event = trace_buffer_lock_reserve(buffer, TRACE_CTX,
388 					  sizeof(*entry), flags, pc);
389 	if (!event)
390 		return;
391 	entry	= ring_buffer_event_data(event);
392 	entry->prev_pid			= prev->pid;
393 	entry->prev_prio		= prev->prio;
394 	entry->prev_state		= task_state_index(prev);
395 	entry->next_pid			= next->pid;
396 	entry->next_prio		= next->prio;
397 	entry->next_state		= task_state_index(next);
398 	entry->next_cpu	= task_cpu(next);
399 
400 	if (!call_filter_check_discard(call, entry, buffer, event))
401 		trace_buffer_unlock_commit(tr, buffer, event, flags, pc);
402 }
403 
404 static void
tracing_sched_wakeup_trace(struct trace_array * tr,struct task_struct * wakee,struct task_struct * curr,unsigned long flags,int pc)405 tracing_sched_wakeup_trace(struct trace_array *tr,
406 			   struct task_struct *wakee,
407 			   struct task_struct *curr,
408 			   unsigned long flags, int pc)
409 {
410 	struct trace_event_call *call = &event_wakeup;
411 	struct ring_buffer_event *event;
412 	struct ctx_switch_entry *entry;
413 	struct trace_buffer *buffer = tr->array_buffer.buffer;
414 
415 	event = trace_buffer_lock_reserve(buffer, TRACE_WAKE,
416 					  sizeof(*entry), flags, pc);
417 	if (!event)
418 		return;
419 	entry	= ring_buffer_event_data(event);
420 	entry->prev_pid			= curr->pid;
421 	entry->prev_prio		= curr->prio;
422 	entry->prev_state		= task_state_index(curr);
423 	entry->next_pid			= wakee->pid;
424 	entry->next_prio		= wakee->prio;
425 	entry->next_state		= task_state_index(wakee);
426 	entry->next_cpu			= task_cpu(wakee);
427 
428 	if (!call_filter_check_discard(call, entry, buffer, event))
429 		trace_buffer_unlock_commit(tr, buffer, event, flags, pc);
430 }
431 
432 static void notrace
probe_wakeup_sched_switch(void * ignore,bool preempt,struct task_struct * prev,struct task_struct * next)433 probe_wakeup_sched_switch(void *ignore, bool preempt,
434 			  struct task_struct *prev, struct task_struct *next)
435 {
436 	struct trace_array_cpu *data;
437 	u64 T0, T1, delta;
438 	unsigned long flags;
439 	long disabled;
440 	int cpu;
441 	int pc;
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 	pc = preempt_count();
461 
462 	/* disable local data, not wakeup_cpu data */
463 	cpu = raw_smp_processor_id();
464 	disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
465 	if (likely(disabled != 1))
466 		goto out;
467 
468 	local_irq_save(flags);
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, flags, pc);
479 	tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
480 	__trace_stack(wakeup_trace, flags, 0, pc);
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 = 0;
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 	unsigned long flags;
533 	long disabled;
534 	int pc;
535 
536 	if (likely(!tracer_enabled))
537 		return;
538 
539 	tracing_record_cmdline(p);
540 	tracing_record_cmdline(current);
541 
542 	/*
543 	 * Semantic is like this:
544 	 *  - wakeup tracer handles all tasks in the system, independently
545 	 *    from their scheduling class;
546 	 *  - wakeup_rt tracer handles tasks belonging to sched_dl and
547 	 *    sched_rt class;
548 	 *  - wakeup_dl handles tasks belonging to sched_dl class only.
549 	 */
550 	if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
551 	    (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
552 	    (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
553 		return;
554 
555 	pc = preempt_count();
556 	disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
557 	if (unlikely(disabled != 1))
558 		goto out;
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 = 1;
581 	else
582 		tracing_dl = 0;
583 
584 	wakeup_task = get_task_struct(p);
585 
586 	local_save_flags(flags);
587 
588 	data = per_cpu_ptr(wakeup_trace->array_buffer.data, wakeup_cpu);
589 	data->preempt_timestamp = ftrace_now(cpu);
590 	tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
591 	__trace_stack(wakeup_trace, flags, 0, pc);
592 
593 	/*
594 	 * We must be careful in using CALLER_ADDR2. But since wake_up
595 	 * is not called by an assembly function  (where as schedule is)
596 	 * it should be safe to use it here.
597 	 */
598 	__trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
599 
600 out_locked:
601 	arch_spin_unlock(&wakeup_lock);
602 out:
603 	atomic_dec(&per_cpu_ptr(wakeup_trace->array_buffer.data, cpu)->disabled);
604 }
605 
start_wakeup_tracer(struct trace_array * tr)606 static void start_wakeup_tracer(struct trace_array *tr)
607 {
608 	int ret;
609 
610 	ret = register_trace_sched_wakeup(probe_wakeup, NULL);
611 	if (ret) {
612 		pr_info("wakeup trace: Couldn't activate tracepoint"
613 			" probe to kernel_sched_wakeup\n");
614 		return;
615 	}
616 
617 	ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
618 	if (ret) {
619 		pr_info("wakeup trace: Couldn't activate tracepoint"
620 			" probe to kernel_sched_wakeup_new\n");
621 		goto fail_deprobe;
622 	}
623 
624 	ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
625 	if (ret) {
626 		pr_info("sched trace: Couldn't activate tracepoint"
627 			" probe to kernel_sched_switch\n");
628 		goto fail_deprobe_wake_new;
629 	}
630 
631 	ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
632 	if (ret) {
633 		pr_info("wakeup trace: Couldn't activate tracepoint"
634 			" probe to kernel_sched_migrate_task\n");
635 		goto fail_deprobe_sched_switch;
636 	}
637 
638 	wakeup_reset(tr);
639 
640 	/*
641 	 * Don't let the tracer_enabled = 1 show up before
642 	 * the wakeup_task is reset. This may be overkill since
643 	 * wakeup_reset does a spin_unlock after setting the
644 	 * wakeup_task to NULL, but I want to be safe.
645 	 * This is a slow path anyway.
646 	 */
647 	smp_wmb();
648 
649 	if (start_func_tracer(tr, is_graph(tr)))
650 		printk(KERN_ERR "failed to start wakeup tracer\n");
651 
652 	return;
653 fail_deprobe_sched_switch:
654 	unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
655 fail_deprobe_wake_new:
656 	unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
657 fail_deprobe:
658 	unregister_trace_sched_wakeup(probe_wakeup, NULL);
659 }
660 
stop_wakeup_tracer(struct trace_array * tr)661 static void stop_wakeup_tracer(struct trace_array *tr)
662 {
663 	tracer_enabled = 0;
664 	stop_func_tracer(tr, is_graph(tr));
665 	unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
666 	unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
667 	unregister_trace_sched_wakeup(probe_wakeup, NULL);
668 	unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
669 }
670 
671 static bool wakeup_busy;
672 
__wakeup_tracer_init(struct trace_array * tr)673 static int __wakeup_tracer_init(struct trace_array *tr)
674 {
675 	save_flags = tr->trace_flags;
676 
677 	/* non overwrite screws up the latency tracers */
678 	set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
679 	set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
680 
681 	tr->max_latency = 0;
682 	wakeup_trace = tr;
683 	ftrace_init_array_ops(tr, wakeup_tracer_call);
684 	start_wakeup_tracer(tr);
685 
686 	wakeup_busy = true;
687 	return 0;
688 }
689 
wakeup_tracer_init(struct trace_array * tr)690 static int wakeup_tracer_init(struct trace_array *tr)
691 {
692 	if (wakeup_busy)
693 		return -EBUSY;
694 
695 	wakeup_dl = 0;
696 	wakeup_rt = 0;
697 	return __wakeup_tracer_init(tr);
698 }
699 
wakeup_rt_tracer_init(struct trace_array * tr)700 static int wakeup_rt_tracer_init(struct trace_array *tr)
701 {
702 	if (wakeup_busy)
703 		return -EBUSY;
704 
705 	wakeup_dl = 0;
706 	wakeup_rt = 1;
707 	return __wakeup_tracer_init(tr);
708 }
709 
wakeup_dl_tracer_init(struct trace_array * tr)710 static int wakeup_dl_tracer_init(struct trace_array *tr)
711 {
712 	if (wakeup_busy)
713 		return -EBUSY;
714 
715 	wakeup_dl = 1;
716 	wakeup_rt = 0;
717 	return __wakeup_tracer_init(tr);
718 }
719 
wakeup_tracer_reset(struct trace_array * tr)720 static void wakeup_tracer_reset(struct trace_array *tr)
721 {
722 	int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
723 	int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
724 
725 	stop_wakeup_tracer(tr);
726 	/* make sure we put back any tasks we are tracing */
727 	wakeup_reset(tr);
728 
729 	set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
730 	set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
731 	ftrace_reset_array_ops(tr);
732 	wakeup_busy = false;
733 }
734 
wakeup_tracer_start(struct trace_array * tr)735 static void wakeup_tracer_start(struct trace_array *tr)
736 {
737 	wakeup_reset(tr);
738 	tracer_enabled = 1;
739 }
740 
wakeup_tracer_stop(struct trace_array * tr)741 static void wakeup_tracer_stop(struct trace_array *tr)
742 {
743 	tracer_enabled = 0;
744 }
745 
746 static struct tracer wakeup_tracer __read_mostly =
747 {
748 	.name		= "wakeup",
749 	.init		= wakeup_tracer_init,
750 	.reset		= wakeup_tracer_reset,
751 	.start		= wakeup_tracer_start,
752 	.stop		= wakeup_tracer_stop,
753 	.print_max	= true,
754 	.print_header	= wakeup_print_header,
755 	.print_line	= wakeup_print_line,
756 	.flag_changed	= wakeup_flag_changed,
757 #ifdef CONFIG_FTRACE_SELFTEST
758 	.selftest    = trace_selftest_startup_wakeup,
759 #endif
760 	.open		= wakeup_trace_open,
761 	.close		= wakeup_trace_close,
762 	.allow_instances = true,
763 	.use_max_tr	= true,
764 };
765 
766 static struct tracer wakeup_rt_tracer __read_mostly =
767 {
768 	.name		= "wakeup_rt",
769 	.init		= wakeup_rt_tracer_init,
770 	.reset		= wakeup_tracer_reset,
771 	.start		= wakeup_tracer_start,
772 	.stop		= wakeup_tracer_stop,
773 	.print_max	= true,
774 	.print_header	= wakeup_print_header,
775 	.print_line	= wakeup_print_line,
776 	.flag_changed	= wakeup_flag_changed,
777 #ifdef CONFIG_FTRACE_SELFTEST
778 	.selftest    = trace_selftest_startup_wakeup,
779 #endif
780 	.open		= wakeup_trace_open,
781 	.close		= wakeup_trace_close,
782 	.allow_instances = true,
783 	.use_max_tr	= true,
784 };
785 
786 static struct tracer wakeup_dl_tracer __read_mostly =
787 {
788 	.name		= "wakeup_dl",
789 	.init		= wakeup_dl_tracer_init,
790 	.reset		= wakeup_tracer_reset,
791 	.start		= wakeup_tracer_start,
792 	.stop		= wakeup_tracer_stop,
793 	.print_max	= true,
794 	.print_header	= wakeup_print_header,
795 	.print_line	= wakeup_print_line,
796 	.flag_changed	= wakeup_flag_changed,
797 #ifdef CONFIG_FTRACE_SELFTEST
798 	.selftest    = trace_selftest_startup_wakeup,
799 #endif
800 	.open		= wakeup_trace_open,
801 	.close		= wakeup_trace_close,
802 	.allow_instances = true,
803 	.use_max_tr	= true,
804 };
805 
init_wakeup_tracer(void)806 __init static int init_wakeup_tracer(void)
807 {
808 	int ret;
809 
810 	ret = register_tracer(&wakeup_tracer);
811 	if (ret)
812 		return ret;
813 
814 	ret = register_tracer(&wakeup_rt_tracer);
815 	if (ret)
816 		return ret;
817 
818 	ret = register_tracer(&wakeup_dl_tracer);
819 	if (ret)
820 		return ret;
821 
822 	return 0;
823 }
824 core_initcall(init_wakeup_tracer);
825