pid: use READ_ONCE() in pid_alive()
PATCH 1/1 pid: use READ_ONCE() in pid_alive()
KCSAN reports pid_alive() reading task->thread_pid while it gets cleared under tasklist_lock. The check only cares about NULL, so READ_ONCE() and WRITE_ONCE() are enough.
---
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.
include/linux/pid.h | 2 +- kernel/pid.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/pid.h +1 -1
@@ -264,7 +264,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)*/ static inline int pid_alive (const struct task_struct *p) { -return p->thread_pid !=NULL ; +return READ_ONCE (p->thread_pid) !=NULL ; }static inline pid_t task_pgrp_nr_ns (struct task_struct *tsk,struct pid_namespace *ns)
kernel/pid.c +1 -1
@@ -411,7 +411,7 @@ static void __change_pid(struct pid **pids, struct task_struct *task, pid = *pid_ptr;hlist_del_rcu (&task->pid_links[type]); - *pid_ptr = new; +WRITE_ONCE (*pid_ptr, new);for (tmp =PIDTYPE_MAX ; --tmp >=0 ; )if (pid_has_task (pid, tmp))
base-commit: ed14a591175bb5f56c2936b082cffb9e4b935e6d
Signature · 1 line
2.43.0
Quoted 7 lines · Babanpreet Singh
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.Reported-by [email protected]Assisted-by Claude:claude-opus-5-5
Coolio
Quoted 37 lines
Signed-off-by Babanpreet Singh <[email protected]>---
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.
include/linux/pid.h| 2 +-kernel/pid.c| 2 +-2 files changed, 2 insertions(+), 2 deletions(-)include/linux/pid.h +1
-1@@ -264,7 +264,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)*/ static inline int pid_alive (const struct task_struct *p) { -return p->thread_pid !=NULL ; +return READ_ONCE (p->thread_pid) !=NULL ; }static inline
pid_ttask_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)kernel/pid.c +1
-1@@ -411,7 +411,7 @@ static void __change_pid(struct pid **pids, struct task_struct *task, pid = *pid_ptr;hlist_del_rcu (&task->pid_links[type]); - *pid_ptr = new; +WRITE_ONCE (*pid_ptr, new);for (tmp =PIDTYPE_MAX ; --tmp >=0 ; )if (pid_has_task (pid, tmp))
Quoted 2 lines
base-commit:
ed14a591175bb5f56c2936b082cffb9e4b935e6d
--- Thanks!
"I'm not a very positive person" - Linus torvalds
Quoted 17 lines · Babanpreet Singh
include/linux/pid.h +1
-1@@ -264,7 +264,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)*/ static inline int pid_alive (const struct task_struct *p) { -return p->thread_pid !=NULL ; +return READ_ONCE (p->thread_pid) !=NULL ; }static inline pid_t task_pgrp_nr_ns (struct task_struct *tsk,struct pid_namespace *ns)kernel/pid.c +0
-0
kernel/pid.c static void __change_pid(struct pid **pids, struct task_struct *task, · line 414
Quoted 5 lines
kernel/pid.c +1
-1@@ -411,7 +411,7 @@ static void __change_pid(struct pid **pids, struct task_struct *task, pid = *pid_ptr;hlist_del_rcu (&task->pid_links[type]); - *pid_ptr = new; +WRITE_ONCE (*pid_ptr, new);
Quoted 3 lines · On Fri, 2 Oct 2026 01:21:41 +0000 Babanpreet Singh <[email protected]>…
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.
I just looked at change_pid() - isn't it completely broken? __change_pid() uses hlist_del_rcu() to remove the item from a list. IIUC this leaves the 'next' pointer valid to allow for concurrent readers. I thought that had to stay valid until the end of the rcu period. But the following attach_pid() adds the item to another list. I think that means that a concurrent reader can switch lists and thus fail to find an item.
This could be (mostly) mitigated by using the 'nulls' variant which lets the reading code detect the crossed lists and rescan.
(I've not checked the history...)
David
Quoted 41 lines
Reported-by [email protected]Assisted-by Claude:claude-opus-5-5Signed-off-by Babanpreet Singh <[email protected]>---
Compile tested only (gcc W=1 and the KCSAN instrumentation diff); I could not reproduce the race in QEMU.include/linux/pid.h | 2 +- kernel/pid.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)include/linux/pid.h +1
-1@@ -264,7 +264,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)*/ static inline int pid_alive (const struct task_struct *p) { -return p->thread_pid !=NULL ; +return READ_ONCE (p->thread_pid) !=NULL ; }static inline pid_t task_pgrp_nr_ns (struct task_struct *tsk,struct pid_namespace *ns)kernel/pid.c +1
-1@@ -411,7 +411,7 @@ static void __change_pid(struct pid **pids, struct task_struct *task, pid = *pid_ptr;hlist_del_rcu (&task->pid_links[type]); - *pid_ptr = new; +WRITE_ONCE (*pid_ptr, new);for (tmp =PIDTYPE_MAX ; --tmp >=0 ; )if (pid_has_task (pid, tmp))base-commit:
ed14a591175bb5f56c2936b082cffb9e4b935e6d
Quoted 9 lines · David Laight
Quoted 3 lines · On Fri, 2 Oct 2026 01:21:41 +0000 Babanpreet Singh <[email protected]>…
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.I just looked at
change_pid()- isn't it completely broken?
No, but...
Quoted 4 lines
__change_pid()useshlist_del_rcu()to remove the item from a list. IIUC this leaves the 'next' pointer valid to allow for concurrent readers. I thought that had to stay valid until the end of the rcu period. But the followingattach_pid()adds the item to another list.
Yep. That is why do_each_pid_task() needs tasklist_lock.
This is the known fact, let me quote the part of my old email
https://lore.kernel.org/all/[email protected]/
> Currently the tasklist_lock is shared mainly in order to observe > the list atomically for the PRIO_PGRP and PRIO_USER cases, as > the actual lookups are already rcu-safe,
not really...
do_each_pid_task(PIDTYPE_PGID) can race with change_pid(PIDTYPE_PGID) which moves the task from one hlist to another. Yes, it is safe in that task_struct can't go away. But still this is not right because do_each_pid_task() can scan the wrong (2nd) hlist.
Somehow I thought this was documented, but it isn't. And this is not obvious. I think this deserves a comment above do_each_pid_task(), will send the patch.
Oleg.
Hmm. grep finds send_sigio() and send_sigurg() which use do_each_pid_task() without tasklist.
00633c4683828acd ("fs/fcntl: fix SOFTIRQ-unsafe lock order in fasync signaling") is wrong.
I'll send email in reply to that patch, I wasn't CC'ed...
Oleg.
Quoted 39 lines · Oleg Nesterov
Quoted 9 lines · David Laight
Quoted 3 lines · On Fri, 2 Oct 2026 01:21:41 +0000 Babanpreet Singh <[email protected]>…
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.I just looked at
change_pid()- isn't it completely broken?No, but...
Quoted 4 lines
__change_pid()useshlist_del_rcu()to remove the item from a list. IIUC this leaves the 'next' pointer valid to allow for concurrent readers. I thought that had to stay valid until the end of the rcu period. But the followingattach_pid()adds the item to another list.Yep. That is why
do_each_pid_task()needstasklist_lock.This is the known fact, let me quote the part of my old email
https://lore.kernel.org/all/[email protected]/> Currently the
tasklist_lockis shared mainly in order to observe > the list atomically for thePRIO_PGRPandPRIO_USERcases, as > the actual lookups are already rcu-safe,not really...
do_each_pid_task(PIDTYPE_PGID)can race withchange_pid(PIDTYPE_PGID)which moves the task from one hlist to another. Yes, it is safe in thattask_structcan't go away. But still this is not right becausedo_each_pid_task()can scan the wrong (2nd) hlist.Somehow I thought this was documented, but it isn't. And this is not obvious. I think this deserves a comment above
do_each_pid_task(), will send the patch.Oleg.
Quoted 29 lines · On Sun, 4 Oct 2026 12:55:34 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 9 lines · David Laight
Quoted 3 lines · On Fri, 2 Oct 2026 01:21:41 +0000 Babanpreet Singh <[email protected]>…
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.I just looked at
change_pid()- isn't it completely broken?No, but...
Quoted 4 lines
__change_pid()useshlist_del_rcu()to remove the item from a list. IIUC this leaves the 'next' pointer valid to allow for concurrent readers. I thought that had to stay valid until the end of the rcu period. But the followingattach_pid()adds the item to another list.Yep. That is why
do_each_pid_task()needstasklist_lock.This is the known fact, let me quote the part of my old email
https://lore.kernel.org/all/[email protected]/> Currently the
tasklist_lockis shared mainly in order to observe > the list atomically for thePRIO_PGRPandPRIO_USERcases, as > the actual lookups are already rcu-safe,not really...
Quoted 7 lines
do_each_pid_task(PIDTYPE_PGID)can race withchange_pid(PIDTYPE_PGID)which moves the task from one hlist to another. Yes, it is safe in thattask_structcan't go away. But still this is not right becausedo_each_pid_task()can scan the wrong (2nd) hlist.Somehow I thought this was documented, but it isn't. And this is not obvious. I think this deserves a comment above
do_each_pid_task(), will send the patch.
I guess the rcu protection lets the task exit without holding the lock? Is that really significant given the other things that happen during task exit.
Could do_each_pid_task() use hlist_nulls_for_each_entry_rcu() and rescan if it got the wrong terminator.
Or does scanning twice cause grief as well.
David
Quoted 3 lines
Oleg.
Quoted 20 lines · David Laight
Quoted 16 lines · On Sun, 4 Oct 2026 12:55:34 +0200 Oleg Nesterov <[email protected]> wrote:
Yep. That is why
do_each_pid_task()needstasklist_lock.This is the known fact, let me quote the part of my old email
https://lore.kernel.org/all/[email protected]/> Currently the
tasklist_lockis shared mainly in order to observe > the list atomically for thePRIO_PGRPandPRIO_USERcases, as > the actual lookups are already rcu-safe,not really...
do_each_pid_task(PIDTYPE_PGID)can race withchange_pid(PIDTYPE_PGID)which moves the task from one hlist to another. Yes, it is safe in thattask_structcan't go away. But still this is not right becausedo_each_pid_task()can scan the wrong (2nd) hlist.
Quoted 5 lines
Quoted 2 lines
Somehow I thought this was documented, but it isn't. And this is not obvious. I think this deserves a comment above
do_each_pid_task(), will send the patch.I guess the rcu protection lets the task exit without holding the lock? Is that really significant given the other things that happen during task exit.
Sorry, I don't understand your question...
Quoted 3 lines
Could
do_each_pid_task()usehlist_nulls_for_each_entry_rcu()and rescan if it got the wrong terminator.
Or does scanning twice cause grief as well.
I don't think it can. Say, __kill_pgrp_info() is a "typical" user of do_each_pid_task(). What can it do if it detects that get_nulls_value() doesn't match after the main loop? The signal was already sent.
Oleg.
Quoted 21 lines · On Sun, 4 Oct 2026 15:14:26 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 20 lines · David Laight
Quoted 16 lines · On Sun, 4 Oct 2026 12:55:34 +0200 Oleg Nesterov <[email protected]> wrote:
Yep. That is why
do_each_pid_task()needstasklist_lock.This is the known fact, let me quote the part of my old email
https://lore.kernel.org/all/[email protected]/> Currently the
tasklist_lockis shared mainly in order to observe > the list atomically for thePRIO_PGRPandPRIO_USERcases, as > the actual lookups are already rcu-safe,not really...
do_each_pid_task(PIDTYPE_PGID)can race withchange_pid(PIDTYPE_PGID)which moves the task from one hlist to another. Yes, it is safe in thattask_structcan't go away. But still this is not right becausedo_each_pid_task()can scan the wrong (2nd) hlist.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Somehow I thought this was documented, but it isn't. And this is not obvious. I think this deserves a comment above
do_each_pid_task(), will send the patch.I guess the rcu protection lets the task exit without holding the lock? Is that really significant given the other things that happen during task exit.
Sorry, I don't understand your question...
I was wondering if the (partial) rcu protection of these lists was worth the trouble.
If the 'add code' all the readers and have to hold the lock then does that leave anything other than task exit doing an rcu-delete.
I wouldn't have though acquiring the lock in the task exit code would be noticeable.
Is there some other path where rcu protection is 'good enough'?
Quoted 8 lines
Quoted 4 lines
Could
do_each_pid_task()usehlist_nulls_for_each_entry_rcu()and rescan if it got the wrong terminator.
Or does scanning twice cause grief as well.I don't think it can. Say,
__kill_pgrp_info()is a "typical" user ofdo_each_pid_task(). What can it do if it detects thatget_nulls_value()doesn't match after the main loop? The signal was already sent.
It would have to check each entry to ensure it was on the correct list. (That probably doesn't need the 'nulls' variant.)
The problem is that the rescan will do things twice.
This is ok for a search, but probably not for sending a signal.
David
Quoted 3 lines
Oleg.
Quoted 3 lines · David Laight
I was wondering if the (partial) rcu protection of these lists was worth the trouble.
At the top of my head, probably not... At least right now. We can improve this, say we can change attach_pid() to use hlist_add_behind_rcu(). This way the lockless do_each_pid() won't "obviously race" with fork(). But this won't solve other problems.
Quoted 4 lines
If the 'add code' all the readers and have to hold the lock then does that leave anything other than task exit doing an rcu-delete.
I wouldn't have though acquiring the lock in the task exit code would be noticeable.
Still can't understand. The exiting task does take tasklist when it calls detach_pid().
Quoted 8 lines
Quoted 3 lines
I don't think it can. Say,
__kill_pgrp_info()is a "typical" user ofdo_each_pid_task(). What can it do if it detects thatget_nulls_value()doesn't match after the main loop? The signal was already sent.It would have to check each entry to ensure it was on the correct list. (That probably doesn't need the 'nulls' variant.)
The problem is that the rescan will do things twice.
This is ok for a search, but probably not for sending a signal.
Yes, exactly.
Oleg.
Quoted 1 line · On Mon, 5 Oct 2026 17:03:42 +0200 Oleg Nesterov <[email protected]> wrote:
On 10/04, David Laight wrote:
...
Quoted 7 lines
Quoted 4 lines
If the 'add code' all the readers and have to hold the lock then does that leave anything other than task exit doing an rcu-delete.
I wouldn't have though acquiring the lock in the task exit code would be noticeable.Still can't understand. The exiting task does take tasklist when it calls
detach_pid().
I think I've confused myself :-)
The delete needs the lock to avoid corrupting the list.
It would be safe for an rcu protected reader - but it doesn't help you.
David
Christian Brauner
AppliedQuoted 3 lines · Babanpreet Singh
KCSAN reports
pid_alive()readingtask->thread_pidwhile it gets cleared undertasklist_lock. The check only cares about NULL, soREAD_ONCE()andWRITE_ONCE()are enough.
Applied to the kernel-7.4.signal branch of the vfs/vfs.git tree. Patches in the kernel-7.4.signal 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: kernel-7.4.signal
[1/1] pid: use READ_ONCE() in pid_alive()
https://git.kernel.org/vfs/vfs/c/12e19bba68ba