1 From 8e52fd71e693619f7a58de2692e59f0c826e9988 Mon Sep 17 00:00:00 2001
2 From: Michael Jeanson <mjeanson@efficios.com>
3 Date: Mon, 4 Apr 2022 13:52:57 -0400
4 Subject: [PATCH 03/10] fix: sched/tracing: Don't re-read p->state when
5 emitting sched_switch event (v5.18)
9 commit fa2c3254d7cfff5f7a916ab928a562d1165f17bb
10 Author: Valentin Schneider <valentin.schneider@arm.com>
11 Date: Thu Jan 20 16:25:19 2022 +0000
13 sched/tracing: Don't re-read p->state when emitting sched_switch event
17 c6e7bd7afaeb ("sched/core: Optimize ttwu() spinning on p->on_cpu")
19 the following sequence becomes possible:
21 p->__state = TASK_INTERRUPTIBLE;
26 p->__state=TASK_WAKING
28 __trace_sched_switch_state()
32 TASK_WAKING isn't in TASK_REPORT, so the task appears as TASK_RUNNING in
35 Prevent this by pushing the value read from __schedule() down the trace
38 Upstream-Status: Backport
40 Change-Id: I46743cd006be4b4d573cae2d77df7d6d16744d04
41 Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
42 Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
44 include/instrumentation/events/sched.h | 88 +++++++++++++++++++++++---
45 1 file changed, 78 insertions(+), 10 deletions(-)
47 diff --git a/include/instrumentation/events/sched.h b/include/instrumentation/events/sched.h
48 index 91953a6f..339bec94 100644
49 --- a/include/instrumentation/events/sched.h
50 +++ b/include/instrumentation/events/sched.h
52 #ifndef _TRACE_SCHED_DEF_
53 #define _TRACE_SCHED_DEF_
55 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
56 +#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
58 +static inline long __trace_sched_switch_state(bool preempt,
59 + unsigned int prev_state,
60 + struct task_struct *p)
64 +#ifdef CONFIG_SCHED_DEBUG
65 + BUG_ON(p != current);
66 +#endif /* CONFIG_SCHED_DEBUG */
69 + * Preemption ignores task state, therefore preempted tasks are always
70 + * RUNNING (we will not have dequeued if state != RUNNING).
73 + return TASK_REPORT_MAX;
76 + * task_state_index() uses fls() and returns a value from 0-8 range.
77 + * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
78 + * it for left shift operation to get the correct task->state
81 + state = __task_state_index(prev_state, p->exit_state);
83 + return state ? (1 << (state - 1)) : state;
86 +#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
88 static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
90 @@ -321,43 +351,81 @@ LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
92 * Tracepoint for task switches, performed by the scheduler:
95 +#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
96 LTTNG_TRACEPOINT_EVENT(sched_switch,
98 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
99 TP_PROTO(bool preempt,
100 - struct task_struct *prev,
101 - struct task_struct *next),
102 + unsigned int prev_state,
103 + struct task_struct *prev,
104 + struct task_struct *next),
106 - TP_ARGS(preempt, prev, next),
107 + TP_ARGS(preempt, prev_state, prev, next),
110 + ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
111 + ctf_integer(pid_t, prev_tid, prev->pid)
112 + ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
113 +#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
114 + ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
116 - TP_PROTO(struct task_struct *prev,
117 + ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
119 + ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
120 + ctf_integer(pid_t, next_tid, next->pid)
121 + ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
125 +#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
127 +LTTNG_TRACEPOINT_EVENT(sched_switch,
129 + TP_PROTO(bool preempt,
130 + struct task_struct *prev,
131 struct task_struct *next),
133 - TP_ARGS(prev, next),
134 -#endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0)) */
135 + TP_ARGS(preempt, prev, next),
138 ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
139 ctf_integer(pid_t, prev_tid, prev->pid)
140 ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
141 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
142 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
143 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev))
145 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev))
147 + ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
148 + ctf_integer(pid_t, next_tid, next->pid)
149 + ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
155 +LTTNG_TRACEPOINT_EVENT(sched_switch,
157 + TP_PROTO(struct task_struct *prev,
158 + struct task_struct *next),
160 + TP_ARGS(prev, next),
163 + ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
164 + ctf_integer(pid_t, prev_tid, prev->pid)
165 + ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
166 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
167 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(prev))
169 ctf_integer(long, prev_state, __trace_sched_switch_state(prev))
172 ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
173 ctf_integer(pid_t, next_tid, next->pid)
174 ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
180 * Tracepoint for a task being migrated: