From patchwork Tue Jun 25 02:32:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Yu X-Patchwork-Id: 13710460 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5E2CC2BD09 for ; Tue, 25 Jun 2024 02:33:05 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1sLvzU-0008EL-88; Tue, 25 Jun 2024 02:33:05 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sLvzS-0008EE-GA for linux-f2fs-devel@lists.sourceforge.net; Tue, 25 Jun 2024 02:33:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=o9H6mIuWXJSGbOFsNk141GvDeofI5HjFbv1WktmRc94=; b=YEm4M5A/7IzqSV+o3/ubVQkkC7 Jz+UPN61w78irkxD5k2q0SAdj8Qu4gcr3xyIQI+cNwsKFPooK2hXr1DFJqn73ycq+zJeq9z3zjINQ yInMY8dD7xMsA0JsNNV0DwwITimMtv0PbAR9GQZ95d3jS2MiYr0Yn3t6UZDpH7zxVkd4=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=o9H6mIuWXJSGbOFsNk141GvDeofI5HjFbv1WktmRc94=; b=Q vWnZtuptZgSPHlY7CmfaiFIBIAhhgTXYoKxlR5HNDavvZcBppHzDD55cFo7QsRkaJNsYztpwLdphs PoU2oDDvtYfX6gVBPejJsfsSOM+Bds1jw1v/wkfU9h+ZeKppaSb04Ob4GymbKlDMgRaKC9LK9y968 JlsITQ67PEYv1pdU=; Received: from sin.source.kernel.org ([145.40.73.55]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1sLvzT-0005Fh-BX for linux-f2fs-devel@lists.sourceforge.net; Tue, 25 Jun 2024 02:33:03 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id D9C55CE0AFF; Tue, 25 Jun 2024 02:32:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE756C2BBFC; Tue, 25 Jun 2024 02:32:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719282770; bh=iQ7FxTqeAItqesS1ZPu7HixwlemAIsTeA636rrAw/M0=; h=From:To:Cc:Subject:Date:From; b=PWV/9sMWLhwLnS+jlFkGQisXJnGE+d/HvLkZEZ489vFobTvseXqnuxnMYMx7OcKek QikUbmXHKTNGYVwk+MT4Ef18PHKwoLGneaFD3mQrtN4DvLsC4z1mzgFX5h3oT85zxL SR4EpU+IJ6G0ykXBku4ks6rDY/51IZpuunh7srXaNzNZNDi3vvJhmr3nC9Tyzt1j6M OpBc2cIl5pLvav8QU/hcYb6DVdmZ/i8Hh3HE+O7Xc8wZ/i50nO/xlOinhXzdpTjG7T k17796UsjEIn4qvp5HcJD5seh556pZoQ7N4G8kGIE6wyfvpPCmY+MeQpjizMJ+8McS iI/upPHC2dzHw== From: Chao Yu To: jaegeuk@kernel.org Date: Tue, 25 Jun 2024 10:32:39 +0800 Message-Id: <20240625023239.3534605-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Headers-End: 1sLvzT-0005Fh-BX Subject: [f2fs-dev] [PATCH v2] f2fs: fix to update user block counts in block_operations() X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net Commit 59c9081bc86e ("f2fs: allow write page cache when writting cp") allows write() to write data to page cache during checkpoint, so block count fields like .total_valid_block_count, .alloc_valid_block_count and .rf_node_block_count may encounter race condition as below: CP Thread A - write_checkpoint - block_operations - f2fs_down_write(&sbi->node_change) - __prepare_cp_block : ckpt->valid_block_count = .total_valid_block_count - f2fs_up_write(&sbi->node_change) - write - f2fs_preallocate_blocks - f2fs_map_blocks(,F2FS_GET_BLOCK_PRE_AIO) - f2fs_map_lock - f2fs_down_read(&sbi->node_change) - f2fs_reserve_new_blocks - inc_valid_block_count : percpu_counter_add(&sbi->alloc_valid_block_count, count) : sbi->total_valid_block_count += count - f2fs_up_read(&sbi->node_change) - do_checkpoint : sbi->last_valid_block_count = sbi->total_valid_block_count : percpu_counter_set(&sbi->alloc_valid_block_count, 0) : percpu_counter_set(&sbi->rf_node_block_count, 0) - fsync - need_do_checkpoint - f2fs_space_for_roll_forward : alloc_valid_block_count was reset to zero, so, it may missed last data during checkpoint Let's change to update .total_valid_block_count, .alloc_valid_block_count and .rf_node_block_count in block_operations(), then their access can be protected by .node_change and .cp_rwsem lock, so that it can avoid above race condition. Fixes: 59c9081bc86e ("f2fs: allow write page cache when writting cp") Cc: Yunlei He Signed-off-by: Chao Yu --- v2: - update user_block_counts in __prepare_cp_block() suggested by Jaegeuk. fs/f2fs/checkpoint.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c index 7cfe4e01dd7e..bdd96329dddd 100644 --- a/fs/f2fs/checkpoint.c +++ b/fs/f2fs/checkpoint.c @@ -1186,6 +1186,11 @@ static void __prepare_cp_block(struct f2fs_sb_info *sbi) ckpt->valid_node_count = cpu_to_le32(valid_node_count(sbi)); ckpt->valid_inode_count = cpu_to_le32(valid_inode_count(sbi)); ckpt->next_free_nid = cpu_to_le32(last_nid); + + /* update user_block_counts */ + sbi->last_valid_block_count = sbi->total_valid_block_count; + percpu_counter_set(&sbi->alloc_valid_block_count, 0); + percpu_counter_set(&sbi->rf_node_block_count, 0); } static bool __need_flush_quota(struct f2fs_sb_info *sbi) @@ -1575,11 +1580,6 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc) start_blk += NR_CURSEG_NODE_TYPE; } - /* update user_block_counts */ - sbi->last_valid_block_count = sbi->total_valid_block_count; - percpu_counter_set(&sbi->alloc_valid_block_count, 0); - percpu_counter_set(&sbi->rf_node_block_count, 0); - /* Here, we have one bio having CP pack except cp pack 2 page */ f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_CP_META_IO); /* Wait for all dirty meta pages to be submitted for IO */