sched_ext: idle: Handle migration-disabled tasks in BPF code
Subsystems (2)
Andrea Righi
reviewerPATCH 1/1 sched_ext: idle: Handle migration-disabled tasks in BPF code
When scx_bpf_select_cpu_dfl()/and() kfuncs are invoked outside of ops.select_cpu() we can't rely on @p->migration_disabled to determine if migration is disabled for the task @p.
In fact, migration is always disabled for the current task while running BPF code: __bpf_prog_enter() disables migration and __bpf_prog_exit() re-enables it.
To handle this, when @p->migration_disabled == 1, check whether @p is the current task. If so, migration was not disabled before entering the callback, otherwise migration was disabled.
This ensures correct idle CPU selection in all cases. The behavior of ops.select_cpu() remains unchanged, because this callback is never invoked for the current task and migration-disabled tasks are always excluded.
Example: without this change scx_bpf_select_cpu_and() called from ops.enqueue() always returns -EBUSY; with this change applied, it correctly returns idle CPUs.
--- kernel/sched/ext_idle.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
kernel/sched/ext_idle.c +27 -1
@@ -880,6 +880,32 @@ static bool check_builtin_idle_enabled(void)return false; } +/* +* Determine whether @p is a migration-disabled task in the context of BPF +* code. +* +* We can't simply check whether @p->migration_disabled is set in a +* sched_ext callback, because migration is always disabled for the current +* task while running BPF code. +* +* The prolog (__bpf_prog_enter) and epilog (__bpf_prog_exit) respectively +* disable and re-enable migration. For this reason, the current task +* inside a sched_ext callback is always a migration-disabled task. +* +* Therefore, when @p->migration_disabled == 1, check whether @p is the +* current task or not: if it is, then migration was not disabled before +* entering the callback, otherwise migration was disabled. +* +* Returns true if @p is migration-disabled, false otherwise. +*/ +static bool is_bpf_migration_disabled (const struct task_struct *p) +{ +if (p->migration_disabled ==1 ) +return p != current; +else +return p->migration_disabled; +} +static s32 select_cpu_from_kfunc (struct task_struct *p,s32 prev_cpu,u64 wake_flags,const struct cpumask *allowed,u64 flags) {
@@ -922,7 +948,7 @@ static s32 select_cpu_from_kfunc(struct task_struct *p, s32 prev_cpu, u64 wake_f* selection optimizations and simply check whether the previously * used CPU is idle and within the allowed cpumask. */ -if (p->nr_cpus_allowed ==1 ||is_migration_disabled (p)) { +if (p->nr_cpus_allowed ==1 ||is_bpf_migration_disabled (p)) {if (cpumask_test_cpu (prev_cpu, allowed ?: p->cpus_ptr) &&scx_idle_test_and_clear_cpu (prev_cpu)) cpu = prev_cpu;
Signature · 1 line
2.51.0
This is a nice catch. Looks good to me.
Quoted 74 lines · Andrea Righi
When scx_bpf_select_cpu_dfl()/and() kfuncs are invoked outside of
ops.select_cpu()we can't rely on @p->migration_disabled to determine if migration is disabled for the task @p.In fact, migration is always disabled for the current task while running BPF code:
__bpf_prog_enter()disables migration and__bpf_prog_exit()re-enables it.To handle this, when @p->migration_disabled == 1, check whether @p is the current task. If so, migration was not disabled before entering the callback, otherwise migration was disabled.
This ensures correct idle CPU selection in all cases. The behavior of
ops.select_cpu()remains unchanged, because this callback is never invoked for the current task and migration-disabled tasks are always excluded.Example: without this change
scx_bpf_select_cpu_and()called fromops.enqueue()always returns-EBUSY; with this change applied, it correctly returns idle CPUs.Fixes 06efc9fe0b8de ("sched_ext: idle: Handle migration-disabled tasks in idle selection")Cc [email protected] # v6.16+Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/ext_idle.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)kernel/sched/ext_idle.c +27
-1@@ -880,6 +880,32 @@ static bool check_builtin_idle_enabled(void)return false; } +/* +* Determine whether @p is a migration-disabled task in the context of BPF +* code. +* +* We can't simply check whether @p->migration_disabled is set in a +* sched_ext callback, because migration is always disabled for the current +* task while running BPF code. +* +* The prolog (__bpf_prog_enter) and epilog (__bpf_prog_exit) respectively +* disable and re-enable migration. For this reason, the current task +* inside a sched_ext callback is always a migration-disabled task. +* +* Therefore, when @p->migration_disabled == 1, check whether @p is the +* current task or not: if it is, then migration was not disabled before +* entering the callback, otherwise migration was disabled. +* +* Returns true if @p is migration-disabled, false otherwise. +*/ +static bool is_bpf_migration_disabled (const struct task_struct *p) +{ +if (p->migration_disabled ==1 ) +return p != current; +else +return p->migration_disabled; +} +static s32 select_cpu_from_kfunc (struct task_struct *p,s32 prev_cpu,u64 wake_flags,const struct cpumask *allowed,u64 flags) {@@ -922,7 +948,7 @@ static s32 select_cpu_from_kfunc(struct task_struct *p, s32 prev_cpu, u64 wake_f* selection optimizations and simply check whether the previously * used CPU is idle and within the allowed cpumask. */ -if (p->nr_cpus_allowed ==1 ||is_migration_disabled (p)) { +if (p->nr_cpus_allowed ==1 ||is_bpf_migration_disabled (p)) {if (cpumask_test_cpu (prev_cpu, allowed ?: p->cpus_ptr) &&scx_idle_test_and_clear_cpu (prev_cpu)) cpu = prev_cpu;
Quoted 17 lines · Andrea Righi
When scx_bpf_select_cpu_dfl()/and() kfuncs are invoked outside of
ops.select_cpu()we can't rely on @p->migration_disabled to determine if migration is disabled for the task @p.In fact, migration is always disabled for the current task while running BPF code:
__bpf_prog_enter()disables migration and__bpf_prog_exit()re-enables it.To handle this, when @p->migration_disabled == 1, check whether @p is the current task. If so, migration was not disabled before entering the callback, otherwise migration was disabled.
This ensures correct idle CPU selection in all cases. The behavior of
ops.select_cpu()remains unchanged, because this callback is never invoked for the current task and migration-disabled tasks are always excluded.
Quoted 7 lines
Example: without this change
scx_bpf_select_cpu_and()called fromops.enqueue()always returns-EBUSY; with this change applied, it correctly returns idle CPUs.Fixes 06efc9fe0b8de ("sched_ext: idle: Handle migration-disabled tasks in idle selection")Cc [email protected] # v6.16+Signed-off-by Andrea Righi <[email protected]>
Applied to sched_ext/for-6.17-fixes.
Thanks.
Signature · 1 line
tejun