fix(linux): bind original runqueue clock accounting

This commit is contained in:
Tyler Smith committed 2026-11-04 09:36:02 +00:00
1 parent 5df028b986
commit fd4510ba11
5 files changed
+356

No files matched your search

+2
View File
@@ -278,6 +278,8 @@ impl Linux {
PathBuf::from(format!("{LINUX}/kernel/sched/fork_load.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/rt_timeslice.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/rq_lock.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/rq_clock.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/clock.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/fork_metadata.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/cputime_group.c")),
PathBuf::from(format!("{LINUX}/kernel/sched/user_affinity.c")),
@@ -9,12 +9,14 @@
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/jump_label.h>
#include <linux/cpu.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/irqflags.h>
#include <linux/interrupt.h>
#include <linux/radix-tree.h>
#include <linux/sched/clock.h>
#include <linux/sched/isolation.h>
#include <linux/rcupdate.h>
#include <linux/srcu.h>
@@ -55,6 +57,7 @@
/* Original init/main.c globals have one native bootstrap owner. The original
* machine start_kernel/main.o must NOT also be activated with this adapter. */
bool early_boot_irqs_disabled __read_mostly;
bool static_key_initialized __read_mostly;
enum system_states system_state __read_mostly = SYSTEM_BOOTING;
/* The selected machine does not link init/main.c or calibrate a second clock. */
@@ -104,6 +107,7 @@ int stela_linux_task_bootstrap_resources(const struct stela_linux_owner *owner)
unsigned long flags;
local_irq_save(flags);
early_boot_irqs_disabled = true;
jump_label_init();
boot_cpu_init();
radix_tree_init();
housekeeping_init();
@@ -114,6 +118,7 @@ int stela_linux_task_bootstrap_resources(const struct stela_linux_owner *owner)
srcu_init();
early_boot_irqs_disabled = false;
local_irq_restore(flags);
sched_clock_init();
/* Linux initializes the descriptor cache during mm_core_init(), before
* creating the first threads. The native owner supplies their stacks. */
@@ -4,6 +4,7 @@
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/limits.h>
#include <linux/sched/clock.h>
#include <linux/timekeeping.h>
#include <stela/linux.h>
#include <stela/time.h>
@@ -16,6 +17,14 @@ ktime_t ktime_get(void)
return owner->services->now_ns(owner->context);
}
notrace u64 sched_clock(void)
{
ktime_t now = ktime_get();
if (now < 0 || now == KTIME_MAX)
stela_linux_fatal("invalid scheduler clock sample");
return (u64)now;
}
int stela_linux_boottime_ns(s64 *now)
{
const struct stela_linux_owner *owner = stela_linux_owner();
+4
View File
@@ -74,6 +74,10 @@
/kernel/sched/stats.h
/kernel/sched/ext/ext.h
/kernel/sched/rq_lock.c
/kernel/sched/rq_clock.c
/kernel/sched/clock.c
/kernel/sched/pelt.h
/kernel/sched/sched-pelt.h
/kernel/sched/cputime.c
/kernel/sched/cputime_group.c
/kernel/sched/user_affinity.c
@@ -0,0 +1,336 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
Date: Wed, 4 Nov 2026 04:31:50 -0500
Subject: [PATCH] refactor(sched): share original runqueue clock accounting
---
kernel/sched/Makefile | 2 +-
kernel/sched/clock.c | 3 +
kernel/sched/core.c | 107 ------------------------------
kernel/sched/fork_sources.json | 15 +++++
kernel/sched/rq_clock.c | 115 +++++++++++++++++++++++++++++++++
5 files changed, 134 insertions(+), 108 deletions(-)
create mode 100644 kernel/sched/rq_clock.c
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 7463c3b352def51b0d8f02de2fce21eeafafe7bf..e4a9801a6052e1e857d007d1d85a498f08c3f7b6 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -41,7 +41,7 @@ endif
# These compilation units have roughly the same size and complexity - so their
# build parallelizes well and finishes roughly at once:
#
-obj-y += core.o rq_lock.o wait_support.o user_affinity.o
+obj-y += core.o rq_lock.o rq_clock.o wait_support.o user_affinity.o
obj-y += fair.o
obj-y += build_policy.o cputime_group.o rt_timeslice.o
obj-y += build_utility.o capacity_state.o
diff --git a/kernel/sched/clock.c b/kernel/sched/clock.c
index 2ae4fbf13431fc83334832cf5666f951c760af95..55388a44710adf04bc0083378b594984f6b92fc5 100644
--- a/kernel/sched/clock.c
+++ b/kernel/sched/clock.c
@@ -55,8 +55,10 @@
*/
#include <linux/sched/clock.h>
+#include <linux/sched_clock.h>
#include "sched.h"
+#ifndef CONFIG_STELA
/*
* Scheduler clock - returns current time in nanosec units.
* This is default implementation.
@@ -68,6 +70,7 @@ notrace unsigned long long __weak sched_clock(void)
* (NSEC_PER_SEC / HZ);
}
EXPORT_SYMBOL_GPL(sched_clock);
+#endif
static DEFINE_STATIC_KEY_FALSE(sched_clock_running);
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 266d0ac8aae12772050e5c538b6c4fbdd2948c1d..1d74b2e5268d8f1df3a0779dc409f488ddf3e373 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -160,20 +160,6 @@ static int __init setup_proxy_exec(char *str)
#endif
__setup("sched_proxy_exec", setup_proxy_exec);
-/*
- * Debugging: various feature bits
- *
- * If SCHED_DEBUG is disabled, each compilation unit has its own copy of
- * sysctl_sched_features, defined in sched.h, to allow constants propagation
- * at compile time and compiler optimization based on features default.
- */
-#define SCHED_FEAT(name, enabled) \
- (1UL << __SCHED_FEAT_##name) * enabled |
-__read_mostly unsigned int sysctl_sched_features =
-#include "features.h"
- 0;
-#undef SCHED_FEAT
-
/*
* Print a warning if need_resched is set for the given duration (if
* LATENCY_WARN is enabled).
@@ -667,99 +653,6 @@ int task_llc(const struct task_struct *p)
*
*/
-/*
- * RQ-clock updating methods:
- */
-
-/* Use CONFIG_PARAVIRT as this will avoid more #ifdef in arch code. */
-#ifdef CONFIG_PARAVIRT
-struct static_key paravirt_steal_rq_enabled;
-#endif
-
-static void update_rq_clock_task(struct rq *rq, s64 delta)
-{
-/*
- * In theory, the compile should just see 0 here, and optimize out the call
- * to sched_rt_avg_update. But I don't trust it...
- */
- s64 __maybe_unused steal = 0, irq_delta = 0;
-
-#ifdef CONFIG_IRQ_TIME_ACCOUNTING
- if (irqtime_enabled()) {
- irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
-
- /*
- * Since irq_time is only updated on {soft,}irq_exit, we might run into
- * this case when a previous update_rq_clock() happened inside a
- * {soft,}IRQ region.
- *
- * When this happens, we stop ->clock_task and only update the
- * prev_irq_time stamp to account for the part that fit, so that a next
- * update will consume the rest. This ensures ->clock_task is
- * monotonic.
- *
- * It does however cause some slight miss-attribution of {soft,}IRQ
- * time, a more accurate solution would be to update the irq_time using
- * the current rq->clock timestamp, except that would require using
- * atomic ops.
- */
- if (irq_delta > delta)
- irq_delta = delta;
-
- rq->prev_irq_time += irq_delta;
- delta -= irq_delta;
- delayacct_irq(rq->curr, irq_delta);
- }
-#endif
-#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
- if (static_key_false((&paravirt_steal_rq_enabled))) {
- u64 prev_steal;
-
- steal = prev_steal = paravirt_steal_clock(cpu_of(rq));
- steal -= rq->prev_steal_time_rq;
-
- if (unlikely(steal > delta))
- steal = delta;
-
- rq->prev_steal_time_rq = prev_steal;
- delta -= steal;
- }
-#endif
-
- rq->clock_task += delta;
-
-#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
- if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
- update_irq_load_avg(rq, irq_delta + steal);
-#endif
- update_rq_clock_pelt(rq, delta);
-}
-
-void update_rq_clock(struct rq *rq)
-{
- s64 delta;
- u64 clock;
-
- lockdep_assert_rq_held(rq);
-
- if (rq->clock_update_flags & RQCF_ACT_SKIP)
- return;
-
- if (sched_feat(WARN_DOUBLE_CLOCK))
- WARN_ON_ONCE(rq->clock_update_flags & RQCF_UPDATED);
- rq->clock_update_flags |= RQCF_UPDATED;
-
- clock = sched_clock_cpu(cpu_of(rq));
- scx_rq_clock_update(rq, clock);
-
- delta = clock - rq->clock;
- if (delta < 0)
- return;
- rq->clock += delta;
-
- update_rq_clock_task(rq, delta);
-}
-
#ifdef CONFIG_SCHED_HRTICK
/*
* Use HR-timers to deliver accurate preemption points.
diff --git a/kernel/sched/fork_sources.json b/kernel/sched/fork_sources.json
index 97bd37628f6ac381c6b0868590f727609f33b9fb..f3351762134879e7810b08569af7033625432a1e 100644
--- a/kernel/sched/fork_sources.json
+++ b/kernel/sched/fork_sources.json
@@ -118,6 +118,12 @@
"sha256": "b36ec6b58c10d3a30ab5e7ed889a9cf708c7ecfc392e33f2b9ff4e6b2d6722bc",
"functions": []
},
+ {
+ "path": "kernel/sched/clock.c",
+ "git_blob": "2ae4fbf13431fc83334832cf5666f951c760af95",
+ "sha256": "efea491eb3617f77cd57349b7a3902678fdc848911ec8372631e3a4cf6315de7",
+ "functions": []
+ },
{
"path": "kernel/sched/sched.h",
"git_blob": "d25defb116caae1d3f01682a8af5a5e47d0b297c",
@@ -138,6 +144,14 @@
"path": "kernel/sched/fork_metadata.c",
"sha256": "75d4b690e902c000033939baac67f12867049deca430db4c4a85b0b200bdb184"
},
+ {
+ "path": "kernel/sched/clock.c",
+ "sha256": "ecd583db71e064af010dbcb247090794cd1e2703c28e8657681cf8188fb4a164"
+ },
+ {
+ "path": "kernel/sched/rq_clock.c",
+ "sha256": "5332d45e0d19f0abf8decaafa74c44d4916a676c40a14411f7c1aff54991cdb7"
+ },
{
"path": "kernel/sched/rq_lock.c",
"sha256": "e187b63ad8a7b3831497bd3e955813bb51268be727a6143d72f856c9cf46df16"
@@ -155,6 +169,7 @@
"set_load_weight is specialized for update_load=false and named stela_linux_sched_load_weight; no CFS runqueue reweighting is implemented.",
"Task-local weights, slice defaults, and average initialization are built separately from the original asymmetric-capacity helper. The native 64-bit non-group branch uses the matching original sched.h scaling and entity rules.",
"set_task_max_allowed_capacity is named stela_linux_sched_fork_fair_capacity; it reads the original capacity/static-key definitions moved from topology.c into capacity_state.c. The native and regular scheduler paths share the original declarations and static-key query in fork_capacity.h.",
+ "Both profiles use the original runqueue clock updates and scheduler feature defaults in rq_clock.c. The native build uses the original stable clock startup and supplies its retained monotonic source in place of the weak jiffy source.",
"The original runqueue lock bodies are built by both scheduler profiles through rq_lock.c. A separate scheduler owner must provide runqueues.",
"The original sched_rr_timeslice definition is shared by the regular and native scheduler builds through rt_timeslice.c.",
"Original sched_fork class and placement decisions require the native admit/select/place operations declared in fork_backend.h.",
diff --git a/kernel/sched/rq_clock.c b/kernel/sched/rq_clock.c
new file mode 100644
index 0000000000000000000000000000000000000000..e7e8c6bb5ee715d025935463f1a95ef31755a8ff
--- /dev/null
+++ b/kernel/sched/rq_clock.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Runqueue clock and scheduler feature defaults from kernel/sched/core.c.
+ * Copyright (C) 1991-2002 Linus Torvalds
+ * Copyright (C) 1998-2024 Ingo Molnar, Red Hat
+ */
+#include <linux/sched/clock.h>
+#include "sched.h"
+#include "pelt.h"
+
+/*
+ * Debugging: various feature bits
+ *
+ * If SCHED_DEBUG is disabled, each compilation unit has its own copy of
+ * sysctl_sched_features, defined in sched.h, to allow constants propagation
+ * at compile time and compiler optimization based on features default.
+ */
+#define SCHED_FEAT(name, enabled) \
+ (1UL << __SCHED_FEAT_##name) * enabled |
+__read_mostly unsigned int sysctl_sched_features =
+#include "features.h"
+ 0;
+#undef SCHED_FEAT
+
+/*
+ * RQ-clock updating methods:
+ */
+
+/* Use CONFIG_PARAVIRT as this will avoid more #ifdef in arch code. */
+#ifdef CONFIG_PARAVIRT
+struct static_key paravirt_steal_rq_enabled;
+#endif
+
+static void update_rq_clock_task(struct rq *rq, s64 delta)
+{
+/*
+ * In theory, the compile should just see 0 here, and optimize out the call
+ * to sched_rt_avg_update. But I don't trust it...
+ */
+ s64 __maybe_unused steal = 0, irq_delta = 0;
+
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
+ if (irqtime_enabled()) {
+ irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
+
+ /*
+ * Since irq_time is only updated on {soft,}irq_exit, we might run into
+ * this case when a previous update_rq_clock() happened inside a
+ * {soft,}IRQ region.
+ *
+ * When this happens, we stop ->clock_task and only update the
+ * prev_irq_time stamp to account for the part that fit, so that a next
+ * update will consume the rest. This ensures ->clock_task is
+ * monotonic.
+ *
+ * It does however cause some slight miss-attribution of {soft,}IRQ
+ * time, a more accurate solution would be to update the irq_time using
+ * the current rq->clock timestamp, except that would require using
+ * atomic ops.
+ */
+ if (irq_delta > delta)
+ irq_delta = delta;
+
+ rq->prev_irq_time += irq_delta;
+ delta -= irq_delta;
+ delayacct_irq(rq->curr, irq_delta);
+ }
+#endif
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+ if (static_key_false((&paravirt_steal_rq_enabled))) {
+ u64 prev_steal;
+
+ steal = prev_steal = paravirt_steal_clock(cpu_of(rq));
+ steal -= rq->prev_steal_time_rq;
+
+ if (unlikely(steal > delta))
+ steal = delta;
+
+ rq->prev_steal_time_rq = prev_steal;
+ delta -= steal;
+ }
+#endif
+
+ rq->clock_task += delta;
+
+#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
+ if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
+ update_irq_load_avg(rq, irq_delta + steal);
+#endif
+ update_rq_clock_pelt(rq, delta);
+}
+
+void update_rq_clock(struct rq *rq)
+{
+ s64 delta;
+ u64 clock;
+
+ lockdep_assert_rq_held(rq);
+
+ if (rq->clock_update_flags & RQCF_ACT_SKIP)
+ return;
+
+ if (sched_feat(WARN_DOUBLE_CLOCK))
+ WARN_ON_ONCE(rq->clock_update_flags & RQCF_UPDATED);
+ rq->clock_update_flags |= RQCF_UPDATED;
+
+ clock = sched_clock_cpu(cpu_of(rq));
+ scx_rq_clock_update(rq, clock);
+
+ delta = clock - rq->clock;
+ if (delta < 0)
+ return;
+ rq->clock += delta;
+
+ update_rq_clock_task(rq, delta);
+}