From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:25:49 +0000 Subject: [PATCH] refactor(sched): share original runqueue locking --- kernel/sched/Makefile | 4 +- kernel/sched/core.c | 133 ------------------------------- kernel/sched/fork_sources.json | 5 ++ kernel/sched/rq_lock.c | 139 +++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 134 deletions(-) create mode 100644 kernel/sched/rq_lock.c diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile index 8165228b7af36ba608245419cbfdf06a568bc35a..7463c3b352def51b0d8f02de2fce21eeafafe7bf 100644 --- a/kernel/sched/Makefile +++ b/kernel/sched/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 CONTEXT_ANALYSIS_core.o := y +CONTEXT_ANALYSIS_rq_lock.o := y CONTEXT_ANALYSIS_wait_support.o := y CONTEXT_ANALYSIS_fair.o := y @@ -25,6 +26,7 @@ ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y) # to get a correct value for the wait-channel (WCHAN in ps). --davidm CFLAGS_core.o := $(PROFILING) -fno-omit-frame-pointer CFLAGS_wait_support.o := $(CFLAGS_core.o) +CFLAGS_rq_lock.o := $(CFLAGS_core.o) endif # Branch profiling isn't noinstr-safe @@ -39,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 wait_support.o user_affinity.o +obj-y += core.o rq_lock.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/core.c b/kernel/sched/core.c index e95be0779c7c84e63f942487d1f3f0231621b521..266d0ac8aae12772050e5c538b6c4fbdd2948c1d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -667,139 +667,6 @@ int task_llc(const struct task_struct *p) * */ -void raw_spin_rq_lock_nested(struct rq *rq, int subclass) - __context_unsafe() -{ - raw_spinlock_t *lock; - - /* Matches synchronize_rcu() in __sched_core_enable() */ - preempt_disable(); - if (sched_core_disabled()) { - raw_spin_lock_nested(&rq->__lock, subclass); - /* preempt_count *MUST* be > 1 */ - preempt_enable_no_resched(); - return; - } - - for (;;) { - lock = __rq_lockp(rq); - raw_spin_lock_nested(lock, subclass); - if (likely(lock == __rq_lockp(rq))) { - /* preempt_count *MUST* be > 1 */ - preempt_enable_no_resched(); - return; - } - raw_spin_unlock(lock); - } -} - -bool raw_spin_rq_trylock(struct rq *rq) - __context_unsafe() -{ - raw_spinlock_t *lock; - bool ret; - - /* Matches synchronize_rcu() in __sched_core_enable() */ - preempt_disable(); - if (sched_core_disabled()) { - ret = raw_spin_trylock(&rq->__lock); - preempt_enable(); - return ret; - } - - for (;;) { - lock = __rq_lockp(rq); - ret = raw_spin_trylock(lock); - if (!ret || (likely(lock == __rq_lockp(rq)))) { - preempt_enable(); - return ret; - } - raw_spin_unlock(lock); - } -} - -/* - * double_rq_lock - safely lock two runqueues - */ -void double_rq_lock(struct rq *rq1, struct rq *rq2) -{ - lockdep_assert_irqs_disabled(); - - if (rq_order_less(rq2, rq1)) - swap(rq1, rq2); - - raw_spin_rq_lock(rq1); - if (__rq_lockp(rq1) != __rq_lockp(rq2)) - raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING); - else - __acquire_ctx_lock(__rq_lockp(rq2)); /* fake acquire */ - - double_rq_clock_clear_update(rq1, rq2); -} - -/* - * ___task_rq_lock - lock the rq @p resides on. - */ -struct rq *___task_rq_lock(struct task_struct *p, struct rq_flags *rf) -{ - struct rq *rq; - - lockdep_assert_held(&p->pi_lock); - - for (;;) { - rq = task_rq(p); - raw_spin_rq_lock(rq); - if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { - rq_pin_lock(rq, rf); - return rq; - } - raw_spin_rq_unlock(rq); - - while (unlikely(task_on_rq_migrating(p))) - cpu_relax(); - } -} - -/* - * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. - */ -struct rq *_task_rq_lock(struct task_struct *p, struct rq_flags *rf) -{ - struct rq *rq; - - for (;;) { - raw_spin_lock_irqsave(&p->pi_lock, rf->flags); - rq = task_rq(p); - raw_spin_rq_lock(rq); - /* - * move_queued_task() task_rq_lock() - * - * ACQUIRE (rq->lock) - * [S] ->on_rq = MIGRATING [L] rq = task_rq() - * WMB (__set_task_cpu()) ACQUIRE (rq->lock); - * [S] ->cpu = new_cpu [L] task_rq() - * [L] ->on_rq - * RELEASE (rq->lock) - * - * If we observe the old CPU in task_rq_lock(), the acquire of - * the old rq->lock will fully serialize against the stores. - * - * If we observe the new CPU in task_rq_lock(), the address - * dependency headed by '[L] rq = task_rq()' and the acquire - * will pair with the WMB to ensure we then also see migrating. - */ - if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { - rq_pin_lock(rq, rf); - return rq; - } - raw_spin_rq_unlock(rq); - raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); - - while (unlikely(task_on_rq_migrating(p))) - cpu_relax(); - } -} - /* * RQ-clock updating methods: */ diff --git a/kernel/sched/fork_sources.json b/kernel/sched/fork_sources.json index e62b3c1583d4ddfafd9c54f5b156bf813c334948..97bd37628f6ac381c6b0868590f727609f33b9fb 100644 --- a/kernel/sched/fork_sources.json +++ b/kernel/sched/fork_sources.json @@ -138,6 +138,10 @@ "path": "kernel/sched/fork_metadata.c", "sha256": "75d4b690e902c000033939baac67f12867049deca430db4c4a85b0b200bdb184" }, + { + "path": "kernel/sched/rq_lock.c", + "sha256": "e187b63ad8a7b3831497bd3e955813bb51268be727a6143d72f856c9cf46df16" + }, { "path": "kernel/sched/rt_timeslice.c", "sha256": "381e0d09ddb4c941a6d9b9aa2dea214299a99c07e5a7199aaabd58b79fa40d3d" @@ -151,6 +155,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.", + "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.", "Disabled SCHED_CACHE, UCLAMP_TASK and SCHED_MM_CID branches remain guarded by their original configuration conditions.", diff --git a/kernel/sched/rq_lock.c b/kernel/sched/rq_lock.c new file mode 100644 index 0000000000000000000000000000000000000000..5436b2015c2b16eeb337849772b65560693bae48 --- /dev/null +++ b/kernel/sched/rq_lock.c @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Runqueue locking from kernel/sched/core.c. + * Copyright (C) 1991-2002 Linus Torvalds + * Copyright (C) 1998-2024 Ingo Molnar, Red Hat + */ +#include "sched.h" + +void raw_spin_rq_lock_nested(struct rq *rq, int subclass) + __context_unsafe() +{ + raw_spinlock_t *lock; + + /* Matches synchronize_rcu() in __sched_core_enable() */ + preempt_disable(); + if (sched_core_disabled()) { + raw_spin_lock_nested(&rq->__lock, subclass); + /* preempt_count *MUST* be > 1 */ + preempt_enable_no_resched(); + return; + } + + for (;;) { + lock = __rq_lockp(rq); + raw_spin_lock_nested(lock, subclass); + if (likely(lock == __rq_lockp(rq))) { + /* preempt_count *MUST* be > 1 */ + preempt_enable_no_resched(); + return; + } + raw_spin_unlock(lock); + } +} + +bool raw_spin_rq_trylock(struct rq *rq) + __context_unsafe() +{ + raw_spinlock_t *lock; + bool ret; + + /* Matches synchronize_rcu() in __sched_core_enable() */ + preempt_disable(); + if (sched_core_disabled()) { + ret = raw_spin_trylock(&rq->__lock); + preempt_enable(); + return ret; + } + + for (;;) { + lock = __rq_lockp(rq); + ret = raw_spin_trylock(lock); + if (!ret || (likely(lock == __rq_lockp(rq)))) { + preempt_enable(); + return ret; + } + raw_spin_unlock(lock); + } +} + +/* + * double_rq_lock - safely lock two runqueues + */ +void double_rq_lock(struct rq *rq1, struct rq *rq2) +{ + lockdep_assert_irqs_disabled(); + + if (rq_order_less(rq2, rq1)) + swap(rq1, rq2); + + raw_spin_rq_lock(rq1); + if (__rq_lockp(rq1) != __rq_lockp(rq2)) + raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING); + else + __acquire_ctx_lock(__rq_lockp(rq2)); /* fake acquire */ + + double_rq_clock_clear_update(rq1, rq2); +} + +/* + * ___task_rq_lock - lock the rq @p resides on. + */ +struct rq *___task_rq_lock(struct task_struct *p, struct rq_flags *rf) +{ + struct rq *rq; + + lockdep_assert_held(&p->pi_lock); + + for (;;) { + rq = task_rq(p); + raw_spin_rq_lock(rq); + if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { + rq_pin_lock(rq, rf); + return rq; + } + raw_spin_rq_unlock(rq); + + while (unlikely(task_on_rq_migrating(p))) + cpu_relax(); + } +} + +/* + * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. + */ +struct rq *_task_rq_lock(struct task_struct *p, struct rq_flags *rf) +{ + struct rq *rq; + + for (;;) { + raw_spin_lock_irqsave(&p->pi_lock, rf->flags); + rq = task_rq(p); + raw_spin_rq_lock(rq); + /* + * move_queued_task() task_rq_lock() + * + * ACQUIRE (rq->lock) + * [S] ->on_rq = MIGRATING [L] rq = task_rq() + * WMB (__set_task_cpu()) ACQUIRE (rq->lock); + * [S] ->cpu = new_cpu [L] task_rq() + * [L] ->on_rq + * RELEASE (rq->lock) + * + * If we observe the old CPU in task_rq_lock(), the acquire of + * the old rq->lock will fully serialize against the stores. + * + * If we observe the new CPU in task_rq_lock(), the address + * dependency headed by '[L] rq = task_rq()' and the acquire + * will pair with the WMB to ensure we then also see migrating. + */ + if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { + rq_pin_lock(rq, rf); + return rq; + } + raw_spin_rq_unlock(rq); + raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); + + while (unlikely(task_on_rq_migrating(p))) + cpu_relax(); + } +}