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