mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiaming Zhang <r772577952@gmail.com>
To: Andreas Gruenbacher <agruenba@redhat.com>, gfs2@lists.linux.dev
Cc: linux-kernel@vger.kernel.org,
	Jiaming Zhang <r772577952@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] gfs2: avoid infinite retry on inode hash collision in gfs2_create_inode
Date: Sun,  6 Dec 2026 19:56:01 +0800	[thread overview]
Message-ID: <20261206115601.482515-1-r772577952@gmail.com> (raw)

When creating a new file or directory, gfs2_create_inode() allocates a
free disk block via alloc_dinode() and hashes the new VFS inode by its
block address ip->i_no_addr, assuming any -EBUSY collision from
insert_inode_locked4() is transient:

	if (error == -EBUSY)
		goto retry;
	if (error)
		goto fail_gunlock2;

Commit 4ec3c19d058f ("gfs2: Handle -EBUSY result of
insert_inode_locked4") added that retry loop for brief races against a
concurrent lookup or failing create of a prior inode at that block.

On a corrupt image whose resource group bitmap marks a live dinode
block (such as statfs, rindex, quota, or root) as GFS2_BLKST_FREE,
alloc_dinode() hands out that already-live block address.

inode_insert5() finds the live inode in inode_hashtable, increments its
reference count, and returns it; insert_inode_locked4() drops that
extra reference without unhashing the live inode and returns -EBUSY.
gfs2_create_inode() then spins at retry forever while holding
inode_lock(dir) and the exclusive directory glock.

Any concurrent lookup or permission check on that directory blocks
forever in TASK_UNINTERRUPTIBLE waiting on the directory glock, hanging
the directory and triggering a hung-task report in gfs2_permission().
Triggering this requires mounting a crafted GFS2 image (CAP_SYS_ADMIN)
and creating a file in a directory.

Use inode_insert5() directly after setting I_CREATING and defer writing
io_gl->gl_no_formal_ino until insertion succeeds.  Retry with a bounded
1-second deadline only when the colliding inode is NULL (I_CREATING) or
has zero links; otherwise report corruption via gfs2_consist_inode(),
skip deallocating the live block, and fail with -EIO.

Fixes: 4ec3c19d058f ("gfs2: Handle -EBUSY result of insert_inode_locked4")
Closes: https://lore.kernel.org/lkml/CANypQFZ_xTpeMAMNh_yQBsyEMvGYEQ-NeHv=tGghj3LHpR=muA@mail.gmail.com/
Cc: stable@vger.kernel.org
Assisted-by: Antigravity:Gemini
Signed-off-by: Jiaming Zhang <r772577952@gmail.com>
---
 fs/gfs2/inode.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
index 75fb1a0c9142..2541c04b9d1f 100644
--- a/fs/gfs2/inode.c
+++ b/fs/gfs2/inode.c
@@ -18,6 +18,7 @@
 #include <linux/iomap.h>
 #include <linux/security.h>
 #include <linux/fiemap.h>
+#include <linux/sched/signal.h>
 #include <linux/uaccess.h>
 
 #include "gfs2.h"
@@ -709,13 +710,14 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	const struct qstr *name = &dentry->d_name;
 	struct posix_acl *default_acl, *acl;
 	struct gfs2_holder d_gh, gh;
-	struct inode *inode = NULL;
+	struct inode *inode = NULL, *old;
 	struct gfs2_inode *dip = GFS2_I(dir), *ip;
 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
 	struct gfs2_glock *gl = NULL, *io_gl;
 	int error, dealloc_error;
 	u32 aflags = 0;
 	unsigned blocks = 1;
+	unsigned long deadline = 0;
 	struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
 	bool xattr_initialized = false;
 
@@ -846,14 +848,33 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
 	if (error)
 		goto fail_dealloc_inode;
 	gfs2_cancel_delete_work(io_gl);
-	io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
 
+	inode_state_set_raw(inode, I_CREATING);
 retry:
-	error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
-	if (error == -EBUSY)
-		goto retry;
-	if (error)
-		goto fail_gunlock2;
+	old = inode_insert5(inode, ip->i_no_addr, iget_test, NULL,
+			    &ip->i_no_addr);
+	if (old != inode) {
+		bool retry = !old || !old->i_nlink;
+
+		iput(old);
+		if (retry && !fatal_signal_pending(current)) {
+			if (!deadline) {
+				deadline = jiffies + HZ;
+				cond_resched();
+				goto retry;
+			}
+			if (time_before(jiffies, deadline)) {
+				schedule_timeout_killable(1);
+				goto retry;
+			}
+		}
+		gfs2_consist_inode(dip);
+		error = -EIO;
+		gfs2_glock_put(io_gl);
+		ip->i_no_addr = 0;
+		goto fail_free_inode;
+	}
+	io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
 
 	error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID,
 				   &ip->i_iopen_gh);

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.43.0


                 reply	other threads:[~2026-12-06 11:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206115601.482515-1-r772577952@gmail.com \
    --to=r772577952@gmail.com \
    --cc=agruenba@redhat.com \
    --cc=gfs2@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®