* [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated
@ 2026-12-02 17:55 Jiayi Zhang
2026-12-03 9:44 ` Pavel Tikhomirov
2026-12-03 12:00 ` Oleg Nesterov
0 siblings, 2 replies; 3+ messages in thread
From: Jiayi Zhang @ 2026-12-02 17:55 UTC (permalink / raw)
To: Christian Brauner
Cc: Andrew Morton, Oleg Nesterov, Pavel Tikhomirov, Jiayuan Chen,
Michal Koutný,
linux-kernel, syzbot+0718f65353d72efaac1e,
syzbot+fab52e3459fa2f95df57, syzkaller-bugs
pid_allocated is changed under pidmap_lock in alloc_pid(), free_pid()
and disable_pid_allocation(). Two places read it without the lock:
copy_process() checks PIDNS_ADDING before it attaches the new task, and
zap_pid_ns_processes() waits for the count to drop to init_pids. KCSAN
reports the lockless read in copy_process():
BUG: KCSAN: data-race in alloc_pid / copy_process
read-write to 0xffffffff8705b7a8 of 4 bytes by task 33 on cpu 1:
alloc_pid+0x6f9/0x980 kernel/pid.c:334
copy_process+0x119e/0x2350 kernel/fork.c:2347
kernel_clone+0x1c6/0x640 kernel/fork.c:2813
read to 0xffffffff8705b7a8 of 4 bytes by task 115 on cpu 0:
copy_process+0x1725/0x2350 kernel/fork.c:2510
kernel_clone+0x1c6/0x640 kernel/fork.c:2813
and the same read against the decrement in free_pid().
The races do not cause a functional problem. copy_process() makes its
check under tasklist_lock, and zap_pid_ns_processes() clears
PIDNS_ADDING before it takes tasklist_lock to send SIGKILL, so a new
child either sees the cleared bit or is visible to the SIGKILL loop.
zap_pid_ns_processes() reads the count again after each wakeup from
free_pid(). The plain accesses still let the compiler tear or reload
the value, and KCSAN reports them.
Use READ_ONCE() for the two lockless reads and WRITE_ONCE() for the
three writes, as Oleg suggested in the review of an earlier version
that used data_race(). The reads under pidmap_lock stay plain.
Fixes: 3fd372262166 ("pid_ns: Fix race between setns'ed fork() and zap_pid_ns_processes()")
Fixes: af4b8a83add9 ("pidns: Wait in zap_pid_ns_processes until pid_ns->nr_hashed == 1")
Reported-by: syzbot+0718f65353d72efaac1e@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=0718f65353d72efaac1e
Reported-by: syzbot+fab52e3459fa2f95df57@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=fab52e3459fa2f95df57
Suggested-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lore.kernel.org/all/20250425100707.GA8093@redhat.com/
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
This follows up Jiayuan Chen's v2 from April 2025, which was not
resent. Both syzbot reports are still in moderation and have not been
sent to the lists; the Closes: links show them.
Tested in QEMU on 946d9ed1aa02 (the patched files are the same as on
master) with a test program that forks short-lived children and
repeatedly creates pid namespaces whose init exits while its children
keep forking. It switches KCSAN on at run time and reports only races
between alloc_pid(), copy_process(), free_pid() and
zap_pid_ns_processes(). Without the patch all 5 instances reported the
race within 45 seconds (4 alloc_pid / copy_process, 1 copy_process /
free_pid). With the patch none of 5 did in about 3 minutes each, after
which the machines stopped responding under the fork load. The
unpatched instances stopped at the first report, so it is not known
whether they hang the same way.
AI tools assisted with the analysis, the code and the changelog.
kernel/fork.c | 2 +-
kernel/pid.c | 10 +++++++---
kernel/pid_namespace.c | 2 +-
3 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d2..2814065 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2507,7 +2507,7 @@ __latent_entropy struct task_struct *copy_process(
* child missed SIGKILL. If current is not in the same namespace,
* we can't rely on fatal_signal_pending() below.
*/
- if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
+ if (unlikely(!(READ_ONCE(ns_of_pid(pid)->pid_allocated) & PIDNS_ADDING))) {
retval = -ENOMEM;
goto bad_fork_core_free;
}
diff --git a/kernel/pid.c b/kernel/pid.c
index adf7216..7b31ec7 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -121,7 +121,10 @@ void free_pid(struct pid *pid)
for (i = 0; i <= pid->level; i++) {
struct upid *upid = pid->numbers + i;
struct pid_namespace *ns = upid->ns;
- switch (--ns->pid_allocated) {
+ unsigned int nr_allocated = ns->pid_allocated - 1;
+
+ WRITE_ONCE(ns->pid_allocated, nr_allocated);
+ switch (nr_allocated) {
case 2:
case 1:
/* When all that is left in the pid namespace
@@ -331,7 +334,8 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,
for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {
/* Make the PID visible to find_pid_ns. */
idr_replace(&upid->ns->idr, pid, upid->nr);
- upid->ns->pid_allocated++;
+ WRITE_ONCE(upid->ns->pid_allocated,
+ upid->ns->pid_allocated + 1);
}
spin_unlock(&pidmap_lock);
idr_preload_end();
@@ -363,7 +367,7 @@ out_abort:
void disable_pid_allocation(struct pid_namespace *ns)
{
spin_lock(&pidmap_lock);
- ns->pid_allocated &= ~PIDNS_ADDING;
+ WRITE_ONCE(ns->pid_allocated, ns->pid_allocated & ~PIDNS_ADDING);
spin_unlock(&pidmap_lock);
}
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index 127a5fd..53b0793 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -270,7 +270,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
*/
for (;;) {
set_current_state(TASK_IDLE | TASK_FREEZABLE);
- if (pid_ns->pid_allocated == init_pids)
+ if (READ_ONCE(pid_ns->pid_allocated) == init_pids)
break;
schedule();
}
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated
2026-12-02 17:55 [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated Jiayi Zhang
@ 2026-12-03 9:44 ` Pavel Tikhomirov
2026-12-03 12:00 ` Oleg Nesterov
1 sibling, 0 replies; 3+ messages in thread
From: Pavel Tikhomirov @ 2026-12-03 9:44 UTC (permalink / raw)
To: Jiayi Zhang
Cc: Christian Brauner, Andrew Morton, Oleg Nesterov,
Pavel Tikhomirov, Jiayuan Chen, Michal Koutný,
linux-kernel, syzbot+0718f65353d72efaac1e,
syzbot+fab52e3459fa2f95df57, syzkaller-bugs
> pid_allocated is changed under pidmap_lock in alloc_pid(), free_pid()
> and disable_pid_allocation(). Two places read it without the lock:
> copy_process() checks PIDNS_ADDING before it attaches the new task, and
> zap_pid_ns_processes() waits for the count to drop to init_pids. KCSAN
> reports the lockless read in copy_process():
>
> BUG: KCSAN: data-race in alloc_pid / copy_process
>
> read-write to 0xffffffff8705b7a8 of 4 bytes by task 33 on cpu 1:
> alloc_pid+0x6f9/0x980 kernel/pid.c:334
> copy_process+0x119e/0x2350 kernel/fork.c:2347
> kernel_clone+0x1c6/0x640 kernel/fork.c:2813
>
> read to 0xffffffff8705b7a8 of 4 bytes by task 115 on cpu 0:
> copy_process+0x1725/0x2350 kernel/fork.c:2510
> kernel_clone+0x1c6/0x640 kernel/fork.c:2813
>
> and the same read against the decrement in free_pid().
>
> The races do not cause a functional problem. copy_process() makes its
> check under tasklist_lock, and zap_pid_ns_processes() clears
> PIDNS_ADDING before it takes tasklist_lock to send SIGKILL, so a new
> child either sees the cleared bit or is visible to the SIGKILL loop.
> zap_pid_ns_processes() reads the count again after each wakeup from
> free_pid(). The plain accesses still let the compiler tear or reload
> the value, and KCSAN reports them.
Maybe worth adding
Cc: <stable+noautosel@kernel.org> # KCSAN annotation only, no functional change
>
> Use READ_ONCE() for the two lockless reads and WRITE_ONCE() for the
> three writes, as Oleg suggested in the review of an earlier version
> that used data_race(). The reads under pidmap_lock stay plain.
>
> Fixes: 3fd372262166 ("pid_ns: Fix race between setns'ed fork() and zap_pid_ns_processes()")
> Fixes: af4b8a83add9 ("pidns: Wait in zap_pid_ns_processes until pid_ns->nr_hashed == 1")
> Reported-by: syzbot+0718f65353d72efaac1e@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=0718f65353d72efaac1e
> Reported-by: syzbot+fab52e3459fa2f95df57@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=fab52e3459fa2f95df57
> Suggested-by: Oleg Nesterov <oleg@redhat.com>
> Link: https://lore.kernel.org/all/20250425100707.GA8093@redhat.com/
> Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index d55a7d21a8fb2..2814065b2e765 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -2507,7 +2507,7 @@ __latent_entropy struct task_struct *copy_process(
> * child missed SIGKILL. If current is not in the same namespace,
> * we can't rely on fatal_signal_pending() below.
> */
> - if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
> + if (unlikely(!(READ_ONCE(ns_of_pid(pid)->pid_allocated) & PIDNS_ADDING))) {
> retval = -ENOMEM;
> goto bad_fork_core_free;
> }
> diff --git a/kernel/pid.c b/kernel/pid.c
> index adf7216067684..7b31ec75d8728 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -121,7 +121,10 @@ void free_pid(struct pid *pid)
> for (i = 0; i <= pid->level; i++) {
> struct upid *upid = pid->numbers + i;
> struct pid_namespace *ns = upid->ns;
> - switch (--ns->pid_allocated) {
> + unsigned int nr_allocated = ns->pid_allocated - 1;
> +
> + WRITE_ONCE(ns->pid_allocated, nr_allocated);
> + switch (nr_allocated) {
> case 2:
> case 1:
> /* When all that is left in the pid namespace
> @@ -331,7 +334,8 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,
> for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {
> /* Make the PID visible to find_pid_ns. */
> idr_replace(&upid->ns->idr, pid, upid->nr);
> - upid->ns->pid_allocated++;
> + WRITE_ONCE(upid->ns->pid_allocated,
> + upid->ns->pid_allocated + 1);
> }
> spin_unlock(&pidmap_lock);
> idr_preload_end();
> @@ -363,7 +367,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,
> void disable_pid_allocation(struct pid_namespace *ns)
> {
> spin_lock(&pidmap_lock);
> - ns->pid_allocated &= ~PIDNS_ADDING;
> + WRITE_ONCE(ns->pid_allocated, ns->pid_allocated & ~PIDNS_ADDING);
> spin_unlock(&pidmap_lock);
> }
>
> diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
> index 127a5fd23e704..53b0793eeed24 100644
> --- a/kernel/pid_namespace.c
> +++ b/kernel/pid_namespace.c
> @@ -270,7 +270,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
> */
> for (;;) {
> set_current_state(TASK_IDLE | TASK_FREEZABLE);
> - if (pid_ns->pid_allocated == init_pids)
> + if (READ_ONCE(pid_ns->pid_allocated) == init_pids)
> break;
> schedule();
> }
Reviewed-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
--
Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated
2026-12-02 17:55 [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated Jiayi Zhang
2026-12-03 9:44 ` Pavel Tikhomirov
@ 2026-12-03 12:00 ` Oleg Nesterov
1 sibling, 0 replies; 3+ messages in thread
From: Oleg Nesterov @ 2026-12-03 12:00 UTC (permalink / raw)
To: Jiayi Zhang
Cc: Christian Brauner, Andrew Morton, Pavel Tikhomirov, Jiayuan Chen,
Michal Koutný,
linux-kernel, syzbot+0718f65353d72efaac1e,
syzbot+fab52e3459fa2f95df57, syzkaller-bugs
On 12/02, Jiayi Zhang wrote:
>
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -2507,7 +2507,7 @@ __latent_entropy struct task_struct *copy_process(
> * child missed SIGKILL. If current is not in the same namespace,
> * we can't rely on fatal_signal_pending() below.
> */
> - if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {
> + if (unlikely(!(READ_ONCE(ns_of_pid(pid)->pid_allocated) & PIDNS_ADDING))) {
> retval = -ENOMEM;
> goto bad_fork_core_free;
> }
> diff --git a/kernel/pid.c b/kernel/pid.c
> index adf7216..7b31ec7 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -121,7 +121,10 @@ void free_pid(struct pid *pid)
> for (i = 0; i <= pid->level; i++) {
> struct upid *upid = pid->numbers + i;
> struct pid_namespace *ns = upid->ns;
> - switch (--ns->pid_allocated) {
> + unsigned int nr_allocated = ns->pid_allocated - 1;
> +
> + WRITE_ONCE(ns->pid_allocated, nr_allocated);
> + switch (nr_allocated) {
> case 2:
> case 1:
> /* When all that is left in the pid namespace
> @@ -331,7 +334,8 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,
> for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {
> /* Make the PID visible to find_pid_ns. */
> idr_replace(&upid->ns->idr, pid, upid->nr);
> - upid->ns->pid_allocated++;
> + WRITE_ONCE(upid->ns->pid_allocated,
> + upid->ns->pid_allocated + 1);
> }
> spin_unlock(&pidmap_lock);
> idr_preload_end();
> @@ -363,7 +367,7 @@ out_abort:
> void disable_pid_allocation(struct pid_namespace *ns)
> {
> spin_lock(&pidmap_lock);
> - ns->pid_allocated &= ~PIDNS_ADDING;
> + WRITE_ONCE(ns->pid_allocated, ns->pid_allocated & ~PIDNS_ADDING);
> spin_unlock(&pidmap_lock);
> }
>
> diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
> index 127a5fd..53b0793 100644
> --- a/kernel/pid_namespace.c
> +++ b/kernel/pid_namespace.c
> @@ -270,7 +270,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
> */
> for (;;) {
> set_current_state(TASK_IDLE | TASK_FREEZABLE);
> - if (pid_ns->pid_allocated == init_pids)
> + if (READ_ONCE(pid_ns->pid_allocated) == init_pids)
> break;
> schedule();
> }
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 12:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 17:55 [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated Jiayi Zhang
2026-12-03 9:44 ` Pavel Tikhomirov
2026-12-03 12:00 ` Oleg Nesterov
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®