kthread: Annotate lockless to_kthread() flags access
PATCH 1/1 kthread: Annotate lockless to_kthread() flags access
do_exit() calls synchronize_group_exit(), which modifies task->flags under sighand->siglock:
tsk->flags |= PF_POSTCOREDUMP;
The sanity check in to_kthread() reads the same flags word without holding that lock:
WARN_ON(!(k->flags & PF_KTHREAD));
PF_KTHREAD is set once when the task becomes a kthread and is never cleared for the lifetime of the task. Since PF_KTHREAD is immutable, the check does not depend on synchronization with updates to other bits in task->flags.
Use READ_ONCE() to annotate the intentional lockless access.
--- kernel/kthread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kthread.c +1 -1
@@ -81,7 +81,7 @@ enum KTHREAD_BITS {static inline struct kthread *to_kthread (struct task_struct *k) { -WARN_ON (!(k->flags &PF_KTHREAD )); +WARN_ON (!(READ_ONCE (k->flags) &PF_KTHREAD ));return k->worker_private; }
Signature · 1 line
2.43.0
Quoted 27 lines · Kunwu Chan
do_exit()callssynchronize_group_exit(), which modifiestask->flagsundersighand->siglock:tsk->flags |= PF_POSTCOREDUMP;The sanity check in
to_kthread()reads the same flags word without holding that lock:WARN_ON(!(k->flags & PF_KTHREAD));
PF_KTHREADis set once when the task becomes a kthread and is never cleared for the lifetime of the task. SincePF_KTHREADis immutable, the check does not depend on synchronization with updates to other bits intask->flags.Use
READ_ONCE()to annotate the intentional lockless access.Signed-off-by Kunwu Chan <[email protected]>---
kernel/kthread.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)kernel/kthread.c +0
-0
kernel/kthread.c enum KTHREAD_BITS { · line 84
Quoted 5 lines
kernel/kthread.c +1
-1@@ -81,7 +81,7 @@ enum KTHREAD_BITS {static inline struct kthread *to_kthread (struct task_struct *k) { -WARN_ON (!(k->flags &PF_KTHREAD )); +WARN_ON (!(READ_ONCE (k->flags) &PF_KTHREAD ));
Nice! Thanks for the fix!
kernel/kthread.c enum KTHREAD_BITS { · line 87
Quoted 4 lines
kernel/kthread.c +0
-0return k->worker_private; }
--- Thanks!
"I'm not a very positive person" - Linus torvalds
Christian Brauner
AppliedQuoted 9 lines · Kunwu Chan
do_exit()callssynchronize_group_exit(), which modifiestask->flagsundersighand->siglock:tsk->flags |= PF_POSTCOREDUMP;The sanity check in
to_kthread()reads the same flags word without holding that lock:[...]
Applied to the vfs-7.4.coredump branch of the vfs/vfs.git tree. Patches in the vfs-7.4.coredump branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.4.coredump
[1/1] kthread: Annotate lockless to_kthread() flags access
https://git.kernel.org/vfs/vfs/c/fd00a46e36a1