Commit 7377e853 authored by Hyeong-Jun Kim's avatar Hyeong-Jun Kim Committed by Jaegeuk Kim

f2fs: compress: fix potential deadlock of compress file

There is a potential deadlock between writeback process and a process
performing write_begin() or write_cache_pages() while trying to write
same compress file, but not compressable, as below:

[Process A] - doing checkpoint
[Process B]                     [Process C]
f2fs_write_cache_pages()
- lock_page() [all pages in cluster, 0-31]
- f2fs_write_multi_pages()
 - f2fs_write_raw_pages()
  - f2fs_write_single_data_page()
   - f2fs_do_write_data_page()
     - return -EAGAIN [f2fs_trylock_op() failed]
   - unlock_page(page) [e.g., page 0]
                                - generic_perform_write()
                                 - f2fs_write_begin()
                                  - f2fs_prepare_compress_overwrite()
                                   - prepare_compress_overwrite()
                                    - lock_page() [e.g., page 0]
                                    - lock_page() [e.g., page 1]
   - lock_page(page) [e.g., page 0]

Since there is no compress process, it is no longer necessary to hold
locks on every pages in cluster within f2fs_write_raw_pages().

This patch changes f2fs_write_raw_pages() to release all locks first
and then perform write same as the non-compress file in
f2fs_write_cache_pages().

Fixes: 4c8ff709 ("f2fs: support data compression")
Signed-off-by: default avatarHyeong-Jun Kim <hj514.kim@samsung.com>
Signed-off-by: default avatarSungjong Seo <sj1557.seo@samsung.com>
Signed-off-by: default avatarYoungjin Gil <youngjin.gil@samsung.com>
Reviewed-by: default avatarChao Yu <chao@kernel.org>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
parent 19bdba52
...@@ -1456,25 +1456,38 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, ...@@ -1456,25 +1456,38 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
enum iostat_type io_type) enum iostat_type io_type)
{ {
struct address_space *mapping = cc->inode->i_mapping; struct address_space *mapping = cc->inode->i_mapping;
int _submitted, compr_blocks, ret; int _submitted, compr_blocks, ret, i;
int i = -1, err = 0;
compr_blocks = f2fs_compressed_blocks(cc); compr_blocks = f2fs_compressed_blocks(cc);
if (compr_blocks < 0) {
err = compr_blocks; for (i = 0; i < cc->cluster_size; i++) {
goto out_err; if (!cc->rpages[i])
continue;
redirty_page_for_writepage(wbc, cc->rpages[i]);
unlock_page(cc->rpages[i]);
} }
if (compr_blocks < 0)
return compr_blocks;
for (i = 0; i < cc->cluster_size; i++) { for (i = 0; i < cc->cluster_size; i++) {
if (!cc->rpages[i]) if (!cc->rpages[i])
continue; continue;
retry_write: retry_write:
lock_page(cc->rpages[i]);
if (cc->rpages[i]->mapping != mapping) { if (cc->rpages[i]->mapping != mapping) {
continue_unlock:
unlock_page(cc->rpages[i]); unlock_page(cc->rpages[i]);
continue; continue;
} }
BUG_ON(!PageLocked(cc->rpages[i])); if (!PageDirty(cc->rpages[i]))
goto continue_unlock;
if (!clear_page_dirty_for_io(cc->rpages[i]))
goto continue_unlock;
ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
NULL, NULL, wbc, io_type, NULL, NULL, wbc, io_type,
...@@ -1489,26 +1502,15 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, ...@@ -1489,26 +1502,15 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
* avoid deadlock caused by cluster update race * avoid deadlock caused by cluster update race
* from foreground operation. * from foreground operation.
*/ */
if (IS_NOQUOTA(cc->inode)) { if (IS_NOQUOTA(cc->inode))
err = 0; return 0;
goto out_err;
}
ret = 0; ret = 0;
cond_resched(); cond_resched();
congestion_wait(BLK_RW_ASYNC, congestion_wait(BLK_RW_ASYNC,
DEFAULT_IO_TIMEOUT); DEFAULT_IO_TIMEOUT);
lock_page(cc->rpages[i]);
if (!PageDirty(cc->rpages[i])) {
unlock_page(cc->rpages[i]);
continue;
}
clear_page_dirty_for_io(cc->rpages[i]);
goto retry_write; goto retry_write;
} }
err = ret; return ret;
goto out_err;
} }
*submitted += _submitted; *submitted += _submitted;
...@@ -1517,14 +1519,6 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, ...@@ -1517,14 +1519,6 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
f2fs_balance_fs(F2FS_M_SB(mapping), true); f2fs_balance_fs(F2FS_M_SB(mapping), true);
return 0; return 0;
out_err:
for (++i; i < cc->cluster_size; i++) {
if (!cc->rpages[i])
continue;
redirty_page_for_writepage(wbc, cc->rpages[i]);
unlock_page(cc->rpages[i]);
}
return err;
} }
int f2fs_write_multi_pages(struct compress_ctx *cc, int f2fs_write_multi_pages(struct compress_ctx *cc,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment