From: Jiayi Zhang <zhangjiayi1014@gmail.com>
To: Christian Brauner <brauner@kernel.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"Oleg Nesterov" <oleg@redhat.com>,
"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
"Jiayuan Chen" <jiayuan.chen@linux.dev>,
"Michal Koutný" <mkoutny@suse.com>,
linux-kernel@vger.kernel.org,
syzbot+0718f65353d72efaac1e@syzkaller.appspotmail.com,
syzbot+fab52e3459fa2f95df57@syzkaller.appspotmail.com,
syzkaller-bugs@googlegroups.com
Subject: [PATCH] pid: use READ_ONCE() and WRITE_ONCE() for pid_namespace::pid_allocated
Date: Wed, 2 Dec 2026 17:55:42 +0000 [thread overview]
Message-ID: <20261202175542.4130656-1-zhangjiayi1014@gmail.com> (raw)
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
next reply other threads:[~2026-12-02 17:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 17:55 Jiayi Zhang [this message]
2026-12-03 9:44 ` Pavel Tikhomirov
2026-12-03 12:00 ` Oleg Nesterov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202175542.4130656-1-zhangjiayi1014@gmail.com \
--to=zhangjiayi1014@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=jiayuan.chen@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=ptikhomirov@virtuozzo.com \
--cc=syzbot+0718f65353d72efaac1e@syzkaller.appspotmail.com \
--cc=syzbot+fab52e3459fa2f95df57@syzkaller.appspotmail.com \
--cc=syzkaller-bugs@googlegroups.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®