mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
To: Joseph Qi <joseph.qi@linux.alibaba.com>,
	Mark Fasheh <mark@fasheh.com>, Joel Becker <jlbec@evilplan.org>
Cc: Heming Zhao <heming.zhao@suse.com>,
	Cen Zhang <zzzccc427@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
	syzbot+38fdbb551940270021f1@syzkaller.appspotmail.com
Subject: [PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
Date: Mon, 23 Nov 2026 16:19:33 +0700	[thread overview]
Message-ID: <20261123091933.42057-1-ngocthang2710.1999@gmail.com> (raw)

ocfs2_journal_shutdown() holds journal_shutdown_sem for write across
jbd2_journal_destroy(), which frees the journal's shrinker and takes
shrinker_mutex. ocfs2_clear_inode() takes the same rwsem for read and
can run from reclaim via super_cache_scan(). Lockdep reports the cycle:

  journal_shutdown_sem -> shrinker_mutex -> fs_reclaim
    -> journal_shutdown_sem

The write side only has to keep the journal alive while
ocfs2_clear_inode() calls jbd2_journal_release_jbd_inode(). The journal
has been flushed by then, so inodes cleaned up afterwards have nothing to
release. Detach j_journal under the rwsem and destroy it outside, and
make ocfs2_clear_inode() skip the release once j_journal is gone. The
lock now only covers pointer updates and the final kfree().

Reported-by: syzbot+38fdbb551940270021f1@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=38fdbb551940270021f1
Fixes: f277230d002d ("ocfs2: serialize inode journal access with shutdown")
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
---
Tested in QEMU (x86_64, syzbot config) with the syzbot C reproducer:
without this patch lockdep reports the circular dependency on the first
run; with it, 9 runs (3 boots x 3) are clean, debug_locks stays 1 and
journal_shutdown_sem no longer has shrinker_mutex or fs_reclaim among
its forward dependencies in /proc/lockdep.

 fs/ocfs2/inode.c   |  7 ++++---
 fs/ocfs2/journal.c | 17 ++++++++++-------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c
index e225a96d37535..8a26964472521 100644
--- a/fs/ocfs2/inode.c
+++ b/fs/ocfs2/inode.c
@@ -1320,11 +1320,12 @@ static void ocfs2_clear_inode(struct inode *inode)
 	/*
 	 * ip_jinode is used to track txns against this inode. The journal is
 	 * flushed before it is shut down, so an inode cleaned up after that
-	 * has nothing left to release. Hold the shutdown lock so a journal
-	 * found here stays allocated until the release has finished.
+	 * has nothing left to release; shutdown detaches j_journal first.
+	 * Hold the shutdown lock so a journal found here stays allocated
+	 * until the release has finished.
 	 */
 	down_read(&osb->journal_shutdown_sem);
-	if (osb->journal)
+	if (osb->journal && osb->journal->j_journal)
 		jbd2_journal_release_jbd_inode(osb->journal->j_journal,
 					       &oi->ip_jinode);
 	up_read(&osb->journal_shutdown_sem);
diff --git a/fs/ocfs2/journal.c b/fs/ocfs2/journal.c
index 9c4a48887c9a3..2529138918bc1 100644
--- a/fs/ocfs2/journal.c
+++ b/fs/ocfs2/journal.c
@@ -1064,6 +1064,7 @@ static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 {
 	struct ocfs2_journal *journal = NULL;
+	journal_t *j_journal;
 	int status = 0;
 	struct inode *inode = NULL;
 	int num_running_trans = 0;
@@ -1076,10 +1077,8 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 
 	inode = journal->j_inode;
 
-	if (journal->j_state != OCFS2_JOURNAL_LOADED) {
-		down_write(&osb->journal_shutdown_sem);
+	if (journal->j_state != OCFS2_JOURNAL_LOADED)
 		goto free;
-	}
 
 	/* need to inc inode use count - jbd2_journal_destroy will iput. */
 	if (!igrab(inode))
@@ -1116,13 +1115,17 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 	}
 
 	/*
-	 * Exclude the final JBD2 inode cleanup in ocfs2_clear_inode() while
-	 * the journal is destroyed and freed.
+	 * The journal is flushed, so ocfs2_clear_inode() has nothing left to
+	 * release. Detach it under the lock but destroy it outside: freeing
+	 * its shrinker must not nest in a lock that reclaim takes.
 	 */
 	down_write(&osb->journal_shutdown_sem);
+	j_journal = journal->j_journal;
+	journal->j_journal = NULL;
+	up_write(&osb->journal_shutdown_sem);
 
 	/* Shutdown the kernel journal system */
-	if (!jbd2_journal_destroy(journal->j_journal) && !status) {
+	if (!jbd2_journal_destroy(j_journal) && !status) {
 		/*
 		 * Do not toggle if flush was unsuccessful otherwise
 		 * will leave dirty metadata in a "clean" journal
@@ -1131,7 +1134,6 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 		if (status < 0)
 			mlog_errno(status);
 	}
-	journal->j_journal = NULL;
 
 	OCFS2_I(inode)->ip_open_count--;
 
@@ -1144,6 +1146,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 	journal->j_state = OCFS2_JOURNAL_FREE;
 
 free:
+	down_write(&osb->journal_shutdown_sem);
 	/* Serialize the removal with ocfs2_ci_fully_checkpointed(). */
 	spin_lock(&trans_inc_lock);
 	osb->journal = NULL;
-- 
2.43.0


             reply	other threads:[~2026-11-23  9:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  9:19 Nguyen Ngoc Thang [this message]
2026-11-23 10:48 ` Joseph Qi

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=20261123091933.42057-1-ngocthang2710.1999@gmail.com \
    --to=ngocthang2710.1999@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=heming.zhao@suse.com \
    --cc=jlbec@evilplan.org \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark@fasheh.com \
    --cc=ocfs2-devel@lists.linux.dev \
    --cc=syzbot+38fdbb551940270021f1@syzkaller.appspotmail.com \
    --cc=zzzccc427@gmail.com \
    /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®