From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796234155; b=GH+zbUPJCWuLmvr2QX2VsgRKq9OL2MvWaZ8VMQYDKljsZqtbrGKcYbFC/ntfgRDWA70X RqykCyBeah3vDnwi6tL/HlTlWXbaQCcuJUHrpMg3MUF2iq76jesCrjU7GCVBnaHjOV1YT gUefKw2r7QiCC/MvFFtNL8LyeHeX0PUMQ8q84/YrFhFfzsmgpA3R/FcXFCO7jITqfhxzy +K/b7kiWYdN+aRj2B3qeCZzcar9s0OHuoWom8PMcoaq+vP5Oezge7R3zSjAhQxZCax7nc Btmvi7FGfCRyl/3Oq3HzI49XCNGywfeIcFRiRRpjWO+Qmfrroj3MywiKlwHoM7A0rlw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796234155; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=bhaljCpPcLQVIONpjG6K44F5nGY7L9ZejBgrsqOYb88=; b=cuIWZCB6Q+GwYRs4UnVjhNhb6f2vh8rPG78rdw9bsdl6eVNs49PCxy488+kr8eLK+QM4 3cuKNdAfrnOF2v41Sls6ULpvUZSeY+3Z+Xm1ReLHin0Mh/HMivWIUasya6ae77lrsFLv3 qb2kklx9dbbGqsxWoURrcTlF2AMH2A5AzIFwcfDK1TTwG4u8mGNEod53QCZbKlPvuMMIY b7ePfIF2k2LzWrasoUGtTyHIqoMVPvfsNn9HZiNphGt+NjiKqPHhQ3xRe0EpvHeoCDXt9 muLwlAirLS1h+XabBRNkO7DEtrntYEWUBbHltIX0DT46Pvaols38xSpxKogGOF6Xqzw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GmbjvIvV; arc=none smtp.remote-ip=74.125.229.5 Received: from mail-dy2-f5.google.com (mail-dy2-f5.google.com [74.125.229.5]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77992606A322 for ; Wed, 2 Dec 2026 17:55:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 77992606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GmbjvIvV Received: by mail-dy2-f5.google.com with SMTP id 5a478bee46e88-3872c47856eso356702eec.1 for ; Wed, 02 Dec 2026 09:55:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796234155; x=1796838955; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=bhaljCpPcLQVIONpjG6K44F5nGY7L9ZejBgrsqOYb88=; b=GmbjvIvVAx6wj6j5/FGL0B3jPymj9gWW3c3kboLE6ahDNy4SbCeFk3vmx2q7uBAwXZ KiUydcuQaqDHZzKmuKRh3JywivtBox9TXOzOCzmEZLmMC5wtu3SDrg46DsFvOcEclzgQ qnG8ElUkbe3RRSnIgd7rhpWRCISp9kSezsq96NOKaXdhQinWYJqJRgmzBgj4hAf5I+iA x10VUiwoHZmDu+aZr/fCdj5rIF0o6ZnMn9Z/1vpxGtQ/a6iTYH38qSuwr66DBwT++RSD Nd/diiCMLmayx3f2W1lluIRMDqOOi/qoP1rlFc4w1SUV/S4pju+xmqFETWjrldn0QaLy 8jlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796234155; x=1796838955; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bhaljCpPcLQVIONpjG6K44F5nGY7L9ZejBgrsqOYb88=; b=udn/86lRjU3Spn6KFTbl9SFl+PUfYzxIpzQNz/SOlPeGJ+YMhkHlGamczpoFoKVGU8 jNfrJ03SCy97yJ5VlTKBr3NY196jM++yLlSP+YCfQSQxNbbCx3mBeaCPlNzUGI4JYa+i /PpVuG6f6Umbex1iNuxOJC0WZZSYp9AAy30vtKgvTzk/lnNLZ2e4hZm450z+N1mma8Jk jWh+inG68AFzhc762j5zW8XudG1+4j2VOLgk7uEvsWXX0JxV2FEW0j1/qxqNnn3ubnbz GUVyurI3pbIoMqjfzAw8DbjiI2hHba+dHleIXJVaq9/COs1hn7W+H5QK36vqpfxdtSK6 QsXA== X-Forwarded-Encrypted: i=1; AL+hErEz18K8B/y+ucuDl+vYC+MIzHsdgtTKEHsZeQBwZ59D+rY+fPPQqzgtYgdteIzwcE4mbPWIoav1iI6gyKk=@vger.kernel.org X-Gm-Message-State: APuU4Z8NWyQ105uG5ePeoo/zGMPId6sF2JV9ieIzJxkA4+P/xxUYr4l/ KrEgyDzhxEhI5fDr9w/MmEJ+i8XwKIHvovy5lsqFLDCkxKEMkuG/SF0O X-Gm-Gg: AZFdTZepknpYcdFixsbZFITjWkTdW3n7qO1V64F5JjBLQ8KryltBiG0SRI/7G2QGCjf pRUNlAoA3fzcS23i1biTVjSzj5w/AZp//vTAlJ53Jd/Ud8aplQn5SSyoO1/4Q7i209Ay8kJ+gh9 Y+INfsKLnZtlzHzxEG3LQC0BtAhXirCNRX87NuEgtaF2KxVUnc8vTvV9XNCrIuHdEbPBGNCeQPI otJJEwBXaXIF1GR2/IAJ0NRU9NjwaXqz88pIV8kWHqjUMFCsCIzYJ6szluW5vsMShKhpsOlbL8/ XFUfxAOPrI+4p1qBdiWMXBFYiq5NfXN8ACZAOOqrvxT7CpHbbB7jt8Q8SpVfjDjkaQkBQsKJgwg rPs2hURrrdVSI/L9Du/ybBUSAQktchL6FqX197DtKaT05r0BAGb50iQYt7TVvQeJOUNZOTmmrVf MmkfM/Xo9EfnKLiY2TDEQlDMBGaZR2mfozdi99NbKh9OaA9ZKBX5BgYCxYaHq+oszUPEH2+C6rY mQR6WY42VfpG38CTDkOH82fU5B8/2EWGfl1j2yAcILTwDCNyPWNTbaUhEUYU+8Yy/jSUE07nrTz O2ej97M8sOkXg+7/i8BhHCA92b9/bq/6icxq3aZVZsVafWpg5dp6gVNt X-Received: by 2002:a05:7300:c00f:10b0:386:c3ad:dc25 with SMTP id 5a478bee46e88-386c3adde30mr31257615eec.34.1796234154520; Wed, 02 Dec 2026 09:55:54 -0800 (PST) Received: from jiayi-vm-sprl-server17.local ([155.69.195.24]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385672c6f31sm117950890eec.15.2026.12.02.09.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 09:55:54 -0800 (PST) From: Jiayi Zhang To: Christian Brauner Cc: Andrew Morton , Oleg Nesterov , Pavel Tikhomirov , Jiayuan Chen , =?UTF-8?q?Michal=20Koutn=C3=BD?= , 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 Message-ID: <20261202175542.4130656-1-zhangjiayi1014@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Link: https://lore.kernel.org/all/20250425100707.GA8093@redhat.com/ Signed-off-by: Jiayi Zhang --- 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