From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796298009; b=iHJJ5Z0W1Jqw2M3xZCQaFfRiqw4kMOMDcX2Q5WH2i4Fy31vgaSFmqJv/s80OuoWbsoYq ltfxYJZtGW6nC9NIz1mAkr4WBblpc5xiq4uPIa8vUwQ1ngy/Vs53D5FSXutmL6NKwmv7i 5zLs3DeaATVp9bp16tw5h8+kaVURgMOKDXtgiyaEd7yhy85PylwCi2ULVX+SSGgJ17E1E cKpOkvZmhrqn2xm/xnn/vreLY/WLJlpg2AEy4wVHQ72jL6eZGYjaPxsxMwdrD4Nn9Lx48 KjfnMg86+mO8niNRb635Z3ulHOwH2CDjD27xVIcxoY6O0B4/r9vNyfoYTF5iavI9w6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298009; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=NIaHN/raNe8gaHdkRaoMX3PIP3mFftt2N/75nvsCiBI=; b=xr3jDVTeKQSK6vLtgJU+ApNIavSQ6u92/C5I4oZ6a9M4x452G/wmC1qjpsrxMSkFuSHP 7jyoaFEBzU6jikMB754dj2R/P7qYfWd0DKoAbvMmhWDHX8tZpxDunvYBHHaS5Ik6wnnOS 7s75MrdG6mG2BsKbK/klCSWgwYRSLgr5ogwTKeEhjheMgEd5xUIBfF+ofosl5biu2FLTp T6f81NE7qljKxEVLmxVGIz+q+MPoZrHcwBj8I1vLwNLkNT4cSFzSow6NC7XjpFbQ6WD8M a3kY2apQ/JDtehhup/aBAwkoBfOHYEzvbJS/HqLSVV7igLD6P1NJNkGCXOGIoVcthSw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=M1pzcO3I; arc=none smtp.remote-ip=95.215.58.170 Received: from mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (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 B26EC606A32F for ; Thu, 3 Dec 2026 11:40:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B26EC606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=M1pzcO3I X-Envelope-To: driver-core@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=Zm44x1fiMSwF3qzRVtIECe+IcN3V0rNCF5TUlAPAoMQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796298005; v=1; x=1796902805; b=M1pzcO3ItopswceANW2lRSC9M7fqiSYrTAuhHvtpNXKPwDlcVxD/w8f0aTfpDDR9MB0tbugT 2u7vGK88wxzUSnMS3do7/4DfMzo7TGPxdnOGZenYGlXQbPxBk7wPbHIZqvU0IgvANMKTtePpeNF qHhMBZ83ebgQvzK6a5pBb0Hc= X-Envelope-To: driver-core@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id 0c950e4e4666c455; Thu, 03 Dec 2026 11:40:02 +0000 X-Mizu-Trace-ID: 0c950e4e4666c455 X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Christian Brauner Cc: Greg Kroah-Hartman , Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Chengming Zhou , Johannes Weiner , Reinette Chatre , Babu Moger , driver-core@lists.linux.dev, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, stable@vger.kernel.org Subject: [PATCH] kernfs: check the active count only before a node's first activation Date: Thu, 3 Dec 2026 03:39:49 -0800 Message-ID: <20261203113949.1415267-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Writing 0 and then 1 to cgroup.pressure hides a cgroup's pressure files and shows them again with kernfs_show(). If a PSI trigger is written to one of these files at the same time, the show can hit this warning: WARNING: fs/kernfs/dir.c:1480 at kernfs_activate_one+0xe6/0xf0 Call Trace: kernfs_show+0xa8/0xe0 cgroup_pressure_write+0x17c/0x220 cgroup_file_write+0x17f/0x440 kernfs_fop_write_iter+0x20c/0x2d0 The trigger writer breaks its active protection in cgroup_kn_lock_live() and waits for cgroup_mutex. The hide cannot wait for the writer, because it holds cgroup_mutex. The writer then takes its reference back with kernfs_unbreak_active_protection(), so the hidden node has one active reference. If the show runs before the writer drops it, kernfs_activate_one() warns. The count is still correct: the writer drops the reference when it finishes. Nothing can hold an active reference before a node's first activation, so check the count only then, before the early return, which also covers a node that is hidden at that point. resctrl can hit the same warning since commit 2d77f9768850 ("fs/resctrl: Prevent deadlock and use-after-free in info file handlers"): its info file handlers break active protection on files it hides and shows. Commit 071d8e4c2a3b ("kernfs: Relax constraint in draining guard") relaxed the same check in kernfs_should_drain_open_files(). Tested in a VM with commit 8996b7c8174a ("kernfs: recheck of->released after acquiring the active reference") applied, which fixes a separate crash in the same test. Six threads write PSI triggers while another thread writes 0 and 1 to cgroup.pressure for 15 seconds. Without this change the warning fired in all 8 runs, also when an unprivileged user does all of it in a delegated cgroup. With it, there was no warning in 11 runs, though a debug counter showed the show still found the extra reference. The kernfs and cgroup selftests pass. Fixes: 783bd07d095b ("kernfs: Implement kernfs_show()") Cc: stable@vger.kernel.org Signed-off-by: Shakeel Butt --- fs/kernfs/dir.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 4cb505242acb..970cc1cd58e4 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1471,13 +1471,20 @@ static void kernfs_activate_one(struct kernfs_node *kn) { lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); - kn->flags |= KERNFS_ACTIVATED; + /* + * Before the first activation nothing can hold an active reference, + * even on a hidden node. A node that kernfs_show() shows again can + * still hold one, taken back by kernfs_unbreak_active_protection(). + */ + if (!(kn->flags & KERNFS_ACTIVATED)) { + WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); + kn->flags |= KERNFS_ACTIVATED; + } if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING))) return; WARN_ON_ONCE(rcu_access_pointer(kn->__parent) && RB_EMPTY_NODE(&kn->rb)); - WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); } @@ -1710,8 +1717,10 @@ void kernfs_unbreak_active_protection(struct kernfs_node *kn) * finishes and this temporary bump can't break anything. If @kn * is alive, nothing changes. If @kn is being deactivated, the * soon-to-follow put will either finish deactivation or restore - * deactivated state. If @kn is already removed, the temporary - * bump is guaranteed to be gone before @kn is released. + * deactivated state. If kernfs_show() hides and shows @kn again + * before the put, the put still drops the bump. If @kn is already + * removed, the temporary bump is guaranteed to be gone before @kn + * is released. */ atomic_inc(&kn->active); if (kernfs_lockdep(kn)) -- 2.53.1-Meta