Skip to contentSkip to conversation

sched_ext: idle: small CPU iteration refactoring

PATCH Applied Andrea Righi 3 participants 4 messages Jan 4, 2025 – Jan 6, 2025 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: idle: small CPU iteration refactoring

Replace the loop to check if all SMT CPUs are idle with cpumask_subset(). This simplifies the code and slightly improves efficiency, while preserving the original behavior.

Note that idle_masks.smt handling remains racy, which is acceptable as it serves as an optimization and is self-correcting.

---
 kernel/sched/ext.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/ext.c +2 -4
index 926579624c41..0ce116e0f67c 100644
@@ -3671,10 +3671,8 @@ void __scx_update_idle(struct rq *rq, bool idle)
 			 * idle_masks.smt handling is racy but that's fine as
 			 * it's only for optimization and self-correcting.
 			 */
-			for_each_cpu(cpu, smt) {
-				if (!cpumask_test_cpu(cpu, idle_masks.cpu))
-					return;
-			}
+			if (!cpumask_subset(smt, idle_masks.cpu))
+				return;
 			cpumask_or(idle_masks.smt, idle_masks.smt, smt);
 		} else {
 			cpumask_andnot(idle_masks.smt, idle_masks.smt, smt);
Signature · 1 line
2.47.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Andrea Righi

loop to check if all SMT CPUs are idle with cpumask_subset(). This simplifies the code and slightly improves efficiency, while preserving the original behavior.

In my experience, replacing iterator with a dedicated function makes you 400-500 times faster. So 'slightly' sounds like an understatement here.

Quoted 5 lines

Note that idle_masks.smt handling remains racy, which is acceptable as it serves as an optimization and is self-correcting.

Quoted 25 lines
---
 kernel/sched/ext.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/ext.c +2 -4
index 926579624c41..0ce116e0f67c 100644
@@ -3671,10 +3671,8 @@ void __scx_update_idle(struct rq *rq, bool idle)
 			 * idle_masks.smt handling is racy but that's fine as
 			 * it's only for optimization and self-correcting.
 			 */
-			for_each_cpu(cpu, smt) {
-				if (!cpumask_test_cpu(cpu, idle_masks.cpu))
-					return;
-			}
+			if (!cpumask_subset(smt, idle_masks.cpu))
+				return;
 			cpumask_or(idle_masks.smt, idle_masks.smt, smt);
 		} else {
 			cpumask_andnot(idle_masks.smt, idle_masks.smt, smt);
Signature · 3 lines
2.47.1

From yury  
#2 lore raw

Hi Yury,

Quoted 8 lines · Yury Norov
Quoted 3 lines · Andrea Righi

loop to check if all SMT CPUs are idle with cpumask_subset(). This simplifies the code and slightly improves efficiency, while preserving the original behavior.

In my experience, replacing iterator with a dedicated function makes you 400-500 times faster. So 'slightly' sounds like an understatement here.

There's also the fact that the smt mask typically will have just 2 bits set (maybe 4 in some high end servers), so I wasn't expecting big improvements. But in any case, it's definitely a good change to do.

Thanks!
-Andrea

Quoted 32 lines
Quoted 6 lines

Note that idle_masks.smt handling remains racy, which is acceptable as it serves as an optimization and is self-correcting.

Quoted 23 lines
---
 kernel/sched/ext.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/ext.c +2 -4
index 926579624c41..0ce116e0f67c 100644
@@ -3671,10 +3671,8 @@ void __scx_update_idle(struct rq *rq, bool idle)
 			 * idle_masks.smt handling is racy but that's fine as
 			 * it's only for optimization and self-correcting.
 			 */
-			for_each_cpu(cpu, smt) {
-				if (!cpumask_test_cpu(cpu, idle_masks.cpu))
-					return;
-			}
+			if (!cpumask_subset(smt, idle_masks.cpu))
+				return;
 			cpumask_or(idle_masks.smt, idle_masks.smt, smt);
 		} else {
 			cpumask_andnot(idle_masks.smt, idle_masks.smt, smt);
Signature · 1 line
2.47.1
Applied
#3 lore raw
In reply to Andrea Righi
Quoted 9 lines · Andrea Righi

Replace the loop to check if all SMT CPUs are idle with cpumask_subset(). This simplifies the code and slightly improves efficiency, while preserving the original behavior.

Note that idle_masks.smt handling remains racy, which is acceptable as it serves as an optimization and is self-correcting.

Applied to sched_ext/for-6.14.

Thanks.

Signature · 1 line
tejun