From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64DFD18B476; Tue, 28 Jan 2025 08:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053758; cv=none; b=M2ZOem6p6CSNAjMBmPAPI5KGGg4mK7U3ulywUsaiKCzkNV273qXYItUBNvRDlIhHPeG39Y4zQxAsqIabFejf/f6J04eTjI2FeW7iBUjnEsDHFtg8XRCnmDhxJmpZtitjgBZPedStYvYEgNVgvxMiFfHXvOIJvYwkF8V9yw+HB6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053758; c=relaxed/simple; bh=O7QYnNqtfos+KX02uWpBSuTC6KY2eysR/3Ql446iBi8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HTJjppFsi+1hWjpKoHg4cufwSWwXjVQhGGF6c5Kjn9KJC1Jv/UEhycuNaAvW53ThglYJuugl70v3qbp07r08EToHtjyCQWIKftgIqCQcDt74xbs26ORZCKh1pCaI5XA08fIR8NN0P8czYd/JtGZ9iRIxMcUU6i3SZcVvgNgZ9Us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mk9aIm8p; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=V/1FyF1E; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mk9aIm8p"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="V/1FyF1E" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053755; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=70ZpbL95PFOkiQLvqaDhiqcr9y/+An7eazJ7AAHJWk4=; b=mk9aIm8pvzmp8LFMYBvY2ouAaIONV2R98V5ypGvjssokmV4CmSViqD1uX6TsYt/AH87CA/ HlhIwAbcH+j08SYW7gZR7cxvchH3k2toeyQBGV294S6hj+qo5TJoyPv3oFtPqXguROPu+f jWIr+lLLflfSiylQd9FJ8fxURAGexcbAHB6WnSadQFEIGP26GymDSu+GfYcv3P3tUGheDb EJrmF8OMBCN4C/JYDQgbnaXCBrSfdtbD+LM/ALtWm1GhRYfKURrVSgQWbby5Xbc9PlTyL9 kdckCITEM5Q45U6hHHD3uN5pn400bSFh+cbu+nMlr4rQ5169UEh7B/hPgqQPTw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053755; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=70ZpbL95PFOkiQLvqaDhiqcr9y/+An7eazJ7AAHJWk4=; b=V/1FyF1E4oiv459EHmFtXZtVoOfZGCQiVU13YduLGk8YeH70i5yFkNHBKxOgOrNIsL3r5x N7bnVcJa4qM+ykBQ== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior Subject: [PATCH v5 1/6] kernfs: Acquire kernfs_rwsem in kernfs_notify_workfn(). Date: Tue, 28 Jan 2025 09:42:21 +0100 Message-ID: <20250128084226.1499291-2-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernfs_notify_workfn() dereferences kernfs_node::name and passes it later to fsnotify(). If the node is renamed then the previously observed name pointer becomes invalid. Acquire kernfs_root::kernfs_rwsem to block renames of the node. Acked-by: Tejun Heo Signed-off-by: Sebastian Andrzej Siewior --- fs/kernfs/file.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 8502ef68459b9..165d8e37976ba 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -911,6 +911,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) /* kick fsnotify */ =20 down_read(&root->kernfs_supers_rwsem); + down_read(&root->kernfs_rwsem); list_for_each_entry(info, &kernfs_root(kn)->supers, node) { struct kernfs_node *parent; struct inode *p_inode =3D NULL; @@ -947,6 +948,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) iput(inode); } =20 + up_read(&root->kernfs_rwsem); up_read(&root->kernfs_supers_rwsem); kernfs_put(kn); goto repeat; --=20 2.47.2 From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 373A11DE8B5; Tue, 28 Jan 2025 08:42:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053759; cv=none; b=GXo7Qo+FkppA7if7oI7uxdRd0WeLOyxqHnBPo2jg2ii/Zfga7ThkSVbGB6jNAtR36m45J6to9g/fw8bTDptxBty/nx1bmqfZmW4texkcxa9tzCV/3CVIYug29vHWqZjBFf97O+dYxyHTvsKftovsqUm6GN3OXBSUr2Vguh2eb+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053759; c=relaxed/simple; bh=DGTpLamWHUkgj5xFOJwd0o2/wv8b1YO9QjSjYXScsrs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YSz8dou6ZtS1kXMvME4o2+iVzQB81aHEkvhbIc3dFOozO3e7TxygCl6B+YQAO09qzivq8KqQHyT2SjYo+w7FWruTMawtDq55gbYo7pU1JM1arqpFttzeyyIujsFSTXC1eQQobn4MWnrJkg7UMFk9nDdiMsZjfiXduLrN3xXzdxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=THq9c1/z; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=oQBTaN83; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="THq9c1/z"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oQBTaN83" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oAwgdfGiuXTUsdwQtdHn2gNdWY/bb3vNi/5j2k4yIQY=; b=THq9c1/zt7lDVu4LlRpBM88yHwBjb0Z+y5sKmgJYr5Ft1ciKvqICKJEjohZ5IZr5bkclbv 4qCrHsIYEPW2CFaWDch7kOFJN5O4IFF2aNsK4ZU7iFIOpd0cXJQZ/Bgne4jNmM/H9FJjmR hbwQvqUziFEIQmwqKuMCFNSeui0urf3yDelXGWAhoFb38C3fCO3xUr7yPrAxt0PYhIJxdF /XR45mXeCf1JeYgyt4PF2EkufpVAlXHCZuX3gjvnhnJ2tO8kM6wGPjBfVR7no8FR11g4wO V6Yd7kFMS6I+TwkLf1mrrXruDH29EebqTroQBR8DTNSF+lqW6+h8rsHCENYiDw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oAwgdfGiuXTUsdwQtdHn2gNdWY/bb3vNi/5j2k4yIQY=; b=oQBTaN83NWtdq87iKQQv1xQ11g+tCao10RcSfcTAiPmzXaVI0JY50T4tWdKMQHo74ou57U Y2WAy/FCWNuw4CAw== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior Subject: [PATCH v5 2/6] kernfs: Acquire kernfs_rwsem in kernfs_get_parent_dentry(). Date: Tue, 28 Jan 2025 09:42:22 +0100 Message-ID: <20250128084226.1499291-3-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernfs_get_parent_dentry() passes kernfs_node::parent to kernfs_get_inode(). Acquire kernfs_root::kernfs_rwsem to ensure kernfs_node::parent isn't replaced during the operation. Acked-by: Tejun Heo Signed-off-by: Sebastian Andrzej Siewior --- fs/kernfs/mount.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index 1358c21837f1a..b9b16e97bff18 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -145,7 +145,9 @@ static struct dentry *kernfs_fh_to_parent(struct super_= block *sb, static struct dentry *kernfs_get_parent_dentry(struct dentry *child) { struct kernfs_node *kn =3D kernfs_dentry_node(child); + struct kernfs_root *root =3D kernfs_root(kn); =20 + guard(rwsem_read)(&root->kernfs_rwsem); return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent)); } =20 --=20 2.47.2 From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDC9A1DF276; Tue, 28 Jan 2025 08:42:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053760; cv=none; b=gpyW3TbTOnkX8LP5l4rt8osOol7e69F1v0SRAktE2OAs9OTBEsj7rPpv4INlbGfU86bz9V7LdqUPBL0Nr41jyIc8YrITPVOExqIIaHbnEHSWlGqH8WwTNQffKCYccOpNSNmDlmal2hs0wZnjW/DM/sndH02I895Phhu/KiBX6aA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053760; c=relaxed/simple; bh=+kwU2zFUMmyb0jS6OU6MFBxnBiiwKa4vGC/wQUaUfq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tj+VAz15gVq7D0vqWnYP2ogHARqzhu1KlovyRbrHhhAnwj0clN+G96Vcm9JKlXUna1Oqvqyib2AEyDjLCl+Nx1cHnQnIB4Oqan3jxHLKKjy8q7jePZ1jkGpOs3PGEm0j6SDC7tYbVPReklQ0Mga0h+svF/S/JuR4TD8Ga0wKKoc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ewSz62oL; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Px56zWbN; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ewSz62oL"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Px56zWbN" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TQFaRwsMWqeRZCHTYyBsT4gxYaSSc81PYJjjcc6W9Sk=; b=ewSz62oLj2/KMFS48hv7Y0XNGe0UJ6XG1CNtyxntwmZk7fYcMPj09tAbLZx0w460/mol9p RXuvG1C+BZhMc/xgevFnD9yRwI/m+vGZl/A2J2eIst9Tj/eiAYjgbjRyylonZyqQ1v9Yof XPn86EsWFfmyJNfbvuPS0+oZEW88xt6JKUA3oGQuxaECzXFDi/oarS57jw4Z8wo4uN4v4h QZSxjbSKawahhqQc4j//2dh77n0Rf3No9rWY/EScWa49o6zG/5gq780RS7iH+eN4zTWL7V zvJSZQxDhtx4A3Qpd3FVP0Q6+HgqjSBBse+hwMO2DE5SAUuRpqbQTGEnGCAR1A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TQFaRwsMWqeRZCHTYyBsT4gxYaSSc81PYJjjcc6W9Sk=; b=Px56zWbNjSZ0+OYjnW/jj4xKl9YoAVAYBr92oMtITr2Alv32/CjA/Pyjmvg35uKTlJBLWB 866Sgjwpe6JOT6CA== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior Subject: [PATCH v5 3/6] kernfs: Acquire kernfs_rwsem in kernfs_node_dentry(). Date: Tue, 28 Jan 2025 09:42:23 +0100 Message-ID: <20250128084226.1499291-4-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernfs_node_dentry() passes kernfs_node::name to lookup_positive_unlocked(). Acquire kernfs_root::kernfs_rwsem to ensure the node is not renamed during the operation. Acked-by: Tejun Heo Signed-off-by: Sebastian Andrzej Siewior --- fs/kernfs/mount.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index b9b16e97bff18..4a0ff08d589ca 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -209,6 +209,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, { struct dentry *dentry; struct kernfs_node *knparent; + struct kernfs_root *root; =20 BUG_ON(sb->s_op !=3D &kernfs_sops); =20 @@ -218,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, if (!kn->parent) return dentry; =20 + root =3D kernfs_root(kn); + guard(rwsem_read)(&root->kernfs_rwsem); + knparent =3D find_next_ancestor(kn, NULL); if (WARN_ON(!knparent)) { dput(dentry); --=20 2.47.2 From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF65F1DF997; Tue, 28 Jan 2025 08:42:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053762; cv=none; b=dzGofJdyxejUo01yB67DvsOkVAHqGQiUfrfJK+H9+JyOZQ21MS2BLppXoYBE1zlqQq41wZWC9StSQ1Vnh62HZrHbUUnv3Vr8rAkY+yr9FerEHrgCf/JevEXXX9iThv4IZWCoZhyeH6SjUIW7E/31XhTQ53cfhmr1jzVzmcXeJCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053762; c=relaxed/simple; bh=tR45VY94czRZPH94WmhYPOQ1mLEIOTHPhxsX88grZxc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NufsLL37YUGHpNX8HEz07bv6MMHfm7Q6JZw87zdIBi7Ihj4WMs3FbGFTzV8dcipnlKUSAKH0E0guqeA8DLhxaFy7pGUsoBCMwWQBAM2wGMYAVVQwvdevY7OrXjJXJD7AAmdqHyZyzO09ZIyjc5AjGezksBT+9xJK8neF7cnej4Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GKESxpaf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ciei6yNc; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GKESxpaf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ciei6yNc" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jHJX6bEa0XtyQR0u+s3KZKeiU8ZuhShX3XwcYletJb0=; b=GKESxpafRsgm7IButNytWF04BcV6DnCs/5V2lHp313vJx0AMJ9oDsCIR8blzhQTmWnZfSd qDF1N5DFIYc2YrXogFVkFTnzsdrpbxRagTdtlsJ+xr+IQr9kZ6GHhuYfct9Rm6AwOsWhPc eDRGnQpRLq6EFgU4bRnUgbN+6fwjyd94tiV17SYaxK1DDqOlOq11YZxUsKayhBwR6grJth PeqKwD8Wib1BfiarmNUJunDpwthI7PH7VcsJyOgJ7FGihoD/+MM+OPPphr5hJOmV8b54Nn h+KzX8XbZGjehB8i2lt0mB5V8g9TK2Zn4EJ4GunVccbNNXP/NUgGCOZf9v2ekA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053758; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jHJX6bEa0XtyQR0u+s3KZKeiU8ZuhShX3XwcYletJb0=; b=ciei6yNcXBLnhB9mBSfCrvcJCLLx1xOIptlZhPhp09Jh/Q/t/Kfqc6V59Fl9bw5hiqjqVH Ug7knbuQGsHZppDg== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior Subject: [PATCH v5 4/6] kernfs: Don't re-lock kernfs_root::kernfs_rwsem in kernfs_fop_readdir(). Date: Tue, 28 Jan 2025 09:42:24 +0100 Message-ID: <20250128084226.1499291-5-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The readdir operation iterates over all entries and invokes dir_emit() for every entry passing kernfs_node::name as argument. Since the name argument can change, and become invalid, the kernfs_root::kernfs_rwsem lock should not be dropped to prevent renames during the operation. The lock drop around dir_emit() has been initially introduced in commit 1e5289c97bba2 ("sysfs: Cache the last sysfs_dirent to improve readdir sc= alability v2") to avoid holding a global lock during a page fault. The lock drop is wrong since the support of renames and not a big burden since the lock is no longer global. Don't re-acquire kernfs_root::kernfs_rwsem while copying the name to the userpace buffer. Acked-by: Tejun Heo Signed-off-by: Sebastian Andrzej Siewior --- fs/kernfs/dir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 458519e416fe7..5a1fea414996e 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -1868,10 +1868,10 @@ static int kernfs_fop_readdir(struct file *file, st= ruct dir_context *ctx) file->private_data =3D pos; kernfs_get(pos); =20 - up_read(&root->kernfs_rwsem); - if (!dir_emit(ctx, name, len, ino, type)) + if (!dir_emit(ctx, name, len, ino, type)) { + up_read(&root->kernfs_rwsem); return 0; - down_read(&root->kernfs_rwsem); + } } up_read(&root->kernfs_rwsem); file->private_data =3D NULL; --=20 2.47.2 From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF6051DF98E; Tue, 28 Jan 2025 08:42:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053763; cv=none; b=Yi15Nzxeo+pvQX0gbeh8TnPYcp7JQZoZctgGY13RKkZk51FDwoiyrHHxXyKOUBmJo6lN8lq63U4thf5thN82C1O3H60HF7ZiDJPbiIl/uEz+eALoYtNL1v3O4cAmj6TJP5tqF4NM2ZBvzlGaF2XkSl3dJ8AloIeGyYO1xw4w7cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053763; c=relaxed/simple; bh=LcNn8VspTbWU7c5co5IbX35mXFlftAEI4Ug9PACrEuk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UQc1hKLabQWXgFjoLWB0uvaFRvb06kruuL3pJ0SdgT1H+N6ZklXxVottIEwrHBTVsF3QR0iyJOJBHKFOOCtKnBlz70cjbn2Ajj+dHW8rBujZlSODv58HG3nDMCbhSWLupWYSjPpmtpyu6J2ZGITy3Y7g4ixef+BMinmY+stsNkY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qomn05Qp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HB7MCVK0; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qomn05Qp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HB7MCVK0" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053759; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g/r9njrhZBYcYvy4TMMIleL6jR5epGWcAWbXgu+TA0A=; b=qomn05QpIVvZuOAwKTNW2qha9OliJtTlJPz/pt1k5xB4Uf26z8KzQLmcmfcuqSMYg9wB2I BzSC2LadXFHLAaEBBIF/8WeRjvrsHjbHuPnxohLRq6OT7y17pxEDbh3CmLixnD+ELYWpbl RK0HmKa7IYYQiB5xPN1gp13hA9nW4AhDfUbZUzzhKjPVyVj3TsYZ8aEVklfia3Mq63saHp 1bmXKRSkGahqEvaJTrF0oHutmy38lVNVRihj4mn+C25YQ9hNyfxkH2FYzJY/sYn5feGtGk KfBGOd/RrW/Mg0aoYy5jX2KJEY7Zc/jIMui1QP/yQmnu3sXi+ilZbrAy60lMHw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053759; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g/r9njrhZBYcYvy4TMMIleL6jR5epGWcAWbXgu+TA0A=; b=HB7MCVK0RnJtMNT3Ylvtn7mGtmsxNEBflqXCcjZF0cQbLtZ2e0HgkxA7fcIVVzOrz77BOc GUSzAGNyLddkNLBg== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior Subject: [PATCH v5 5/6] kernfs: Use RCU to access kernfs_node::parent. Date: Tue, 28 Jan 2025 09:42:25 +0100 Message-ID: <20250128084226.1499291-6-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernfs_rename_lock is used to obtain stable kernfs_node::{name|parent} pointer. This is a preparation to access kernfs_node::parent under RCU and ensure that the pointer remains stable under the RCU lifetime guarantees. For a complete path, as it is done in kernfs_path_from_node(), the kernfs_rename_lock is still required in order to obtain a stable parent relationship while computing the relevant node depth. This must not change while the nodes are inspected in order to build the path. If the kernfs user never moves the nodes (changes the parent) then the kernfs_rename_lock is not required and the RCU guarantees are sufficient. This "restriction" can be set with KERNFS_ROOT_INVARIANT_PARENT. Otherwise the lock is required. Rename kernfs_node::parent to kernfs_node::__parent to denote the RCU access and use RCU accessor while accessing the node. Make cgroup use KERNFS_ROOT_INVARIANT_PARENT since the parent here can not change. Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 65 +++++++++++------ fs/kernfs/dir.c | 99 +++++++++++++++----------- fs/kernfs/kernfs-internal.h | 32 ++++++++- fs/kernfs/mount.c | 10 +-- fs/kernfs/symlink.c | 23 +++--- fs/sysfs/file.c | 24 ++++--- include/linux/kernfs.h | 10 ++- kernel/cgroup/cgroup-v1.c | 2 +- kernel/cgroup/cgroup.c | 24 +++++-- 9 files changed, 193 insertions(+), 96 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/r= esctrl/rdtgroup.c index d906a1cd84917..d9e24100fd8b0 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -947,10 +947,20 @@ static int rdt_last_cmd_status_show(struct kernfs_ope= n_file *of, return 0; } =20 +static void *rdt_get_kn_parent_priv(struct kernfs_node *kn) +{ + /* + * The parent pointer is only valid within RCU section since it can be + * replaced. + */ + guard(rcu)(); + return rcu_dereference(kn->__parent)->priv; +} + static int rdt_num_closids_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); =20 seq_printf(seq, "%u\n", s->num_closid); return 0; @@ -959,7 +969,7 @@ static int rdt_num_closids_show(struct kernfs_open_file= *of, static int rdt_default_ctrl_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%x\n", r->default_ctrl); @@ -969,7 +979,7 @@ static int rdt_default_ctrl_show(struct kernfs_open_fil= e *of, static int rdt_min_cbm_bits_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%u\n", r->cache.min_cbm_bits); @@ -979,7 +989,7 @@ static int rdt_min_cbm_bits_show(struct kernfs_open_fil= e *of, static int rdt_shareable_bits_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%x\n", r->cache.shareable_bits); @@ -1003,7 +1013,7 @@ static int rdt_shareable_bits_show(struct kernfs_open= _file *of, static int rdt_bit_usage_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); /* * Use unsigned long even though only 32 bits are used to ensure * test_bit() is used safely. @@ -1085,7 +1095,7 @@ static int rdt_bit_usage_show(struct kernfs_open_file= *of, static int rdt_min_bw_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%u\n", r->membw.min_bw); @@ -1095,7 +1105,7 @@ static int rdt_min_bw_show(struct kernfs_open_file *o= f, static int rdt_num_rmids_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); =20 seq_printf(seq, "%d\n", r->num_rmid); =20 @@ -1105,7 +1115,7 @@ static int rdt_num_rmids_show(struct kernfs_open_file= *of, static int rdt_mon_features_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); struct mon_evt *mevt; =20 list_for_each_entry(mevt, &r->evt_list, list) { @@ -1120,7 +1130,7 @@ static int rdt_mon_features_show(struct kernfs_open_f= ile *of, static int rdt_bw_gran_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%u\n", r->membw.bw_gran); @@ -1130,7 +1140,7 @@ static int rdt_bw_gran_show(struct kernfs_open_file *= of, static int rdt_delay_linear_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%u\n", r->membw.delay_linear); @@ -1148,7 +1158,7 @@ static int max_threshold_occ_show(struct kernfs_open_= file *of, static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 if (r->membw.throttle_mode =3D=3D THREAD_THROTTLE_PER_THREAD) @@ -1213,7 +1223,7 @@ static enum resctrl_conf_type resctrl_peer_type(enum = resctrl_conf_type my_type) static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct resctrl_schema *s =3D of->kn->parent->priv; + struct resctrl_schema *s =3D rdt_get_kn_parent_priv(of->kn); struct rdt_resource *r =3D s->res; =20 seq_printf(seq, "%u\n", r->cache.arch_has_sparse_bitmasks); @@ -1625,7 +1635,7 @@ static int mbm_config_show(struct seq_file *s, struct= rdt_resource *r, u32 evtid static int mbm_total_bytes_config_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); =20 mbm_config_show(seq, r, QOS_L3_MBM_TOTAL_EVENT_ID); =20 @@ -1635,7 +1645,7 @@ static int mbm_total_bytes_config_show(struct kernfs_= open_file *of, static int mbm_local_bytes_config_show(struct kernfs_open_file *of, struct seq_file *seq, void *v) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); =20 mbm_config_show(seq, r, QOS_L3_MBM_LOCAL_EVENT_ID); =20 @@ -1741,7 +1751,7 @@ static ssize_t mbm_total_bytes_config_write(struct ke= rnfs_open_file *of, char *buf, size_t nbytes, loff_t off) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); int ret; =20 /* Valid input requires a trailing newline */ @@ -1767,7 +1777,7 @@ static ssize_t mbm_local_bytes_config_write(struct ke= rnfs_open_file *of, char *buf, size_t nbytes, loff_t off) { - struct rdt_resource *r =3D of->kn->parent->priv; + struct rdt_resource *r =3D rdt_get_kn_parent_priv(of->kn); int ret; =20 /* Valid input requires a trailing newline */ @@ -2429,12 +2439,13 @@ static struct rdtgroup *kernfs_to_rdtgroup(struct k= ernfs_node *kn) * resource. "info" and its subdirectories don't * have rdtgroup structures, so return NULL here. */ - if (kn =3D=3D kn_info || kn->parent =3D=3D kn_info) + if (kn =3D=3D kn_info || + rcu_access_pointer(kn->__parent) =3D=3D kn_info) return NULL; else return kn->priv; } else { - return kn->parent->priv; + return rdt_get_kn_parent_priv(kn); } } =20 @@ -3758,9 +3769,18 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtg= rp, cpumask_var_t tmpmask) return 0; } =20 +static struct kernfs_node *rdt_kn_parent(struct kernfs_node *kn) +{ + /* + * Valid within the RCU section it was obtained or while rdtgroup_mutex + * is held. + */ + return rcu_dereference_check(kn->__parent, lockdep_is_held(&rdtgroup_mute= x)); +} + static int rdtgroup_rmdir(struct kernfs_node *kn) { - struct kernfs_node *parent_kn =3D kn->parent; + struct kernfs_node *parent_kn; struct rdtgroup *rdtgrp; cpumask_var_t tmpmask; int ret =3D 0; @@ -3773,6 +3793,7 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) ret =3D -EPERM; goto out; } + parent_kn =3D rdt_kn_parent(kn); =20 /* * If the rdtgroup is a ctrl_mon group and parent directory @@ -3841,6 +3862,7 @@ static void mongrp_reparent(struct rdtgroup *rdtgrp, static int rdtgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, const char *new_name) { + struct kernfs_node *kn_parent; struct rdtgroup *new_prdtgrp; struct rdtgroup *rdtgrp; cpumask_var_t tmpmask; @@ -3875,8 +3897,9 @@ static int rdtgroup_rename(struct kernfs_node *kn, goto out; } =20 - if (rdtgrp->type !=3D RDTMON_GROUP || !kn->parent || - !is_mon_groups(kn->parent, kn->name)) { + kn_parent =3D rdt_kn_parent(kn); + if (rdtgrp->type !=3D RDTMON_GROUP || !kn_parent || + !is_mon_groups(kn_parent, kn->name)) { rdt_last_cmd_puts("Source must be a MON group\n"); ret =3D -EPERM; goto out; diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 5a1fea414996e..16d268345e3b7 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ =20 #include "kernfs-internal.h" =20 -static DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ +DEFINE_RWLOCK(kernfs_rename_lock); /* kn->parent and ->name */ /* * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to * call pr_cont() while holding rename_lock. Because sometimes pr_cont() @@ -56,7 +56,7 @@ static int kernfs_name_locked(struct kernfs_node *kn, cha= r *buf, size_t buflen) if (!kn) return strscpy(buf, "(null)", buflen); =20 - return strscpy(buf, kn->parent ? kn->name : "/", buflen); + return strscpy(buf, rcu_access_pointer(kn->__parent) ? kn->name : "/", bu= flen); } =20 /* kernfs_node_depth - compute depth from @from to @to */ @@ -64,9 +64,9 @@ static size_t kernfs_depth(struct kernfs_node *from, stru= ct kernfs_node *to) { size_t depth =3D 0; =20 - while (to->parent && to !=3D from) { + while (rcu_dereference(to->__parent) && to !=3D from) { depth++; - to =3D to->parent; + to =3D rcu_dereference(to->__parent); } return depth; } @@ -84,18 +84,18 @@ static struct kernfs_node *kernfs_common_ancestor(struc= t kernfs_node *a, db =3D kernfs_depth(rb->kn, b); =20 while (da > db) { - a =3D a->parent; + a =3D rcu_dereference(a->__parent); da--; } while (db > da) { - b =3D b->parent; + b =3D rcu_dereference(b->__parent); db--; } =20 /* worst case b and a will be the same at root */ while (b !=3D a) { - b =3D b->parent; - a =3D a->parent; + b =3D rcu_dereference(b->__parent); + a =3D rcu_dereference(a->__parent); } =20 return a; @@ -168,8 +168,9 @@ static int kernfs_path_from_node_locked(struct kernfs_n= ode *kn_to, =20 /* Calculate how many bytes we need for the rest */ for (i =3D depth_to - 1; i >=3D 0; i--) { + for (kn =3D kn_to, j =3D 0; j < i; j++) - kn =3D kn->parent; + kn =3D rcu_dereference(kn->__parent); =20 len +=3D scnprintf(buf + len, buflen - len, "/%s", kn->name); } @@ -226,6 +227,7 @@ int kernfs_path_from_node(struct kernfs_node *to, struc= t kernfs_node *from, unsigned long flags; int ret; =20 + guard(rcu)(); read_lock_irqsave(&kernfs_rename_lock, flags); ret =3D kernfs_path_from_node_locked(to, from, buf, buflen); read_unlock_irqrestore(&kernfs_rename_lock, flags); @@ -295,7 +297,7 @@ struct kernfs_node *kernfs_get_parent(struct kernfs_nod= e *kn) unsigned long flags; =20 read_lock_irqsave(&kernfs_rename_lock, flags); - parent =3D kn->parent; + parent =3D kernfs_parent(kn); kernfs_get(parent); read_unlock_irqrestore(&kernfs_rename_lock, flags); =20 @@ -360,8 +362,12 @@ static int kernfs_sd_compare(const struct kernfs_node = *left, */ static int kernfs_link_sibling(struct kernfs_node *kn) { - struct rb_node **node =3D &kn->parent->dir.children.rb_node; struct rb_node *parent =3D NULL; + struct kernfs_node *kn_parent; + struct rb_node **node; + + kn_parent =3D kernfs_parent(kn); + node =3D &kn_parent->dir.children.rb_node; =20 while (*node) { struct kernfs_node *pos; @@ -380,13 +386,13 @@ static int kernfs_link_sibling(struct kernfs_node *kn) =20 /* add new node and rebalance the tree */ rb_link_node(&kn->rb, parent, node); - rb_insert_color(&kn->rb, &kn->parent->dir.children); + rb_insert_color(&kn->rb, &kn_parent->dir.children); =20 /* successfully added, account subdir number */ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); if (kernfs_type(kn) =3D=3D KERNFS_DIR) - kn->parent->dir.subdirs++; - kernfs_inc_rev(kn->parent); + kn_parent->dir.subdirs++; + kernfs_inc_rev(kn_parent); up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); =20 return 0; @@ -407,16 +413,19 @@ static int kernfs_link_sibling(struct kernfs_node *kn) */ static bool kernfs_unlink_sibling(struct kernfs_node *kn) { + struct kernfs_node *kn_parent; + if (RB_EMPTY_NODE(&kn->rb)) return false; =20 + kn_parent =3D kernfs_parent(kn); down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); if (kernfs_type(kn) =3D=3D KERNFS_DIR) - kn->parent->dir.subdirs--; - kernfs_inc_rev(kn->parent); + kn_parent->dir.subdirs--; + kernfs_inc_rev(kn_parent); up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); =20 - rb_erase(&kn->rb, &kn->parent->dir.children); + rb_erase(&kn->rb, &kn_parent->dir.children); RB_CLEAR_NODE(&kn->rb); return true; } @@ -558,11 +567,7 @@ void kernfs_put(struct kernfs_node *kn) return; root =3D kernfs_root(kn); repeat: - /* - * Moving/renaming is always done while holding reference. - * kn->parent won't change beneath us. - */ - parent =3D kn->parent; + parent =3D kernfs_parent(kn); =20 WARN_ONCE(atomic_read(&kn->active) !=3D KN_DEACTIVATED_BIAS, "kernfs_put: %s/%s: released with incorrect active_ref %d\n", @@ -701,7 +706,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node = *parent, name, mode, uid, gid, flags); if (kn) { kernfs_get(parent); - kn->parent =3D parent; + rcu_assign_pointer(kn->__parent, parent); } return kn; } @@ -769,13 +774,14 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(st= ruct kernfs_root *root, */ int kernfs_add_one(struct kernfs_node *kn) { - struct kernfs_node *parent =3D kn->parent; - struct kernfs_root *root =3D kernfs_root(parent); + struct kernfs_root *root =3D kernfs_root(kn); struct kernfs_iattrs *ps_iattr; + struct kernfs_node *parent; bool has_ns; int ret; =20 down_write(&root->kernfs_rwsem); + parent =3D kernfs_parent(kn); =20 ret =3D -EINVAL; has_ns =3D kernfs_ns_enabled(parent); @@ -949,6 +955,11 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kern= fs_node *parent, return kn; } =20 +unsigned int kernfs_root_flags(struct kernfs_node *kn) +{ + return kernfs_root(kn)->flags; +} + /** * kernfs_create_root - create a new kernfs hierarchy * @scops: optional syscall operations for the hierarchy @@ -1111,7 +1122,7 @@ struct kernfs_node *kernfs_create_empty_dir(struct ke= rnfs_node *parent, =20 static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) { - struct kernfs_node *kn; + struct kernfs_node *kn, *parent; struct kernfs_root *root; =20 if (flags & LOOKUP_RCU) @@ -1162,8 +1173,9 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) if (!kernfs_active(kn)) goto out_bad; =20 + parent =3D kernfs_parent(kn); /* The kernfs node has been moved? */ - if (kernfs_dentry_node(dentry->d_parent) !=3D kn->parent) + if (kernfs_dentry_node(dentry->d_parent) !=3D parent) goto out_bad; =20 /* The kernfs node has been renamed */ @@ -1171,7 +1183,7 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) goto out_bad; =20 /* The kernfs node has been moved to a different namespace */ - if (kn->parent && kernfs_ns_enabled(kn->parent) && + if (parent && kernfs_ns_enabled(parent) && kernfs_info(dentry->d_sb)->ns !=3D kn->ns) goto out_bad; =20 @@ -1364,7 +1376,7 @@ static struct kernfs_node *kernfs_next_descendant_pos= t(struct kernfs_node *pos, return kernfs_leftmost_descendant(rb_to_kn(rbn)); =20 /* no sibling left, visit parent */ - return pos->parent; + return kernfs_parent(pos); } =20 static void kernfs_activate_one(struct kernfs_node *kn) @@ -1376,7 +1388,7 @@ static void kernfs_activate_one(struct kernfs_node *k= n) if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING))) return; =20 - WARN_ON_ONCE(kn->parent && RB_EMPTY_NODE(&kn->rb)); + WARN_ON_ONCE(kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb)); WARN_ON_ONCE(atomic_read(&kn->active) !=3D KN_DEACTIVATED_BIAS); =20 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); @@ -1446,7 +1458,7 @@ void kernfs_show(struct kernfs_node *kn, bool show) =20 static void __kernfs_remove(struct kernfs_node *kn) { - struct kernfs_node *pos; + struct kernfs_node *pos, *parent; =20 /* Short-circuit if non-root @kn has already finished removal. */ if (!kn) @@ -1458,7 +1470,7 @@ static void __kernfs_remove(struct kernfs_node *kn) * This is for kernfs_remove_self() which plays with active ref * after removal. */ - if (kn->parent && RB_EMPTY_NODE(&kn->rb)) + if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb)) return; =20 pr_debug("kernfs %s: removing\n", kn->name); @@ -1484,14 +1496,14 @@ static void __kernfs_remove(struct kernfs_node *kn) kernfs_get(pos); =20 kernfs_drain(pos); - + parent =3D kernfs_parent(pos); /* * kernfs_unlink_sibling() succeeds once per node. Use it * to decide who's responsible for cleanups. */ - if (!pos->parent || kernfs_unlink_sibling(pos)) { + if (!parent || kernfs_unlink_sibling(pos)) { struct kernfs_iattrs *ps_iattr =3D - pos->parent ? pos->parent->iattr : NULL; + parent ? parent->iattr : NULL; =20 /* update timestamps on the parent */ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); @@ -1721,7 +1733,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, int error; =20 /* can't move or rename root */ - if (!kn->parent) + if (!rcu_access_pointer(kn->__parent)) return -EINVAL; =20 root =3D kernfs_root(kn); @@ -1732,8 +1744,15 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct = kernfs_node *new_parent, (new_parent->flags & KERNFS_EMPTY_DIR)) goto out; =20 + old_parent =3D kernfs_parent(kn); + if (root->flags & KERNFS_ROOT_INVARIANT_PARENT) { + error =3D -EINVAL; + if (WARN_ON_ONCE(old_parent !=3D new_parent)) + goto out; + } + error =3D 0; - if ((kn->parent =3D=3D new_parent) && (kn->ns =3D=3D new_ns) && + if ((old_parent =3D=3D new_parent) && (kn->ns =3D=3D new_ns) && (strcmp(kn->name, new_name) =3D=3D 0)) goto out; /* nothing to rename */ =20 @@ -1760,8 +1779,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, /* rename_lock protects ->parent and ->name accessors */ write_lock_irq(&kernfs_rename_lock); =20 - old_parent =3D kn->parent; - kn->parent =3D new_parent; + old_parent =3D kernfs_parent(kn); + rcu_assign_pointer(kn->__parent, new_parent); =20 kn->ns =3D new_ns; if (new_name) { @@ -1794,7 +1813,7 @@ static struct kernfs_node *kernfs_dir_pos(const void = *ns, { if (pos) { int valid =3D kernfs_active(pos) && - pos->parent =3D=3D parent && hash =3D=3D pos->hash; + kernfs_parent(pos) =3D=3D parent && hash =3D=3D pos->hash; kernfs_put(pos); if (!valid) pos =3D NULL; diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index b42ee6547cdc1..c43bee18b79f7 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -19,6 +19,8 @@ #include #include =20 +extern rwlock_t kernfs_rename_lock; + struct kernfs_iattrs { kuid_t ia_uid; kgid_t ia_gid; @@ -64,11 +66,14 @@ struct kernfs_root { * * Return: the kernfs_root @kn belongs to. */ -static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn) +static inline struct kernfs_root *kernfs_root(const struct kernfs_node *kn) { + const struct kernfs_node *knp; /* if parent exists, it's always a dir; otherwise, @sd is a dir */ - if (kn->parent) - kn =3D kn->parent; + guard(rcu)(); + knp =3D rcu_dereference(kn->__parent); + if (knp) + kn =3D knp; return kn->dir.root; } =20 @@ -97,6 +102,27 @@ struct kernfs_super_info { }; #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info)) =20 +static inline bool kernfs_root_is_locked(const struct kernfs_node *kn) +{ + return lockdep_is_held(&kernfs_root(kn)->kernfs_rwsem); +} + +static inline struct kernfs_node *kernfs_parent(const struct kernfs_node *= kn) +{ + /* + * The kernfs_node::__parent remains valid within a RCU section. The kn + * can be reparented (and renamed) which changes the entry. This can be + * avoided by locking kernfs_root::kernfs_rwsem or kernfs_rename_lock. + * Both locks can be used to obtain a reference on __parent. Once the + * reference count reaches 0 then the node is about to be freed + * and can not be renamed (or become a different parent) anymore. + */ + return rcu_dereference_check(kn->__parent, + kernfs_root_is_locked(kn) || + lockdep_is_held(&kernfs_rename_lock) || + !atomic_read(&kn->count)); +} + static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry) { if (d_really_is_negative(dentry)) diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index 4a0ff08d589ca..2252b16e6ef0b 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -148,7 +148,7 @@ static struct dentry *kernfs_get_parent_dentry(struct d= entry *child) struct kernfs_root *root =3D kernfs_root(kn); =20 guard(rwsem_read)(&root->kernfs_rwsem); - return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent)); + return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_parent(kn))); } =20 static const struct export_operations kernfs_export_ops =3D { @@ -188,10 +188,10 @@ static struct kernfs_node *find_next_ancestor(struct = kernfs_node *child, return NULL; } =20 - while (child->parent !=3D parent) { - if (!child->parent) + while (kernfs_parent(child) !=3D parent) { + child =3D kernfs_parent(child); + if (!child) return NULL; - child =3D child->parent; } =20 return child; @@ -216,7 +216,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, dentry =3D dget(sb->s_root); =20 /* Check if this is the root kernfs_node */ - if (!kn->parent) + if (!rcu_access_pointer(kn->__parent)) return dentry; =20 root =3D kernfs_root(kn); diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 45371a70caa71..05c62ca93c53d 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -62,10 +62,10 @@ static int kernfs_get_target_path(struct kernfs_node *p= arent, =20 /* go up to the root, stop at the base */ base =3D parent; - while (base->parent) { - kn =3D target->parent; - while (kn->parent && base !=3D kn) - kn =3D kn->parent; + while (kernfs_parent(base)) { + kn =3D kernfs_parent(target); + while (kernfs_parent(kn) && base !=3D kn) + kn =3D kernfs_parent(kn); =20 if (base =3D=3D kn) break; @@ -75,14 +75,14 @@ static int kernfs_get_target_path(struct kernfs_node *p= arent, =20 strcpy(s, "../"); s +=3D 3; - base =3D base->parent; + base =3D kernfs_parent(base); } =20 /* determine end of target string for reverse fillup */ kn =3D target; - while (kn->parent && kn !=3D base) { + while (kernfs_parent(kn) && kn !=3D base) { len +=3D strlen(kn->name) + 1; - kn =3D kn->parent; + kn =3D kernfs_parent(kn); } =20 /* check limits */ @@ -94,7 +94,7 @@ static int kernfs_get_target_path(struct kernfs_node *par= ent, =20 /* reverse fillup of target string from target to base */ kn =3D target; - while (kn->parent && kn !=3D base) { + while (kernfs_parent(kn) && kn !=3D base) { int slen =3D strlen(kn->name); =20 len -=3D slen; @@ -102,7 +102,7 @@ static int kernfs_get_target_path(struct kernfs_node *p= arent, if (len) s[--len] =3D '/'; =20 - kn =3D kn->parent; + kn =3D kernfs_parent(kn); } =20 return 0; @@ -111,12 +111,13 @@ static int kernfs_get_target_path(struct kernfs_node = *parent, static int kernfs_getlink(struct inode *inode, char *path) { struct kernfs_node *kn =3D inode->i_private; - struct kernfs_node *parent =3D kn->parent; + struct kernfs_node *parent; struct kernfs_node *target =3D kn->symlink.target_kn; - struct kernfs_root *root =3D kernfs_root(parent); + struct kernfs_root *root =3D kernfs_root(kn); int error; =20 down_read(&root->kernfs_rwsem); + parent =3D kernfs_parent(kn); error =3D kernfs_get_target_path(parent, target, path); up_read(&root->kernfs_rwsem); =20 diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index 785408861c01c..1d16b4a386aa9 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -19,13 +19,19 @@ =20 #include "sysfs.h" =20 +static struct kobject *sysfs_file_kobj(struct kernfs_node *kn) +{ + guard(rcu)(); + return rcu_dereference(kn->__parent)->priv; +} + /* * Determine ktype->sysfs_ops for the given kernfs_node. This function * must be called while holding an active reference. */ static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn) { - struct kobject *kobj =3D kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(kn); =20 if (kn->flags & KERNFS_LOCKDEP) lockdep_assert_held(kn); @@ -40,7 +46,7 @@ static const struct sysfs_ops *sysfs_file_ops(struct kern= fs_node *kn) static int sysfs_kf_seq_show(struct seq_file *sf, void *v) { struct kernfs_open_file *of =3D sf->private; - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); const struct sysfs_ops *ops =3D sysfs_file_ops(of->kn); ssize_t count; char *buf; @@ -78,7 +84,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file = *of, char *buf, size_t count, loff_t pos) { struct bin_attribute *battr =3D of->kn->priv; - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); loff_t size =3D file_inode(of->file)->i_size; =20 if (!count) @@ -105,7 +111,7 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *o= f, char *buf, size_t count, loff_t pos) { const struct sysfs_ops *ops =3D sysfs_file_ops(of->kn); - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); ssize_t len; =20 /* @@ -131,7 +137,7 @@ static ssize_t sysfs_kf_write(struct kernfs_open_file *= of, char *buf, size_t count, loff_t pos) { const struct sysfs_ops *ops =3D sysfs_file_ops(of->kn); - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); =20 if (!count) return 0; @@ -144,7 +150,7 @@ static ssize_t sysfs_kf_bin_write(struct kernfs_open_fi= le *of, char *buf, size_t count, loff_t pos) { struct bin_attribute *battr =3D of->kn->priv; - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); loff_t size =3D file_inode(of->file)->i_size; =20 if (size) { @@ -168,7 +174,7 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *o= f, struct vm_area_struct *vma) { struct bin_attribute *battr =3D of->kn->priv; - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); =20 return battr->mmap(of->file, kobj, battr, vma); } @@ -177,7 +183,7 @@ static loff_t sysfs_kf_bin_llseek(struct kernfs_open_fi= le *of, loff_t offset, int whence) { struct bin_attribute *battr =3D of->kn->priv; - struct kobject *kobj =3D of->kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(of->kn); =20 if (battr->llseek) return battr->llseek(of->file, kobj, battr, offset, whence); @@ -494,7 +500,7 @@ EXPORT_SYMBOL_GPL(sysfs_break_active_protection); */ void sysfs_unbreak_active_protection(struct kernfs_node *kn) { - struct kobject *kobj =3D kn->parent->priv; + struct kobject *kobj =3D sysfs_file_kobj(kn); =20 kernfs_unbreak_active_protection(kn); kernfs_put(kn); diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 87c79d076d6d7..5dda9a268e44c 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -147,6 +147,11 @@ enum kernfs_root_flag { * Support user xattrs to be written to nodes rooted at this root. */ KERNFS_ROOT_SUPPORT_USER_XATTR =3D 0x0008, + + /* + * Renames must not change the parent node. + */ + KERNFS_ROOT_INVARIANT_PARENT =3D 0x0010, }; =20 /* type-specific structures for kernfs_node union members */ @@ -199,8 +204,8 @@ struct kernfs_node { * never moved to a different parent, it is safe to access the * parent directly. */ - struct kernfs_node *parent; const char *name; + struct kernfs_node __rcu *__parent; =20 struct rb_node rb; =20 @@ -416,6 +421,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags, void *priv); void kernfs_destroy_root(struct kernfs_root *root); +unsigned int kernfs_root_flags(struct kernfs_node *kn); =20 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, umode_t mode, @@ -514,6 +520,8 @@ kernfs_create_root(struct kernfs_syscall_ops *scops, un= signed int flags, { return ERR_PTR(-ENOSYS); } =20 static inline void kernfs_destroy_root(struct kernfs_root *root) { } +static inline unsigned int kernfs_root_flags(struct kernfs_node *kn) +{ return 0; } =20 static inline struct kernfs_node * kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c index e28d5f0d20ed0..c9752eb607ec9 100644 --- a/kernel/cgroup/cgroup-v1.c +++ b/kernel/cgroup/cgroup-v1.c @@ -844,7 +844,7 @@ static int cgroup1_rename(struct kernfs_node *kn, struc= t kernfs_node *new_parent =20 if (kernfs_type(kn) !=3D KERNFS_DIR) return -ENOTDIR; - if (kn->parent !=3D new_parent) + if (rcu_access_pointer(kn->__parent) !=3D new_parent) return -EIO; =20 /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index d9061bd55436b..214aa378936cd 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -633,9 +633,22 @@ int cgroup_task_count(const struct cgroup *cgrp) return count; } =20 +static struct cgroup *kn_get_priv(struct kernfs_node *kn) +{ + struct kernfs_node *parent; + /* + * The parent can not be replaced due to KERNFS_ROOT_INVARIANT_PARENT. + * Therefore it is always safe to dereference this pointer outside of a + * RCU section. + */ + parent =3D rcu_dereference_check(kn->__parent, + kernfs_root_flags(kn) & KERNFS_ROOT_INVARIANT_PARENT); + return parent->priv; +} + struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) { - struct cgroup *cgrp =3D of->kn->parent->priv; + struct cgroup *cgrp =3D kn_get_priv(of->kn); struct cftype *cft =3D of_cft(of); =20 /* @@ -1612,7 +1625,7 @@ void cgroup_kn_unlock(struct kernfs_node *kn) if (kernfs_type(kn) =3D=3D KERNFS_DIR) cgrp =3D kn->priv; else - cgrp =3D kn->parent->priv; + cgrp =3D kn_get_priv(kn); =20 cgroup_unlock(); =20 @@ -1644,7 +1657,7 @@ struct cgroup *cgroup_kn_lock_live(struct kernfs_node= *kn, bool drain_offline) if (kernfs_type(kn) =3D=3D KERNFS_DIR) cgrp =3D kn->priv; else - cgrp =3D kn->parent->priv; + cgrp =3D kn_get_priv(kn); =20 /* * We're gonna grab cgroup_mutex which nests outside kernfs @@ -2118,7 +2131,8 @@ int cgroup_setup_root(struct cgroup_root *root, u16 s= s_mask) root->kf_root =3D kernfs_create_root(kf_sops, KERNFS_ROOT_CREATE_DEACTIVATED | KERNFS_ROOT_SUPPORT_EXPORTOP | - KERNFS_ROOT_SUPPORT_USER_XATTR, + KERNFS_ROOT_SUPPORT_USER_XATTR | + KERNFS_ROOT_INVARIANT_PARENT, root_cgrp); if (IS_ERR(root->kf_root)) { ret =3D PTR_ERR(root->kf_root); @@ -4119,7 +4133,7 @@ static ssize_t cgroup_file_write(struct kernfs_open_f= ile *of, char *buf, size_t nbytes, loff_t off) { struct cgroup_file_ctx *ctx =3D of->priv; - struct cgroup *cgrp =3D of->kn->parent->priv; + struct cgroup *cgrp =3D kn_get_priv(of->kn); struct cftype *cft =3D of_cft(of); struct cgroup_subsys_state *css; int ret; --=20 2.47.2 From nobody Mon Feb 9 03:34:12 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 728A71DFDB4; Tue, 28 Jan 2025 08:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053763; cv=none; b=iEi0s1POn0MT1g/tpbn2OiEDDWifSepxFrq13P22d+4lNVKsJMDiDlUcDaVgsc8DjeYYF3rU9g3dA2fYfrIrNCiY3kThWMq7Vt6lgm0AVx+cklCagQMqJzyLPKx+047x8XaO7G5nycY/ZPPag6SU3s7myp/MCPD2yxCmSx3Ti1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738053763; c=relaxed/simple; bh=D218n9mzlMbUpdwdcTQV1Qon1QZ+GvATTDRx5AJza5U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E9JPUUmCjTQpg16j0tTgJB+iVBYYijPyjoDnXFQlMH7U8ETsr3iK3ckYP1GilK4XRz+ZHLOnbbDEXEN45GlHQpcWLwjtWa5dNpEuR2OuAkvZo0kklEojmzGE+sydMA9Wsc36fDNqlBo1A5UoXtfV6HEVagFTPn3+l73Bxp0M6iQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=r3TYxInd; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=+eAUu1Po; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="r3TYxInd"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="+eAUu1Po" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1738053760; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4/+M0gZmrAUoqqWanlPt3+D6tSIJOQFs8M/m4xqZd9A=; b=r3TYxIndqr//ahLjRsManLhDMQVYi/+S6JoTTSZ4XOo3iC4UQFh8DZ0hBYXHCW5UAuaw8q FoDm4U0XBi9M8V5Rph/rMB9mcxcdcl5AJsE3pgCsHWjChWOCu6lMD9zCILI1B6Bezlx4hS aLLzgtgCU2WZNz1ri5tJmlL2bpCKnwqqKMIFaLcQQfWPq6pPeGTKz6ytuI3cGSBR+MDZ/m IL3twpHAvpGIBEsXPVGbrP8H8ySpe3JeMoFdiW8Mg3T4QbvcU5u0IvB2emMTogBfqbiadw m7iW6Omrpxlk+0DJtmTx1jOB6spCEKVfH97lemdljVnLmtjvshNVE8+7gnRJ2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1738053760; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4/+M0gZmrAUoqqWanlPt3+D6tSIJOQFs8M/m4xqZd9A=; b=+eAUu1PorDonno0M5O9j1fZSItreHMrsjpGR0TGGynyaOdcIKapv3Z5qOMxz7J6yjS9yQv QrEyAEwHVjhp94DQ== To: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Paul E. McKenney" , Boqun Feng , Greg Kroah-Hartman , Hillf Danton , Johannes Weiner , Marco Elver , Tejun Heo , tglx@linutronix.de, Sebastian Andrzej Siewior , syzbot+6ea37e2e6ffccf41a7e6@syzkaller.appspotmail.com Subject: [PATCH v5 6/6] kernfs: Use RCU to access kernfs_node::name. Date: Tue, 28 Jan 2025 09:42:26 +0100 Message-ID: <20250128084226.1499291-7-bigeasy@linutronix.de> In-Reply-To: <20250128084226.1499291-1-bigeasy@linutronix.de> References: <20250128084226.1499291-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using RCU lifetime rules to access kernfs_node::name can avoid the trouble kernfs_rename_lock in kernfs_name() and kernfs_path_from_node() if the fs was created with KERNFS_ROOT_INVARIANT_PARENT. This is useful as it allows to implement kernfs_path_from_node() only with RCU protection and avoiding kernfs_rename_lock. The lock is only required if the __parent node can be changed and the function requires an unchanged hierarchy while it iterates from the node to its parent. I went through all ::name users and added the required access for the lookup with a few extensions: - rdtgroup_pseudo_lock_create() drops all locks and then uses the name later on. resctrl supports rename with different parents. Here I made a temporal copy of the name while it is used outside of the lock. - kernfs_rename_ns() accepts NULL as new_parent. This simplifies sysfs_move_dir_ns() where it can set NULL in order to reuse the current name. - kernfs_rename_ns() is only using kernfs_rename_lock if the parents are different. All users use either kernfs_rwsem (for stable path view) or just RCU for the lookup. The ::name uses always RCU free. Use RCU lifetime guarantees to access kernfs_node::name. Suggested-by: Tejun Heo Reported-by: syzbot+6ea37e2e6ffccf41a7e6@syzkaller.appspotmail.com Closes: https://lore.kernel.org/lkml/67251dc6.050a0220.529b6.015e.GAE@googl= e.com/ Reported-by: Hillf Danton Closes: https://lore.kernel.org/20241102001224.2789-1-hdanton@sina.com Signed-off-by: Sebastian Andrzej Siewior Acked-by: Tejun Heo --- arch/x86/kernel/cpu/resctrl/internal.h | 5 + arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 14 ++- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 10 +- fs/kernfs/dir.c | 113 ++++++++++++---------- fs/kernfs/file.c | 4 +- fs/kernfs/kernfs-internal.h | 5 + fs/kernfs/mount.c | 5 +- fs/kernfs/symlink.c | 7 +- fs/sysfs/dir.c | 2 +- include/linux/kernfs.h | 4 +- security/selinux/hooks.c | 7 +- 11 files changed, 105 insertions(+), 71 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/r= esctrl/internal.h index 955999aecfca9..cc83dbc70a8ef 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -505,6 +505,11 @@ int parse_bw(struct rdt_parse_data *data, struct resct= rl_schema *s, =20 extern struct mutex rdtgroup_mutex; =20 +static inline const char *rdt_kn_get_name(const struct kernfs_node *kn) +{ + return rcu_dereference_check(kn->name, lockdep_is_held(&rdtgroup_mutex)); +} + extern struct rdt_hw_resource rdt_resources_all[]; extern struct rdtgroup rdtgroup_default; extern struct dentry *debugfs_resctrl; diff --git a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c b/arch/x86/kernel/cp= u/resctrl/pseudo_lock.c index 972e6b6b0481f..efe1fc5a6866f 100644 --- a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c +++ b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c @@ -52,7 +52,8 @@ static char *pseudo_lock_devnode(const struct device *dev= , umode_t *mode) rdtgrp =3D dev_get_drvdata(dev); if (mode) *mode =3D 0600; - return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdtgrp->kn->name); + guard(mutex)(&rdtgroup_mutex); + return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdt_kn_get_name(rdtgrp->kn= )); } =20 static const struct class pseudo_lock_class =3D { @@ -1301,6 +1302,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtg= rp) struct task_struct *thread; unsigned int new_minor; struct device *dev; + char *kn_name __free(kfree) =3D NULL; int ret; =20 ret =3D pseudo_lock_region_alloc(plr); @@ -1312,6 +1314,11 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdt= grp) ret =3D -EINVAL; goto out_region; } + kn_name =3D kstrdup(rdt_kn_get_name(rdtgrp->kn), GFP_KERNEL); + if (!kn_name) { + ret =3D -ENOMEM; + goto out_cstates; + } =20 plr->thread_done =3D 0; =20 @@ -1360,8 +1367,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtg= rp) mutex_unlock(&rdtgroup_mutex); =20 if (!IS_ERR_OR_NULL(debugfs_resctrl)) { - plr->debugfs_dir =3D debugfs_create_dir(rdtgrp->kn->name, - debugfs_resctrl); + plr->debugfs_dir =3D debugfs_create_dir(kn_name, debugfs_resctrl); if (!IS_ERR_OR_NULL(plr->debugfs_dir)) debugfs_create_file("pseudo_lock_measure", 0200, plr->debugfs_dir, rdtgrp, @@ -1370,7 +1376,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtg= rp) =20 dev =3D device_create(&pseudo_lock_class, NULL, MKDEV(pseudo_lock_major, new_minor), - rdtgrp, "%s", rdtgrp->kn->name); + rdtgrp, "%s", kn_name); =20 mutex_lock(&rdtgroup_mutex); =20 diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/r= esctrl/rdtgroup.c index d9e24100fd8b0..bf06b34cd1e96 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -907,14 +907,14 @@ int proc_resctrl_show(struct seq_file *s, struct pid_= namespace *ns, continue; =20 seq_printf(s, "res:%s%s\n", (rdtg =3D=3D &rdtgroup_default) ? "/" : "", - rdtg->kn->name); + rdt_kn_get_name(rdtg->kn)); seq_puts(s, "mon:"); list_for_each_entry(crg, &rdtg->mon.crdtgrp_list, mon.crdtgrp_list) { if (!resctrl_arch_match_rmid(tsk, crg->mon.parent->closid, crg->mon.rmid)) continue; - seq_printf(s, "%s", crg->kn->name); + seq_printf(s, "%s", rdt_kn_get_name(crg->kn)); break; } seq_putc(s, '\n'); @@ -3662,7 +3662,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node= *parent_kn, */ static bool is_mon_groups(struct kernfs_node *kn, const char *name) { - return (!strcmp(kn->name, "mon_groups") && + return (!strcmp(rdt_kn_get_name(kn), "mon_groups") && strcmp(name, "mon_groups")); } =20 @@ -3811,7 +3811,7 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) ret =3D rdtgroup_rmdir_ctrl(rdtgrp, tmpmask); } } else if (rdtgrp->type =3D=3D RDTMON_GROUP && - is_mon_groups(parent_kn, kn->name)) { + is_mon_groups(parent_kn, rdt_kn_get_name(kn))) { ret =3D rdtgroup_rmdir_mon(rdtgrp, tmpmask); } else { ret =3D -EPERM; @@ -3899,7 +3899,7 @@ static int rdtgroup_rename(struct kernfs_node *kn, =20 kn_parent =3D rdt_kn_parent(kn); if (rdtgrp->type !=3D RDTMON_GROUP || !kn_parent || - !is_mon_groups(kn_parent, kn->name)) { + !is_mon_groups(kn_parent, rdt_kn_get_name(kn))) { rdt_last_cmd_puts("Source must be a MON group\n"); ret =3D -EPERM; goto out; diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 16d268345e3b7..42c451045d7c2 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -51,14 +51,6 @@ static bool kernfs_lockdep(struct kernfs_node *kn) #endif } =20 -static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t bu= flen) -{ - if (!kn) - return strscpy(buf, "(null)", buflen); - - return strscpy(buf, rcu_access_pointer(kn->__parent) ? kn->name : "/", bu= flen); -} - /* kernfs_node_depth - compute depth from @from to @to */ static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *t= o) { @@ -168,11 +160,13 @@ static int kernfs_path_from_node_locked(struct kernfs= _node *kn_to, =20 /* Calculate how many bytes we need for the rest */ for (i =3D depth_to - 1; i >=3D 0; i--) { + const char *name; =20 for (kn =3D kn_to, j =3D 0; j < i; j++) kn =3D rcu_dereference(kn->__parent); =20 - len +=3D scnprintf(buf + len, buflen - len, "/%s", kn->name); + name =3D rcu_dereference(kn->name); + len +=3D scnprintf(buf + len, buflen - len, "/%s", name); } =20 return len; @@ -196,13 +190,18 @@ static int kernfs_path_from_node_locked(struct kernfs= _node *kn_to, */ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) { - unsigned long flags; - int ret; + struct kernfs_node *kn_parent; =20 - read_lock_irqsave(&kernfs_rename_lock, flags); - ret =3D kernfs_name_locked(kn, buf, buflen); - read_unlock_irqrestore(&kernfs_rename_lock, flags); - return ret; + if (!kn) + return strscpy(buf, "(null)", buflen); + + guard(rcu)(); + /* + * KERNFS_ROOT_INVARIANT_PARENT is ignored here. The name is RCU freed and + * the parent is either existing or not. + */ + kn_parent =3D rcu_dereference(kn->__parent); + return strscpy(buf, kn_parent ? rcu_dereference(kn->name) : "/", buflen); } =20 /** @@ -224,14 +223,17 @@ int kernfs_name(struct kernfs_node *kn, char *buf, si= ze_t buflen) int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from, char *buf, size_t buflen) { - unsigned long flags; - int ret; + struct kernfs_root *root; =20 guard(rcu)(); - read_lock_irqsave(&kernfs_rename_lock, flags); - ret =3D kernfs_path_from_node_locked(to, from, buf, buflen); - read_unlock_irqrestore(&kernfs_rename_lock, flags); - return ret; + if (to) { + root =3D kernfs_root(to); + if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) { + guard(read_lock_irqsave)(&kernfs_rename_lock); + return kernfs_path_from_node_locked(to, from, buf, buflen); + } + } + return kernfs_path_from_node_locked(to, from, buf, buflen); } EXPORT_SYMBOL_GPL(kernfs_path_from_node); =20 @@ -338,13 +340,13 @@ static int kernfs_name_compare(unsigned int hash, con= st char *name, return -1; if (ns > kn->ns) return 1; - return strcmp(name, kn->name); + return strcmp(name, kernfs_rcu_name(kn)); } =20 static int kernfs_sd_compare(const struct kernfs_node *left, const struct kernfs_node *right) { - return kernfs_name_compare(left->hash, left->name, left->ns, right); + return kernfs_name_compare(left->hash, kernfs_rcu_name(left), left->ns, r= ight); } =20 /** @@ -542,7 +544,8 @@ static void kernfs_free_rcu(struct rcu_head *rcu) { struct kernfs_node *kn =3D container_of(rcu, struct kernfs_node, rcu); =20 - kfree_const(kn->name); + /* If the whole node goes away, then name can't be used outside */ + kfree_const(rcu_access_pointer(kn->name)); =20 if (kn->iattr) { simple_xattrs_free(&kn->iattr->xattrs, NULL); @@ -571,7 +574,8 @@ void kernfs_put(struct kernfs_node *kn) =20 WARN_ONCE(atomic_read(&kn->active) !=3D KN_DEACTIVATED_BIAS, "kernfs_put: %s/%s: released with incorrect active_ref %d\n", - parent ? parent->name : "", kn->name, atomic_read(&kn->active)); + parent ? rcu_dereference(parent->name) : "", + rcu_dereference(kn->name), atomic_read(&kn->active)); =20 if (kernfs_type(kn) =3D=3D KERNFS_LINK) kernfs_put(kn->symlink.target_kn); @@ -648,7 +652,7 @@ static struct kernfs_node *__kernfs_new_node(struct ker= nfs_root *root, atomic_set(&kn->active, KN_DEACTIVATED_BIAS); RB_CLEAR_NODE(&kn->rb); =20 - kn->name =3D name; + rcu_assign_pointer(kn->name, name); kn->mode =3D mode; kn->flags =3D flags; =20 @@ -786,7 +790,8 @@ int kernfs_add_one(struct kernfs_node *kn) ret =3D -EINVAL; has_ns =3D kernfs_ns_enabled(parent); if (WARN(has_ns !=3D (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' fo= r '%s'\n", - has_ns ? "required" : "invalid", parent->name, kn->name)) + has_ns ? "required" : "invalid", + kernfs_rcu_name(parent), kernfs_rcu_name(kn))) goto out_unlock; =20 if (kernfs_type(parent) !=3D KERNFS_DIR) @@ -796,7 +801,7 @@ int kernfs_add_one(struct kernfs_node *kn) if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR)) goto out_unlock; =20 - kn->hash =3D kernfs_name_hash(kn->name, kn->ns); + kn->hash =3D kernfs_name_hash(kernfs_rcu_name(kn), kn->ns); =20 ret =3D kernfs_link_sibling(kn); if (ret) @@ -852,7 +857,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs= _node *parent, =20 if (has_ns !=3D (bool)ns) { WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", - has_ns ? "required" : "invalid", parent->name, name); + has_ns ? "required" : "invalid", kernfs_rcu_name(parent), name); return NULL; } =20 @@ -1130,8 +1135,6 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) =20 /* Negative hashed dentry? */ if (d_really_is_negative(dentry)) { - struct kernfs_node *parent; - /* If the kernfs parent node has changed discard and * proceed to ->lookup. * @@ -1179,7 +1182,7 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) goto out_bad; =20 /* The kernfs node has been renamed */ - if (strcmp(dentry->d_name.name, kn->name) !=3D 0) + if (strcmp(dentry->d_name.name, kernfs_rcu_name(kn)) !=3D 0) goto out_bad; =20 /* The kernfs node has been moved to a different namespace */ @@ -1473,7 +1476,7 @@ static void __kernfs_remove(struct kernfs_node *kn) if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb)) return; =20 - pr_debug("kernfs %s: removing\n", kn->name); + pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn)); =20 /* prevent new usage by marking all nodes removing and deactivating */ pos =3D NULL; @@ -1729,7 +1732,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, { struct kernfs_node *old_parent; struct kernfs_root *root; - const char *old_name =3D NULL; + const char *old_name; int error; =20 /* can't move or rename root */ @@ -1752,8 +1755,11 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct = kernfs_node *new_parent, } =20 error =3D 0; + old_name =3D kernfs_rcu_name(kn); + if (!new_name) + new_name =3D old_name; if ((old_parent =3D=3D new_parent) && (kn->ns =3D=3D new_ns) && - (strcmp(kn->name, new_name) =3D=3D 0)) + (strcmp(old_name, new_name) =3D=3D 0)) goto out; /* nothing to rename */ =20 error =3D -EEXIST; @@ -1761,7 +1767,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, goto out; =20 /* rename kernfs_node */ - if (strcmp(kn->name, new_name) !=3D 0) { + if (strcmp(old_name, new_name) !=3D 0) { error =3D -ENOMEM; new_name =3D kstrdup_const(new_name, GFP_KERNEL); if (!new_name) @@ -1774,27 +1780,32 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct= kernfs_node *new_parent, * Move to the appropriate place in the appropriate directories rbtree. */ kernfs_unlink_sibling(kn); - kernfs_get(new_parent); =20 - /* rename_lock protects ->parent and ->name accessors */ - write_lock_irq(&kernfs_rename_lock); + /* rename_lock protects ->parent accessors */ + if (old_parent !=3D new_parent) { + kernfs_get(new_parent); + write_lock_irq(&kernfs_rename_lock); =20 - old_parent =3D kernfs_parent(kn); - rcu_assign_pointer(kn->__parent, new_parent); + rcu_assign_pointer(kn->__parent, new_parent); =20 - kn->ns =3D new_ns; - if (new_name) { - old_name =3D kn->name; - kn->name =3D new_name; + kn->ns =3D new_ns; + if (new_name) + rcu_assign_pointer(kn->name, new_name); + + write_unlock_irq(&kernfs_rename_lock); + kernfs_put(old_parent); + } else { + /* name assignment is RCU protected, parent is the same */ + kn->ns =3D new_ns; + if (new_name) + rcu_assign_pointer(kn->name, new_name); } =20 - write_unlock_irq(&kernfs_rename_lock); - - kn->hash =3D kernfs_name_hash(kn->name, kn->ns); + kn->hash =3D kernfs_name_hash(new_name ?: old_name, kn->ns); kernfs_link_sibling(kn); =20 - kernfs_put(old_parent); - kfree_const(old_name); + if (new_name && !is_kernel_rodata((unsigned long)old_name)) + kfree_rcu_mightsleep(old_name); =20 error =3D 0; out: @@ -1878,7 +1889,7 @@ static int kernfs_fop_readdir(struct file *file, stru= ct dir_context *ctx) for (pos =3D kernfs_dir_pos(ns, parent, ctx->pos, pos); pos; pos =3D kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { - const char *name =3D pos->name; + const char *name =3D kernfs_rcu_name(pos); unsigned int type =3D fs_umode_to_dtype(pos->mode); int len =3D strlen(name); ino_t ino =3D kernfs_ino(pos); diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 165d8e37976ba..970bf37a37fb7 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -915,6 +915,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) list_for_each_entry(info, &kernfs_root(kn)->supers, node) { struct kernfs_node *parent; struct inode *p_inode =3D NULL; + const char *kn_name; struct inode *inode; struct qstr name; =20 @@ -928,7 +929,8 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) if (!inode) continue; =20 - name =3D (struct qstr)QSTR_INIT(kn->name, strlen(kn->name)); + kn_name =3D kernfs_rcu_name(kn); + name =3D (struct qstr)QSTR_INIT(kn_name, strlen(kn_name)); parent =3D kernfs_get_parent(kn); if (parent) { p_inode =3D ilookup(info->sb, kernfs_ino(parent)); diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index c43bee18b79f7..40a2a9cd819d0 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -107,6 +107,11 @@ static inline bool kernfs_root_is_locked(const struct = kernfs_node *kn) return lockdep_is_held(&kernfs_root(kn)->kernfs_rwsem); } =20 +static inline const char *kernfs_rcu_name(const struct kernfs_node *kn) +{ + return rcu_dereference_check(kn->name, kernfs_root_is_locked(kn)); +} + static inline struct kernfs_node *kernfs_parent(const struct kernfs_node *= kn) { /* diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index 2252b16e6ef0b..d1f512b7bf867 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -231,6 +231,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, do { struct dentry *dtmp; struct kernfs_node *kntmp; + const char *name; =20 if (kn =3D=3D knparent) return dentry; @@ -239,8 +240,8 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *k= n, dput(dentry); return ERR_PTR(-EINVAL); } - dtmp =3D lookup_positive_unlocked(kntmp->name, dentry, - strlen(kntmp->name)); + name =3D rcu_dereference(kntmp->name); + dtmp =3D lookup_positive_unlocked(name, dentry, strlen(name)); dput(dentry); if (IS_ERR(dtmp)) return dtmp; diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 05c62ca93c53d..0bd8a2143723d 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -81,7 +81,7 @@ static int kernfs_get_target_path(struct kernfs_node *par= ent, /* determine end of target string for reverse fillup */ kn =3D target; while (kernfs_parent(kn) && kn !=3D base) { - len +=3D strlen(kn->name) + 1; + len +=3D strlen(kernfs_rcu_name(kn)) + 1; kn =3D kernfs_parent(kn); } =20 @@ -95,10 +95,11 @@ static int kernfs_get_target_path(struct kernfs_node *p= arent, /* reverse fillup of target string from target to base */ kn =3D target; while (kernfs_parent(kn) && kn !=3D base) { - int slen =3D strlen(kn->name); + const char *name =3D kernfs_rcu_name(kn); + int slen =3D strlen(name); =20 len -=3D slen; - memcpy(s + len, kn->name, slen); + memcpy(s + len, name, slen); if (len) s[--len] =3D '/'; =20 diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index 4df2afa551dc6..94e12efd92f21 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c @@ -123,7 +123,7 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobj= ect *new_parent_kobj, new_parent =3D new_parent_kobj && new_parent_kobj->sd ? new_parent_kobj->sd : sysfs_root_kn; =20 - return kernfs_rename_ns(kn, new_parent, kn->name, new_ns); + return kernfs_rename_ns(kn, new_parent, NULL, new_ns); } =20 /** diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 5dda9a268e44c..b5a5f32fdfd1a 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -204,8 +204,8 @@ struct kernfs_node { * never moved to a different parent, it is safe to access the * parent directly. */ - const char *name; struct kernfs_node __rcu *__parent; + const char __rcu *name; =20 struct rb_node rb; =20 @@ -400,7 +400,7 @@ static inline bool kernfs_ns_enabled(struct kernfs_node= *kn) } =20 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen); -int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node = *kn, +int kernfs_path_from_node(struct kernfs_node *kn_to, struct kernfs_node *k= n_from, char *buf, size_t buflen); void pr_cont_kernfs_name(struct kernfs_node *kn); void pr_cont_kernfs_path(struct kernfs_node *kn); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 171dd7fceac54..9e92057d2caac 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3583,10 +3583,13 @@ static int selinux_kernfs_init_security(struct kern= fs_node *kn_dir, newsid =3D tsec->create_sid; } else { u16 secclass =3D inode_mode_to_security_class(kn->mode); + const char *kn_name; struct qstr q; =20 - q.name =3D kn->name; - q.hash_len =3D hashlen_string(kn_dir, kn->name); + /* kn is fresh, can't be renamed, name goes not away */ + kn_name =3D rcu_dereference_check(kn->name, true); + q.name =3D kn_name; + q.hash_len =3D hashlen_string(kn_dir, kn_name); =20 rc =3D security_transition_sid(tsec->sid, parent_sid, secclass, &q, --=20 2.47.2