ocfs2_journal_shutdown() holds journal_shutdown_sem for write across jbd2_journal_destroy(). Destroying the journal frees its shrinker and takes shrinker_mutex. That mutex is also held around GFP_KERNEL allocations, so it ranks above fs_reclaim. ocfs2_evict_inode() takes the same rwsem for read from reclaim, and lockdep reports: journal_shutdown_sem -> shrinker_mutex -> fs_reclaim -> journal_shutdown_sem The journal has already been flushed. Detach j_journal while holding the rwsem so ocfs2_clear_inode() skips jbd2_journal_release_jbd_inode(), then destroy the journal after dropping the lock. Reproduced with the syzbot C reproducer in QEMU (PREEMPT_DYNAMIC, KASAN, PROVE_LOCKING, 4 GiB). On 007722b8211f, lockdep reports the cycle. With this patch, the same reproducer completes both unmounts and does not report it. Fixes: f277230d002d ("ocfs2: serialize inode journal access with shutdown") Reported-by: syzbot+00969d07ff718daa64c0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=00969d07ff718daa64c0 Signed-off-by: Franklin-Qi --- fs/ocfs2/inode.c | 10 +++++----- fs/ocfs2/journal.c | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c index e225a96d3753..8f8db3528414 100644 --- a/fs/ocfs2/inode.c +++ b/fs/ocfs2/inode.c @@ -1318,13 +1318,13 @@ static void ocfs2_clear_inode(struct inode *inode) oi->ip_blkno = 0ULL; /* - * 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. + * ip_jinode tracks transactions against this inode. The journal is + * flushed before shutdown and j_journal is cleared under this rwsem + * before jbd2_journal_destroy(), so an inode cleaned up after that + * has nothing left to release. */ 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 9c4a48887c9a..f11bc8967a6a 100644 --- a/fs/ocfs2/journal.c +++ b/fs/ocfs2/journal.c @@ -1067,6 +1067,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb) int status = 0; struct inode *inode = NULL; int num_running_trans = 0; + journal_t *jbd2_journal; BUG_ON(!osb); @@ -1116,13 +1117,20 @@ 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. + * ocfs2_clear_inode() takes this rwsem from reclaim and releases + * ip_jinode only while j_journal is still set. The journal has + * already been flushed, so an inode evicted from here on has + * nothing to release. Clear the pointer before destroying the + * journal: jbd2_journal_destroy() frees its shrinker under + * shrinker_mutex, which is ordered above fs_reclaim. */ down_write(&osb->journal_shutdown_sem); + jbd2_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(jbd2_journal) && !status) { /* * Do not toggle if flush was unsuccessful otherwise * will leave dirty metadata in a "clean" journal @@ -1131,7 +1139,8 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb) if (status < 0) mlog_errno(status); } - journal->j_journal = NULL; + + down_write(&osb->journal_shutdown_sem); OCFS2_I(inode)->ip_open_count--; base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29 -- 2.55.0