Skip to content

Commit 39ad058

Browse files
Hyeong-Jun Kimgregkh
authored andcommitted
f2fs: compress: fix potential deadlock of compress file
commit 7377e85 upstream. 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: 4c8ff70 ("f2fs: support data compression") Signed-off-by: Hyeong-Jun Kim <hj514.kim@samsung.com> Signed-off-by: Sungjong Seo <sj1557.seo@samsung.com> Signed-off-by: Youngjin Gil <youngjin.gil@samsung.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e184036 commit 39ad058

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

‎fs/f2fs/compress.c‎

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,25 +1362,38 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
13621362
enum iostat_type io_type)
13631363
{
13641364
struct address_space *mapping = cc->inode->i_mapping;
1365-
int _submitted, compr_blocks, ret;
1366-
int i = -1, err = 0;
1365+
int _submitted, compr_blocks, ret, i;
13671366

13681367
compr_blocks = f2fs_compressed_blocks(cc);
1369-
if (compr_blocks < 0) {
1370-
err = compr_blocks;
1371-
goto out_err;
1368+
1369+
for (i = 0; i < cc->cluster_size; i++) {
1370+
if (!cc->rpages[i])
1371+
continue;
1372+
1373+
redirty_page_for_writepage(wbc, cc->rpages[i]);
1374+
unlock_page(cc->rpages[i]);
13721375
}
13731376

1377+
if (compr_blocks < 0)
1378+
return compr_blocks;
1379+
13741380
for (i = 0; i < cc->cluster_size; i++) {
13751381
if (!cc->rpages[i])
13761382
continue;
13771383
retry_write:
1384+
lock_page(cc->rpages[i]);
1385+
13781386
if (cc->rpages[i]->mapping != mapping) {
1387+
continue_unlock:
13791388
unlock_page(cc->rpages[i]);
13801389
continue;
13811390
}
13821391

1383-
BUG_ON(!PageLocked(cc->rpages[i]));
1392+
if (!PageDirty(cc->rpages[i]))
1393+
goto continue_unlock;
1394+
1395+
if (!clear_page_dirty_for_io(cc->rpages[i]))
1396+
goto continue_unlock;
13841397

13851398
ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
13861399
NULL, NULL, wbc, io_type,
@@ -1395,26 +1408,15 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
13951408
* avoid deadlock caused by cluster update race
13961409
* from foreground operation.
13971410
*/
1398-
if (IS_NOQUOTA(cc->inode)) {
1399-
err = 0;
1400-
goto out_err;
1401-
}
1411+
if (IS_NOQUOTA(cc->inode))
1412+
return 0;
14021413
ret = 0;
14031414
cond_resched();
14041415
congestion_wait(BLK_RW_ASYNC,
14051416
DEFAULT_IO_TIMEOUT);
1406-
lock_page(cc->rpages[i]);
1407-
1408-
if (!PageDirty(cc->rpages[i])) {
1409-
unlock_page(cc->rpages[i]);
1410-
continue;
1411-
}
1412-
1413-
clear_page_dirty_for_io(cc->rpages[i]);
14141417
goto retry_write;
14151418
}
1416-
err = ret;
1417-
goto out_err;
1419+
return ret;
14181420
}
14191421

14201422
*submitted += _submitted;
@@ -1423,14 +1425,6 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
14231425
f2fs_balance_fs(F2FS_M_SB(mapping), true);
14241426

14251427
return 0;
1426-
out_err:
1427-
for (++i; i < cc->cluster_size; i++) {
1428-
if (!cc->rpages[i])
1429-
continue;
1430-
redirty_page_for_writepage(wbc, cc->rpages[i]);
1431-
unlock_page(cc->rpages[i]);
1432-
}
1433-
return err;
14341428
}
14351429

14361430
int f2fs_write_multi_pages(struct compress_ctx *cc,

0 commit comments

Comments
 (0)