[tip: sched/core] sched/core: Add assertions to QUEUE_CLASS

tip-bot2 for Peter Zijlstra
2025-12-14T07:46:46Z

The following commit has been merged into the sched/core branch of tip:

Commit-ID: 47efe2ddccb1f285a02bfcf1e079f49bf7a9ccb3
Gitweb: https://git.kernel.org/tip/47efe2ddccb1f285a02bfcf1e079f49bf7a9ccb3
Author: Peter Zijlstra <peterz@infradead.org>
AuthorDate: Thu, 30 Oct 2025 12:56:34 +01:00
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Sun, 14 Dec 2025 08:25:02 +01:00

sched/core: Add assertions to QUEUE_CLASS

Add some checks to the sched_change pattern to validate assumptions
around changing classes.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://patch.msgid.link/20251127154725.771691954@infradead.org
---
kernel/sched/core.c | 13 +++++++++++++
kernel/sched/sched.h | 1 +
2 files changed, 14 insertions(+)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 41ba0be..4479f7d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10782,6 +10782,7 @@ struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned int

*ctx = (struct sched_change_ctx){
.p = p,
+ .class = p->sched_class,
.flags = flags,
.queued = task_on_rq_queued(p),
.running = task_current_donor(rq, p),
@@ -10812,6 +10813,11 @@ void sched_change_end(struct sched_change_ctx *ctx)

lockdep_assert_rq_held(rq);

+ /*
+ * Changing class without *QUEUE_CLASS is bad.
+ */
+ WARN_ON_ONCE(p->sched_class != ctx->class && !(ctx->flags & ENQUEUE_CLASS));
+
if ((ctx->flags & ENQUEUE_CLASS) && p->sched_class->switching_to)
p->sched_class->switching_to(rq, p);

@@ -10823,6 +10829,13 @@ void sched_change_end(struct sched_change_ctx *ctx)
if (ctx->flags & ENQUEUE_CLASS) {
if (p->sched_class->switched_to)
p->sched_class->switched_to(rq, p);
+
+ /*
+ * If this was a degradation in class someone should have set
+ * need_resched by now.
+ */
+ WARN_ON_ONCE(sched_class_above(ctx->class, p->sched_class) &&
+ !test_tsk_need_resched(p));
} else {
p->sched_class->prio_changed(rq, p, ctx->prio);
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 67cff7d..a40582d 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3968,6 +3968,7 @@ extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
struct sched_change_ctx {
u64 prio;
struct task_struct *p;
+ const struct sched_class *class;
int flags;
bool queued;
bool running;