f2fs: fix to avoid racing in between read and OPU dio write
authorChao Yu <chao@kernel.org>
Thu, 27 Jun 2024 07:15:21 +0000 (15:15 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Thu, 15 Aug 2024 15:26:40 +0000 (15:26 +0000)
commit0cac51185e65dc2a20686184e02f3cafc99eb202
tree9b067d2862d9dd232690f20740bbd4776f659726
parent96cfeb0389530ae32ade8a48ae3ae1ac3b6c009d
f2fs: fix to avoid racing in between read and OPU dio write

If lfs mode is on, buffered read may race w/ OPU dio write as below,
it may cause buffered read hits unwritten data unexpectly, and for
dio read, the race condition exists as well.

Thread A Thread B
- f2fs_file_write_iter
 - f2fs_dio_write_iter
  - __iomap_dio_rw
   - f2fs_iomap_begin
    - f2fs_map_blocks
     - __allocate_data_block
      - allocated blkaddr #x
       - iomap_dio_submit_bio
- f2fs_file_read_iter
 - filemap_read
  - f2fs_read_data_folio
   - f2fs_mpage_readpages
    - f2fs_map_blocks
     : get blkaddr #x
    - f2fs_submit_read_bio
IRQ
- f2fs_read_end_io
 : read IO on blkaddr #x complete
IRQ
- iomap_dio_bio_end_io
 : direct write IO on blkaddr #x complete

In LFS mode, if there is inflight dio, let's wait for its completion,
this policy won't cover all race cases, however it is a tradeoff which
avoids abusing lock around IO paths.

Fixes: f847c699cff3 ("f2fs: allow out-place-update for direct IO in LFS mode")
Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/file.c