Skip to contentSkip to conversation

More pipeline abstraction cleanups

PATCH New Jan Kiszka 1 participant 4 messages Jun 15, 2021

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover More pipeline abstraction cleanups

More de-duplications, see patches for details

Jan

Jan Kiszka (3):
cobalt: Move common affinity handling code out of pipeline diversion
again
cobalt: Move common cleanup code out of pipeline diversion again
cobalt: Move common user-return code out of pipeline diversion again

 .../cobalt/kernel/dovetail/pipeline/kevents.h |   2 +
 .../cobalt/kernel/ipipe/pipeline/kevents.h    |   2 +
 kernel/cobalt/dovetail/kevents.c              | 258 +----------------
 kernel/cobalt/ipipe/kevents.c                 | 260 +-----------------
 kernel/cobalt/posix/process.c                 | 242 ++++++++++++++++
 kernel/cobalt/posix/process.h                 |  14 +
 6 files changed, 286 insertions(+), 492 deletions(-)
Signature · 1 line
2.26.2
author
#1 lore raw

PATCH 1/3 cobalt: Move common affinity handling code out of pipeline diversion again

From: Jan Kiszka <[email protected]>

Same pattern as for previous changes: All that code it common, no need to duplicate it.

---
 kernel/cobalt/dovetail/kevents.c | 113 +-----------------------------
 kernel/cobalt/ipipe/kevents.c    | 115 +------------------------------
 kernel/cobalt/posix/process.c    |  99 ++++++++++++++++++++++++++
 kernel/cobalt/posix/process.h    |   8 +++
 4 files changed, 113 insertions(+), 222 deletions(-)
kernel/cobalt/dovetail/kevents.c +3 -110
index f27d5a966d..e2ded266c0 100644
@@ -94,118 +94,11 @@ void handle_oob_trap_entry(unsigned int trapnr, struct pt_regs *regs)
 	xnthread_relax(xnarch_fault_notify(trapnr), SIGDEBUG_MIGRATE_FAULT);
 }
 
-#ifdef CONFIG_SMP
-
-static int handle_setaffinity_event(struct dovetail_migration_data *d)
-{
-	struct task_struct *p = d->task;
-	struct xnthread *thread;
-	spl_t s;
-
-	thread = xnthread_from_task(p);
-	if (thread == NULL)
-		return KEVENT_PROPAGATE;
-
-	/*
-	 * Detect a Cobalt thread sleeping in primary mode which is
-	 * required to migrate to another CPU by the host kernel.
-	 *
-	 * We may NOT fix up thread->sched immediately using the
-	 * passive migration call, because that latter always has to
-	 * take place on behalf of the target thread itself while
-	 * running in secondary mode. Therefore, that thread needs to
-	 * go through secondary mode first, then move back to primary
-	 * mode, so that affinity_ok() does the fixup work.
-	 *
-	 * We force this by sending a SIGSHADOW signal to the migrated
-	 * thread, asking it to switch back to primary mode from the
-	 * handler, at which point the interrupted syscall may be
-	 * restarted.
-	 */
-	xnlock_get_irqsave(&nklock, s);
-
-	if (xnthread_test_state(thread, XNTHREAD_BLOCK_BITS & ~XNRELAX))
-		__xnthread_signal(thread, SIGSHADOW, SIGSHADOW_ACTION_HARDEN);
-
-	xnlock_put_irqrestore(&nklock, s);
-
-	return KEVENT_PROPAGATE;
-}
-
-static inline bool affinity_ok(struct task_struct *p) /* nklocked, IRQs off */
-{
-	struct xnthread *thread = xnthread_from_task(p);
-	struct xnsched *sched;
-	int cpu = task_cpu(p);
-
-	/*
-	 * To maintain consistency between both Cobalt and host
-	 * schedulers, reflecting a thread migration to another CPU
-	 * into the Cobalt scheduler state must happen from secondary
-	 * mode only, on behalf of the migrated thread itself once it
-	 * runs on the target CPU.
-	 *
-	 * This means that the Cobalt scheduler state regarding the
-	 * CPU information lags behind the host scheduler state until
-	 * the migrated thread switches back to primary mode
-	 * (i.e. task_cpu(p) != xnsched_cpu(xnthread_from_task(p)->sched)).
-	 * This is ok since Cobalt does not schedule such thread until then.
-	 *
-	 * check_affinity() detects when a Cobalt thread switching
-	 * back to primary mode did move to another CPU earlier while
-	 * in secondary mode. If so, do the fixups to reflect the
-	 * change.
-	 */
-	if (!xnsched_threading_cpu(cpu)) {
-		/*
-		 * The thread is about to switch to primary mode on a
-		 * non-rt CPU, which is damn wrong and hopeless.
-		 * Whine and cancel that thread.
-		 */
-		printk(XENO_WARNING "thread %s[%d] switched to non-rt CPU%d, aborted.\n",
-		       thread->name, xnthread_host_pid(thread), cpu);
-		/*
-		 * Can't call xnthread_cancel() from a migration
-		 * point, that would break. Since we are on the wakeup
-		 * path to hardening, just raise XNCANCELD to catch it
-		 * in xnthread_harden().
-		 */
-		xnthread_set_info(thread, XNCANCELD);
-		return false;
-	}
-
-	sched = xnsched_struct(cpu);
-	if (sched == thread->sched)
-		return true;
-
-	/*
-	 * The current thread moved to a supported real-time CPU,
-	 * which is not part of its original affinity mask
-	 * though. Assume user wants to extend this mask.
-	 */
-	if (!cpumask_test_cpu(cpu, &thread->affinity))
-		cpumask_set_cpu(cpu, &thread->affinity);
-
-	xnthread_run_handler_stack(thread, move_thread, cpu);
-	xnthread_migrate_passive(thread, sched);
-
-	return true;
-}
-
-#else /* !CONFIG_SMP */
-
-static int handle_setaffinity_event(struct dovetail_migration_data *d)
+static inline int handle_setaffinity_event(struct dovetail_migration_data *d)
 {
-	return KEVENT_PROPAGATE;
+	return cobalt_handle_setaffinity_event(d->task);
 }
 
-static inline bool affinity_ok(struct task_struct *p)
-{
-	return true;
-}
-
-#endif /* CONFIG_SMP */
-
 static void __handle_taskexit_event(struct task_struct *p)
 {
 	struct cobalt_ppd *sys_ppd;
@@ -531,7 +424,7 @@ void resume_oob_task(struct task_struct *p) /* inband, oob stage stalled */
 	 * relax_thread/harden_thread handlers.
 	 */
 	xnthread_run_handler_stack(thread, harden_thread);
-	if (affinity_ok(p))
+	if (cobalt_affinity_ok(p))
 		xnthread_resume(thread, XNRELAX);
 
 	/*
kernel/cobalt/ipipe/kevents.c +3 -112

130 lines not on this page · raw message

kernel/cobalt/posix/process.c +99 -0

105 lines not on this page · raw message

kernel/cobalt/posix/process.h +8 -0
index 767d75e8ba..051a0d90be 100644
@@ -161,4 +161,12 @@ extern struct xnthread_personality *cobalt_personalities[];
 
 extern struct xnthread_personality cobalt_personality;
 
+int cobalt_handle_setaffinity_event(struct task_struct *task);
+
+#ifdef CONFIG_SMP
+bool cobalt_affinity_ok(struct task_struct *task);
+#else
+#define cobalt_affinity_ok(task)	(true)
+#endif
+
 #endif /* !_COBALT_POSIX_PROCESS_H */
Signature · 1 line
2.26.2
author
#2 lore raw

PATCH 2/3 cobalt: Move common cleanup code out of pipeline diversion again

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Same pattern as for previous changes: All that code it common, no need to duplicate it. The only difference between the two versions of handle_cleanup_event was what is now encapsulated in pipeline_cleanup_process.

---
 .../cobalt/kernel/dovetail/pipeline/kevents.h |   2 +
 .../cobalt/kernel/ipipe/pipeline/kevents.h    |   2 +
 kernel/cobalt/dovetail/kevents.c              | 114 ++---------------
 kernel/cobalt/ipipe/kevents.c                 | 119 ++----------------
 kernel/cobalt/posix/process.c                 | 111 ++++++++++++++++
 kernel/cobalt/posix/process.h                 |   4 +
 6 files changed, 138 insertions(+), 214 deletions(-)
include/cobalt/kernel/dovetail/pipeline/kevents.h +2 -0
index 5cc78721a2..fa47f0375c 100644
@@ -31,4 +31,6 @@ int pipeline_trap_kevents(void);
 
 void pipeline_enable_kevents(void);
 
+void pipeline_cleanup_process(void);
+
 #endif /* !_COBALT_KERNEL_DOVETAIL_KEVENTS_H */
include/cobalt/kernel/ipipe/pipeline/kevents.h +2 -0
index 2f186a57ee..f3f0c2f95e 100644
@@ -31,4 +31,6 @@ int pipeline_trap_kevents(void);
 
 void pipeline_enable_kevents(void);
 
+void pipeline_cleanup_process(void);
+
 #endif /* !_COBALT_KERNEL_IPIPE_KEVENTS_H */
kernel/cobalt/dovetail/kevents.c +8 -106

141 lines not on this page · raw message

kernel/cobalt/ipipe/kevents.c +11 -108

151 lines not on this page · raw message

kernel/cobalt/posix/process.c +111 -0

117 lines not on this page · raw message

kernel/cobalt/posix/process.h +4 -0
index 051a0d90be..de3d9144d7 100644
@@ -169,4 +169,8 @@ bool cobalt_affinity_ok(struct task_struct *task);
 #define cobalt_affinity_ok(task)	(true)
 #endif
 
+int cobalt_handle_taskexit_event(struct task_struct *task);
+
+int cobalt_handle_cleanup_event(struct mm_struct *mm);
+
 #endif /* !_COBALT_POSIX_PROCESS_H */
Signature · 1 line
2.26.2
author
#3 lore raw

PATCH 3/3 cobalt: Move common user-return code out of pipeline diversion again

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Same pattern as for previous changes: All that code it common, no need to duplicate it. The only difference was that I-pipe needs to call ipipe_disable_user_intret_notifier first while dovetail does this disabling automatically.

---
 kernel/cobalt/dovetail/kevents.c | 31 ++-----------------------------
 kernel/cobalt/ipipe/kevents.c    | 32 ++------------------------------
 kernel/cobalt/posix/process.c    | 32 ++++++++++++++++++++++++++++++++
 kernel/cobalt/posix/process.h    |  2 ++
 4 files changed, 38 insertions(+), 59 deletions(-)
kernel/cobalt/dovetail/kevents.c +2 -29
index 96180731e6..3fbb5e6d64 100644
@@ -102,36 +102,9 @@ static inline int handle_taskexit_event(struct task_struct *p)
 	return cobalt_handle_taskexit_event(p);
 }
 
-static int handle_user_return(struct task_struct *task)
+static inline int handle_user_return(struct task_struct *task)
 {
-	struct xnthread *thread;
-	spl_t s;
-	int err;
-
-	thread = xnthread_from_task(task);
-	if (thread == NULL)
-		return KEVENT_PROPAGATE;
-
-	if (xnthread_test_info(thread, XNCONTHI)) {
-		xnlock_get_irqsave(&nklock, s);
-		xnthread_clear_info(thread, XNCONTHI);
-		xnlock_put_irqrestore(&nklock, s);
-
-		err = xnthread_harden();
-
-		/*
-		 * XNCONTHI may or may not have been re-applied if
-		 * harden bailed out due to pending signals. Make sure
-		 * it is set in that case.
-		 */
-		if (err == -ERESTARTSYS) {
-			xnlock_get_irqsave(&nklock, s);
-			xnthread_set_info(thread, XNCONTHI);
-			xnlock_put_irqrestore(&nklock, s);
-		}
-	}
-
-	return KEVENT_PROPAGATE;
+	return cobalt_handle_user_return(task);
 }
 
 void handle_oob_mayday(struct pt_regs *regs)
kernel/cobalt/ipipe/kevents.c +2 -30
index 94a951d0a6..40ad93f398 100644
@@ -401,38 +401,10 @@ static inline int handle_clockfreq_event(unsigned int *p)
 	return KEVENT_PROPAGATE;
 }
 
-static int handle_user_return(struct task_struct *task)
+static inline int handle_user_return(struct task_struct *task)
 {
-	struct xnthread *thread;
-	spl_t s;
-	int err;
-
 	ipipe_disable_user_intret_notifier();
-
-	thread = xnthread_from_task(task);
-	if (thread == NULL)
-		return KEVENT_PROPAGATE;
-
-	if (xnthread_test_info(thread, XNCONTHI)) {
-		xnlock_get_irqsave(&nklock, s);
-		xnthread_clear_info(thread, XNCONTHI);
-		xnlock_put_irqrestore(&nklock, s);
-
-		err = xnthread_harden();
-
-		/*
-		 * XNCONTHI may or may not have been re-applied if
-		 * harden bailed out due to pending signals. Make sure
-		 * it is set in that case.
-		 */
-		if (err == -ERESTARTSYS) {
-			xnlock_get_irqsave(&nklock, s);
-			xnthread_set_info(thread, XNCONTHI);
-			xnlock_put_irqrestore(&nklock, s);
-		}
-	}
-
-	return KEVENT_PROPAGATE;
+	return cobalt_handle_user_return(task);
 }
 
 int handle_ptrace_resume(struct ipipe_ptrace_resume_data *resume)
kernel/cobalt/posix/process.c +32 -0
index 3cbad9bbb0..aa384155a1 100644
@@ -870,6 +870,38 @@ static void __handle_taskexit_event(struct task_struct *p)
 	}
 }
 
+int cobalt_handle_user_return(struct task_struct *task)
+{
+	struct xnthread *thread;
+	spl_t s;
+	int err;
+
+	thread = xnthread_from_task(task);
+	if (thread == NULL)
+		return KEVENT_PROPAGATE;
+
+	if (xnthread_test_info(thread, XNCONTHI)) {
+		xnlock_get_irqsave(&nklock, s);
+		xnthread_clear_info(thread, XNCONTHI);
+		xnlock_put_irqrestore(&nklock, s);
+
+		err = xnthread_harden();
+
+		/*
+		 * XNCONTHI may or may not have been re-applied if
+		 * harden bailed out due to pending signals. Make sure
+		 * it is set in that case.
+		 */
+		if (err == -ERESTARTSYS) {
+			xnlock_get_irqsave(&nklock, s);
+			xnthread_set_info(thread, XNCONTHI);
+			xnlock_put_irqrestore(&nklock, s);
+		}
+	}
+
+	return KEVENT_PROPAGATE;
+}
+
 static void detach_current(void)
 {
 	struct cobalt_threadinfo *p = pipeline_current();
kernel/cobalt/posix/process.h +2 -0
index de3d9144d7..45964ae57a 100644
@@ -173,4 +173,6 @@ int cobalt_handle_taskexit_event(struct task_struct *task);
 
 int cobalt_handle_cleanup_event(struct mm_struct *mm);
 
+int cobalt_handle_user_return(struct task_struct *task);
+
 #endif /* !_COBALT_POSIX_PROCESS_H */
Signature · 1 line
2.26.2