mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs()
@ 2026-05-19  1:14 Chao Yu
  2026-05-22  4:30 ` [f2fs-dev] " patchwork-bot+f2fs
  0 siblings, 1 reply; 2+ messages in thread
From: Chao Yu @ 2026-05-19  1:14 UTC (permalink / raw)
  To: jaegeuk
  Cc: linux-f2fs-devel, linux-kernel, Chao Yu, stable, Ruipeng Qi, Chao Yu

When we mount device w/ gc_merge mount option, we may suffer below
potential deadlock:

Kworker					GC trehad			Truncator
- f2fs_write_cache_pages
 - f2fs_write_single_data_page
  - f2fs_do_write_data_page
   - folio_start_writeback  --- set writeback flag on folio
   - f2fs_outplace_write_data
   : cached folio in internal bio cache
  - f2fs_balance_fs
   - wake_up(gc_thread)
   : wake up gc thread to run foreground GC
   - finish_wait(fggc_wq)
   : wait on the waitqueue --- wait on GC thread to finish the work
									- truncate_inode_pages_range
									 - __filemap_get_folio(, FGP_LOCK)  --- lock folio
									 - truncate_inode_partial_folio
									  - folio_wait_writeback            --- wait on writeback being cleared
					- do_garbage_collect
					 - move_data_page
					  - f2fs_get_lock_data_folio
					   - lock on folio  --- blocked on folio's lock

In order to avoid such deadlock, let's call below functions to commit
cached bios in GC_MERGE path of f2fs_balance_fs() as the same as we did
in NOGC_MERGE path.
- f2fs_submit_merged_write(sbi, DATA);
- f2fs_submit_all_merged_ipu_writes(sbi);

Cc: stable@kernel.org
Fixes: 351df4b20115 ("f2fs: add segment operations")
Cc: Ruipeng Qi <ruipengqi3@gmail.com>
Reported: Sandeep Dhavale <dhavale@google.com>
Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Chao Yu <chaseyu@google.com>
---
 fs/f2fs/segment.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 7c8ac62b1b0d..1ef4edb77078 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -445,6 +445,13 @@ void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
 	if (has_enough_free_secs(sbi, 0, 0))
 		return;
 
+	/*
+	 * Submit all cached OPU/IPU DATA bios before triggering
+	 * foreground GC to avoid potential deadlocks.
+	 */
+	f2fs_submit_merged_write(sbi, DATA);
+	f2fs_submit_all_merged_ipu_writes(sbi);
+
 	if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
 				sbi->gc_thread->f2fs_gc_task) {
 		DEFINE_WAIT(wait);
@@ -464,13 +471,6 @@ void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
 			.err_gc_skipped = false,
 			.nr_free_secs = 1 };
 
-		/*
-		 * Submit all cached OPU/IPU DATA bios before triggering
-		 * foreground GC to avoid potential deadlocks.
-		 */
-		f2fs_submit_merged_write(sbi, DATA);
-		f2fs_submit_all_merged_ipu_writes(sbi);
-
 		f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
 		stat_inc_gc_call_count(sbi, FOREGROUND);
 		f2fs_gc(sbi, &gc_control);
-- 
2.49.0


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

* Re: [f2fs-dev] [PATCH] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs()
  2026-05-19  1:14 [PATCH] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs() Chao Yu
@ 2026-05-22  4:30 ` patchwork-bot+f2fs
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+f2fs @ 2026-05-22  4:30 UTC (permalink / raw)
  To: Chao Yu
  Cc: jaegeuk, chaseyu, ruipengqi3, linux-kernel, linux-f2fs-devel, stable

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <jaegeuk@kernel.org>:

On Tue, 19 May 2026 01:14:38 +0000 you wrote:
> When we mount device w/ gc_merge mount option, we may suffer below
> potential deadlock:
> 
> Kworker					GC trehad			Truncator
> - f2fs_write_cache_pages
>  - f2fs_write_single_data_page
>   - f2fs_do_write_data_page
>    - folio_start_writeback  --- set writeback flag on folio
>    - f2fs_outplace_write_data
>    : cached folio in internal bio cache
>   - f2fs_balance_fs
>    - wake_up(gc_thread)
>    : wake up gc thread to run foreground GC
>    - finish_wait(fggc_wq)
>    : wait on the waitqueue --- wait on GC thread to finish the work
> 									- truncate_inode_pages_range
> 									 - __filemap_get_folio(, FGP_LOCK)  --- lock folio
> 									 - truncate_inode_partial_folio
> 									  - folio_wait_writeback            --- wait on writeback being cleared
> 					- do_garbage_collect
> 					 - move_data_page
> 					  - f2fs_get_lock_data_folio
> 					   - lock on folio  --- blocked on folio's lock
> 
> [...]

Here is the summary with links:
  - [f2fs-dev] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs()
    https://git.kernel.org/jaegeuk/f2fs/c/cf69827e772c

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2026-05-22  4:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-19  1:14 [PATCH] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs() Chao Yu
2026-05-22  4:30 ` [f2fs-dev] " patchwork-bot+f2fs

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

Powered by JetHome