From 22c362701469ea8c222444455286c6e201a8c268 Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Mon, 8 Jan 2024 11:51:31 -1000 Subject: [PATCH] scx: Make scx_task_state handling more idiomatic Functionally equivalent. Just a bit more idiomatic. Signed-off-by: Tejun Heo --- include/linux/sched/ext.h | 34 ++++++++++++++-------------------- kernel/sched/ext.c | 34 +++++++++++++++------------------- 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h index 90383453d65d16..5f457194e3a8c8 100644 --- a/include/linux/sched/ext.h +++ b/include/linux/sched/ext.h @@ -613,15 +613,24 @@ enum scx_ent_flags { SCX_TASK_QUEUED = 1 << 0, /* on ext runqueue */ SCX_TASK_BAL_KEEP = 1 << 1, /* balance decided to keep current */ SCX_TASK_DDSP_PRIQ = 1 << 2, /* task should be enqueued on priq when directly dispatched */ - SCX_TASK_STATE_0 = 1 << 3, /* first bit encoding the task's current state */ - SCX_TASK_STATE_1 = 1 << 4, /* second bit encoding the task's current state */ + SCX_TASK_RESET_RUNNABLE_AT = 1 << 3, /* runnable_at should be reset */ + SCX_TASK_DEQD_FOR_SLEEP = 1 << 4, /* last dequeue was for SLEEP */ - SCX_TASK_RESET_RUNNABLE_AT = 1 << 16, /* runnable_at should be reset */ - SCX_TASK_DEQD_FOR_SLEEP = 1 << 17, /* last dequeue was for SLEEP */ + SCX_TASK_STATE_SHIFT = 8, /* bit 8 and 9 are used to carry scx_task_state */ + SCX_TASK_STATE_BITS = 2, + SCX_TASK_STATE_MASK = ((1 << SCX_TASK_STATE_BITS) - 1) << SCX_TASK_STATE_SHIFT, SCX_TASK_CURSOR = 1 << 31, /* iteration cursor, not a task */ +}; + +/* scx_entity.flags & SCX_TASK_STATE_MASK */ +enum scx_task_state { + SCX_TASK_NONE, /* ops.init_task() not called yet */ + SCX_TASK_INIT, /* ops.init_task() succeeded, but task can be cancelled */ + SCX_TASK_READY, /* fully initialized, but not in sched_ext */ + SCX_TASK_ENABLED, /* fully initialized and in sched_ext */ - SCX_TASK_STATE_MASK = SCX_TASK_STATE_0 | SCX_TASK_STATE_1, + SCX_TASK_NR_STATES, }; /* scx_entity.dsq_flags */ @@ -655,21 +664,6 @@ enum scx_kf_mask { __SCX_KF_TERMINAL = SCX_KF_ENQUEUE | SCX_KF_SELECT_CPU | SCX_KF_REST, }; -/* scx_entity.task_state */ -enum scx_task_state { - /* ops.prep_enable() has not yet been called on task */ - SCX_TASK_NONE, - - /* ops.prep_enable() succeeded on task, but it still be cancelled */ - SCX_TASK_INIT, - - /* Task is fully initialized, but not being scheduled in sched_ext */ - SCX_TASK_READY, - - /* Task is fully initialized and is being scheduled in sched_ext */ - SCX_TASK_ENABLED, -}; - /* * The following is embedded in task_struct and contains all fields necessary * for a task to be scheduled by SCX. diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index 53024ebd180941..2a552efeec6f31 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -2313,41 +2313,34 @@ static struct cgroup *tg_cgrp(struct task_group *tg) static enum scx_task_state scx_get_task_state(const struct task_struct *p) { - int state = p->scx.flags & SCX_TASK_STATE_MASK; - - switch (state) { - case SCX_TASK_STATE_0 | SCX_TASK_STATE_1: - return SCX_TASK_ENABLED; - case SCX_TASK_STATE_1: - return SCX_TASK_READY; - case SCX_TASK_STATE_0: - return SCX_TASK_INIT; - default: - return SCX_TASK_NONE; - } + return (p->scx.flags & SCX_TASK_STATE_MASK) >> SCX_TASK_STATE_SHIFT; } static void scx_set_task_state(struct task_struct *p, enum scx_task_state state) { enum scx_task_state prev_state = scx_get_task_state(p); - p->scx.flags &= ~SCX_TASK_STATE_MASK; + BUILD_BUG_ON(SCX_TASK_NR_STATES > (1 << SCX_TASK_STATE_BITS)); + switch (state) { case SCX_TASK_NONE: - return; + break; case SCX_TASK_INIT: WARN_ON_ONCE(prev_state != SCX_TASK_NONE); - p->scx.flags |= SCX_TASK_STATE_0; - return; + break; case SCX_TASK_READY: WARN_ON_ONCE(prev_state == SCX_TASK_NONE); - p->scx.flags |= SCX_TASK_STATE_1; - return; + break; case SCX_TASK_ENABLED: WARN_ON_ONCE(prev_state != SCX_TASK_READY); - p->scx.flags |= (SCX_TASK_STATE_0 | SCX_TASK_STATE_1); + break; + default: + WARN_ON_ONCE(true); return; } + + p->scx.flags &= ~SCX_TASK_STATE_MASK; + p->scx.flags |= state << SCX_TASK_STATE_SHIFT; } static int scx_ops_init_task(struct task_struct *p, struct task_group *tg) @@ -2447,6 +2440,9 @@ static void scx_ops_exit_task(struct task_struct *p) case SCX_TASK_ENABLED: scx_ops_disable_task(p); break; + default: + WARN_ON_ONCE(true); + return; } if (SCX_HAS_OP(exit_task))