ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
PATCH 1/1 ocfs2: don't destroy the jbd2 journal under journal_shutdown_sem
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().
---
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(-)
fs/ocfs2/inode.c +4 -3
@@ -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);
fs/ocfs2/journal.c +10 -7
@@ -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 ;
Signature · 1 line
2.43.0
Joseph Qi
maintainerHi,
It has been fixed in thread:
https://lore.kernel.org/ocfs2-devel/[email protected]/T/#t
Quoted 10 lines · Nguyen Ngoc Thang
ocfs2_journal_shutdown()holdsjournal_shutdown_semfor write acrossjbd2_journal_destroy(), which frees the journal's shrinker and takesshrinker_mutex.ocfs2_clear_inode()takes the same rwsem for read and can run from reclaim viasuper_cache_scan(). Lockdep reports the cycle:
journal_shutdown_sem->shrinker_mutex->fs_reclaim-> journal_shutdown_semThe write side only has to keep the journal alive while
ocfs2_clear_inode()callsjbd2_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
Quoted 101 lines
has been flushed by then, so inodes cleaned up afterwards have nothing to release. Detach
j_journalunder the rwsem and destroy it outside, and makeocfs2_clear_inode()skip the release oncej_journalis gone. The lock now only covers pointer updates and the finalkfree().Reported-by [email protected]Fixes f277230d002d ("ocfs2: serialize inode journal access with shutdown")Signed-off-by Nguyen Ngoc Thang <[email protected]>---
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_locksstays 1 andjournal_shutdown_semno longer hasshrinker_mutexorfs_reclaimamong its forward dependencies in/proc/lockdep.fs/ocfs2/inode.c | 7 ++++--- fs/ocfs2/journal.c | 17 ++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-)fs/ocfs2/inode.c +4
-3@@ -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);fs/ocfs2/journal.c +10
-7@@ -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 ;