Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH] ext4: submit pending IO before restarting writeback handle
@ 2026-12-02  8:41 Julian Sun
  2026-12-02  8:51 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Julian Sun @ 2026-12-02  8:41 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang

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 <sunjunchao@bytedance.com>
---
 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;
 			}
-- 

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

* Re: [PATCH] ext4: submit pending IO before restarting writeback handle
  2026-12-02  8:41 [PATCH] ext4: submit pending IO before restarting writeback handle Julian Sun
@ 2026-12-02  8:51 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  8:51 UTC (permalink / raw)
  To: Julian Sun, linux-ext4; +Cc: tytso

> 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)
> [ ... ]
> 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 <sunjunchao@bytedance.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202084138.3973158-1-sunjunchao@bytedance.com?part=1

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

end of thread, other threads:[~2026-12-02  8:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  8:41 [PATCH] ext4: submit pending IO before restarting writeback handle Julian Sun
2026-12-02  8:51 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox