mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kthread: Annotate lockless to_kthread() flags access
@ 2026-09-20  7:07 Kunwu Chan
  2026-09-20 13:17 ` Bradley Morgan
  2026-09-25  9:34 ` Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Kunwu Chan @ 2026-09-20  7:07 UTC (permalink / raw)
  To: frederic, longman, juri.lelli, peterz, kees+treewide, brauner
  Cc: linux-kernel, paulmck, 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.

Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>
---
 kernel/kthread.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/kthread.c b/kernel/kthread.c
index a3f95c90456b..cc8cb5d3eab7 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -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;
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kthread: Annotate lockless to_kthread() flags access
  2026-09-20  7:07 [PATCH] kthread: Annotate lockless to_kthread() flags access Kunwu Chan
@ 2026-09-20 13:17 ` Bradley Morgan
  2026-09-25  9:34 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Bradley Morgan @ 2026-09-20 13:17 UTC (permalink / raw)
  To: kunwu.chan
  Cc: brauner, frederic, juri.lelli, kees+treewide, linux-kernel,
	longman, paulmck, peterz

On 20 September 2026 08:07:50 BST, Kunwu Chan <kunwu.chan@gmail.com> wrote:
>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.
>
>Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>
>---
> kernel/kthread.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/kernel/kthread.c b/kernel/kthread.c
>index a3f95c90456b..cc8cb5d3eab7 100644
>--- a/kernel/kthread.c
>+++ b/kernel/kthread.c
>@@ -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!

Reviewed-by: Bradley Morgan <brads@mainlining.org>

> 	return k->worker_private;
> }
> 
>


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kthread: Annotate lockless to_kthread() flags access
  2026-09-20  7:07 [PATCH] kthread: Annotate lockless to_kthread() flags access Kunwu Chan
  2026-09-20 13:17 ` Bradley Morgan
@ 2026-09-25  9:34 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2026-09-25  9:34 UTC (permalink / raw)
  To: frederic, longman, juri.lelli, peterz, kees+treewide, Kunwu Chan
  Cc: Christian Brauner, linux-kernel, paulmck

On Sun, 20 Sep 2026 15:07:50 +0800, Kunwu Chan wrote:
> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-25  9:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-20  7:07 [PATCH] kthread: Annotate lockless to_kthread() flags access Kunwu Chan
2026-09-20 13:17 ` Bradley Morgan
2026-09-25  9:34 ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®