Skip to contentSkip to conversation

sched_ext: don't deliver duplicate ops.cgroup_set_idle() for same value

PATCH Superseded Tao Cui 3 participants 4 messages Sep 1

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 sched_ext: don't deliver duplicate ops.cgroup_set_idle() for same value

From: Tao Cui <[email protected]>

ops.cgroup_set_idle() is documented to be invoked when a cgroup transitions between idle and non-idle states, and scx_group_set_weight() already skips value-preserving writes. scx_group_set_idle() delivers every write unconditionally, so rewriting an already-correct cpu.idle value feeds the BPF scheduler a transition callback each time, which toggle- or accounting-based schedulers miscount. Mirror the weight guard and only deliver on an actual change.

Verified with a probe scheduler printing each callback: rewriting cpu.idle=1 twice on an already-idle cgroup delivered two callbacks before and none after.

Fixes 347ed2d566da ("sched/ext: Implement cgroup_set_idle() callback")
---
 kernel/sched/ext/ext.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +2 -1
index 8041c87a3562..8b3625107b72 100644
@@ -4933,7 +4933,8 @@ void scx_group_set_idle(struct task_group *tg, bool idle)
 	percpu_down_read(&scx_cgroup_ops_rwsem);
 	sch = scx_tg_knob_sched(tg);
 
-	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle))
+	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle) &&
+	    tg->scx.sched_idle != idle)
 		SCX_CALL_OP(sch, cgroup_set_idle, NULL, tg_cgrp(tg), idle);
 
 	/* Update the task group's idle state */
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch references a non-existent struct member `tg->scx.sched_idle`, which causes a build…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch references a non-existent struct member tg->scx.sched_idle, which causes a build failure. --

commit b3dc4baced455427fac828517318398556404ffa
Author: Tao Cui <[email protected]>

sched_ext: don't deliver duplicate ops.cgroup_set_idle() for same value

This commit modifies scx_group_set_idle() to prevent delivering duplicate cgroup transition callbacks to the BPF scheduler. It checks if the new idle value differs from the existing value before invoking the callback.

kernel/sched/ext/ext.c void scx_group_set_idle(struct task_group *tg, bool idle) · line 4934

Quoted 11 lines
kernel/sched/ext/ext.c +2 -1
index c539d15cda637..c72d9780830b8 100644
@@ -4930,7 +4930,8 @@ void scx_group_set_idle(struct task_group *tg, bool idle)
 	percpu_down_read(&scx_cgroup_ops_rwsem);
 	sch = scx_tg_knob_sched(tg);
 
-	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle))
+	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle) &&
+	    tg->scx.sched_idle != idle)

[Severity: Low]
Does this code build successfully when CONFIG_EXT_GROUP_SCHED is enabled? It appears tg->scx.sched_idle is referencing a non-existent member in struct scx_task_group.

Looking at include/linux/sched/ext.h, the scx_task_group struct defines the field simply as idle:

struct scx_task_group {
    ...
    bool idle;
    ...
};

Should this condition check tg->scx.idle != idle instead?

Quoted 3 lines
kernel/sched/ext/ext.c +0 -0
 		SCX_CALL_OP(sch, cgroup_set_idle, NULL, tg_cgrp(tg), idle);
 
 	/* Update the task group's idle state */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Tao Cui

Hi Tao,

Quoted 10 lines · Tao Cui

From: Tao Cui <[email protected]>

ops.cgroup_set_idle() is documented to be invoked when a cgroup transitions between idle and non-idle states, and scx_group_set_weight() already skips value-preserving writes. scx_group_set_idle() delivers every write unconditionally, so rewriting an already-correct cpu.idle value feeds the BPF scheduler a transition callback each time, which toggle- or accounting-based schedulers miscount. Mirror the weight guard and only deliver on an actual change.

Quoted 6 lines

Verified with a probe scheduler printing each callback: rewriting cpu.idle=1 twice on an already-idle cgroup delivered two callbacks before and none after.

Fixes 347ed2d566da ("sched/ext: Implement cgroup_set_idle() callback")

This link seems broken, I think the right one is:

Quoted 1 line

Other than that looks good to me.

Thanks,
-Andrea

Quoted 21 lines
---
 kernel/sched/ext/ext.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +2 -1
index 8041c87a3562..8b3625107b72 100644
@@ -4933,7 +4933,8 @@ void scx_group_set_idle(struct task_group *tg, bool idle)
 	percpu_down_read(&scx_cgroup_ops_rwsem);
 	sch = scx_tg_knob_sched(tg);
 
-	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle))
+	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle) &&
+	    tg->scx.sched_idle != idle)
 		SCX_CALL_OP(sch, cgroup_set_idle, NULL, tg_cgrp(tg), idle);
 
 	/* Update the task group's idle state */
Signature · 1 line
2.43.0
author
#3 lore raw

Hi Andrea,

Quoted 17 lines · Andrea Righi

Hi Tao,

Quoted 14 lines · Tao Cui

From: Tao Cui <[email protected]>

ops.cgroup_set_idle() is documented to be invoked when a cgroup transitions between idle and non-idle states, and scx_group_set_weight() already skips value-preserving writes. scx_group_set_idle() delivers every write unconditionally, so rewriting an already-correct cpu.idle value feeds the BPF scheduler a transition callback each time, which toggle- or accounting-based schedulers miscount. Mirror the weight guard and only deliver on an actual change.

Verified with a probe scheduler printing each callback: rewriting cpu.idle=1 twice on an already-idle cgroup delivered two callbacks before and none after.

Quoted 7 lines
Quoted 2 lines
Fixes 347ed2d566da ("sched/ext: Implement cgroup_set_idle() callback")

This link seems broken, I think the right one is:

Thanks! Somehow my vim seems to have eaten the @linux.dev part of the Message-ID. I'll fix the Link tag in the next revision.

Thanks for the review!

Best,
Tao

Quoted 30 lines
Quoted 1 line

Other than that looks good to me.

Thanks,
-Andrea

Quoted 21 lines
---
 kernel/sched/ext/ext.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +2 -1
index 8041c87a3562..8b3625107b72 100644
@@ -4933,7 +4933,8 @@ void scx_group_set_idle(struct task_group *tg, bool idle)
 	percpu_down_read(&scx_cgroup_ops_rwsem);
 	sch = scx_tg_knob_sched(tg);
 
-	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle))
+	if (scx_cgroup_enabled && sch && SCX_HAS_OP(sch, cgroup_set_idle) &&
+	    tg->scx.sched_idle != idle)
 		SCX_CALL_OP(sch, cgroup_set_idle, NULL, tg_cgrp(tg), idle);
 
 	/* Update the task group's idle state */
Signature · 1 line
2.43.0