Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * trace context switch |
| 4 | * |
| 5 | * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com> |
| 6 | * |
| 7 | */ |
| 8 | #include <linux/module.h> |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 9 | #include <linux/kallsyms.h> |
| 10 | #include <linux/uaccess.h> |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 11 | #include <linux/ftrace.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 12 | #include <trace/events/sched.h> |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 13 | |
| 14 | #include "trace.h" |
| 15 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 16 | #define RECORD_CMDLINE 1 |
| 17 | #define RECORD_TGID 2 |
| 18 | |
| 19 | static int sched_cmdline_ref; |
| 20 | static int sched_tgid_ref; |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 21 | static DEFINE_MUTEX(sched_register_mutex); |
Frederic Weisbecker | 82e04af | 2009-07-29 18:00:29 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 23 | static void |
Peter Zijlstra | c73464b | 2015-09-28 18:06:56 +0200 | [diff] [blame] | 24 | probe_sched_switch(void *ignore, bool preempt, |
| 25 | struct task_struct *prev, struct task_struct *next) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 26 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 27 | int flags; |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 28 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 29 | flags = (RECORD_TGID * !!sched_tgid_ref) + |
| 30 | (RECORD_CMDLINE * !!sched_cmdline_ref); |
| 31 | |
| 32 | if (!flags) |
| 33 | return; |
| 34 | tracing_record_taskinfo_sched_switch(prev, next, flags); |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 37 | static void |
Peter Zijlstra | fbd705a | 2015-06-09 11:13:36 +0200 | [diff] [blame] | 38 | probe_sched_wakeup(void *ignore, struct task_struct *wakee) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 39 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 40 | int flags; |
Zhaolei | dcef788 | 2009-03-31 15:26:14 +0800 | [diff] [blame] | 41 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 42 | flags = (RECORD_TGID * !!sched_tgid_ref) + |
| 43 | (RECORD_CMDLINE * !!sched_cmdline_ref); |
| 44 | |
| 45 | if (!flags) |
| 46 | return; |
| 47 | tracing_record_taskinfo(current, flags); |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 50 | static int tracing_sched_register(void) |
| 51 | { |
| 52 | int ret; |
| 53 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 54 | ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 55 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 56 | pr_info("wakeup trace: Couldn't activate tracepoint" |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 57 | " probe to kernel_sched_wakeup\n"); |
| 58 | return ret; |
| 59 | } |
| 60 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 61 | ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 62 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 63 | pr_info("wakeup trace: Couldn't activate tracepoint" |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 64 | " probe to kernel_sched_wakeup_new\n"); |
| 65 | goto fail_deprobe; |
| 66 | } |
| 67 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 68 | ret = register_trace_sched_switch(probe_sched_switch, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 69 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 70 | pr_info("sched trace: Couldn't activate tracepoint" |
Wenji Huang | 73d8b8b | 2009-02-17 01:10:02 -0500 | [diff] [blame] | 71 | " probe to kernel_sched_switch\n"); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 72 | goto fail_deprobe_wake_new; |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | fail_deprobe_wake_new: |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 77 | unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 78 | fail_deprobe: |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 79 | unregister_trace_sched_wakeup(probe_sched_wakeup, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static void tracing_sched_unregister(void) |
| 84 | { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 85 | unregister_trace_sched_switch(probe_sched_switch, NULL); |
| 86 | unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL); |
| 87 | unregister_trace_sched_wakeup(probe_sched_wakeup, NULL); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 90 | static void tracing_start_sched_switch(int ops) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 91 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 92 | bool sched_register = (!sched_cmdline_ref && !sched_tgid_ref); |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 93 | mutex_lock(&sched_register_mutex); |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 94 | |
| 95 | switch (ops) { |
| 96 | case RECORD_CMDLINE: |
| 97 | sched_cmdline_ref++; |
| 98 | break; |
| 99 | |
| 100 | case RECORD_TGID: |
| 101 | sched_tgid_ref++; |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | if (sched_register && (sched_cmdline_ref || sched_tgid_ref)) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 106 | tracing_sched_register(); |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 107 | mutex_unlock(&sched_register_mutex); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 110 | static void tracing_stop_sched_switch(int ops) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 111 | { |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 112 | mutex_lock(&sched_register_mutex); |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 113 | |
| 114 | switch (ops) { |
| 115 | case RECORD_CMDLINE: |
| 116 | sched_cmdline_ref--; |
| 117 | break; |
| 118 | |
| 119 | case RECORD_TGID: |
| 120 | sched_tgid_ref--; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | if (!sched_cmdline_ref && !sched_tgid_ref) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 125 | tracing_sched_unregister(); |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 126 | mutex_unlock(&sched_register_mutex); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 129 | void tracing_start_cmdline_record(void) |
| 130 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 131 | tracing_start_sched_switch(RECORD_CMDLINE); |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void tracing_stop_cmdline_record(void) |
| 135 | { |
Joel Fernandes | d914ba3 | 2017-06-26 19:01:55 -0700 | [diff] [blame] | 136 | tracing_stop_sched_switch(RECORD_CMDLINE); |
| 137 | } |
| 138 | |
| 139 | void tracing_start_tgid_record(void) |
| 140 | { |
| 141 | tracing_start_sched_switch(RECORD_TGID); |
| 142 | } |
| 143 | |
| 144 | void tracing_stop_tgid_record(void) |
| 145 | { |
| 146 | tracing_stop_sched_switch(RECORD_TGID); |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 147 | } |