Skip to contentSkip to conversation

Add monitors to validate task switch

RFC PATCH Superseded Gabriele Monaco 4 participants 22 messages Apr 4, 2025 – Apr 16, 2025

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Gabriele Monaco

author maintainer
#0 lore raw

cover Add monitors to validate task switch

This series adds two monitors to the sched collection that imply two previously existing monitors:

tss => sts:
Not only prove that switches occur in scheduling context but also that each call to the scheduler implies a switch (also a vain one) and disables interrupts doing so.

srs => snroc:
Describe different types of switches and their requirements, e.g.:
* preemption requires need resched which is cleared by any switch
* suspension requires setting the task to sleepable and, after the switch occurs, the task requires a wakeup to come back to runnable
The monitor implies setting the task state occurs only when the task is running, which is what snroc was guaranteeing.

We also include some minor cleanup patches (1-4) tracepoints (5) and preparatory fixes (6-7) covering some corner cases:

Patch 1 fixes the behaviour of the rv tool with -s and idle tasks.

Patch 2 allows the rv tool to gracefully terminate with SIGTERM

Patch 3 adds da_handle_start_run_event_ also to per-task monitors

Patch 4 removes a trailing whitespace from the rv tracepoint string

Patch 5 adds the need_resched and switch_vain tracepoints

Patch 6 treats scheduling out sleepable tasks with pending signals as wakeup (with appropriate tracepoint calls and changing the state)

Patch 7 detects race conditions when rv monitors run concurrently and retries applying the events

Patch 8 adds the sts monitor

Patch 9 adds the srs monitor

NOTES

The srs monitor is quite complex and there are a few things that I cannot fully explain:

* A sleeping task requires to set the state to sleepable, but in case of a task sleeping on an rtlock, the set sleepable and wakeup events race and we don't always see the right order:

5d..2. 107.488369: event: 639: sleepable x set_sleepable -> sleepable 4d..5. 107.488369: event: 639: sleepable x wakeup -> running (final) 5d..3. 107.488385: error: 639: switch_suspend not expected in the state running

wakeup() set_state()
state=RUNNING
trace_set_state()
trace_wakeup()
state=SLEEPING

I added a special event (switch_block) but there may be a better way. Changing the order between tracepoints and setting the state doesn't change, we can still be out of sync.

