Re: [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated
From: Pavel Tikhomirov
Date: Thu Dec 03 2026 - 04:44:31 EST
> 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@xxxxxxxxxx> # 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@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=0718f65353d72efaac1e
> Reported-by: syzbot+fab52e3459fa2f95df57@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=fab52e3459fa2f95df57
> Suggested-by: Oleg Nesterov <oleg@xxxxxxxxxx>
> Link: https://lore.kernel.org/all/20250425100707.GA8093@xxxxxxxxxx/
> Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
>
> 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@xxxxxxxxxxxxx>
--
Pavel Tikhomirov <ptikhomirov@xxxxxxxxxxxxx>