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/13] 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 Change-Id: I46743cd006be4b4d573cae2d77df7d6d16744d04
39 Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
40 Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
42 include/instrumentation/events/sched.h | 88 +++++++++++++++++++++++---
43 1 file changed, 78 insertions(+), 10 deletions(-)
45 diff --git a/include/instrumentation/events/sched.h b/include/instrumentation/events/sched.h
46 index 91953a6f..339bec94 100644
47 --- a/include/instrumentation/events/sched.h
48 +++ b/include/instrumentation/events/sched.h
50 #ifndef _TRACE_SCHED_DEF_
51 #define _TRACE_SCHED_DEF_
53 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
54 +#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
56 +static inline long __trace_sched_switch_state(bool preempt,
57 + unsigned int prev_state,
58 + struct task_struct *p)
62 +#ifdef CONFIG_SCHED_DEBUG
63 + BUG_ON(p != current);
64 +#endif /* CONFIG_SCHED_DEBUG */
67 + * Preemption ignores task state, therefore preempted tasks are always
68 + * RUNNING (we will not have dequeued if state != RUNNING).
71 + return TASK_REPORT_MAX;
74 + * task_state_index() uses fls() and returns a value from 0-8 range.
75 + * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
76 + * it for left shift operation to get the correct task->state
79 + state = __task_state_index(prev_state, p->exit_state);
81 + return state ? (1 << (state - 1)) : state;
84 +#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
86 static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
88 @@ -321,43 +351,81 @@ LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
90 * Tracepoint for task switches, performed by the scheduler:
93 +#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
94 LTTNG_TRACEPOINT_EVENT(sched_switch,
96 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
97 TP_PROTO(bool preempt,
98 - struct task_struct *prev,
99 - struct task_struct *next),
100 + unsigned int prev_state,
101 + struct task_struct *prev,
102 + struct task_struct *next),
104 - TP_ARGS(preempt, prev, next),
105 + TP_ARGS(preempt, prev_state, prev, next),
108 + ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
109 + ctf_integer(pid_t, prev_tid, prev->pid)
110 + ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
111 +#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
112 + ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
114 - TP_PROTO(struct task_struct *prev,
115 + ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
117 + ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
118 + ctf_integer(pid_t, next_tid, next->pid)
119 + ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
123 +#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
125 +LTTNG_TRACEPOINT_EVENT(sched_switch,
127 + TP_PROTO(bool preempt,
128 + struct task_struct *prev,
129 struct task_struct *next),
131 - TP_ARGS(prev, next),
132 -#endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0)) */
133 + TP_ARGS(preempt, prev, next),
136 ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
137 ctf_integer(pid_t, prev_tid, prev->pid)
138 ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
139 -#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,4,0))
140 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
141 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev))
143 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev))
145 + ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
146 + ctf_integer(pid_t, next_tid, next->pid)
147 + ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
153 +LTTNG_TRACEPOINT_EVENT(sched_switch,
155 + TP_PROTO(struct task_struct *prev,
156 + struct task_struct *next),
158 + TP_ARGS(prev, next),
161 + ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
162 + ctf_integer(pid_t, prev_tid, prev->pid)
163 + ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
164 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
165 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(prev))
167 ctf_integer(long, prev_state, __trace_sched_switch_state(prev))
170 ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
171 ctf_integer(pid_t, next_tid, next->pid)
172 ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
178 * Tracepoint for a task being migrated: