mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 05:20:28 +08:00
Fold the 56 kernel patches into 32: one arch patch (which now includes the riscv ISA patch, parking, atomics, SMP, blocking waits, unfair spinlocks, the arm64 user ABI, pc sampling, ptrace, single-step and core dumps), each driver with its SMP part, the generic changes one per piece (the cond_resched() poll split out of the arch patch), and kw_defconfig last. The messages describe the code as it is; every patch that was kernel-wasm@localhost's is Pooya Parsa's. The tree is unchanged. kernel/build.sh patches (kernel/tools/export-patches.sh) exports the series without what changed in every later patch on each edit: no numbers in the file names (the order is the new series file, which build.sh applies), a zero commit hash, no git version signature and no index lines, with the diff options fixed.
224 lines
7.3 KiB
Diff
224 lines
7.3 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Pooya Parsa <pooya@pi0.io>
|
|
Date: Mon, 16 Nov 2026 16:06:57 +0000
|
|
Subject: [PATCH] kthread, workqueue: restart points for ARCH_TASKS_STAY_ON_CPU
|
|
|
|
A new kthread first runs only to report that it exists, and is often
|
|
bound to its CPU only afterwards: the per-CPU threads of a CPU that is
|
|
not online yet run and park on the CPU that created them, and a
|
|
per-CPU workqueue pool's first worker idles there until the CPU comes
|
|
online and the workers are rebound. On an arch whose kthreads cannot
|
|
leave the CPU they ran on, they would stay on the wrong CPU.
|
|
|
|
At those sleeps nothing on the thread's stack is needed after the
|
|
wakeup. Mark them with arch_kthread_restart_point(fn, arg) and
|
|
arch_kthread_restart_cancel(): in between the arch may let the thread
|
|
migrate and, when it is woken on another CPU, start it there on a
|
|
fresh stack in kthread_exit(fn(arg)). kthread()'s part after the first
|
|
wakeup becomes kthread_start(), which needs only current, and the park
|
|
before the thread function and worker_thread()'s idle sleep get the
|
|
same marks.
|
|
|
|
A workqueue rescuer that must serve a per-CPU pool on another CPU
|
|
cannot be moved while it runs (worker_attach_to_pool() would leave it
|
|
where it is, and it would run that CPU's work items on its own). It
|
|
leaves the request queued and calls arch_kthread_move(cpu, fn, arg),
|
|
which moves it there and starts it again in kthread_exit(fn(arg)).
|
|
|
|
Without ARCH_TASKS_STAY_ON_CPU the marks are empty.
|
|
---
|
|
include/linux/kthread.h | 30 ++++++++++++++++++++
|
|
kernel/kthread.c | 62 +++++++++++++++++++++++++++++++++--------
|
|
kernel/workqueue.c | 24 ++++++++++++++++
|
|
3 files changed, 105 insertions(+), 11 deletions(-)
|
|
|
|
diff --git a/include/linux/kthread.h b/include/linux/kthread.h
|
|
--- a/include/linux/kthread.h
|
|
+++ b/include/linux/kthread.h
|
|
@@ -7,6 +7,36 @@
|
|
|
|
struct mm_struct;
|
|
|
|
+#ifdef CONFIG_ARCH_TASKS_STAY_ON_CPU
|
|
+/*
|
|
+ * The kthread is about to sleep in schedule() holding no locks, with
|
|
+ * nothing on its stack that it needs after the wakeup: if it is woken on
|
|
+ * another CPU, the arch may start it there again on a fresh stack, in
|
|
+ * kthread_exit(@fn(@arg)), instead of returning from schedule(). Ends at
|
|
+ * arch_kthread_restart_cancel(), after schedule() returned.
|
|
+ */
|
|
+void arch_kthread_restart_point(int (*fn)(void *), void *arg);
|
|
+void arch_kthread_restart_cancel(void);
|
|
+/*
|
|
+ * The running kthread holds no locks and needs nothing on its stack: move
|
|
+ * it to @cpu (its affinity becomes that CPU) and start it again there in
|
|
+ * kthread_exit(@fn(@arg)). Returns only if it could not move.
|
|
+ */
|
|
+void arch_kthread_move(int cpu, int (*fn)(void *), void *arg);
|
|
+#else
|
|
+static inline void arch_kthread_restart_point(int (*fn)(void *), void *arg)
|
|
+{
|
|
+}
|
|
+
|
|
+static inline void arch_kthread_restart_cancel(void)
|
|
+{
|
|
+}
|
|
+
|
|
+static inline void arch_kthread_move(int cpu, int (*fn)(void *), void *arg)
|
|
+{
|
|
+}
|
|
+#endif
|
|
+
|
|
/* opaque kthread data */
|
|
struct kthread;
|
|
|
|
diff --git a/kernel/kthread.c b/kernel/kthread.c
|
|
--- a/kernel/kthread.c
|
|
+++ b/kernel/kthread.c
|
|
@@ -257,7 +257,15 @@ void *kthread_probe_data(struct task_struct *task)
|
|
return data;
|
|
}
|
|
|
|
-static void __kthread_parkme(struct kthread *self)
|
|
+static int kthread_start(void *unused);
|
|
+
|
|
+/*
|
|
+ * @restartable: before the thread function (kthread_start()), where the
|
|
+ * thread may be started again instead (arch_kthread_restart_point()): a
|
|
+ * per-CPU thread parks there, on the CPU that created it, until its CPU
|
|
+ * comes online.
|
|
+ */
|
|
+static void ___kthread_parkme(struct kthread *self, bool restartable)
|
|
{
|
|
for (;;) {
|
|
/*
|
|
@@ -280,12 +288,26 @@ static void __kthread_parkme(struct kthread *self)
|
|
*/
|
|
preempt_disable();
|
|
complete(&self->parked);
|
|
+ if (restartable)
|
|
+ arch_kthread_restart_point(kthread_start, NULL);
|
|
schedule_preempt_disabled();
|
|
+ if (restartable)
|
|
+ arch_kthread_restart_cancel();
|
|
preempt_enable();
|
|
}
|
|
__set_current_state(TASK_RUNNING);
|
|
}
|
|
|
|
+static void __kthread_parkme(struct kthread *self)
|
|
+{
|
|
+ ___kthread_parkme(self, false);
|
|
+}
|
|
+
|
|
+static void __kthread_parkme_restartable(struct kthread *self)
|
|
+{
|
|
+ ___kthread_parkme(self, true);
|
|
+}
|
|
+
|
|
void kthread_parkme(void)
|
|
{
|
|
__kthread_parkme(to_kthread(current));
|
|
@@ -387,7 +409,6 @@ static int kthread(void *_create)
|
|
void *data = create->data;
|
|
struct completion *done;
|
|
struct kthread *self;
|
|
- int ret;
|
|
|
|
self = to_kthread(current);
|
|
|
|
@@ -418,23 +439,42 @@ static int kthread(void *_create)
|
|
*/
|
|
preempt_disable();
|
|
complete(done);
|
|
+ /* Nothing on this stack is needed after the wakeup (kthread_start()). */
|
|
+ arch_kthread_restart_point(kthread_start, NULL);
|
|
schedule_preempt_disabled();
|
|
+ arch_kthread_restart_cancel();
|
|
preempt_enable();
|
|
|
|
- self->started = 1;
|
|
+ return kthread_start(NULL);
|
|
+}
|
|
|
|
- /*
|
|
- * Apply default node affinity if no call to kthread_bind[_mask]() nor
|
|
- * kthread_affine_preferred() was issued before the first wake-up.
|
|
- */
|
|
- if (!(current->flags & PF_NO_SETAFFINITY) && !self->preferred_affinity)
|
|
- kthread_affine_node();
|
|
+/*
|
|
+ * The rest of kthread() after the first wakeup; it needs only current. An
|
|
+ * arch whose tasks stay on their CPU (ARCH_TASKS_STAY_ON_CPU) may enter it
|
|
+ * on a fresh stack instead, after kthread_bind() moved the new thread, and
|
|
+ * again after the park before threadfn.
|
|
+ */
|
|
+static int kthread_start(void *unused)
|
|
+{
|
|
+ struct kthread *self = to_kthread(current);
|
|
+ int ret;
|
|
+
|
|
+ if (!self->started) {
|
|
+ self->started = 1;
|
|
+
|
|
+ /*
|
|
+ * Apply default node affinity if no call to kthread_bind[_mask]() nor
|
|
+ * kthread_affine_preferred() was issued before the first wake-up.
|
|
+ */
|
|
+ if (!(current->flags & PF_NO_SETAFFINITY) && !self->preferred_affinity)
|
|
+ kthread_affine_node();
|
|
+ }
|
|
|
|
ret = -EINTR;
|
|
if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
|
|
cgroup_kthread_ready();
|
|
- __kthread_parkme(self);
|
|
- ret = threadfn(data);
|
|
+ __kthread_parkme_restartable(self);
|
|
+ ret = self->threadfn(self->data);
|
|
}
|
|
kthread_exit(ret);
|
|
}
|
|
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
|
|
--- a/kernel/workqueue.c
|
|
+++ b/kernel/workqueue.c
|
|
@@ -3575,7 +3575,14 @@ static int worker_thread(void *__worker)
|
|
worker_enter_idle(worker);
|
|
__set_current_state(TASK_IDLE);
|
|
raw_spin_unlock_irq(&pool->lock);
|
|
+ /*
|
|
+ * Idle, no locks: it may start again at woke_up, on a fresh stack
|
|
+ * (a per-CPU pool's first worker runs before its CPU is online, and
|
|
+ * is rebound to it then).
|
|
+ */
|
|
+ arch_kthread_restart_point(worker_thread, __worker);
|
|
schedule();
|
|
+ arch_kthread_restart_cancel();
|
|
goto woke_up;
|
|
}
|
|
|
|
@@ -3695,6 +3702,23 @@ static int rescuer_thread(void *__rescuer)
|
|
__set_current_state(TASK_RUNNING);
|
|
list_del_init(&pwq->mayday_node);
|
|
|
|
+ /*
|
|
+ * ARCH_TASKS_STAY_ON_CPU: a running rescuer cannot be moved to
|
|
+ * a per-CPU pool's CPU (worker_attach_to_pool() would leave it
|
|
+ * where it is). Leave the request queued and start again
|
|
+ * there.
|
|
+ */
|
|
+ if (IS_ENABLED(CONFIG_ARCH_TASKS_STAY_ON_CPU) && pool->cpu >= 0 &&
|
|
+ !(pool->flags & POOL_DISASSOCIATED) &&
|
|
+ pool->cpu != raw_smp_processor_id()) {
|
|
+ list_add(&pwq->mayday_node, &wq->maydays);
|
|
+ raw_spin_unlock_irq(&wq_mayday_lock);
|
|
+ arch_kthread_move(pool->cpu, rescuer_thread, __rescuer);
|
|
+ /* It could not move: serve the pool from here. */
|
|
+ raw_spin_lock_irq(&wq_mayday_lock);
|
|
+ list_del_init(&pwq->mayday_node);
|
|
+ }
|
|
+
|
|
raw_spin_unlock_irq(&wq_mayday_lock);
|
|
|
|
worker_attach_to_pool(rescuer, pool);
|