hulk inclusion category: cleanup bugzilla: https://atomgit.com/openeuler/kernel/issues/8423 -------------------------------- This commit refactors the xsched_entity (xse) initialization logic to be scheduling-class-aware, improving modularity and extensibility for future scheduler classes. - Introduce a new helper function find_xsched_class() that determines the appropriate scheduling class (e.g., CFS, RT) based on entity or task properties. - Replace monolithic initialization code with a dispatch mechanism: after identifying the scheduling class, invoke its xse_init() callback to initialize class-specific fields in the xsched_entity. This refactor enhances code clarity, reduces duplication, and establishes a clean interface for supporting multiple scheduling policies under the unified xsched_entity model. Signed-off-by: Liu Kai <liukai284@huawei.com> --- include/linux/xsched.h | 6 ++-- kernel/xsched/cfs.c | 1 + kernel/xsched/cgroup.c | 10 +++---- kernel/xsched/core.c | 61 ++++++++++++++++++++--------------------- kernel/xsched/rt.c | 6 ++-- kernel/xsched/vstream.c | 2 +- 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/include/linux/xsched.h b/include/linux/xsched.h index 08f0c0a74555..e48e6d404b1c 100644 --- a/include/linux/xsched.h +++ b/include/linux/xsched.h @@ -54,9 +54,6 @@ extern struct xsched_cu *xsched_cu_mgr[XSCHED_NR_CUS]; extern struct xsched_class rt_xsched_class; extern struct xsched_class fair_xsched_class; -#define xsched_first_class \ - list_first_entry(&(xsched_class_list), struct xsched_class, node) - #define for_each_xsched_class(class) \ list_for_each_entry((class), &(xsched_class_list), node) @@ -455,7 +452,7 @@ static inline void xsched_init_vsm(struct vstream_metadata *vsm, int xsched_xcu_init(struct xsched_cu *xcu, struct xcu_group *group, int xcu_id); int xsched_schedule(void *input_xcu); -int xsched_init_entity(struct xsched_context *ctx, struct vstream_info *vs); +int init_xsched_entity(struct xsched_context *ctx, struct vstream_info *vs); int ctx_bind_to_xcu(vstream_info_t *vstream_info, struct xsched_context *ctx); int xsched_vsm_add_tail(struct vstream_info *vs, vstream_args_t *arg); struct vstream_metadata *xsched_vsm_fetch_first(struct vstream_info *vs); @@ -463,6 +460,7 @@ void xsched_rt_prio_set(pid_t tgid, unsigned int prio); void enqueue_ctx(struct xsched_entity *xse, struct xsched_cu *xcu); void dequeue_ctx(struct xsched_entity *xse); int delete_ctx(struct xsched_context *ctx); +const struct xsched_class *find_xsched_class(int class_id); #ifdef CONFIG_CGROUP_XCU /* Xsched group manage functions */ diff --git a/kernel/xsched/cfs.c b/kernel/xsched/cfs.c index 14397d0e48f4..d8298f574387 100644 --- a/kernel/xsched/cfs.c +++ b/kernel/xsched/cfs.c @@ -240,6 +240,7 @@ void rq_init_fair(struct xsched_rq *xrq) void xse_init_fair(struct xsched_entity *xse) { + xse->class = &fair_xsched_class; xse->cfs.weight = XSCHED_CFS_WEIGHT_DFLT; } diff --git a/kernel/xsched/cgroup.c b/kernel/xsched/cgroup.c index fa6523cc451f..9e3659dc08f4 100644 --- a/kernel/xsched/cgroup.c +++ b/kernel/xsched/cgroup.c @@ -103,7 +103,7 @@ void xcu_cfs_root_cg_init(struct xsched_cu *xcu) root_xcg->perxcu_priv[id].self = root_xcg; root_xcg->perxcu_priv[id].cfs_rq = &xcu->xrq.cfs; root_xcg->perxcu_priv[id].xse.is_group = true; - root_xcg->perxcu_priv[id].xse.cfs.weight = XSCHED_CFS_WEIGHT_DFLT; + fair_xsched_class.xse_init(&root_xcg->perxcu_priv[id].xse); } static void xcg_perxcu_cfs_rq_deinit(struct xsched_group *xcg, int max_id) @@ -151,13 +151,11 @@ static int xcu_cfs_cg_init(struct xsched_group *xcg, xcg->perxcu_priv[id].cfs_rq->ctx_timeline = RB_ROOT_CACHED; xcg->perxcu_priv[id].cfs_rq->min_xruntime = XSCHED_TIME_INF; - xcg->perxcu_priv[id].xse.is_group = true; - xcg->perxcu_priv[id].xse.xcu = xcu; - xcg->perxcu_priv[id].xse.class = &fair_xsched_class; - /* Put new empty groups to the right in parent's rbtree: */ - xcg->perxcu_priv[id].xse.cfs.weight = XSCHED_CFS_WEIGHT_DFLT; + fair_xsched_class.xse_init(&xcg->perxcu_priv[id].xse); + xcg->perxcu_priv[id].xse.is_group = true; xcg->perxcu_priv[id].xse.parent_grp = parent_xg; + xcg->perxcu_priv[id].xse.xcu = xcu; } xcg->shares_cfg = XSCHED_CFG_SHARE_DFLT; diff --git a/kernel/xsched/core.c b/kernel/xsched/core.c index 631920ae11d3..5c2fd2f7dbd6 100644 --- a/kernel/xsched/core.c +++ b/kernel/xsched/core.c @@ -184,25 +184,19 @@ int delete_ctx(struct xsched_context *ctx) return 0; } -int xsched_xse_set_class(struct xsched_entity *xse) +const struct xsched_class *find_xsched_class(int class_id) { - struct xsched_class *sched = xsched_first_class; + struct xsched_class *sched; - if (!sched) { - XSCHED_ERR("No xsched classes registered @ %s\n", __func__); - return -EINVAL; - } + if (class_id >= XSCHED_TYPE_NUM) + return NULL; -#ifdef CONFIG_CGROUP_XCU - xsched_group_inherit(current, xse); for_each_xsched_class(sched) { - if (sched->class_id == xse->parent_grp->sched_class) - break; + if (sched->class_id == class_id) + return sched; } -#endif - xse->class = sched; - return 0; + return NULL; } static void submit_kick(struct vstream_metadata *vsm) @@ -463,10 +457,28 @@ int xsched_xcu_init(struct xsched_cu *xcu, struct xcu_group *group, int xcu_id) return 0; } -int xsched_init_entity(struct xsched_context *ctx, struct vstream_info *vs) +int init_xsched_entity(struct xsched_context *ctx, struct vstream_info *vs) { - int err = 0; - struct xsched_entity *xse = &ctx->xse; + int err = 0, class_id = XSCHED_TYPE_DFLT; + struct xsched_entity *xse; + const struct xsched_class *sched; + + if (!ctx || !vs || WARN_ON(vs->xcu == NULL)) + return -EINVAL; + + xse = &ctx->xse; + +#ifdef CONFIG_CGROUP_XCU + xsched_group_inherit(current, xse); + /* inherit the scheduler class from the parent group */ + class_id = xse->parent_grp->sched_class; +#endif + + sched = find_xsched_class(class_id); + if (!sched) + return -ENOENT; + + sched->xse_init(xse); atomic_set(&xse->kicks_pending_cnt, 0); atomic_set(&xse->submitted_one_kick, 0); @@ -483,28 +495,15 @@ int xsched_init_entity(struct xsched_context *ctx, struct vstream_info *vs) XSCHED_ERR( "Couldn't find valid xcu for vstream %u dev_id %u @ %s\n", vs->id, vs->dev_id, __func__); - return -EINVAL; + return err; } xse->ctx = ctx; - - if (vs->xcu == NULL) { - WARN_ON(vs->xcu == NULL); - return -EINVAL; - } - xse->xcu = vs->xcu; - err = xsched_xse_set_class(xse); - if (err) { - XSCHED_ERR("Fail to set xse class @ %s\n", __func__); - return err; - } - xse->class->xse_init(xse); - WRITE_ONCE(xse->on_rq, false); - spin_lock_init(&xse->xse_lock); + return err; } diff --git a/kernel/xsched/rt.c b/kernel/xsched/rt.c index f62500634347..3e39135bd065 100644 --- a/kernel/xsched/rt.c +++ b/kernel/xsched/rt.c @@ -130,8 +130,10 @@ void xse_init_rt(struct xsched_entity *xse) struct task_struct *p; p = find_task_by_vpid(xse->tgid); - xse->rt.prio = p->_resvd->xse_attr.xsched_priority; - XSCHED_DEBUG("Xse init: set priority=%d.\n", xse->rt.prio); + if (p) + xse->rt.prio = p->_resvd->xse_attr.xsched_priority; + + xse->class = &rt_xsched_class; xse->rt.timeslice = XSCHED_RT_TIMESLICE; INIT_LIST_HEAD(&xse->rt.list_node); } diff --git a/kernel/xsched/vstream.c b/kernel/xsched/vstream.c index 3ea6fcb97530..80986e04d7b0 100644 --- a/kernel/xsched/vstream.c +++ b/kernel/xsched/vstream.c @@ -229,7 +229,7 @@ static int alloc_ctx_from_vstream(struct vstream_info *vstream_info, init_xsched_ctx(*ctx, vstream_info); - ret = xsched_init_entity(*ctx, vstream_info); + ret = init_xsched_entity(*ctx, vstream_info); if (ret) { XSCHED_ERR("Fail to initialize XSE for context @ %s\n", __func__); -- 2.34.1