* I consider preemption any scheduling with preempt==true and assume this can happen only if need resched is set (either via tif or preemption count). The tracepoint tracks only when the tif is set and I added a special event with a schedule entry with (preempt && !tif_need_resched() && test_preempt_need_resched()) to account for preemptions where the tif was not set (perhaps there's a better way). Nevertheless, I still rarely see some preemptions without either set. Not sure if it's a glitch in the model or a case I didn't consider.

To: Ingo Molnar <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Steven Rostedt <[email protected]>
Cc: Tomas Glozar <[email protected]>
Cc: Juri Lelli <[email protected]>

Gabriele Monaco (9):
tools/rv: Do not skip idle in trace
tools/rv: Stop gracefully also on SIGTERM
rv: Add da_handle_start_run_event_ to per-task monitors
rv: Remove trailing whitespace from tracepoint string
sched: Add sched tracepoints for RV task model
sched: Treat try_to_block_task with pending signal as wakeup
rv: Retry when da monitor detects race conditions
rv: Replace tss monitor with more complete sts
rv: Add srs per-task monitor

 Documentation/trace/rv/monitor_sched.rst      | 237 +++++++++++++++---
 include/linux/rv.h                            |   3 +-
 include/linux/sched.h                         |   7 +-
 include/rv/da_monitor.h                       |  75 +++++-
 include/trace/events/sched.h                  |   8 +
 kernel/sched/core.c                           |  20 +-
 kernel/trace/rv/Kconfig                       |   4 +-
 kernel/trace/rv/Makefile                      |   4 +-
 kernel/trace/rv/monitors/snroc/snroc.c        |  85 -------
 kernel/trace/rv/monitors/snroc/snroc.h        |  47 ----
 .../trace/rv/monitors/{snroc => srs}/Kconfig  |   8 +-
 kernel/trace/rv/monitors/srs/srs.c            | 135 ++++++++++
 kernel/trace/rv/monitors/srs/srs.h            |  87 +++++++
 .../{snroc/snroc_trace.h => srs/srs_trace.h}  |   8 +-
 kernel/trace/rv/monitors/sts/Kconfig          |  18 ++
 kernel/trace/rv/monitors/sts/sts.c            | 118 +++++++++
 kernel/trace/rv/monitors/sts/sts.h            |  62 +++++
 .../{tss/tss_trace.h => sts/sts_trace.h}      |   8 +-
 kernel/trace/rv/monitors/tss/Kconfig          |  14 --
 kernel/trace/rv/monitors/tss/tss.c            |  91 -------
 kernel/trace/rv/monitors/tss/tss.h            |  47 ----
 kernel/trace/rv/rv_trace.h                    |  12 +-
 tools/verification/models/sched/snroc.dot     |  18 --
 tools/verification/models/sched/srs.dot       |  61 +++++
 tools/verification/models/sched/sts.dot       |  29 +++
 tools/verification/models/sched/tss.dot       |  18 --
 tools/verification/rv/src/in_kernel.c         |   2 +-
 tools/verification/rv/src/rv.c                |   1 +
 28 files changed, 828 insertions(+), 399 deletions(-)
 delete mode 100644 kernel/trace/rv/monitors/snroc/snroc.c
 delete mode 100644 kernel/trace/rv/monitors/snroc/snroc.h
 rename kernel/trace/rv/monitors/{snroc => srs}/Kconfig (52%)
 create mode 100644 kernel/trace/rv/monitors/srs/srs.c
 create mode 100644 kernel/trace/rv/monitors/srs/srs.h
 rename kernel/trace/rv/monitors/{snroc/snroc_trace.h => srs/srs_trace.h} (67%)
 create mode 100644 kernel/trace/rv/monitors/sts/Kconfig
 create mode 100644 kernel/trace/rv/monitors/sts/sts.c
 create mode 100644 kernel/trace/rv/monitors/sts/sts.h
 rename kernel/trace/rv/monitors/{tss/tss_trace.h => sts/sts_trace.h} (67%)
 delete mode 100644 kernel/trace/rv/monitors/tss/Kconfig
 delete mode 100644 kernel/trace/rv/monitors/tss/tss.c
 delete mode 100644 kernel/trace/rv/monitors/tss/tss.h
 delete mode 100644 tools/verification/models/sched/snroc.dot
 create mode 100644 tools/verification/models/sched/srs.dot
 create mode 100644 tools/verification/models/sched/sts.dot
 delete mode 100644 tools/verification/models/sched/tss.dot

base-commit: e48e99b6edf41c69c5528aa7ffb2daf3c59ee105

Signature · 1 line
2.49.0
author maintainer
#1 lore raw

RFC PATCH 1/9 tools/rv: Do not skip idle in trace

Currently, the userspace RV tool skips trace events triggered by the RV tool itself, this can be changed by passing the parameter -s, which sets the variable config_my_pid to 0 (instead of the tool's PID). The current condition for per-task monitors (config_has_id) does not check that config_my_pid isn't 0 to skip. In case we pass -s, we show events triggered by RV but don't show those triggered by idle (PID 0).

Fix the condition to account this scenario.

Fixes 6d60f89691fc ("tools/rv: Add in-kernel monitor interface")
---
 tools/verification/rv/src/in_kernel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/verification/rv/src/in_kernel.c +1 -1
index c0dcee795c0de..72b03bae021cd 100644
@@ -429,7 +429,7 @@ ikm_event_handler(struct trace_seq *s, struct tep_record *record,
 
 	tep_get_common_field_val(s, trace_event, "common_pid", record, &pid, 1);
 
-	if (config_has_id && (config_my_pid == id))
+	if (config_my_pid && config_has_id && (config_my_pid == id))
 		return 0;
 	else if (config_my_pid && (config_my_pid == pid))
 		return 0;
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

RFC PATCH 2/9 tools/rv: Stop gracefully also on SIGTERM

In reply to Gabriele Monaco

Currently the userspace RV tool starts a monitor and waits for the user to press Ctrl-C (SIGINT) to terminate and stop the monitor. This doesn't account for a scenario where a user starts RV in background and simply kills it (SIGTERM unless the user specifies differently). E.g.:

 # rv mon wip &
 # kill %

Would terminate RV without stopping the monitor and next RV executions won't start correctly.

Register the signal handler used for SIGINT also to SIGTERM.

---
 tools/verification/rv/src/rv.c | 1 +
 1 file changed, 1 insertion(+)
tools/verification/rv/src/rv.c +1 -0
index 239de054d1e06..b8fe24a87d97c 100644
@@ -191,6 +191,7 @@ int main(int argc, char **argv)
 		 * and exit.
 		 */
 		signal(SIGINT, stop_rv);
+		signal(SIGTERM, stop_rv);
 
 		rv_mon(argc - 1, &argv[1]);
 	}
Signature · 1 line
2.49.0
author maintainer
#3 lore raw

RFC PATCH 3/9 Add da_handle_start_run_event_ to per-task monitors

In reply to Gabriele Monaco

The RV da_monitor API allows to start monitors in two ways:
da_handle_start_event_NAME and da_handle_start_run_event_NAME. The former is used when the event is followed by the initial state of the module, so we ignore the event but we know the monitor is in the initial state and can start monitoring, the latter can be used if the event can only occur in the initial state, so we do handle the event as if the monitor was in the initial state.
This latter API is defined for implicit monitors but not per-task ones.

Define da_handle_start_run_event_NAME macro also for per-task monitors.

---
 include/rv/da_monitor.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
include/rv/da_monitor.h +24 -0
index 510c88bfabd43..215c3eb770ccc 100644
@@ -512,6 +512,30 @@ da_handle_start_event_##name(struct task_struct *tsk, enum events_##name event)
 	__da_handle_event_##name(da_mon, tsk, event);						\
 												\
 	return 1;										\
+}												\
+												\
+/*												\
+ * da_handle_start_run_event_##name - start monitoring and handle event				\
+ *												\
+ * This function is used to notify the monitor that the system is in the			\
+ * initial state, so the monitor can start monitoring and handling event.			\
+ */												\
+static inline bool										\
+da_handle_start_run_event_##name(struct task_struct *tsk, enum events_##name event)		\
+{												\
+	struct da_monitor *da_mon;								\
+												\
+	if (!da_monitor_enabled_##name())							\
+		return 0;									\
+												\
+	da_mon = da_get_monitor_##name(tsk);							\
+												\
+	if (unlikely(!da_monitoring_##name(da_mon)))						\
+		da_monitor_start_##name(da_mon);						\
+												\
+	__da_handle_event_##name(da_mon, tsk, event);						\
+												\
+	return 1;										\
 }
 
 /*
Signature · 1 line
2.49.0
author maintainer
#4 lore raw

RFC PATCH 4/9 Remove trailing whitespace from tracepoint string

In reply to Gabriele Monaco

RV event tracepoints print a line with the format:

    "event_xyz: S0 x event -> S1 "
    "event_xyz: S1 x event -> S0 (final)"

While printing an event leading to a non-final state, the line has a trailing white space (visible above before the closing ").

Adapt the format string not to print the trailing whitespace if we are not printing "(final)".

---
 kernel/trace/rv/rv_trace.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/rv/rv_trace.h +4 -4
index 422b75f58891e..18fa0e358a30e 100644
@@ -29,11 +29,11 @@ DECLARE_EVENT_CLASS(event_da_monitor,
 		__entry->final_state		= final_state;
 	),
 
-	TP_printk("%s x %s -> %s %s",
+	TP_printk("%s x %s -> %s%s",
 		__entry->state,
 		__entry->event,
 		__entry->next_state,
-		__entry->final_state ? "(final)" : "")
+		__entry->final_state ? " (final)" : "")
 );
 
 DECLARE_EVENT_CLASS(error_da_monitor,
@@ -90,12 +90,12 @@ DECLARE_EVENT_CLASS(event_da_monitor_id,
 		__entry->final_state		= final_state;
 	),
 
-	TP_printk("%d: %s x %s -> %s %s",
+	TP_printk("%d: %s x %s -> %s%s",
 		__entry->id,
 		__entry->state,
 		__entry->event,
 		__entry->next_state,
-		__entry->final_state ? "(final)" : "")
+		__entry->final_state ? " (final)" : "")
 );
 
 DECLARE_EVENT_CLASS(error_da_monitor_id,
Signature · 1 line
2.49.0
author maintainer
#5 lore raw

RFC PATCH 5/9 sched: Add sched tracepoints for RV task model

In reply to Gabriele Monaco

Add the following tracepoints:
* sched_set_need_resched(tsk, cpu, tif)
Called when a task is set the need resched [lazy] flag
* sched_switch_vain(preempt, tsk, tsk_state) Called when a task is selected again during __schedule i.e. prev == next == tsk : no real context switch

These tracepoints are useful to describe the Linux task model and are adapted from the patches by Daniel Bristot de Oliveira
(https://bristot.me/linux-task-model/).

---
 include/linux/sched.h        | 7 ++++++-
 include/trace/events/sched.h | 8 ++++++++
 kernel/sched/core.c          | 9 +++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
include/linux/sched.h +6 -1
index f96ac19828934..7bcd37493ab07 100644
@@ -340,9 +340,11 @@ extern void io_schedule_finish(int token);
 extern long io_schedule_timeout(long timeout);
 extern void io_schedule(void);
 
-/* wrapper function to trace from this header file */
+/* wrapper functions to trace from this header file */
 DECLARE_TRACEPOINT(sched_set_state_tp);
 extern void __trace_set_current_state(int state_value);
+DECLARE_TRACEPOINT(sched_set_need_resched_tp);
+extern void __trace_set_need_resched(struct task_struct *curr, int tif);
 
 /**
  * struct prev_cputime - snapshot of system and user cputime
@@ -2064,6 +2066,9 @@ static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
 
 static inline void set_tsk_need_resched(struct task_struct *tsk)
 {
+	if (tracepoint_enabled(sched_set_need_resched_tp) &&
+	    !test_tsk_thread_flag(tsk, TIF_NEED_RESCHED))
+		__trace_set_need_resched(tsk, TIF_NEED_RESCHED);
 	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
 }
 
include/trace/events/sched.h +8 -0
index 8994e97d86c13..77bbcce407cb4 100644
@@ -835,6 +835,14 @@ DECLARE_TRACE_CONDITION(sched_set_state_tp,
 	TP_ARGS(tsk, state),
 	TP_CONDITION(!!(tsk->__state) != !!state));
 
+DECLARE_TRACE(sched_set_need_resched_tp,
+	TP_PROTO(struct task_struct *tsk, int cpu, int tif),
+	TP_ARGS(tsk, cpu, tif));
+
+DECLARE_TRACE(sched_switch_vain_tp,
+	TP_PROTO(bool preempt, struct task_struct *tsk, unsigned int prev_state),
+	TP_ARGS(preempt, tsk, prev_state));
+
 #endif /* _TRACE_SCHED_H */
 
 /* This part must be outside protection */
kernel/sched/core.c +9 -0
index cfaca3040b2f8..f2f79236d5811 100644
@@ -1109,6 +1109,7 @@ static void __resched_curr(struct rq *rq, int tif)
 
 	cpu = cpu_of(rq);
 
+	trace_sched_set_need_resched_tp(curr, cpu, tif);
 	if (cpu == smp_processor_id()) {
 		set_ti_thread_flag(cti, tif);
 		if (tif == TIF_NEED_RESCHED)
@@ -1124,6 +1125,13 @@ static void __resched_curr(struct rq *rq, int tif)
 	}
 }
 
+#ifdef CONFIG_SMP
+void __trace_set_need_resched(struct task_struct *curr, int tif)
+{
+	trace_sched_set_need_resched_tp(curr, smp_processor_id(), tif);
+}
+#endif
+
 void resched_curr(struct rq *rq)
 {
 	__resched_curr(rq, TIF_NEED_RESCHED);
@@ -6767,6 +6775,7 @@ static void __sched notrace __schedule(int sched_mode)
 		rq = context_switch(rq, prev, next, &rf);
 	} else {
 		rq_unpin_lock(rq, &rf);
+		trace_sched_switch_vain_tp(preempt, prev, prev_state);
 		__balance_callbacks(rq);
 		raw_spin_rq_unlock_irq(rq);
 	}
Signature · 1 line
2.49.0
author maintainer
#8 lore raw

RFC PATCH 6/9 sched: Treat try_to_block_task with pending signal as wakeup

In reply to Gabriele Monaco

If a task sets itself to interruptible and schedules, the __schedule function checks whether there's a pending signal and, if that's the case, updates the state of the task to runnable instead of dequeuing. By looking at the tracepoints, we see the task enters the scheduler while sleepable but exits as runnable. From a modelling perspective, this is equivalent to a wakeup and the tracepoints should reflect that.

Add the waking/wakeup tracepoints in the try_to_block_task function and set the prev_state used by the sched_switch tracepoint to TASK_RUNNING if the task had a pending signal and was not blocked.

---
 kernel/sched/core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/core.c +9 -2
index f2f79236d5811..48cb32abce01a 100644
@@ -6584,7 +6584,12 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
 	int flags = DEQUEUE_NOCLOCK;
 
 	if (signal_pending_state(task_state, p)) {
-		WRITE_ONCE(p->__state, TASK_RUNNING);
+		/*
+		 * From a modelling perspective, this is equivalent to a wakeup
+		 * before dequeuing the task: trace accordingly.
+		 */
+		trace_sched_waking(p);
+		ttwu_do_wakeup(p);
 		return false;
 	}
 
@@ -6721,7 +6726,9 @@ static void __sched notrace __schedule(int sched_mode)
 			goto picked;
 		}
 	} else if (!preempt && prev_state) {
-		try_to_block_task(rq, prev, prev_state);
+		/* Task was not blocked due to a signal and is again runnable */
+		if (!try_to_block_task(rq, prev, prev_state))
+			prev_state = TASK_RUNNING;
 		switch_count = &prev->nvcsw;
 	}
 
Signature · 1 line
2.49.0
author maintainer
#15 lore raw

RFC PATCH 7/9 Retry when da monitor detects race conditions

In reply to Gabriele Monaco

DA monitor can be accessed from multiple cores simultaneously, this is likely, for instance when dealing with per-task monitors reacting on events that do not always occur on the CPU where the task is running. This can cause race conditions where two events change the next state and we see inconsistent values. E.g.:

[62] event_srs: 27: sleepable x sched_wakeup -> running (final)
[63] event_srs: 27: sleepable x sched_set_state_sleepable -> sleepable
[63] error_srs: 27: event sched_switch_suspend not expected in the state running

In this case the monitor fails because the event on CPU 62 wins against the one on CPU 63, although the correct state should have been sleepable, since the task get suspended.

Detect if the current state was modified by using try_cmpxchg while storing the next value. If it was, try again reading the current state. After a maximum number of failed retries, react as if it was an error with invalid current state (we cannot determine it).

Monitors where this type of condition can occur must be able to account for racing events in any possible order, as we cannot know the winner.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/rv.h      |  3 ++-
 include/rv/da_monitor.h | 53 +++++++++++++++++++++++++++++++----------
 2 files changed, 42 insertions(+), 14 deletions(-)
include/linux/rv.h +2 -1
index 3452b5e4b29e7..a83a81ac6e466 100644
@@ -7,7 +7,8 @@
 #ifndef _LINUX_RV_H
 #define _LINUX_RV_H
 
-#define MAX_DA_NAME_LEN	32
+#define MAX_DA_NAME_LEN			32
+#define MAX_DA_RETRY_RACING_EVENTS	3
 
 #ifdef CONFIG_RV
 /*
include/rv/da_monitor.h +40 -13

90 lines not on this page · raw message

Signature · 1 line
2.49.0
author maintainer
#18 lore raw

RFC PATCH 8/9 Replace tss monitor with more complete sts

In reply to Gabriele Monaco

The tss monitor currently guarantees context switches can happen only while scheduling, but it doesn't enforce that each scheduling call implies a task switch. This can be implied only if we rely on the newly introduced sched_switch_vain tracepoint, which represents a scheduler call where the previously running task is the same that is picked to run next, in fact no context is switched.

Replace the monitor with a more comprehensive specification which implies tss but also ensures that:
* each scheduler call switches context (or has a vain switch)
* each context switch happens with interrupts disabled

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 Documentation/trace/rv/monitor_sched.rst      |  68 +++++++---
 kernel/trace/rv/Kconfig                       |   2 +-
 kernel/trace/rv/Makefile                      |   2 +-
 kernel/trace/rv/monitors/sts/Kconfig          |  18 +++
 kernel/trace/rv/monitors/sts/sts.c            | 118 ++++++++++++++++++
 kernel/trace/rv/monitors/sts/sts.h            |  62 +++++++++
 .../{tss/tss_trace.h => sts/sts_trace.h}      |   8 +-
 kernel/trace/rv/monitors/tss/Kconfig          |  14 ---
 kernel/trace/rv/monitors/tss/tss.c            |  91 --------------
 kernel/trace/rv/monitors/tss/tss.h            |  47 -------
 kernel/trace/rv/rv_trace.h                    |   2 +-
 tools/verification/models/sched/sts.dot       |  29 +++++
 tools/verification/models/sched/tss.dot       |  18 ---
 13 files changed, 282 insertions(+), 197 deletions(-)
 create mode 100644 kernel/trace/rv/monitors/sts/Kconfig
 create mode 100644 kernel/trace/rv/monitors/sts/sts.c
 create mode 100644 kernel/trace/rv/monitors/sts/sts.h
 rename kernel/trace/rv/monitors/{tss/tss_trace.h => sts/sts_trace.h} (67%)
 delete mode 100644 kernel/trace/rv/monitors/tss/Kconfig
 delete mode 100644 kernel/trace/rv/monitors/tss/tss.c
 delete mode 100644 kernel/trace/rv/monitors/tss/tss.h
 create mode 100644 tools/verification/models/sched/sts.dot
 delete mode 100644 tools/verification/models/sched/tss.dot
Documentation/trace/rv/monitor_sched.rst +48 -20
index 24b2c62a3bc26..641e70e359aaa 100644
@@ -40,26 +40,6 @@ defined in by Daniel Bristot in [1].
 
 Currently we included the following:
 
-Monitor tss
-~~~~~~~~~~~
-
-The task switch while scheduling (tss) monitor ensures a task switch happens
-only in scheduling context, that is inside a call to `__schedule`::
-
-                     |
-                     |
-                     v
-                   +-----------------+
-                   |     thread      | <+
-                   +-----------------+  |
-                     |                  |
-                     | schedule_entry   | schedule_exit
-                     v                  |
-    sched_switch                        |
-  +---------------                      |
-  |                       sched         |
-  +-------------->                     -+
-
 Monitor sco
 ~~~~~~~~~~~
 
@@ -165,6 +145,54 @@ schedule is not called with interrupt disabled::
                                        |
                         cant_sched    -+
 
+Monitor sts
+~~~~~~~~~~~
+
+The schedule implies task switch (sts) monitor ensures a task switch happens in
+every scheduling context, that is inside a call to ``__schedule``, as well as no
+task switch can happen without scheduling and before interrupts are disabled.
+This require the special type of switch called vain, which occurs when the next
+task picked for execution is the same as the previously running one, in fact no
+real task switch occurs::
+
+                    |
+                    |
+                    v
+                  #====================#   irq_disable
+                  H                    H   irq_enable
+                  H       thread       H --------------+
+                  H                    H               |
+  +-------------> H                    H <-------------+
+  |               #====================#
+  |                 |
+  |                 | schedule_entry
+  |                 v
+  |               +--------------------+
+  |               |     scheduling     | <+
+  |               +--------------------+  |
+  |                 |                     |
+  |                 | irq_disable         | irq_enable
+  |                 v                     |
+  |               +--------------------+  |
+  |               | disable_to_switch  | -+
+  | schedule_exit +--------------------+
+  |                 |
+  |                 | sched_switch
+  |                 | sched_switch_vain
+  |                 v
+  |               +--------------------+
+  |               |     switching      |
+  |               +--------------------+
+  |                 |
+  |                 | irq_enable
+  |                 v
+  |               +--------------------+   irq_disable
+  |               |                    |   irq_enable
+  |               |   enable_to_exit   | --------------+
+  |               |                    |               |
+  +-------------- |                    | <-------------+
+                  +--------------------+
+
 References
 ----------
 
kernel/trace/rv/Kconfig +1 -1
index b39f36013ef23..a53a3eca9616d 100644
@@ -28,12 +28,12 @@ menuconfig RV
 source "kernel/trace/rv/monitors/wip/Kconfig"
 source "kernel/trace/rv/monitors/wwnr/Kconfig"
 source "kernel/trace/rv/monitors/sched/Kconfig"
-source "kernel/trace/rv/monitors/tss/Kconfig"
 source "kernel/trace/rv/monitors/sco/Kconfig"
 source "kernel/trace/rv/monitors/snroc/Kconfig"
 source "kernel/trace/rv/monitors/scpd/Kconfig"
 source "kernel/trace/rv/monitors/snep/Kconfig"
 source "kernel/trace/rv/monitors/sncid/Kconfig"
+source "kernel/trace/rv/monitors/sts/Kconfig"
 # Add new monitors here
 
 config RV_REACTORS
kernel/trace/rv/Makefile +1 -1
index f9b2cd0483c3c..c609b72275cb8 100644
@@ -6,12 +6,12 @@ obj-$(CONFIG_RV) += rv.o
 obj-$(CONFIG_RV_MON_WIP) += monitors/wip/wip.o
 obj-$(CONFIG_RV_MON_WWNR) += monitors/wwnr/wwnr.o
 obj-$(CONFIG_RV_MON_SCHED) += monitors/sched/sched.o
-obj-$(CONFIG_RV_MON_TSS) += monitors/tss/tss.o
 obj-$(CONFIG_RV_MON_SCO) += monitors/sco/sco.o
 obj-$(CONFIG_RV_MON_SNROC) += monitors/snroc/snroc.o
 obj-$(CONFIG_RV_MON_SCPD) += monitors/scpd/scpd.o
 obj-$(CONFIG_RV_MON_SNEP) += monitors/snep/snep.o
 obj-$(CONFIG_RV_MON_SNCID) += monitors/sncid/sncid.o
+obj-$(CONFIG_RV_MON_STS) += monitors/sts/sts.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/sts/Kconfig new file +18 -0
new file mode 100644
index 0000000000000..5b486dac3f10f
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_STS
+	depends on RV
+	depends on IRQSOFF_TRACER
+	depends on RV_MON_SCHED
+	default y
+	select DA_MON_EVENTS_IMPLICIT
+	bool "sts monitor"
+	help
+	  Monitor to ensure relationships between scheduler and switches
+	   * each call to the scheduler implies a switch
+	   * switches only happen inside the scheduler
+	   * switches happen with interrupt disabled
+	  This monitor is part of the sched monitors collection.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_sched.rst
kernel/trace/rv/monitors/sts/sts.c new file +118 -0

118 lines not on this page · raw message

kernel/trace/rv/monitors/sts/sts.h new file +62 -0

62 lines not on this page · raw message

kernel/trace/rv/monitors/sts/sts_trace.h renamed from kernel/trace/rv/monitors/tss/tss_trace.h +4 -4
similarity index 67%
rename from kernel/trace/rv/monitors/tss/tss_trace.h
rename to kernel/trace/rv/monitors/sts/sts_trace.h
index 4619dbb50cc06..d78beb58d5b3d 100644
@@ -4,12 +4,12 @@
  * Snippet to be included in rv_trace.h
  */
 
-#ifdef CONFIG_RV_MON_TSS
-DEFINE_EVENT(event_da_monitor, event_tss,
+#ifdef CONFIG_RV_MON_STS
+DEFINE_EVENT(event_da_monitor, event_sts,
 	     TP_PROTO(char *state, char *event, char *next_state, bool final_state),
 	     TP_ARGS(state, event, next_state, final_state));
 
-DEFINE_EVENT(error_da_monitor, error_tss,
+DEFINE_EVENT(error_da_monitor, error_sts,
 	     TP_PROTO(char *state, char *event),
 	     TP_ARGS(state, event));
-#endif /* CONFIG_RV_MON_TSS */
+#endif /* CONFIG_RV_MON_STS */
kernel/trace/rv/monitors/tss/Kconfig deleted +0 -14
deleted file mode 100644
index 479f86f52e60d..0000000000000
@@ -1,14 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-#
-config RV_MON_TSS
-	depends on RV
-	depends on RV_MON_SCHED
-	default y
-	select DA_MON_EVENTS_IMPLICIT
-	bool "tss monitor"
-	help
-	  Monitor to ensure sched_switch happens only in scheduling context.
-	  This monitor is part of the sched monitors collection.
-
-	  For further information, see:
-	    Documentation/trace/rv/monitor_sched.rst
kernel/trace/rv/monitors/tss/tss.c deleted +0 -91

91 lines not on this page · raw message

kernel/trace/rv/monitors/tss/tss.h deleted +0 -47

47 lines not on this page · raw message

kernel/trace/rv/rv_trace.h +1 -1
index 18fa0e358a30e..a5e1c52e29926 100644
@@ -58,11 +58,11 @@ DECLARE_EVENT_CLASS(error_da_monitor,
 );
 
 #include <monitors/wip/wip_trace.h>
-#include <monitors/tss/tss_trace.h>
 #include <monitors/sco/sco_trace.h>
 #include <monitors/scpd/scpd_trace.h>
 #include <monitors/snep/snep_trace.h>
 #include <monitors/sncid/sncid_trace.h>
+#include <monitors/sts/sts_trace.h>
 // Add new monitors based on CONFIG_DA_MON_EVENTS_IMPLICIT here
 
 #endif /* CONFIG_DA_MON_EVENTS_IMPLICIT */
tools/verification/models/sched/sts.dot new file +29 -0
new file mode 100644
index 0000000000000..8152675b4f528
@@ -0,0 +1,29 @@
+digraph state_automaton {
+	center = true;
+	size = "7,11";
+	{node [shape = circle] "disable_to_switch"};
+	{node [shape = circle] "enable_to_exit"};
+	{node [shape = circle] "scheduling"};
+	{node [shape = circle] "switching"};
+	{node [shape = plaintext, style=invis, label=""] "__init_thread"};
+	{node [shape = doublecircle] "thread"};
+	{node [shape = circle] "thread"};
+	"__init_thread" -> "thread";
+	"disable_to_switch" [label = "disable_to_switch"];
+	"disable_to_switch" -> "scheduling" [ label = "irq_enable" ];
+	"disable_to_switch" -> "switching" [ label = "sched_switch\nsched_switch_vain" ];
+	"enable_to_exit" [label = "enable_to_exit"];
+	"enable_to_exit" -> "enable_to_exit" [ label = "irq_disable\nirq_enable" ];
+	"enable_to_exit" -> "thread" [ label = "schedule_exit" ];
+	"scheduling" [label = "scheduling"];
+	"scheduling" -> "disable_to_switch" [ label = "irq_disable" ];
+	"switching" [label = "switching"];
+	"switching" -> "enable_to_exit" [ label = "irq_enable" ];
+	"thread" [label = "thread", color = green3];
+	"thread" -> "scheduling" [ label = "schedule_entry" ];
+	"thread" -> "thread" [ label = "irq_disable\nirq_enable" ];
+	{ rank = min ;
+		"__init_thread";
+		"thread";
+	}
+}
tools/verification/models/sched/tss.dot deleted +0 -18
deleted file mode 100644
index 7dfa1d9121bbd..0000000000000
@@ -1,18 +0,0 @@
-digraph state_automaton {
-	center = true;
-	size = "7,11";
-	{node [shape = plaintext] "sched"};
-	{node [shape = plaintext, style=invis, label=""] "__init_thread"};
-	{node [shape = ellipse] "thread"};
-	{node [shape = plaintext] "thread"};
-	"__init_thread" -> "thread";
-	"sched" [label = "sched"];
-	"sched" -> "sched" [ label = "sched_switch" ];
-	"sched" -> "thread" [ label = "schedule_exit" ];
-	"thread" [label = "thread", color = green3];
-	"thread" -> "sched" [ label = "schedule_entry" ];
-	{ rank = min ;
-		"__init_thread";
-		"thread";
-	}
-}
Signature · 1 line
2.49.0
author maintainer
#19 lore raw

RFC PATCH 9/9 Add srs per-task monitor

In reply to Gabriele Monaco

Add a per-task monitor for task switches as part of the sched model:

* srs:
Monitor to describe conditions for different types of task switch. This monitor enforces rules such as preempt after setting need for reschedule and suspend after setting a task to sleepable.

This new monitor implies the previously introduced snroc (set non runnable on its own context), so replace that monitor with srs.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 Documentation/trace/rv/monitor_sched.rst      | 169 +++++++++++++++---
 kernel/trace/rv/Kconfig                       |   2 +-
 kernel/trace/rv/Makefile                      |   2 +-
 kernel/trace/rv/monitors/snroc/snroc.c        |  85 ---------
 kernel/trace/rv/monitors/snroc/snroc.h        |  47 -----
 .../trace/rv/monitors/{snroc => srs}/Kconfig  |   8 +-
 kernel/trace/rv/monitors/srs/srs.c            | 135 ++++++++++++++
 kernel/trace/rv/monitors/srs/srs.h            |  87 +++++++++
 .../{snroc/snroc_trace.h => srs/srs_trace.h}  |   8 +-
 kernel/trace/rv/rv_trace.h                    |   2 +-
 tools/verification/models/sched/snroc.dot     |  18 --
 tools/verification/models/sched/srs.dot       |  61 +++++++
 12 files changed, 443 insertions(+), 181 deletions(-)
 delete mode 100644 kernel/trace/rv/monitors/snroc/snroc.c
 delete mode 100644 kernel/trace/rv/monitors/snroc/snroc.h
 rename kernel/trace/rv/monitors/{snroc => srs}/Kconfig (52%)
 create mode 100644 kernel/trace/rv/monitors/srs/srs.c
 create mode 100644 kernel/trace/rv/monitors/srs/srs.h
 rename kernel/trace/rv/monitors/{snroc/snroc_trace.h => srs/srs_trace.h} (67%)
 delete mode 100644 tools/verification/models/sched/snroc.dot
 create mode 100644 tools/verification/models/sched/srs.dot
Documentation/trace/rv/monitor_sched.rst +148 -21
index 641e70e359aaa..c1516b71b8e73 100644
@@ -61,27 +61,6 @@ happen only in thread context::
                                             |
                        scheduling_context  -+
 
-Monitor snroc
-~~~~~~~~~~~~~
-
-The set non runnable on its own context (snroc) monitor ensures changes in a
-task state happens only in the respective task's context. This is a per-task
-monitor::
-
-                        |
-                        |
-                        v
-                      +------------------+
-                      |  other_context   | <+
-                      +------------------+  |
-                        |                   |
-                        | sched_switch_in   | sched_switch_out
-                        v                   |
-    sched_set_state                         |
-  +------------------                       |
-  |                       own_context       |
-  +----------------->                      -+
-
 Monitor scpd
 ~~~~~~~~~~~~
 
@@ -193,6 +172,154 @@ real task switch occurs::
   +-------------- |                    | <-------------+
                   +--------------------+
 
+Monitor srs
+-----------
+
+The switch after resched or sleep (srs) monitor describes conditions for
+different types of task switch. This is a complex model, below we are going to
+explain it step by step. Unfortunately splitting this into smaller monitor is
+not trivial due to some shared events such as ``switch_in``::
+
+                                                      set_runnable
+                                  |                        wakeup +---+
+                                  |                   switch_vain |   |
+                                  v                               |   v      wakeup
+                         #================================================#  set_runnable
+      switch_in          H                                                H <----------+
+    +------------------> H                    running                     H            |
+    |                    H                                                H -----+     |
+    |                    #================================================#      |     |
+    |                         |        |                |         ^    ^         |     |
+    |                         |  switch_yield      need_resched   |    |         |     |
+    |                         |        |      need_resched_lazy   |    |         |     |
+    |                set_sleepable     v                |         |    |         |     |
+    |                         |      +-------------+    |         |    |         |     |
+    |                +--------+----> |  preempted  | ---+- switch_in   |         |     |
+    |                |        |      +-------------+    |              |         |     |
+    |        switch_preempt   |        |                |              |         |     |
+    |        switch_yield     |   need_resched          |   +- switch_vain       |     |
+    |                |        |        v                |   |                    |     |
+    |                |        |      +-------------+    |   |                    |     |
+    |  need_resched -+--------+----> | resched_out |    |   |                    |     |
+    |  |             |        |      +-------------+    |   |                    |     |
+    |  |             |        |        |                |   |     need_resched   |     |
+    |  |             |        |    switch_in            |   |     wakeup         |     |
+    |  |             |        |        v                v   |     set_runnable   |     |
+    |  |             |        |      +--------------------------+ -------+       |     |
+    |  |             |        |      |                          |        |       |     |
+    |  |             +--------+----- |       rescheduling       | <------+       |     |
+    |  |                      |      |                          |                |     |
+    |  |                      |      +--------------------------+ -----------+   |     |
+    |  |                      |        |           ^ wakeup                  |   |     |
+    |  |                      |  set_sleepable   set_runnable                |   |     |
+    |  |                      |        v           |                         |   |     |
+    |  |   +------------------+----- +---------------------------+           |   |     |
+    |  |   |                  |      |                           |           |   |     |
+ +--+--+---+------------------+----> |     resched_sleepable     | ---+      |   |     |
+ |  |  |   |                  |      |                           |    |      |   |     |
+ |  |  |   |    +-------------+----> +---------------------------+    |      |   |     |
+ |  |  |   |    |             |        |           ^      |           |      |   |     |
+ |  |  |   |    |             |  switch_preempt    | need_resched     |      |   |     |
+ |  |  |   |    |             |        |           | set_sleepable    |      |   |     |
+ |  |  |   |    |             |        v           +------+           |      |   |     |
+ |  |  |   |    |             |       +---------------------------+ --+------+---+-----+--+
+ |  |  |   |    |             |       |    preempted_sleepable    |   |      |   |     |  |
+ |  |  |   |    |             |       +---------------------------+ --+------+---+--+  |  |
+ |  |  |   |    |             |         |             ^               |      |   |  |  |  |
+ |  |  |   |    |             |     switch_in   switch_preempt        |      |   |  |  |  |
+ |  |  |   |    |             |         v             |          switch_vain |   |  |  |  |
+ |  |  |   |    |             |        +-------------------------+    |      |   |  |  |  |
+ |  |  |   |    |             +------> |                         | <--+      |   |  |  |  |
+ |  |  |   |    |                      |        sleepable        |           |   |  |  |  |
+ |  |  |   |    +- need_resched------- |                         | ----------+---+--+--+  |
+ |  |  |   |       need_resched_lazy   +-------------------------+           |   |  |     |
+ |  |  |   |                              |      ^      |          switch_block  |  |     |
+ |  |  |   |                              |      | set_sleepable             |   |  |     |
+ |  |  |   |                      switch_block   | switch_vain    +----------+   |  |     |
+ |  |  |   |                    switch_suspend   +------+         |              |  |     |
+ |  |  |   |                              v                       v              |  |     |
+ |  |  |   |   switch_block          +-----------------------------+  switch_block  |     |
+ |  |  |   +-switch_suspend--------> |          sleeping           | <-----------+  |     |
+ |  |  |                             +-----------------------------+                |     |
+ |  |  |                               | wakeup                                     |     |
+ |  |  |                               v                                            |     |
+ |  |  +- need_resched ------------- +-------------+  wakeup                        |     |
+ |  |                                |   waking    | <------------------------------+     |
+ |  +------------------------------- +-------------+                                      |
+ |                                                                                        |
+ |                         +-----------------------+                                      |
+ +----- switch_in -------- | resched_out_sleepable | <-- sched_need_resched --------------+
+                           +-----------------------+
+
+Types of switches:
+
+* ``switch_in``:
+  a non running task is scheduled in, this leads to ``running`` if the task is
+  runnable and ``sleepable`` if the task was preempted before sleeping.
+* ``switch_suspend``:
+  a task puts itself to sleep, this can happen only after explicitly setting
+  the task to ``sleepable``. After a task is suspended, it needs to be woken up
+  (``waking`` state) before being switched in again. The task can be set to
+  ``resched_sleepable`` via a ``need_resched`` but not preempted, in which case it
+  is equivalent to ``sleepable``.
+  Setting the task's state to ``sleepable`` can be reverted before switching if it
+  is woken up or set to runnable.
+* ``switch_blocked``:
+  a special case of a ``switch_suspend`` where the task is waiting on a
+  sleeping RT lock (``PREEMPT_RT`` only), it is common to see wakeup and set
+  state events racing with each other and this leads the model to perceive this
+  type of switch when the task is not set to sleepable. This is a limitation of
+  the model in SMP system and workarounds may require to slow down the
+  scheduler.
+* ``switch_yield``:
+  a task explicitly calls the scheduler, this looks like a preemption as the
+  task is still runnable but the ``need_resched`` flag is not set. It can
+  happen after a ``yield`` system call or from the idle task.
+* ``switch_preempt``:
+  a task is ``preempted``, this can happen after the need for ``rescheduling``
+  has been set, also in its ``lazy`` flavour. ``need_resched`` can be set as a
+  flag to the task or in the per-core preemption count, either of them can
+  trigger a preemption.
+  The task was previously running and can be switched in directly, but it is
+  possible that a task is preempted after it sets itself as ``sleepable``
+  (``preempted_sleepable``), in this condition, once the task is switched back
+  in, it will not be ``running`` but continue its sleeping process in
+  ``sleepable``.
+* ``switch_vain``:
+  a task goes through the scheduler but it is picked as the next task to run,
+  hence no real task switch occurs. Since we run the scheduler, this clears the
+  need to reschedule.
+
+The ``resched_out`` state (``resched_out_sleepable`` if the task is sleepable)
+is a special case reached if the ``need_resched`` is set after picking the next
+task but before switching. In this case, the previous task is switched out
+normally but once it switches in, it can be preempted in ``rescheduling``. This
+can happen, for instance, when a task disables migration and we do a
+dequeue/enqueue before actually switching out.
+
+This monitor has several events that can race with each other, for instance if
+running from multiple CPUs (e.g. ``need_resched`` and ``wakeup`` can occur from
+a remote CPU), we need to account for them in any possible order.
+
+This monitor allows set state (runnable or sleepable) only in states
+``running``, ``rescheduling``, ``resched_sleepable``, and ``sleepable``, and
+not for states where the task is ``sleeping`` or ``preempted``. This implies
+the set state event occurs only in the task's context::
+
+                        |
+                        |
+                        v
+                      +------------------+
+                      |  other_context   | <+
+                      +------------------+  |
+                        |                   |
+                        | switch_in         | switch_out
+                        v                   |
+    sched_set_state                         |
+  +------------------                       |
+  |                       own_context       |
+  +----------------->                      -+
+
 References
 ----------
 
kernel/trace/rv/Kconfig +1 -1
index a53a3eca9616d..f296095f1df28 100644
@@ -29,11 +29,11 @@ source "kernel/trace/rv/monitors/wip/Kconfig"
 source "kernel/trace/rv/monitors/wwnr/Kconfig"
 source "kernel/trace/rv/monitors/sched/Kconfig"
 source "kernel/trace/rv/monitors/sco/Kconfig"
-source "kernel/trace/rv/monitors/snroc/Kconfig"
 source "kernel/trace/rv/monitors/scpd/Kconfig"
 source "kernel/trace/rv/monitors/snep/Kconfig"
 source "kernel/trace/rv/monitors/sncid/Kconfig"
 source "kernel/trace/rv/monitors/sts/Kconfig"
+source "kernel/trace/rv/monitors/srs/Kconfig"
 # Add new monitors here
 
 config RV_REACTORS
kernel/trace/rv/Makefile +1 -1
index c609b72275cb8..5aba1d85ad662 100644
@@ -7,11 +7,11 @@ obj-$(CONFIG_RV_MON_WIP) += monitors/wip/wip.o
 obj-$(CONFIG_RV_MON_WWNR) += monitors/wwnr/wwnr.o
 obj-$(CONFIG_RV_MON_SCHED) += monitors/sched/sched.o
 obj-$(CONFIG_RV_MON_SCO) += monitors/sco/sco.o
-obj-$(CONFIG_RV_MON_SNROC) += monitors/snroc/snroc.o
 obj-$(CONFIG_RV_MON_SCPD) += monitors/scpd/scpd.o
 obj-$(CONFIG_RV_MON_SNEP) += monitors/snep/snep.o
 obj-$(CONFIG_RV_MON_SNCID) += monitors/sncid/sncid.o
 obj-$(CONFIG_RV_MON_STS) += monitors/sts/sts.o
+obj-$(CONFIG_RV_MON_SRS) += monitors/srs/srs.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/snroc/snroc.c deleted +0 -85

85 lines not on this page · raw message

kernel/trace/rv/monitors/snroc/snroc.h deleted +0 -47

47 lines not on this page · raw message

kernel/trace/rv/monitors/srs/Kconfig renamed from kernel/trace/rv/monitors/snroc/Kconfig +5 -3
similarity index 52%
rename from kernel/trace/rv/monitors/snroc/Kconfig
rename to kernel/trace/rv/monitors/srs/Kconfig
index 6e4365a2fea3b..a5b3998b32328 100644
@@ -1,13 +1,15 @@
 # SPDX-License-Identifier: GPL-2.0-only
 #
-config RV_MON_SNROC
+config RV_MON_SRS
 	depends on RV
 	depends on RV_MON_SCHED
 	default y
 	select DA_MON_EVENTS_ID
-	bool "snroc monitor"
+	bool "srs monitor"
 	help
-	  Monitor to ensure sched_set_state happens only in the respective task's context.
+	  Monitor to describe conditions for different types of task switch.
+	  This monitor enforces rules such as preempt after setting need
+	  for reschedule and suspend after setting a task to sleepable.
 	  This monitor is part of the sched monitors collection.
 
 	  For further information, see:
kernel/trace/rv/monitors/srs/srs.c new file +135 -0

135 lines not on this page · raw message

kernel/trace/rv/monitors/srs/srs.h new file +87 -0

87 lines not on this page · raw message

kernel/trace/rv/monitors/srs/srs_trace.h renamed from kernel/trace/rv/monitors/snroc/snroc_trace.h +4 -4
similarity index 67%
rename from kernel/trace/rv/monitors/snroc/snroc_trace.h
rename to kernel/trace/rv/monitors/srs/srs_trace.h
index 50114cef51229..fd033de926542 100644
@@ -4,12 +4,12 @@
  * Snippet to be included in rv_trace.h
  */
 
-#ifdef CONFIG_RV_MON_SNROC
-DEFINE_EVENT(event_da_monitor_id, event_snroc,
+#ifdef CONFIG_RV_MON_SRS
+DEFINE_EVENT(event_da_monitor_id, event_srs,
 	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
 	     TP_ARGS(id, state, event, next_state, final_state));
 
-DEFINE_EVENT(error_da_monitor_id, error_snroc,
+DEFINE_EVENT(error_da_monitor_id, error_srs,
 	     TP_PROTO(int id, char *state, char *event),
 	     TP_ARGS(id, state, event));
-#endif /* CONFIG_RV_MON_SNROC */
+#endif /* CONFIG_RV_MON_SRS */
kernel/trace/rv/rv_trace.h +1 -1
index a5e1c52e29926..f6ead55484751 100644
@@ -123,7 +123,7 @@ DECLARE_EVENT_CLASS(error_da_monitor_id,
 );
 
 #include <monitors/wwnr/wwnr_trace.h>
-#include <monitors/snroc/snroc_trace.h>
+#include <monitors/srs/srs_trace.h>
 // Add new monitors based on CONFIG_DA_MON_EVENTS_ID here
 
 #endif /* CONFIG_DA_MON_EVENTS_ID */
tools/verification/models/sched/snroc.dot deleted +0 -18
deleted file mode 100644
index 8b71c32d4dca4..0000000000000
@@ -1,18 +0,0 @@
-digraph state_automaton {
-	center = true;
-	size = "7,11";
-	{node [shape = plaintext, style=invis, label=""] "__init_other_context"};
-	{node [shape = ellipse] "other_context"};
-	{node [shape = plaintext] "other_context"};
-	{node [shape = plaintext] "own_context"};
-	"__init_other_context" -> "other_context";
-	"other_context" [label = "other_context", color = green3];
-	"other_context" -> "own_context" [ label = "sched_switch_in" ];
-	"own_context" [label = "own_context"];
-	"own_context" -> "other_context" [ label = "sched_switch_out" ];
-	"own_context" -> "own_context" [ label = "sched_set_state" ];
-	{ rank = min ;
-		"__init_other_context";
-		"other_context";
-	}
-}
tools/verification/models/sched/srs.dot new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.49.0
#6 lore raw

Re: [RFC PATCH 5/9] sched: Add sched tracepoints for RV task model

In reply to Gabriele Monaco
kernel test robot noticed the following build errors:

Hi Gabriele,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on e48e99b6edf41c69c5528aa7ffb2daf3c59ee105]

url:    https://github.com/intel-lab-lkp/linux/commits/Gabriele-Monaco/tools-rv-Do-not-skip-idle-in-trace/20250404-165047
base:   e48e99b6edf41c69c5528aa7ffb2daf3c59ee105
patch link:    https://lore.kernel.org/r/20250404084512.98552-16-gmonaco%40redhat.com
patch subject: [RFC PATCH 5/9] sched: Add sched tracepoints for RV task model
config: arm-randconfig-001-20250405 (https://download.01.org/0day-ci/archive/20250405/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250405/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: __trace_set_need_resched
   >>> referenced by sched.h:2071 (include/linux/sched.h:2071)
   >>>               kernel/sched/build_policy.o:(idle_inject_timer_fn) in archive vmlinux.a
   >>> referenced by sched.h:2071 (include/linux/sched.h:2071)
   >>>               kernel/rcu/tiny.o:(rcu_sched_clock_irq) in archive vmlinux.a
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [RFC PATCH 5/9] sched: Add sched tracepoints for RV task model

In reply to Gabriele Monaco
kernel test robot noticed the following build errors:

Hi Gabriele,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on e48e99b6edf41c69c5528aa7ffb2daf3c59ee105]

url:    https://github.com/intel-lab-lkp/linux/commits/Gabriele-Monaco/tools-rv-Do-not-skip-idle-in-trace/20250404-165047
base:   e48e99b6edf41c69c5528aa7ffb2daf3c59ee105
patch link:    https://lore.kernel.org/r/20250404084512.98552-16-gmonaco%40redhat.com
patch subject: [RFC PATCH 5/9] sched: Add sched tracepoints for RV task model
config: i386-buildonly-randconfig-001-20250405 (https://download.01.org/0day-ci/archive/20250405/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250405/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: kernel/rcu/tree.o: in function `rcu_exp_handler':

>> tree.c:(.text+0x3dbf): undefined reference to `__trace_set_need_resched'
   ld: kernel/rcu/tree.o: in function `print_cpu_stall':
   tree.c:(.text+0x5a92): undefined reference to `__trace_set_need_resched'
   ld: kernel/rcu/tree.o: in function `rcu_read_unlock_special':
   tree.c:(.text+0xb906): undefined reference to `__trace_set_need_resched'
   ld: kernel/rcu/tree.o: in function `rcu_core':
   tree.c:(.text+0xcbd9): undefined reference to `__trace_set_need_resched'
   ld: kernel/rcu/tree.o: in function `rcu_sched_clock_irq':
   tree.c:(.text+0xd662): undefined reference to `__trace_set_need_resched'
   ld: kernel/rcu/tree.o:tree.c:(.text+0xd682): more undefined references to `__trace_set_need_resched' follow
   ld: drivers/media/cec/i2c/tda9950.o: in function `tda9950_read_range':
   tda9950.c:(.text+0x90): undefined reference to `i2c_transfer'
   ld: drivers/media/cec/i2c/tda9950.o: in function `tda9950_write_range':
   tda9950.c:(.text+0x114): undefined reference to `i2c_transfer'
   ld: drivers/media/cec/i2c/tda9950.o: in function `tda9950_read':
   tda9950.c:(.text+0x392): undefined reference to `i2c_transfer'
   ld: drivers/media/cec/i2c/tda9950.o: in function `tda9950_driver_init':
   tda9950.c:(.init.text+0xb): undefined reference to `i2c_register_driver'
   ld: drivers/media/cec/i2c/tda9950.o: in function `tda9950_driver_exit':
   tda9950.c:(.exit.text+0x9): undefined reference to `i2c_del_driver'
   ld: kernel/sched/build_policy.o: in function `idle_inject_timer_fn':
>> build_policy.c:(.text+0x33e): undefined reference to `__trace_set_need_resched'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#20 lore raw

Re: [RFC PATCH 9/9] rv: Add srs per-task monitor

In reply to Gabriele Monaco

Hi Gabriele,

Quoted 14 lines · Gabriele Monaco

Add a per-task monitor for task switches as part of the sched model:

* srs:
Monitor to describe conditions for different types of task switch. This monitor enforces rules such as preempt after setting need for reschedule and suspend after setting a task to sleepable.

This new monitor implies the previously introduced snroc (set non runnable on its own context), so replace that monitor with srs.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

...

Documentation/trace/rv/monitor_sched.rst real task switch occurs:: · line 179

Quoted 5 lines
Documentation/trace/rv/monitor_sched.rst +5 -0
+Monitor srs
+-----------
+
+The switch after resched or sleep (srs) monitor describes conditions for
+different types of task switch. This is a complex model, below we are going to

Quite the ASCII art indeed. :-)

Documentation/trace/rv/monitor_sched.rst real task switch occurs:: · line 181

Quoted 2 lines
Documentation/trace/rv/monitor_sched.rst +2 -0
+explain it step by step. Unfortunately splitting this into smaller monitor is
+not trivial due to some shared events such as ``switch_in``::

Not splitting, but maybe providing several separate diagrams for the key cases and transitions might help grasping the complete picture? Not sure, just a thought.

In the below, set_{sleepable,runnable} corresponds to set_state_ {sleepable,runnable} in the code, does it? Not a big deal, but I was confused at first.

Thanks,
Juri

Quoted 111 lines
Documentation/trace/rv/monitor_sched.rst +111 -0
+
+                                                      set_runnable
+                                  |                        wakeup +---+
+                                  |                   switch_vain |   |
+                                  v                               |   v      wakeup
+                         #================================================#  set_runnable
+      switch_in          H                                                H <----------+
+    +------------------> H                    running                     H            |
+    |                    H                                                H -----+     |
+    |                    #================================================#      |     |
+    |                         |        |                |         ^    ^         |     |
+    |                         |  switch_yield      need_resched   |    |         |     |
+    |                         |        |      need_resched_lazy   |    |         |     |
+    |                set_sleepable     v                |         |    |         |     |
+    |                         |      +-------------+    |         |    |         |     |
+    |                +--------+----> |  preempted  | ---+- switch_in   |         |     |
+    |                |        |      +-------------+    |              |         |     |
+    |        switch_preempt   |        |                |              |         |     |
+    |        switch_yield     |   need_resched          |   +- switch_vain       |     |
+    |                |        |        v                |   |                    |     |
+    |                |        |      +-------------+    |   |                    |     |
+    |  need_resched -+--------+----> | resched_out |    |   |                    |     |
+    |  |             |        |      +-------------+    |   |                    |     |
+    |  |             |        |        |                |   |     need_resched   |     |
+    |  |             |        |    switch_in            |   |     wakeup         |     |
+    |  |             |        |        v                v   |     set_runnable   |     |
+    |  |             |        |      +--------------------------+ -------+       |     |
+    |  |             |        |      |                          |        |       |     |
+    |  |             +--------+----- |       rescheduling       | <------+       |     |
+    |  |                      |      |                          |                |     |
+    |  |                      |      +--------------------------+ -----------+   |     |
+    |  |                      |        |           ^ wakeup                  |   |     |
+    |  |                      |  set_sleepable   set_runnable                |   |     |
+    |  |                      |        v           |                         |   |     |
+    |  |   +------------------+----- +---------------------------+           |   |     |
+    |  |   |                  |      |                           |           |   |     |
+ +--+--+---+------------------+----> |     resched_sleepable     | ---+      |   |     |
+ |  |  |   |                  |      |                           |    |      |   |     |
+ |  |  |   |    +-------------+----> +---------------------------+    |      |   |     |
+ |  |  |   |    |             |        |           ^      |           |      |   |     |
+ |  |  |   |    |             |  switch_preempt    | need_resched     |      |   |     |
+ |  |  |   |    |             |        |           | set_sleepable    |      |   |     |
+ |  |  |   |    |             |        v           +------+           |      |   |     |
+ |  |  |   |    |             |       +---------------------------+ --+------+---+-----+--+
+ |  |  |   |    |             |       |    preempted_sleepable    |   |      |   |     |  |
+ |  |  |   |    |             |       +---------------------------+ --+------+---+--+  |  |
+ |  |  |   |    |             |         |             ^               |      |   |  |  |  |
+ |  |  |   |    |             |     switch_in   switch_preempt        |      |   |  |  |  |
+ |  |  |   |    |             |         v             |          switch_vain |   |  |  |  |
+ |  |  |   |    |             |        +-------------------------+    |      |   |  |  |  |
+ |  |  |   |    |             +------> |                         | <--+      |   |  |  |  |
+ |  |  |   |    |                      |        sleepable        |           |   |  |  |  |
+ |  |  |   |    +- need_resched------- |                         | ----------+---+--+--+  |
+ |  |  |   |       need_resched_lazy   +-------------------------+           |   |  |     |
+ |  |  |   |                              |      ^      |          switch_block  |  |     |
+ |  |  |   |                              |      | set_sleepable             |   |  |     |
+ |  |  |   |                      switch_block   | switch_vain    +----------+   |  |     |
+ |  |  |   |                    switch_suspend   +------+         |              |  |     |
+ |  |  |   |                              v                       v              |  |     |
+ |  |  |   |   switch_block          +-----------------------------+  switch_block  |     |
+ |  |  |   +-switch_suspend--------> |          sleeping           | <-----------+  |     |
+ |  |  |                             +-----------------------------+                |     |
+ |  |  |                               | wakeup                                     |     |
+ |  |  |                               v                                            |     |
+ |  |  +- need_resched ------------- +-------------+  wakeup                        |     |
+ |  |                                |   waking    | <------------------------------+     |
+ |  +------------------------------- +-------------+                                      |
+ |                                                                                        |
+ |                         +-----------------------+                                      |
+ +----- switch_in -------- | resched_out_sleepable | <-- sched_need_resched --------------+
+                           +-----------------------+
+
+Types of switches:
+
+* ``switch_in``:
+  a non running task is scheduled in, this leads to ``running`` if the task is
+  runnable and ``sleepable`` if the task was preempted before sleeping.
+* ``switch_suspend``:
+  a task puts itself to sleep, this can happen only after explicitly setting
+  the task to ``sleepable``. After a task is suspended, it needs to be woken up
+  (``waking`` state) before being switched in again. The task can be set to
+  ``resched_sleepable`` via a ``need_resched`` but not preempted, in which case it
+  is equivalent to ``sleepable``.
+  Setting the task's state to ``sleepable`` can be reverted before switching if it
+  is woken up or set to runnable.
+* ``switch_blocked``:
+  a special case of a ``switch_suspend`` where the task is waiting on a
+  sleeping RT lock (``PREEMPT_RT`` only), it is common to see wakeup and set
+  state events racing with each other and this leads the model to perceive this
+  type of switch when the task is not set to sleepable. This is a limitation of
+  the model in SMP system and workarounds may require to slow down the
+  scheduler.
+* ``switch_yield``:
+  a task explicitly calls the scheduler, this looks like a preemption as the
+  task is still runnable but the ``need_resched`` flag is not set. It can
+  happen after a ``yield`` system call or from the idle task.
+* ``switch_preempt``:
+  a task is ``preempted``, this can happen after the need for ``rescheduling``
+  has been set, also in its ``lazy`` flavour. ``need_resched`` can be set as a
+  flag to the task or in the per-core preemption count, either of them can
+  trigger a preemption.
+  The task was previously running and can be switched in directly, but it is
+  possible that a task is preempted after it sets itself as ``sleepable``
+  (``preempted_sleepable``), in this condition, once the task is switched back
+  in, it will not be ``running`` but continue its sleeping process in
+  ``sleepable``.
+* ``switch_vain``:
+  a task goes through the scheduler but it is picked as the next task to run,
+  hence no real task switch occurs. Since we run the scheduler, this clears the
+  need to reschedule.
+

...

Quoted 15 lines
kernel/trace/rv/monitors/srs/srs.h new file +15 -0
+enum events_srs {
+	sched_need_resched_srs = 0,
+	sched_need_resched_lazy_srs,
+	sched_set_state_runnable_srs,
+	sched_set_state_sleepable_srs,
+	sched_switch_blocking_srs,
+	sched_switch_in_srs,
+	sched_switch_preempt_srs,
+	sched_switch_suspend_srs,
+	sched_switch_vain_srs,
+	sched_switch_yield_srs,
+	sched_wakeup_srs,
+	event_max_srs
+};
+
#16 lore raw

Re: [RFC PATCH 7/9] rv: Retry when da monitor detects race conditions

In reply to Gabriele Monaco
Quoted 14 lines · Gabriele Monaco

DA monitor can be accessed from multiple cores simultaneously, this is likely, for instance when dealing with per-task monitors reacting on events that do not always occur on the CPU where the task is running. This can cause race conditions where two events change the next state and we see inconsistent values. E.g.:

[62] event_srs: 27: sleepable x sched_wakeup -> running (final)
[63] event_srs: 27: sleepable x sched_set_state_sleepable -> sleepable
[63] error_srs: 27: event sched_switch_suspend not expected in the state running

In this case the monitor fails because the event on CPU 62 wins against the one on CPU 63, although the correct state should have been sleepable, since the task get suspended.

Quoted 7 lines

Detect if the current state was modified by using try_cmpxchg while storing the next value. If it was, try again reading the current state. After a maximum number of failed retries, react as if it was an error with invalid current state (we cannot determine it).

Monitors where this type of condition can occur must be able to account for racing events in any possible order, as we cannot know the winner.

Is locking not simpler? I understand raw_spin_lock() doesn't work because it steps on some tracepoints. But how about adding something like raw_spin_lock_notrace()?

static inline bool raw_spin_lock_notrace(raw_spinlock_t *lock)

{
	preempt_disable_notrace(); //probably not required, tracepoint handlers do this already

	if (!do_raw_spin_trylock(lock))
		do_raw_spin_lock(lock);
}

My LTL series theoretically also has this problem, but I have never got it during testing yet. We should use the same solution for both DA and LTL.

Also, can you please Cc me in your RV patches?

Best regards,
Nam

author maintainer
#17 lore raw

Re: [RFC PATCH 7/9] rv: Retry when da monitor detects race conditions

Quoted 34 lines · Nam Cao
Quoted 28 lines · Gabriele Monaco

DA monitor can be accessed from multiple cores simultaneously, this is
likely, for instance when dealing with per-task monitors reacting on
events that do not always occur on the CPU where the task is running.
This can cause race conditions where two events change the next state
and we see inconsistent values. E.g.:

  [62] event_srs: 27: sleepable x sched_wakeup -> running (final)
  [63] event_srs: 27: sleepable x sched_set_state_sleepable -> sleepable
  [63] error_srs: 27: event sched_switch_suspend not expected in the state running

In this case the monitor fails because the event on CPU 62 wins against
the one on CPU 63, although the correct state should have been sleepable, since the task get suspended.

Detect if the current state was modified by using try_cmpxchg while storing the next value. If it was, try again reading the current state.
After a maximum number of failed retries, react as if it was an error
with invalid current state (we cannot determine it).

Quoted 4 lines

Monitors where this type of condition can occur must be able to account
for racing events in any possible order, as we cannot know the winner.

Quoted 4 lines

Is locking not simpler? I understand raw_spin_lock() doesn't work because
it steps on some tracepoints. But how about adding something like raw_spin_lock_notrace()?

It is probably simpler, but I think it would require also to disable interrupts (some events occur in interrupt context), I'm not sure the introduced overhead is going to be worth it in the fast path, but that's kinda what I wanted to learn in this RFC ;)

Quoted 6 lines

static inline bool raw_spin_lock_notrace(raw_spinlock_t *lock)

{
	preempt_disable_notrace(); //probably not required,

tracepoint handlers do this already

Quoted 8 lines
	if (!do_raw_spin_trylock(lock))
		do_raw_spin_lock(lock);
}

My LTL series theoretically also has this problem, but I have never got it
during testing yet. We should use the same solution for both DA and LTL.

Yes totally, on the long run we might get some common utilities for this kind of things that aren't too monitor specific. But for now I wouldn't worry too much.

Quoted 3 lines

Also, can you please Cc me in your RV patches?

Right.. will do!

Thanks for your feedback,
Gabriele

author maintainer
#21 lore raw

Re: [RFC PATCH 9/9] rv: Add srs per-task monitor

In reply to Juri Lelli
Quoted 32 lines · Juri Lelli

Hi Gabriele,

Quoted 16 lines · Gabriele Monaco

Add a per-task monitor for task switches as part of the sched model:

* srs:
    Monitor to describe conditions for different types of task switch.
    This monitor enforces rules such as preempt after setting need for reschedule and suspend after setting a task to sleepable.

This new monitor implies the previously introduced snroc (set non runnable on its own context), so replace that monitor with srs.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

...

Quoted 7 lines
Documentation/trace/rv/monitor_sched.rst +4 -0
+Monitor srs
+-----------
+
+The switch after resched or sleep (srs) monitor describes

conditions for
+different types of task switch. This is a complex model, below we are going to

Quite the ASCII art indeed. :-)

Quoted 8 lines
Quoted 3 lines

+explain it step by step. Unfortunately splitting this into smaller monitor is
+not trivial due to some shared events such as ``switch_in``::

Not splitting, but maybe providing several separate diagrams for the key
cases and transitions might help grasping the complete picture? Not sure, just a thought.

I tried to do that but then dropped the idea because it was hard to maintain, not that this one is much easier though..
I might re-consider it!

Quoted 4 lines

In the below, set_{sleepable,runnable} corresponds to set_state_ {sleepable,runnable} in the code, does it? Not a big deal, but I was confused at first.

Yeah, I'm trying to keep the names short for the model not to explode horizontally, but it seems I'm losing on clarity so probably better finding other solutions.

Thanks,
Gabriele

Quoted 226 lines

Thanks,
Juri

Quoted 203 lines

+
+                                                      set_runnable +                                  |                        wakeup

+---+
+                                  |                   switch_vain
|   |

+                                  v
|   v      wakeup
+
#================================================#  set_runnable +      switch_in
H                                                H <----------+ +    +------------------> H
running                     H            |
+    |
H                                                H -----+     | +    | #================================================#      |     | +    |                         |        |                | ^    ^         |     |

+    |                         |  switch_yield      need_resched  
|    |         |     |
+    |                         |        |      need_resched_lazy  
|    |         |     |
+    |                set_sleepable     v                |        
|    |         |     |
+    |                         |      +-------------+    |        
|    |         |     |
+    |                +--------+----> |  preempted  | ---+-
switch_in   |         |     |
+    |                |        |      +-------------+   
|              |         |     |
+    |        switch_preempt   |        |               
|              |         |     |
+    |        switch_yield     |   need_resched          |   +-
switch_vain       |     |
+    |                |        |        v                |  
|                    |     |
+    |                |        |      +-------------+    |  
|                    |     |
+    |  need_resched -+--------+----> | resched_out |    |  
|                    |     |
+    |  |             |        |      +-------------+    |  
|                    |     |
+    |  |             |        |        |                |   |    
need_resched   |     |
+    |  |             |        |    switch_in            |   |    
wakeup         |     |
+    |  |             |        |        v                v   |    
set_runnable   |     |
+    |  |             |        |      +--------------------------+
-------+       |     |
+    |  |             |        |      |                         
|        |       |     |
+    |  |             +--------+----- |       rescheduling       |
<------+       |     |
+    |  |                      |      |                         
|                |     |
+    |  |                      |      +--------------------------+
-----------+   |     |
+    |  |                      |        |           ^
wakeup                  |   |     |
+    |  |                      |  set_sleepable  
set_runnable                |   |     |
+    |  |                      |        v          
|                         |   |     |
+    |  |   +------------------+----- +---------------------------
+           |   |     |
+    |  |   |                  |      |                          
|           |   |     |
+ +--+--+---+------------------+----> |     resched_sleepable     |
---+      |   |     |
+ |  |  |   |                  |      |                          
|    |      |   |     |
+ |  |  |   |    +-------------+----> +---------------------------
+    |      |   |     |
+ |  |  |   |    |             |        |           ^     
|           |      |   |     |
+ |  |  |   |    |             |  switch_preempt    |
need_resched     |      |   |     |
+ |  |  |   |    |             |        |           |
set_sleepable    |      |   |     |
+ |  |  |   |    |             |        v           +------
+           |      |   |     |
+ |  |  |   |    |             |       +---------------------------
+ --+------+---+-----+--+
+ |  |  |   |    |             |       |    preempted_sleepable   
|   |      |   |     |  |
+ |  |  |   |    |             |       +---------------------------

+ --+------+---+--+  |  |
+ |  |  |   |    |             |         |
^               |      |   |  |  |  |
+ |  |  |   |    |             |     switch_in switch_preempt        |      |   |  |  |  |
+ |  |  |   |    |             |         v             | switch_vain |   |  |  |  |
+ |  |  |   |    |             |        +------------------------- +    |      |   |  |  |  |
+ |  |  |   |    |             +------> |                         | <--+      |   |  |  |  |
+ |  |  |   |    |                      |        sleepable

|           |   |  |  |  |
+ |  |  |   |    +- need_resched------- |                         |
----------+---+--+--+  |
+ |  |  |   |       need_resched_lazy   +-------------------------
+           |   |  |     |
+ |  |  |   |                              |      ^      |         
switch_block  |  |     |
+ |  |  |   |                              |      |
set_sleepable             |   |  |     |
+ |  |  |   |                      switch_block   | switch_vain   
+----------+   |  |     |
+ |  |  |   |                    switch_suspend   +------+        
|              |  |     |
+ |  |  |   |                              v                      
v              |  |     |
+ |  |  |   |   switch_block          +----------------------------
-+  switch_block  |     |
+ |  |  |   +-switch_suspend--------> |          sleeping          
| <-----------+  |     |
+ |  |  |                             +----------------------------
-+                |     |
+ |  |  |                               |
wakeup                                     |     |
+ |  |  |                              
v                                            |     |
+ |  |  +- need_resched ------------- +-------------+ 
wakeup                        |     |
+ |  |                                |   waking    | <------------
------------------+     |
+ |  +------------------------------- +-------------
+                                      |

+
|
                      |
+ |                         +----------------------- +                                      |
+ +----- switch_in -------- | resched_out_sleepable | <-- sched_need_resched --------------+
+                           +-----------------------+

Documentation/trace/rv/monitor_sched.rst +5 -0
+
+Types of switches:
+
+* ``switch_in``:
+  a non running task is scheduled in, this leads to ``running`` if

the task is
+  runnable and ``sleepable`` if the task was preempted before sleeping.
+* ``switch_suspend``:
+  a task puts itself to sleep, this can happen only after explicitly setting
+  the task to ``sleepable``. After a task is suspended, it needs to be woken up
+  (``waking`` state) before being switched in again. The task can be set to
+  ``resched_sleepable`` via a ``need_resched`` but not preempted, in which case it
+  is equivalent to ``sleepable``.
+  Setting the task's state to ``sleepable`` can be reverted before switching if it
+  is woken up or set to runnable.
+* ``switch_blocked``:
+  a special case of a ``switch_suspend`` where the task is waiting on a
+  sleeping RT lock (``PREEMPT_RT`` only), it is common to see wakeup and set
+  state events racing with each other and this leads the model to perceive this
+  type of switch when the task is not set to sleepable. This is a limitation of
+  the model in SMP system and workarounds may require to slow down the
+  scheduler.
+* ``switch_yield``:
+  a task explicitly calls the scheduler, this looks like a preemption as the
+  task is still runnable but the ``need_resched`` flag is not set. It can
+  happen after a ``yield`` system call or from the idle task. +* ``switch_preempt``:
+  a task is ``preempted``, this can happen after the need for ``rescheduling``
+  has been set, also in its ``lazy`` flavour. ``need_resched`` can be set as a
+  flag to the task or in the per-core preemption count, either of them can
+  trigger a preemption.
+  The task was previously running and can be switched in directly, but it is
+  possible that a task is preempted after it sets itself as ``sleepable``
+  (``preempted_sleepable``), in this condition, once the task is switched back
+  in, it will not be ``running`` but continue its sleeping process in
+  ``sleepable``.
+* ``switch_vain``:
+  a task goes through the scheduler but it is picked as the next task to run,
+  hence no real task switch occurs. Since we run the scheduler, this clears the
+  need to reschedule.
+

...

Quoted 15 lines
kernel/trace/rv/monitors/srs/srs.h new file +15 -0
+enum events_srs {
+	sched_need_resched_srs = 0,
+	sched_need_resched_lazy_srs,
+	sched_set_state_runnable_srs,
+	sched_set_state_sleepable_srs,
+	sched_switch_blocking_srs,
+	sched_switch_in_srs,
+	sched_switch_preempt_srs,
+	sched_switch_suspend_srs,
+	sched_switch_vain_srs,
+	sched_switch_yield_srs,
+	sched_wakeup_srs,
+	event_max_srs
+};
+
#9 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

In reply to Gabriele Monaco
Quoted 21 lines · Gabriele Monaco

If a task sets itself to interruptible and schedules, the __schedule function checks whether there's a pending signal and, if that's the case, updates the state of the task to runnable instead of dequeuing. By looking at the tracepoints, we see the task enters the scheduler while sleepable but exits as runnable. From a modelling perspective, this is equivalent to a wakeup and the tracepoints should reflect that.

Add the waking/wakeup tracepoints in the try_to_block_task function and set the prev_state used by the sched_switch tracepoint to TASK_RUNNING if the task had a pending signal and was not blocked.

---
 kernel/sched/core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index f2f79236d5811..48cb32abce01a 100644

kernel/sched/core.c static bool try_to_block_task(struct rq *rq, struct task_struct *p, · line 6592

Quoted 10 lines
kernel/sched/core.c +6 -1
@@ -6584,7 +6584,12 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
 	int flags = DEQUEUE_NOCLOCK;
 
 	if (signal_pending_state(task_state, p)) {
-		WRITE_ONCE(p->__state, TASK_RUNNING);
+		/*
+		 * From a modelling perspective, this is equivalent to a wakeup
+		 * before dequeuing the task: trace accordingly.
+		 */
+		trace_sched_waking(p);
+		ttwu_do_wakeup(p);

I don't think we should put trace_sched_waking() here. trace_sched_waking() "is guaranteed to be called from the waking context", and this is not the waking context.

There is already a trace_sched_waking() in signal_wake_up_state(). This is duplicating that, in the wrong context.

ttwu_do_wakeup() alone should be sufficient?

Best regards,
Nam

author maintainer
#10 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

Quoted 47 lines · Nam Cao
Quoted 35 lines · Gabriele Monaco

If a task sets itself to interruptible and schedules, the __schedule
function checks whether there's a pending signal and, if that's the case, updates the state of the task to runnable instead of dequeuing.
By looking at the tracepoints, we see the task enters the scheduler while sleepable but exits as runnable. From a modelling perspective,
this is equivalent to a wakeup and the tracepoints should reflect that.

Add the waking/wakeup tracepoints in the try_to_block_task function and
set the prev_state used by the sched_switch tracepoint to TASK_RUNNING
if the task had a pending signal and was not blocked.

---
 kernel/sched/core.c | 11 +++++++++--

 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index f2f79236d5811..48cb32abce01a 100644
@@ -6584,7 +6584,12 @@ static bool try_to_block_task(struct rq *rq,
 struct task_struct *p,
  	int flags = DEQUEUE_NOCLOCK;


  if (signal_pending_state(task_state, p)) {

kernel/sched/core.c +2 -1
-		WRITE_ONCE(p->__state, TASK_RUNNING);
+		/*
+		 * From a modelling perspective, this is

equivalent to a wakeup

kernel/sched/core.c static bool try_to_block_task(struct rq *rq, struct task_struct *p, · line 6592

Quoted 4 lines
kernel/sched/core.c +4 -0
+		 * before dequeuing the task: trace accordingly.
+		 */
+		trace_sched_waking(p);
+		ttwu_do_wakeup(p);

I don't think we should put trace_sched_waking() here. trace_sched_waking()
"is guaranteed to be called from the waking context", and this is not the
waking context.

Quoted 5 lines

There is already a trace_sched_waking() in signal_wake_up_state(). This is
duplicating that, in the wrong context.

ttwu_do_wakeup() alone should be sufficient?

Mmh, that's a good point.
The thing is: this happens when the signal is generated while we are scheduling (on different CPUs), so we take a short-cut and put the task to running directly.
This thing is already racy, so we may or may not see the waking/wakeup.

Now probably waking shouldn't be there for the reason you said, but I'm not sure a wakeup not following a waking would be correct either. I might be missing something here, though.

Thanks,
Gabriele

#11 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

Quoted 9 lines · Gabriele Monaco

Mmh, that's a good point.
The thing is: this happens when the signal is generated while we are scheduling (on different CPUs), so we take a short-cut and put the task to running directly.
This thing is already racy, so we may or may not see the waking/wakeup.

Now probably waking shouldn't be there for the reason you said, but I'm not sure a wakeup not following a waking would be correct either. I might be missing something here, though.

I'm not familiar with signal and sched, so I don't have anything more to add, sorry.

I presume this is to make the srs monitor works? Perhaps it is possible to modify the model so that this patch is not required? Let me stare at srs, maybe I will have something..

Nam

author maintainer
#12 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

2025-04-15T11:05:06Z Nam Cao <[email protected]>:
Quoted 7 lines · Gabriele Monaco
Quoted 6 lines

Mmh, that's a good point.
The thing is: this happens when the signal is generated while we are scheduling (on different CPUs), so we take a short-cut and put the task to running directly.
This thing is already racy, so we may or may not see the waking/wakeup.

Quoted 7 lines
Quoted 3 lines

Now probably waking shouldn't be there for the reason you said, but I'm not sure a wakeup not following a waking would be correct either. I might be missing something here, though.

I'm not familiar with signal and sched, so I don't have anything more to add, sorry.

No worries, that was helpful already!

Quoted 3 lines

I presume this is to make the srs monitor works? Perhaps it is possible to modify the model so that this patch is not required? Let me stare at srs, maybe I will have something..

Yeah, that's another option, we could have a special case just for that, but I wanted to avoid it. But still, let me know if you got a good idea how to rearrange it!

Thanks,
Gabriele

#13 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

Quoted 35 lines · Gabriele Monaco

If a task sets itself to interruptible and schedules, the __schedule function checks whether there's a pending signal and, if that's the case, updates the state of the task to runnable instead of dequeuing. By looking at the tracepoints, we see the task enters the scheduler while sleepable but exits as runnable. From a modelling perspective, this is equivalent to a wakeup and the tracepoints should reflect that.

Add the waking/wakeup tracepoints in the try_to_block_task function and set the prev_state used by the sched_switch tracepoint to TASK_RUNNING if the task had a pending signal and was not blocked.

---
 kernel/sched/core.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/core.c +6 -1
index f2f79236d5811..48cb32abce01a 100644
@@ -6584,7 +6584,12 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
 	int flags = DEQUEUE_NOCLOCK;
 
 	if (signal_pending_state(task_state, p)) {
-		WRITE_ONCE(p->__state, TASK_RUNNING);
+		/*
+		 * From a modelling perspective, this is equivalent to a wakeup
+		 * before dequeuing the task: trace accordingly.
+		 */
+		trace_sched_waking(p);
+		ttwu_do_wakeup(p);
 		return false;
 	}
 

kernel/sched/core.c static void __sched notrace __schedule(int sched_mode) · line 6733

Quoted 9 lines
kernel/sched/core.c +3 -1
@@ -6721,7 +6726,9 @@ static void __sched notrace __schedule(int sched_mode)
 			goto picked;
 		}
 	} else if (!preempt && prev_state) {
-		try_to_block_task(rq, prev, prev_state);
+		/* Task was not blocked due to a signal and is again runnable */
+		if (!try_to_block_task(rq, prev, prev_state))
+			prev_state = TASK_RUNNING;
 		switch_count = &prev->nvcsw;
 	}

I couldn't reproduce the problem that this patch is solving. But staring at the srs monitor, I made an educated guess that this is to accomodate the transition "sleepable x wakeup -> running"?

But for this transition, no real wakeup happens, just the task's state is changed to "sleepable" then back to "running", right? Sleep hasn't actually happened yet?

If that is the case, would the patch below also solves it? It would turn the transition into "sleepable x set_runnable -> running", which I think describe it more accurately.

Best regards,
Nam

kernel/sched/core.c +1 -0
index 9a14d792a681..e39b4aadeba2 100644
@@ -6592,6 +6592,7 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
 	int flags = DEQUEUE_NOCLOCK;
 
 	if (signal_pending_state(task_state, p)) {
+		trace_sched_set_state_tp(p, TASK_RUNNING);
 		WRITE_ONCE(p->__state, TASK_RUNNING);
 		return false;
 	}
author maintainer
#14 lore raw

Re: [RFC PATCH 6/9] sched: Treat try_to_block_task with pending signal as wakeup

Quoted 68 lines · Nam Cao
Quoted 52 lines · Gabriele Monaco

If a task sets itself to interruptible and schedules, the __schedule
function checks whether there's a pending signal and, if that's the case, updates the state of the task to runnable instead of dequeuing.
By looking at the tracepoints, we see the task enters the scheduler while sleepable but exits as runnable. From a modelling perspective,
this is equivalent to a wakeup and the tracepoints should reflect that.

Add the waking/wakeup tracepoints in the try_to_block_task function and
set the prev_state used by the sched_switch tracepoint to TASK_RUNNING
if the task had a pending signal and was not blocked.

---
 kernel/sched/core.c | 11 +++++++++--

 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index f2f79236d5811..48cb32abce01a 100644
@@ -6584,7 +6584,12 @@ static bool try_to_block_task(struct rq *rq,
 struct task_struct *p,
  	int flags = DEQUEUE_NOCLOCK;


  if (signal_pending_state(task_state, p)) {

kernel/sched/core.c +2 -1
-		WRITE_ONCE(p->__state, TASK_RUNNING);
+		/*
+		 * From a modelling perspective, this is

equivalent to a wakeup

kernel/sched/core.c +4 -0
+		 * before dequeuing the task: trace accordingly.
+		 */
+		trace_sched_waking(p);
+		ttwu_do_wakeup(p);

  return false;
  }

kernel/sched/core.c +1 -1
@@ -6721,7 +6726,9 @@ static void __sched notrace __schedule(int
 sched_mode)
  			goto picked;
  		}
  	} else if (!preempt && prev_state) {
-		try_to_block_task(rq, prev, prev_state);
+		/* Task was not blocked due to a signal and is

again runnable */

kernel/sched/core.c +2 -0
+		if (!try_to_block_task(rq, prev, prev_state))
+			prev_state = TASK_RUNNING;
Quoted 2 lines

  switch_count = &prev->nvcsw;
  }

I couldn't reproduce the problem that this patch is solving. But staring at
the srs monitor, I made an educated guess that this is to accomodate the
transition "sleepable x wakeup -> running"?

But for this transition, no real wakeup happens, just the task's state is
changed to "sleepable" then back to "running", right? Sleep hasn't actually
happened yet?

Quoted 5 lines

If that is the case, would the patch below also solves it? It would turn
the transition into "sleepable x set_runnable -> running", which I think
describe it more accurately.

Yeah that's pretty much it, there are a few problems though:
1. set_state should occur in task context and not while scheduling
2. set_state doesn't expect a task switch to occur

One way to solve this is to do like you said but add a flag to the tracepoint to tell the model this set state is a special one happening while scheduling, after that one, we may be scheduled out.

I didn't really like adding another state so I dropped that.

However, a task can be woken up before being scheduled out (I'd agree with you it's not quite a wakeup as it wasn't yet sleeping, but it happens, e.g. p == current in try_to_wake_up).
This case with the signal is, in that sense, a wakeup. We can even see the tracepoint at times.

Anyway, that issue was mostly hypothetical, the patch also fixes the prev_state (there's a patch by Peter on tip doing the same thing) and I need to make sure it's really possible to see the issue after that too.

Thanks for looking into it,
Gabriele