Skip to contentSkip to conversation

kthread: Annotate lockless to_kthread() flags access

PATCH Applied Kunwu Chan 3 participants 3 messages Sep 20 – Sep 25

lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

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
index a3f95c90456b..cc8cb5d3eab7 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Kunwu Chan

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 +0 -0
index a3f95c90456b..cc8cb5d3eab7 100644

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 -0
 	return k->worker_private;
 }
 

--- Thanks!
"I'm not a very positive person" - Linus torvalds

#2 lore raw
In reply to Kunwu Chan
Quoted 9 lines · Kunwu Chan

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:

[...]

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