[PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
From: Nguyen Ngoc Thang
Date: Mon Nov 23 2026 - 04:20:28 EST
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@xxxxxxxxxxxxxxxxxxxxxxxxx
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@xxxxxxxxx>
---
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