From: Xiang Shen <turyshen@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>, Mateusz Guzik <mjguzik@gmail.com>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Xiang Shen <turyshen@gmail.com>
Subject: [PATCH 1/1] fs: fix deadlock in insert_inode_locked() waiting for inode eviction
Date: Fri, 27 Mar 2026 10:21:30 +1100 [thread overview]
Message-ID: <20260326232130.1891210-1-turyshen@gmail.com> (raw)
Commit 88ec797c4680 ("fs: make insert_inode_locked() wait for inode
destruction") changed insert_inode_locked() to sleep via
__wait_on_freeing_inode() when it encounters an inode being freed. This
introduces a deadlock when the caller already holds resources that the
eviction path needs.
For example, ext4_new_inode() holds an active jbd2 journal handle when
it calls insert_inode_locked(). If a stale inode with the same ino is
being freed, the function now sleeps waiting for eviction to complete.
However, ext4_evict_inode() needs to start a new journal transaction via
ext4_journal_start(), which may block in add_transaction_credits()
waiting for the current transaction to commit. That transaction cannot
commit because the caller's handle (in ext4_new_inode) is still active,
resulting in a deadlock:
Thread A (ext4_new_inode) Thread B (evicting old inode)
------------------------- ----------------------------
jbd2_journal_start() -> handle
insert_inode_locked()
finds old inode I_FREEING
__wait_on_freeing_inode()
schedule() [waits for B] ext4_evict_inode()
ext4_journal_start()
add_transaction_credits()
[waits for T to commit]
[T blocked by A's handle]
Fix this by replacing the blocking __wait_on_freeing_inode() call with a
non-blocking drop-and-retry loop. When a freeing inode is encountered,
drop both i_lock and inode_hash_lock, yield the CPU with cond_resched(),
and restart the outer while(1) loop via continue.
Unlike the pre-88ec797c4680 code which skipped freeing inodes within the
inner hlist_for_each_entry loop (risking duplicate inodes in the hash),
this fix restarts the entire lookup from scratch -- the new inode is only
inserted when no matching entry (including freeing ones) exists in the
hash chain.
Fixes: 88ec797c4680 ("fs: make insert_inode_locked() wait for inode destruction")
Cc: stable@vger.kernel.org
Signed-off-by: Xiang Shen <turyshen@gmail.com>
---
fs/inode.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index cc12b68e021b..8ecdd297c83f 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1842,7 +1842,6 @@ int insert_inode_locked(struct inode *inode)
while (1) {
struct inode *old = NULL;
spin_lock(&inode_hash_lock);
-repeat:
hlist_for_each_entry(old, head, i_hash) {
if (old->i_ino != ino)
continue;
@@ -1860,9 +1859,10 @@ int insert_inode_locked(struct inode *inode)
return 0;
}
if (inode_state_read(old) & (I_FREEING | I_WILL_FREE)) {
- __wait_on_freeing_inode(old, true, false);
- old = NULL;
- goto repeat;
+ spin_unlock(&old->i_lock);
+ spin_unlock(&inode_hash_lock);
+ cond_resched();
+ continue;
}
if (unlikely(inode_state_read(old) & I_CREATING)) {
spin_unlock(&old->i_lock);
--
2.34.1
next reply other threads:[~2026-03-26 23:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 23:21 Xiang Shen [this message]
2026-03-26 23:29 ` Mateusz Guzik
[not found] ` <CAHLHtjzi5uKrNDyjL60nZ6TUZnae5gDaEHL8dwbMECBT1L6tdg@mail.gmail.com>
2026-03-26 23:42 ` Xiang Shen
2026-03-26 23:44 ` Mateusz Guzik
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=20260326232130.1891210-1-turyshen@gmail.com \
--to=turyshen@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mjguzik@gmail.com \
--cc=stable@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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®