History

sched_ext: Make scx_enable() take scx_enable_cmd

Pass struct scx_enable_cmd to scx_enable() rather than unpacking @ops at every call site and re-packing into a fresh cmd inside. bpf_scx_reg() now builds the cmd on its stack and hands it in; scx_enable() just wires up the kthread work and waits. Relocate struct scx_enable_cmd above scx_alloc_and_add_sched() so upcoming patches that also want the cmd can see it. No behavior change.

Tejun Heo committed 6 months ago

Commit c97ce721 parent 1e315c41 file · +23 −23Browse files

Unified · Split

kernel/sched/ext.c+23−23
@@ -6529,6 +6529,19 @@ static struct scx_sched_pnode *alloc_pnode(struct scx_sched *sch, int node) · 6528 unmodified lines
6529 return pnode;6529 return pnode;
6530}6530}
6531 6531
6532/*
6533 * scx_enable() is offloaded to a dedicated system-wide RT kthread to avoid
6534 * starvation. During the READY -> ENABLED task switching loop, the calling
6535 * thread's sched_class gets switched from fair to ext. As fair has higher
6536 * priority than ext, the calling thread can be indefinitely starved under
6537 * fair-class saturation, leading to a system hang.
6538 */
6539struct scx_enable_cmd {
6540 struct kthread_work work;
6541 struct sched_ext_ops *ops;
6542 int ret;
6543};
6544
6532/*6545/*
6533 * Allocate and initialize a new scx_sched. @cgrp's reference is always6546 * Allocate and initialize a new scx_sched. @cgrp's reference is always
6534 * consumed whether the function succeeds or fails.6547 * consumed whether the function succeeds or fails.
@@ -6771,19 +6784,6 @@ static int validate_ops(struct scx_sched *sch, const struct sched_ext_ops *ops) · 236 unmodified lines
6771 return 0;6784 return 0;
6772}6785}
6773 6786
6774/*
6775 * scx_enable() is offloaded to a dedicated system-wide RT kthread to avoid
6776 * starvation. During the READY -> ENABLED task switching loop, the calling
6777 * thread's sched_class gets switched from fair to ext. As fair has higher
6778 * priority than ext, the calling thread can be indefinitely starved under
6779 * fair-class saturation, leading to a system hang.
6780 */
6781struct scx_enable_cmd {
6782 struct kthread_work work;
6783 struct sched_ext_ops *ops;
6784 int ret;
6785};
6786
6787static void scx_root_enable_workfn(struct kthread_work *work)6787static void scx_root_enable_workfn(struct kthread_work *work)
6788{6788{
6789 struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work);6789 struct scx_enable_cmd *cmd = container_of(work, struct scx_enable_cmd, work);
@@ -7368,11 +7368,10 @@ static s32 __init scx_cgroup_lifetime_notifier_init(void) · 578 unmodified lines
7368core_initcall(scx_cgroup_lifetime_notifier_init);7368core_initcall(scx_cgroup_lifetime_notifier_init);
7369#endif /* CONFIG_EXT_SUB_SCHED */7369#endif /* CONFIG_EXT_SUB_SCHED */
7370 7370
7371static s32 scx_enable(struct sched_ext_ops *ops, struct bpf_link *link)7371static s32 scx_enable(struct scx_enable_cmd *cmd, struct bpf_link *link)
7372{7372{
7373 static struct kthread_worker *helper;7373 static struct kthread_worker *helper;
7374 static DEFINE_MUTEX(helper_mutex);7374 static DEFINE_MUTEX(helper_mutex);
7375 struct scx_enable_cmd cmd;
7376 7375
7377 if (!cpumask_equal(housekeeping_cpumask(HK_TYPE_DOMAIN),7376 if (!cpumask_equal(housekeeping_cpumask(HK_TYPE_DOMAIN),
7378 cpu_possible_mask)) {7377 cpu_possible_mask)) {
@@ -7396,16 +7395,15 @@ static s32 scx_enable(struct sched_ext_ops *ops, struct bpf_link *link) · 17 unmodified lines
7396 }7395 }
7397 7396
7398#ifdef CONFIG_EXT_SUB_SCHED7397#ifdef CONFIG_EXT_SUB_SCHED
7399 if (ops->sub_cgroup_id > 1)7398 if (cmd->ops->sub_cgroup_id > 1)
7400 kthread_init_work(&cmd.work, scx_sub_enable_workfn);7399 kthread_init_work(&cmd->work, scx_sub_enable_workfn);
7401 else7400 else
7402#endif /* CONFIG_EXT_SUB_SCHED */7401#endif /* CONFIG_EXT_SUB_SCHED */
7403 kthread_init_work(&cmd.work, scx_root_enable_workfn);7402 kthread_init_work(&cmd->work, scx_root_enable_workfn);
7404 cmd.ops = ops;
7405 7403
7406 kthread_queue_work(READ_ONCE(helper), &cmd.work);7404 kthread_queue_work(READ_ONCE(helper), &cmd->work);
7407 kthread_flush_work(&cmd.work);7405 kthread_flush_work(&cmd->work);
7408 return cmd.ret;7406 return cmd->ret;
7409}7407}
7410 7408
7411 7409
@@ -7577,7 +7575,9 @@ static int bpf_scx_check_member(const struct btf_type *t, · 165 unmodified lines
7577 7575
7578static int bpf_scx_reg(void *kdata, struct bpf_link *link)7576static int bpf_scx_reg(void *kdata, struct bpf_link *link)
7579{7577{
7580 return scx_enable(kdata, link);7578 struct scx_enable_cmd cmd = { .ops = kdata };
7579
7580 return scx_enable(&cmd, link);
7581}7581}
7582 7582
7583static void bpf_scx_unreg(void *kdata, struct bpf_link *link)7583static void bpf_scx_unreg(void *kdata, struct bpf_link *link)