mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
@ 2026-11-23  9:19 Nguyen Ngoc Thang
  2026-11-23 10:48 ` Joseph Qi
  0 siblings, 1 reply; 2+ messages in thread
From: Nguyen Ngoc Thang @ 2026-11-23  9:19 UTC (permalink / raw)
  To: Joseph Qi, Mark Fasheh, Joel Becker
  Cc: Heming Zhao, Cen Zhang, Andrew Morton, ocfs2-devel, linux-kernel,
	syzbot+38fdbb551940270021f1

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
  2026-11-23  9:19 [PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem Nguyen Ngoc Thang
@ 2026-11-23 10:48 ` Joseph Qi
  0 siblings, 0 replies; 2+ messages in thread
From: Joseph Qi @ 2026-11-23 10:48 UTC (permalink / raw)
  To: Nguyen Ngoc Thang
  Cc: Mark Fasheh, Joel Becker, Heming Zhao, Cen Zhang, Andrew Morton,
	ocfs2-devel, linux-kernel, syzbot+38fdbb551940270021f1

Hi,
It has been fixed in thread:
https://lore.kernel.org/ocfs2-devel/f946ba25-6454-4831-9d0a-83fc58721ee2@linux.alibaba.com/T/#t

On 11/23/26 5:19 PM, Nguyen Ngoc Thang wrote:
> 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

It also blocks ocfs2_clear_inode() from returning, which is what keeps the
inode alive while destroy walks transaction->t_inode_list.

Once the write side is dropped before destroy, ocfs2_clear_inode() takes the
read side successfully, sees j_journal == NULL, skips the release, letting
evict() free the inode while journal_submit_data_buffers() is mid-walk with
JI_COMMIT_RUNNING set on that jinode. ocfs2_journal_submit_inode_data_buffers()
then dereferences jinode->i_vfs_inode->i_mapping on freed memory.

Thanks,
Joseph

> 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;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 10:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  9:19 [PATCH] ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem Nguyen Ngoc Thang
2026-11-23 10:48 ` Joseph Qi

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®