Skip to contentSkip to conversation

cobalt: Move relax related lostage_wakeup work from stack to thread

PATCH New Jan Kiszka 1 participant 1 message Oct 8, 2024

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 cobalt: Move relax related lostage_wakeup work from stack to thread

From: Jan Kiszka <[email protected]>

We cannot rely on the lostage_wakeup work being scheduled only synchronously over the CPU of the relaxing thread. It thread might have a CPU migration and other wakeup events pending and resume on different CPU. As the lostage_wakeup work is currently on the stack, it can become corrupt when xnthread_relax leaves earlier on the new CPU than the work was processed on the old one - ugly crashes ahead as seen in [1].

Analogously to the signal wakeups, move the relax work into the xnthread structure to avoid such corruptions. We intentionally refrain from using irq_work_sync in the relaxation exit path to avoid starting to wait a CPU that is already busy again with a lengthy RT task.

[1] https://lore.kernel.org/xenomai/[email protected]/T/#t

Backport of 95ccd795ec5db6c2d4d867b0f29fae964cffcf82, only affecting dovetail kernels, I-pipe copies the work message.

---
 include/cobalt/kernel/thread.h |  6 ++++++
 kernel/cobalt/thread.c         | 17 ++++++-----------
 2 files changed, 12 insertions(+), 11 deletions(-)
include/cobalt/kernel/thread.h +6 -0
index b79cb84296..325859b6a4 100644
@@ -65,6 +65,11 @@ struct lostage_signal {
 	struct lostage_signal *self; /* Revisit: I-pipe requirement */
 };
 
+struct lostage_wakeup {
+	struct pipeline_inband_work inband_work; /* Must be first. */
+	struct task_struct *task;
+};
+
 struct xnthread_init_attr {
 	struct xnthread_personality *personality;
 	cpumask_t affinity;
@@ -215,6 +220,7 @@ struct xnthread {
 	u32 proghash;		/* Hash value for exe_path */
 #endif
 	struct lostage_signal sigarray[XNTHREAD_MAX_SIGNALS];
+	struct lostage_wakeup relax_work;
 };
 
 static inline int xnthread_get_state(const struct xnthread *thread)
kernel/cobalt/thread.c +6 -11
index ff12f288ad..343f1f0e09 100644
@@ -1967,11 +1967,6 @@ int xnthread_harden(void)
 }
 EXPORT_SYMBOL_GPL(xnthread_harden);
 
-struct lostage_wakeup {
-	struct pipeline_inband_work inband_work; /* Must be first. */
-	struct task_struct *task;
-};
-
 static void lostage_task_wakeup(struct pipeline_inband_work *inband_work)
 {
 	struct lostage_wakeup *rq;
@@ -2045,11 +2040,6 @@ void __xnthread_propagate_schedparam(struct xnthread *curr)
 void xnthread_relax(int notify, int reason)
 {
 	struct task_struct *p = current;
-	struct lostage_wakeup wakework = {
-		.inband_work = PIPELINE_INBAND_WORK_INITIALIZER(wakework,
-					lostage_task_wakeup),
-		.task = p,
-	};
 	struct xnthread *thread = xnthread_current();
 	int cpu __maybe_unused, suspension;
 	kernel_siginfo_t si;
@@ -2063,6 +2053,11 @@ void xnthread_relax(int notify, int reason)
 	 */
 	trace_cobalt_shadow_gorelax(reason);
 
+	thread->relax_work.inband_work = (struct pipeline_inband_work)
+		PIPELINE_INBAND_WORK_INITIALIZER(thread->relax_work,
+						 lostage_task_wakeup);
+	thread->relax_work.task = p;
+
 	/*
 	 * If you intend to change the following interrupt-free
 	 * sequence, /first/ make sure to check the special handling
@@ -2075,7 +2070,7 @@ void xnthread_relax(int notify, int reason)
 	 */
 	splmax();
 	trace_cobalt_lostage_request("wakeup", p);
-	pipeline_post_inband_work(&wakework);
+	pipeline_post_inband_work(&thread->relax_work);
 	/*
 	 * Grab the nklock to synchronize the Linux task state
 	 * manipulation with handle_sigwake_event. This lock will be
Signature · 1 line
2.43.0