1 From e8859acb23a33ce1eeee3c6cf6c15038198aa75a Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 2 Nov 2021 16:10:02 +0900
4 Subject: f2fs: invalidate META_MAPPING before IPU/DIO write
6 From: Hyeong-Jun Kim <hj514.kim@samsung.com>
8 [ Upstream commit e3b49ea36802053f312013fd4ccb6e59920a9f76 ]
10 Encrypted pages during GC are read and cached in META_MAPPING.
11 However, due to cached pages in META_MAPPING, there is an issue where
12 newly written pages are lost by IPU or DIO writes.
14 Thread A - f2fs_gc() Thread B
16 down_write(i_gc_rwsem)
17 ra_data_block() ---- (a)
20 - down_read(i_gc_rwsem)
21 - __blockdev_direct_io()
22 - get_data_block_dio_write()
23 - f2fs_dio_submit_bio() ---- (b)
26 down_write(i_gc_rwsem)
27 move_data_block() ---- (c)
30 (a) In phase 3 of f2fs_gc(), up-to-date page is read from storage and
31 cached in META_MAPPING.
32 (b) In thread B, writing new data by IPU or DIO write on same blkaddr as
33 read in (a). cached page in META_MAPPING become out-dated.
34 (c) In phase 4 of f2fs_gc(), out-dated page in META_MAPPING is copied to
35 new blkaddr. In conclusion, the newly written data in (b) is lost.
37 To address this issue, invalidating pages in META_MAPPING before IPU or
40 Fixes: 6aa58d8ad20a ("f2fs: readahead encrypted block during GC")
41 Signed-off-by: Hyeong-Jun Kim <hj514.kim@samsung.com>
42 Reviewed-by: Chao Yu <chao@kernel.org>
43 Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
44 Stable-dep-of: fd244524c2cf ("f2fs: compress: fix to cover normal cluster write with cp_rwsem")
45 Signed-off-by: Sasha Levin <sashal@kernel.org>
48 fs/f2fs/segment.c | 3 +++
49 2 files changed, 5 insertions(+)
51 diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
52 index e005f97fd273e..25dafd1261d71 100644
55 @@ -1711,6 +1711,8 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
57 f2fs_wait_on_block_writeback_range(inode,
58 map->m_pblk, map->m_len);
59 + invalidate_mapping_pages(META_MAPPING(sbi),
60 + map->m_pblk, map->m_pblk);
62 if (map->m_multidev_dio) {
63 block_t blk_addr = map->m_pblk;
64 diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
65 index 25d53617a50e6..5eca50e50e16b 100644
66 --- a/fs/f2fs/segment.c
67 +++ b/fs/f2fs/segment.c
68 @@ -3622,6 +3622,9 @@ int f2fs_inplace_write_data(struct f2fs_io_info *fio)
72 + invalidate_mapping_pages(META_MAPPING(sbi),
73 + fio->new_blkaddr, fio->new_blkaddr);
75 stat_inc_inplace_blocks(fio->sbi);
77 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))