From nobody Wed Dec 24 17:59:40 2025 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 1FBD685C5D for ; Wed, 24 Jan 2024 14:49:22 +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=1706107763; cv=none; b=Fv3z+FyOF+qILFv3JnnQvtpCxut9eeu9Z8XEt1kakAE1bvF2uo9KNi1ey0TB9WL4w1xEP6+FnMKyhy9l06WTylQXO4KnaYjrSizJmMPmK3f11ElP4pNIIne/IIWZ7Q70LtB0z9TevwrzvF7rqRpJGmf2AE+myozq9apoDeIxBWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706107763; c=relaxed/simple; bh=wrZ8xW7UkYZOxzyfUsSQ27WNRFcJn00rozogY+4egik=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=pW8uXZ/2jhkuwIr1Nk4hwE+ONoYSL2oTShhqoWVCzvZk9Oi9S3MJgwEiR5Voe+Z6RxkDl6nL6nHlkvg3AxoKmRzhyXCwJj+7qPVE8ZCtXlNLxm82mWGq+y/uAAQSaMDzjoQuoSYdy5qB4T9mF7g8qryyIkTnP801dackdnvRgss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ozvxQbs2; 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="ozvxQbs2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3CECC433F1; Wed, 24 Jan 2024 14:49:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706107762; bh=wrZ8xW7UkYZOxzyfUsSQ27WNRFcJn00rozogY+4egik=; h=From:To:Cc:Subject:Date:From; b=ozvxQbs2hx8v1XXKc82x4SIPBaYV5sJ9tivRFo+j0ZnsPepSLYgTsegf5CsWISwaH 72hxLcJZY3gW53fvG3uazQX4L94WvwQMB48t1lY97P2UY0jSkjcult8KFVYj4BO9lD hThJx+SV6k4tUHQ5gVlWwW4fPdD7zAOH3dVFnp9pcsNRzoDJc82uckZjTXrAG+vQlp 8LM+YoLrWTvVWMx/VyQxQJWc6ky02ALA4KpLwTUyK14jPsxiY3SMgJldTykRKgNT7I cV7Tejfm2iW4misVc6JL2LIYGe7VieiZKdUoQ1uhkMexHAPhdRjctwZH3kmj0l/k/p 4JWNKj//oU7Ag== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , Zhiguo Niu Subject: [PATCH] f2fs: fix to avoid potential panic during recovery Date: Wed, 24 Jan 2024 22:49:15 +0800 Message-Id: <20240124144915.19445-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 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" During recovery, if FAULT_BLOCK is on, it is possible that f2fs_reserve_new_block() will return -ENOSPC during recovery, then it may trigger panic. Also, if fault injection rate is 1 and only FAULT_BLOCK fault type is on, it may encounter deadloop in loop of block reservation. Let's change as below to fix these issues: - remove bug_on() to avoid panic. - limit the loop count of block reservation to avoid potential deadloop. Fixes: 956fa1ddc132 ("f2fs: fix to check return value of f2fs_reserve_new_b= lock()") Reported-by: Zhiguo Niu Signed-off-by: Chao Yu --- fs/f2fs/f2fs.h | 5 +++++ fs/f2fs/recovery.c | 33 ++++++++++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index a62c7e037456..5993b721e92c 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -76,6 +76,11 @@ struct f2fs_fault_info { =20 extern const char *f2fs_fault_name[FAULT_MAX]; #define IS_FAULT_SET(fi, type) ((fi)->inject_type & BIT(type)) + +/* maximum retry count for injected failure */ +#define DEFAULT_FAILURE_RETRY_COUNT 8 +#else +#define DEFAULT_FAILURE_RETRY_COUNT 1 #endif =20 /* diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c index d0f24ccbd1ac..aad1d1a9b3d6 100644 --- a/fs/f2fs/recovery.c +++ b/fs/f2fs/recovery.c @@ -611,6 +611,19 @@ static int check_index_in_prev_nodes(struct f2fs_sb_in= fo *sbi, return 0; } =20 +static int f2fs_reserve_new_block_retry(struct dnode_of_data *dn) +{ + int i, err =3D 0; + + for (i =3D DEFAULT_FAILURE_RETRY_COUNT; i > 0; i--) { + err =3D f2fs_reserve_new_block(dn); + if (!err) + break; + } + + return err; +} + static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode, struct page *page) { @@ -712,14 +725,8 @@ static int do_recover_data(struct f2fs_sb_info *sbi, s= truct inode *inode, */ if (dest =3D=3D NEW_ADDR) { f2fs_truncate_data_blocks_range(&dn, 1); - do { - err =3D f2fs_reserve_new_block(&dn); - if (err =3D=3D -ENOSPC) { - f2fs_bug_on(sbi, 1); - break; - } - } while (err && - IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)); + + err =3D f2fs_reserve_new_block_retry(&dn); if (err) goto err; continue; @@ -727,16 +734,8 @@ static int do_recover_data(struct f2fs_sb_info *sbi, s= truct inode *inode, =20 /* dest is valid block, try to recover from src to dest */ if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) { - if (src =3D=3D NULL_ADDR) { - do { - err =3D f2fs_reserve_new_block(&dn); - if (err =3D=3D -ENOSPC) { - f2fs_bug_on(sbi, 1); - break; - } - } while (err && - IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)); + err =3D f2fs_reserve_new_block_retry(&dn); if (err) goto err; } --=20 2.40.1