blob: e304196d7c2853ae0056c06c78d74819468265ca [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Steven Rostedt35e8e302008-05-12 21:20:42 +02002/*
3 * trace context switch
4 *
5 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
6 *
7 */
8#include <linux/module.h>
Steven Rostedt35e8e302008-05-12 21:20:42 +02009#include <linux/kallsyms.h>
10#include <linux/uaccess.h>
Steven Rostedt35e8e302008-05-12 21:20:42 +020011#include <linux/ftrace.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040012#include <trace/events/sched.h>
Steven Rostedt35e8e302008-05-12 21:20:42 +020013
14#include "trace.h"
15
Joel Fernandesd914ba32017-06-26 19:01:55 -070016#define RECORD_CMDLINE 1
17#define RECORD_TGID 2
18
19static int sched_cmdline_ref;
20static int sched_tgid_ref;
Frederic Weisbeckerefade6e2008-10-31 13:28:58 +010021static DEFINE_MUTEX(sched_register_mutex);
Frederic Weisbecker82e04af2009-07-29 18:00:29 +020022
Ingo Molnare309b412008-05-12 21:20:51 +020023static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +020024probe_sched_switch(void *ignore, bool preempt,
25 struct task_struct *prev, struct task_struct *next)
Steven Rostedt35e8e302008-05-12 21:20:42 +020026{
Joel Fernandesd914ba32017-06-26 19:01:55 -070027 int flags;
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -040028
Joel Fernandesd914ba32017-06-26 19:01:55 -070029 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 Rostedt35e8e302008-05-12 21:20:42 +020035}
36
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020037static void
Peter Zijlstrafbd705a2015-06-09 11:13:36 +020038probe_sched_wakeup(void *ignore, struct task_struct *wakee)
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020039{
Joel Fernandesd914ba32017-06-26 19:01:55 -070040 int flags;
Zhaoleidcef7882009-03-31 15:26:14 +080041
Joel Fernandesd914ba32017-06-26 19:01:55 -070042 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 Molnar57422792008-05-12 21:20:51 +020048}
49
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020050static int tracing_sched_register(void)
51{
52 int ret;
53
Steven Rostedt38516ab2010-04-20 17:04:50 -040054 ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020055 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -040056 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020057 " probe to kernel_sched_wakeup\n");
58 return ret;
59 }
60
Steven Rostedt38516ab2010-04-20 17:04:50 -040061 ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020062 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -040063 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020064 " probe to kernel_sched_wakeup_new\n");
65 goto fail_deprobe;
66 }
67
Steven Rostedt38516ab2010-04-20 17:04:50 -040068 ret = register_trace_sched_switch(probe_sched_switch, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020069 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -040070 pr_info("sched trace: Couldn't activate tracepoint"
Wenji Huang73d8b8b2009-02-17 01:10:02 -050071 " probe to kernel_sched_switch\n");
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020072 goto fail_deprobe_wake_new;
73 }
74
75 return ret;
76fail_deprobe_wake_new:
Steven Rostedt38516ab2010-04-20 17:04:50 -040077 unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020078fail_deprobe:
Steven Rostedt38516ab2010-04-20 17:04:50 -040079 unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020080 return ret;
81}
82
83static void tracing_sched_unregister(void)
84{
Steven Rostedt38516ab2010-04-20 17:04:50 -040085 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 Desnoyers5b82a1b2008-05-12 21:21:10 +020088}
89
Joel Fernandesd914ba32017-06-26 19:01:55 -070090static void tracing_start_sched_switch(int ops)
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +020091{
Mathieu Desnoyers64ae5722019-08-17 10:12:08 -040092 bool sched_register;
93
Frederic Weisbeckerefade6e2008-10-31 13:28:58 +010094 mutex_lock(&sched_register_mutex);
Mathieu Desnoyers64ae5722019-08-17 10:12:08 -040095 sched_register = (!sched_cmdline_ref && !sched_tgid_ref);
Joel Fernandesd914ba32017-06-26 19:01:55 -070096
97 switch (ops) {
98 case RECORD_CMDLINE:
99 sched_cmdline_ref++;
100 break;
101
102 case RECORD_TGID:
103 sched_tgid_ref++;
104 break;
105 }
106
107 if (sched_register && (sched_cmdline_ref || sched_tgid_ref))
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200108 tracing_sched_register();
Frederic Weisbeckerefade6e2008-10-31 13:28:58 +0100109 mutex_unlock(&sched_register_mutex);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200110}
111
Joel Fernandesd914ba32017-06-26 19:01:55 -0700112static void tracing_stop_sched_switch(int ops)
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200113{
Frederic Weisbeckerefade6e2008-10-31 13:28:58 +0100114 mutex_lock(&sched_register_mutex);
Joel Fernandesd914ba32017-06-26 19:01:55 -0700115
116 switch (ops) {
117 case RECORD_CMDLINE:
118 sched_cmdline_ref--;
119 break;
120
121 case RECORD_TGID:
122 sched_tgid_ref--;
123 break;
124 }
125
126 if (!sched_cmdline_ref && !sched_tgid_ref)
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200127 tracing_sched_unregister();
Frederic Weisbeckerefade6e2008-10-31 13:28:58 +0100128 mutex_unlock(&sched_register_mutex);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200129}
130
Steven Rostedt41bc8142008-05-22 11:49:22 -0400131void tracing_start_cmdline_record(void)
132{
Joel Fernandesd914ba32017-06-26 19:01:55 -0700133 tracing_start_sched_switch(RECORD_CMDLINE);
Steven Rostedt41bc8142008-05-22 11:49:22 -0400134}
135
136void tracing_stop_cmdline_record(void)
137{
Joel Fernandesd914ba32017-06-26 19:01:55 -0700138 tracing_stop_sched_switch(RECORD_CMDLINE);
139}
140
141void tracing_start_tgid_record(void)
142{
143 tracing_start_sched_switch(RECORD_TGID);
144}
145
146void tracing_stop_tgid_record(void)
147{
148 tracing_stop_sched_switch(RECORD_TGID);
Steven Rostedt41bc8142008-05-22 11:49:22 -0400149}