From mboxrd@z Thu Jan 1 00:00:00 1970 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 04CE5C3064D for ; Mon, 24 Jun 2024 17:53:43 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1sLnss-0004Qe-Od; Mon, 24 Jun 2024 17:53:43 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sLnsq-0004QL-DG for linux-f2fs-devel@lists.sourceforge.net; Mon, 24 Jun 2024 17:53:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=In-Reply-To:Content-Type:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=J4ZLYf4a3EtGn29nYSnecDE5wiUsHD+CaE8lcr0dORQ=; b=Mjqnj3povbUVQ/TluEJl19wIEc fag3yIR7KX5eNL7qOe6d1obnpUe43sTdARj5bIVESOEhtyfO2rKk/6ptyEbcMH+gTalWfAFAUbGsY F5JyYiuejqeO8Q4VuwVUcCflFkg0phxAHM1ICaeje/kXmF1keqzi1FYVaUCAsd5uAXfI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To :From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=J4ZLYf4a3EtGn29nYSnecDE5wiUsHD+CaE8lcr0dORQ=; b=iAchfps9CaAle6j/SYVxgeLtlU MZE2RYON5lO/vj6Pqavnlh/5MRL4AdlcOn7msplLwTqVIhY72pQoBgohQQhcgKTVLdrimKbhd8Jim DwLCKTxSW4Wf9bN3T7yqX4Vo7NV1yFPoeGmoRtcWVu7Sfvh11YYIslI0kKPKzpHUAwe0=; 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 1sLnsq-0003tO-DU for linux-f2fs-devel@lists.sourceforge.net; Mon, 24 Jun 2024 17:53:40 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id DBFD2CE137E; Mon, 24 Jun 2024 17:53:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C41ADC32789; Mon, 24 Jun 2024 17:53:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719251607; bh=sV7eppB4xqZxl7orWq5DEhrQtYxq9XdZxd/7zbygi5Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=emrSZSLO81R0j6/M/RlR4nDU5/cXe/8EtimLnx4K76ipJHenhNkolVfyNvHthEJw8 PIPWOvrZOPigrlWsyjSEWsvgNZjERHYPodUeom2ibbJCHBBn//pjt+fWq8ov1KF5OE 0BcQt2WEYpQICQU8QMaain3NtTb7mfmUWOECXtjKvF2HKiPGmUd283CW/8M+/Q1Qk2 prvZ8U88rPTjY8220bzZZkqgEqtBZdbUpwFTxw8rrIV5TBaTzaZedfETcLRoODwZlT mFeWQqvRoC20pkbCFb6VC8L2AFrHxRWUtkT58oXkUFhYLT2q0cA+PblXkglIkk69uR lF0nx0KaNr8Ow== Date: Mon, 24 Jun 2024 17:53:25 +0000 From: Jaegeuk Kim To: Chao Yu Message-ID: References: <20240606095451.4088735-1-chao@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240606095451.4088735-1-chao@kernel.org> X-Headers-End: 1sLnsq-0003tO-DU Subject: Re: [f2fs-dev] [PATCH] 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net On 06/06, Chao Yu wrote: > 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 > --- > fs/f2fs/checkpoint.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c > index 66eaad591b60..010bbd5af211 100644 > --- a/fs/f2fs/checkpoint.c > +++ b/fs/f2fs/checkpoint.c > @@ -1298,6 +1298,12 @@ static int block_operations(struct f2fs_sb_info *sbi) > * dirty node blocks and some checkpoint values by block allocation. > */ > __prepare_cp_block(sbi); > + > + /* 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); Need to add this in __prepare_cp_block()? > + > f2fs_up_write(&sbi->node_change); > return err; > } > @@ -1575,11 +1581,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 */ > -- > 2.40.1 > _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel