cgroup: Use READ_ONCE() for task->flags in task_css_set_check()
PATCH 1/1 cgroup: Use READ_ONCE() for task->flags in task_css_set_check()
From: Guopeng Zhang <[email protected]>
task_css_set_check() uses rcu_dereference_check() to verify that task->cgroups can be dereferenced. One accepted condition is that the task is already exiting, tested by checking PF_EXITING in task->flags.
This is a lockless snapshot used only for the CONFIG_PROVE_RCU debug predicate. This was found by KCSAN during fuzz testing. KCSAN can report a data race when another task flag bit is updated concurrently. One report shows pids_release() reading task->flags through task_css_set_check() while do_task_dead() sets PF_NOFREEZE:
KCSAN: data-race in task_css() [inline]
KCSAN: data-race in pids_release()
task_css()
pids_release()
cgroup_release()
release_task()
wait_task_zombie()
value changed: 0x0040004c -> 0x0040804c
The changed bit is PF_NOFREEZE, not PF_EXITING. PF_EXITING remains set before and after the update, so the task_css_set_check() condition does not change. This is not a race on task->cgroups and does not indicate incorrect pids charging or uncharging.
Use READ_ONCE() to document the intended lockless snapshot of task->flags.
No functional change intended.
--- include/linux/cgroup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/cgroup.h +1 -1
@@ -480,7 +480,7 @@ static inline void cgroup_unlock(void) rcu_read_lock_sched_held() || \ lockdep_is_held(&cgroup_mutex) || \ lockdep_is_held(&css_set_lock) || \ - ((task)->flags & PF_EXITING) || (__c)) + (READ_ONCE((task)->flags) & PF_EXITING) || (__c))#else #define task_css_set_check (task, __c) \ rcu_dereference((task)->cgroups)
Signature · 1 line
2.25.1
Looks fine — this is a benign, PROVE_RCU-only race, and READ_ONCE() documents the lockless snapshot with no functional change.
Quoted 22 lines · Guopeng Zhang
From: Guopeng Zhang <[email protected]>
task_css_set_check()usesrcu_dereference_check()to verify thattask->cgroupscan be dereferenced. One accepted condition is that the task is already exiting, tested by checkingPF_EXITINGintask->flags.This is a lockless snapshot used only for the
CONFIG_PROVE_RCUdebug predicate. This was found by KCSAN during fuzz testing. KCSAN can report a data race when another task flag bit is updated concurrently. One report showspids_release()readingtask->flagsthroughtask_css_set_check()whiledo_task_dead()setsPF_NOFREEZE:
...
The changed bit isPF_NOFREEZE, notPF_EXITING.PF_EXITINGremains set before and after the update, so thetask_css_set_check()condition does not change. This is not a race ontask->cgroupsand does not indicate incorrect pids charging or uncharging.Use
READ_ONCE()to document the intended lockless snapshot oftask->flags.No functional change intended.
Signed-off-by Guopeng Zhang <[email protected]>
Tejun Heo
maintainerinclude/linux/cgroup.h static inline void cgroup_unlock(void) · line 483
Quoted 2 lines · Guopeng Zhang
include/linux/cgroup.h +1
-1- ((task)->flags & PF_EXITING) || (__c)) + (READ_ONCE((task)->flags) & PF_EXITING) || (__c))
This only feeds the CONFIG_PROVE_RCU lockdep predicate, so it's a diagnostic-only read. tools/memory-model/Documentation/access-marking.txt recommends data_race() over READ_ONCE() for those:
(data_race((task)->flags) & PF_EXITING) || (__c))
Please update the changelog to match.
Thanks.