In data=journal mode, mpage_prepare_extent_to_map() can deadlock with jbd2 commit when restarting the handle while holding an unsubmitted bio: Writeback worker jbd2 commit thread ---------------- ------------------ mpage_submit_folio() set folio writeback queue bio in io_bio mpage_journal_page_buffers() redirty folio ext4_journal_ensure_credits() jbd2__journal_restart() wait for commit ------> ext4_journalled_submit_inode_data_buffers() folio_wait_writeback() <--------------------------- (blocked on unsubmitted bio) Submitting the pending bio alone is not enough. With data_err=abort, a failed bio does not call folio_end_writeback() immediately; it defers completion until the last reference to mpd->io_submit.io_end is dropped. Since ext4_do_writepages() holds that reference until mpage_prepare_extent_to_map() returns, keeping the same io_end across a handle restart can still deadlock. Fix this by using ext4_journal_ensure_credits_fn() with a callback that submits any pending bio, drops the current reference to the old io_end, and replaces it with a new one before restarting the handle. This only runs when a restart is actually needed, so normal bio merging is unaffected. This deadlock was caught by syzbot; with this patch applied, the reproducer no longer hangs. Fixes: 3f079114bf52 ("ext4: Convert data=journal writeback to use ext4_writepages()") Reported-by: syzbot+2cc28770f99213896a7c@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=2cc28770f99213896a7c Cc: stable@vger.kernel.org Signed-off-by: Julian Sun --- fs/ext4/inode.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index e3129353d4ed..998ff0eaba50 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2712,6 +2712,20 @@ static int mpage_journal_page_buffers(handle_t *handle, return ext4_journal_folio_buffers(handle, folio, len); } +static int mpage_io_restart_fn(struct mpage_da_data *mpd) +{ + ext4_io_end_t *new_io_end; + + ext4_io_submit(&mpd->io_submit); + new_io_end = ext4_init_io_end(mpd->inode, GFP_KERNEL); + if (!new_io_end) + return -ENOMEM; + + ext4_put_io_end_defer(mpd->io_submit.io_end); + mpd->io_submit.io_end = new_io_end; + return 0; +} + /* * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages * needing mapping, submit mapped pages @@ -2785,8 +2799,17 @@ static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) goto out; if (handle) { - err = ext4_journal_ensure_credits(handle, bpp, - 0); + /* + * Restarting the handle may wait for a commit, + * which can wait for writeback of folios in our + * pending bio or deferred io_end completion. + * Submit pending bios and rotate io_end before + * restarting so writeback can complete. + */ + err = ext4_journal_ensure_credits_fn(handle, + bpp, bpp, + 0, + mpage_io_restart_fn(mpd)); if (err < 0) goto out; } --