Skip to contentSkip to conversation

sched_ext: Use atomic cpumask_clear_cpu in scx_idle_test_and_clear_cpu()

PATCH In review Michal Blaszczyk 3 participants 4 messages Sep 1 – Sep 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Use atomic cpumask_clear_cpu in scx_idle_test_and_clear_cpu()

In scx_idle_test_and_clear_cpu(), the shared idle_smts mask is modified locklessly by concurrent CPUs. Currently, the code uses __cpumask_clear_cpu() to clear a CPU from the mask. Because this is a non-atomic read-modify-write operation, concurrent modifications to different bits within the same memory word can lead to data races and lost updates.

Fix this by replacing __cpumask_clear_cpu() with the atomic cpumask_clear_cpu().

Fixes 48849271e661 ("sched_ext: idle: Per-node idle cpumasks")
---
 kernel/sched/ext/idle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext/idle.c +1 -1
index d2973fb3af6d..8985b48c83a5 100644
@@ -104,7 +104,7 @@ static bool scx_idle_test_and_clear_cpu(int cpu)
 		if (cpumask_intersects(smt, idle_smts))
 			cpumask_andnot(idle_smts, idle_smts, smt);
 		else if (cpumask_test_cpu(cpu, idle_smts))
-			__cpumask_clear_cpu(cpu, idle_smts);
+			cpumask_clear_cpu(cpu, idle_smts);
 	}
 
 	return cpumask_test_and_clear_cpu(cpu, idle_cpus);
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch attempts to fix a data race on the locklessly shared idle_smts mask by replacing __cpumask_clear_cpu() with the atomic cpumask_clear_cpu(). However, the fix is incomplete because adjacent non-atomic read-modify-write operations (cpumask_andnot() and cpumask_or()) are still performed on the exact same shared mask. Mixing atomic bitops and non-atomic whole-word operations on the same memory words allows the non-atomic operations to silently clobber and lose the atomic updates. --

commit f5e98ee81f9e1114c10e89759d2151f581c39afa
Author: Michal Blaszczyk <[email protected]>

sched_ext: Use atomic cpumask_clear_cpu in scx_idle_test_and_clear_cpu()

In scx_idle_test_and_clear_cpu(), the shared idle_smts mask is modified locklessly by concurrent CPUs. Currently, the code uses __cpumask_clear_cpu() to clear a CPU from the mask. Because this is a non-atomic read-modify-write operation, concurrent modifications to different bits within the same memory word can lead to data races and lost updates.

Fix this by replacing __cpumask_clear_cpu() with the atomic cpumask_clear_cpu().

kernel/sched/ext/idle.c static bool scx_idle_test_and_clear_cpu(int cpu) · line 106

Quoted 10 lines
kernel/sched/ext/idle.c +1 -1
index 6f93cc32b650e..7be0b5d189cdc 100644
@@ -103,7 +103,7 @@ static bool scx_idle_test_and_clear_cpu(int cpu)
 		if (cpumask_intersects(smt, idle_smts))
 			cpumask_andnot(idle_smts, idle_smts, smt);
 		else if (cpumask_test_cpu(cpu, idle_smts))
-			__cpumask_clear_cpu(cpu, idle_smts);
+			cpumask_clear_cpu(cpu, idle_smts);

[Severity: High]
This is a pre-existing issue, but does the adjacent non-atomic operation defeat the purpose of the patch's atomic fix?

If CPU A locklessly updates idle_smts using non-atomic bulk operations like cpumask_andnot() here, or cpumask_or() in update_builtin_idle(), it reads the whole memory word into a register.

If CPU B concurrently executes the atomic cpumask_clear_cpu() introduced by this patch, CPU A will then write its register back to memory, silently overwriting and discarding CPU B's atomic clear.

Could this lead to lost updates to the shared idle_smts mask, potentially causing scx_pick_idle_cpu() to get caught in an infinite loop as mentioned in the code comments?

Quoted 3 lines
kernel/sched/ext/idle.c +0 -0
 	}
 
 	return cpumask_test_and_clear_cpu(cpu, idle_cpus);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Michal Blaszczyk

Hi Michal,

Quoted 9 lines · Michal Blaszczyk

In scx_idle_test_and_clear_cpu(), the shared idle_smts mask is modified locklessly by concurrent CPUs. Currently, the code uses __cpumask_clear_cpu() to clear a CPU from the mask. Because this is a non-atomic read-modify-write operation, concurrent modifications to different bits within the same memory word can lead to data races and lost updates.

Fix this by replacing __cpumask_clear_cpu() with the atomic cpumask_clear_cpu().

I think the atomic clear makes sense here, but, as sashiko also pointed out, it does not fully address the race, because idle_smts is also modified by the non-atomic cpumask_andnot() below and cpumask_or() in update_builtin_idle().

Quoted 2 lines
Fixes 48849271e661 ("sched_ext: idle: Per-node idle cpumasks")

And the race existed way before this commit, the idle SMT tracking has been always documented as racy and self-correcting.

This change may still be a best-effort improvement, but the commit message should describe it in this way. Did you notice any improvements/benefits with some workloads with this patch applied?

Thanks,
-Andrea

Quoted 21 lines
---
 kernel/sched/ext/idle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext/idle.c +1 -1
index d2973fb3af6d..8985b48c83a5 100644
@@ -104,7 +104,7 @@ static bool scx_idle_test_and_clear_cpu(int cpu)
 		if (cpumask_intersects(smt, idle_smts))
 			cpumask_andnot(idle_smts, idle_smts, smt);
 		else if (cpumask_test_cpu(cpu, idle_smts))
-			__cpumask_clear_cpu(cpu, idle_smts);
+			cpumask_clear_cpu(cpu, idle_smts);
 	}
 
 	return cpumask_test_and_clear_cpu(cpu, idle_cpus);
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
#3 lore raw

Hi Andrea,

Quoted 4 lines · Andrea Righi

I think the atomic clear makes sense here, but, as sashiko also pointed out, it does not fully address the race, because idle_smts is also modified by the non-atomic cpumask_andnot() below and cpumask_or() in update_builtin_idle().

Quoted 8 lines
Quoted 1 line
Fixes 48849271e661 ("sched_ext: idle: Per-node idle cpumasks")

And the race existed way before this commit, the idle SMT tracking has been always documented as racy and self-correcting.

This change may still be a best-effort improvement, but the commit message should describe it in this way. Did you notice any improvements/benefits with some workloads with this patch applied?

I was investigating an automated static analysis report from Sashiko which flagged the use of __cpumask_clear_cpu() as a concurrency bug that could lead to lost updates. I mistakenly assumed this specific call was an isolated oversight and that the other surrounding cpumask_* operations were properly atomic.

I hadn't initially realized that cpumask_andnot() and cpumask_or() are also non-atomic bulk operations. After reading your explanation and looking closer at the code, it's clear to me now that this non-atomic handling was intentional to avoid lock overhead on the fast path.

Since my original intent was to fix what I incorrectly thought was a strict logical bug, and not a profiled optimization, I think it makes the most sense to just drop this patch. Making just this one operation atomic while the rest of the mask is manipulated non-atomically would just be inconsistent and add unnecessary overhead.

Thank you for taking the time to look at this and explain the context to me.

Best,
Michal