* [f2fs-dev] [PATCH v2] f2fs: fix to update user block counts in block_operations()
@ 2024-06-25 2:32 Chao Yu
2024-07-15 14:40 ` patchwork-bot+f2fs
0 siblings, 1 reply; 2+ messages in thread
From: Chao Yu @ 2024-06-25 2:32 UTC (permalink / raw)
To: jaegeuk; +Cc: linux-kernel, linux-f2fs-devel
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 <heyunlei@oppo.com>
Signed-off-by: Chao Yu <chao@kernel.org>
---
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 */
--
2.40.1
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [f2fs-dev] [PATCH v2] f2fs: fix to update user block counts in block_operations()
2024-06-25 2:32 [f2fs-dev] [PATCH v2] f2fs: fix to update user block counts in block_operations() Chao Yu
@ 2024-07-15 14:40 ` patchwork-bot+f2fs
0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+f2fs @ 2024-07-15 14:40 UTC (permalink / raw)
To: Chao Yu; +Cc: jaegeuk, linux-kernel, linux-f2fs-devel
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <jaegeuk@kernel.org>:
On Tue, 25 Jun 2024 10:32:39 +0800 you 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
>
> [...]
Here is the summary with links:
- [f2fs-dev,v2] f2fs: fix to update user block counts in block_operations()
https://git.kernel.org/jaegeuk/f2fs/c/f06c0f82e38b
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-15 14:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-25 2:32 [f2fs-dev] [PATCH v2] f2fs: fix to update user block counts in block_operations() Chao Yu
2024-07-15 14:40 ` 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;
as well as URLs for NNTP newsgroup(s).