From nobody Mon May 25 04:34:00 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72D812DAFB0 for ; Tue, 19 May 2026 01:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779153287; cv=none; b=F7Ti2QPcHdk3yeGdl7FxvKiGU7T5METTzLRLh9pZXjbj269Qi7nDZ8wtRW9GEakAVxpOUjrAJ/RWm352Qn2KZGknBtXOKBBVNnQy8CSQ32EoAdA6o2ZzYvX3nYMUXZ+U17LRet1ex3S3jW+A5YfStdhJPVeZR7Vzepf2HOzssKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779153287; c=relaxed/simple; bh=NlDIn6FBCtFi7BuO96dUHpwwKydu65DhhbDzgT81XKE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=drEpd1f7Cd8x+KtqAHVe91oY1wpBAsiGeC3Icyks+hsxPzmkBKJOWyIAsveXWv0VsbghjOjQA5FRKKZpLFuLc5vsQnXgmspaOXTX95HvO+civxMl18XYMk3Az2z3VFVy/bjv3fKp36TbzbSGHuFSH8V/fFL6NjU+GdHEU0RoBgY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DNmAbJKT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DNmAbJKT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EE3FC2BCF5; Tue, 19 May 2026 01:14:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779153287; bh=NlDIn6FBCtFi7BuO96dUHpwwKydu65DhhbDzgT81XKE=; h=From:To:Cc:Subject:Date:From; b=DNmAbJKTjBQj4epS6/R0WV3Nc13wc7Mp+V5lUOoXwmFkFDsDMr3xFZmrJhN64q2PA WV0wpbzv55DfFYq7D+6N/G9dlYf4p8v45PIN1hIu7GIADQ05WAiGvqzRlTOIhOwWT/ YaaqU+jToTqbCFZY7kei2qqCeOlHuDxOTDpvpImfYTLxgyNE7OXlqv6KkUn4ULGnRu 2pzhNORfPP0B0POg3F3ZNzpDe8HBgPtjXlFvFSL0VLOjXEdFWiBcB7QSpFxp4gYzex gh2sA1O/8laFEtslzjUCwA4NlGxSGyYiycptGJMVU7d6h5/HWc2Vp/dOIPeZQTO6G5 iv6F+fVc13MSA== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@kernel.org, Ruipeng Qi , Chao Yu Subject: [PATCH] f2fs: fix potential deadlock in gc_merge path of f2fs_balance_fs() Date: Tue, 19 May 2026 01:14:38 +0000 Message-ID: <20260519011438.1168155-1-chao@kernel.org> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 cl= eared - 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 Reported: Sandeep Dhavale Signed-off-by: Chao Yu Signed-off-by: Chao Yu --- 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 ne= ed) if (has_enough_free_secs(sbi, 0, 0)) return; =20 + /* + * 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 ne= ed) .err_gc_skipped =3D false, .nr_free_secs =3D 1 }; =20 - /* - * 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); --=20 2.49.0