Skip to content

Commit 7377e85

Browse files
Hyeong-Jun KimJaegeuk Kim
authored andcommitted
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: 4c8ff70 ("f2fs: support data compression") Signed-off-by: Hyeong-Jun Kim <[email protected]> Signed-off-by: Sungjong Seo <[email protected]> Signed-off-by: Youngjin Gil <[email protected]> Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 19bdba5 commit 7377e85

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

fs/f2fs/compress.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,25 +1456,38 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
14561456
enum iostat_type io_type)
14571457
{
14581458
struct address_space *mapping = cc->inode->i_mapping;
1459-
int _submitted, compr_blocks, ret;
1460-
int i = -1, err = 0;
1459+
int _submitted, compr_blocks, ret, i;
14611460

14621461
compr_blocks = f2fs_compressed_blocks(cc);
1463-
if (compr_blocks < 0) {
1464-
err = compr_blocks;
1465-
goto out_err;
1462+
1463+
for (i = 0; i < cc->cluster_size; i++) {
1464+
if (!cc->rpages[i])
1465+
continue;
1466+
1467+
redirty_page_for_writepage(wbc, cc->rpages[i]);
1468+
unlock_page(cc->rpages[i]);
14661469
}
14671470

1471+
if (compr_blocks < 0)
1472+
return compr_blocks;
1473+
14681474
for (i = 0; i < cc->cluster_size; i++) {
14691475
if (!cc->rpages[i])
14701476
continue;
14711477
retry_write:
1478+
lock_page(cc->rpages[i]);
1479+
14721480
if (cc->rpages[i]->mapping != mapping) {
1481+
continue_unlock:
14731482
unlock_page(cc->rpages[i]);
14741483
continue;
14751484
}
14761485

1477-
BUG_ON(!PageLocked(cc->rpages[i]));
1486+
if (!PageDirty(cc->rpages[i]))
1487+
goto continue_unlock;
1488+
1489+
if (!clear_page_dirty_for_io(cc->rpages[i]))
1490+
goto continue_unlock;
14781491

14791492
ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
14801493
NULL, NULL, wbc, io_type,
@@ -1489,26 +1502,15 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
14891502
* avoid deadlock caused by cluster update race
14901503
* from foreground operation.
14911504
*/
1492-
if (IS_NOQUOTA(cc->inode)) {
1493-
err = 0;
1494-
goto out_err;
1495-
}
1505+
if (IS_NOQUOTA(cc->inode))
1506+
return 0;
14961507
ret = 0;
14971508
cond_resched();
14981509
congestion_wait(BLK_RW_ASYNC,
14991510
DEFAULT_IO_TIMEOUT);
1500-
lock_page(cc->rpages[i]);
1501-
1502-
if (!PageDirty(cc->rpages[i])) {
1503-
unlock_page(cc->rpages[i]);
1504-
continue;
1505-
}
1506-
1507-
clear_page_dirty_for_io(cc->rpages[i]);
15081511
goto retry_write;
15091512
}
1510-
err = ret;
1511-
goto out_err;
1513+
return ret;
15121514
}
15131515

15141516
*submitted += _submitted;
@@ -1517,14 +1519,6 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
15171519
f2fs_balance_fs(F2FS_M_SB(mapping), true);
15181520

15191521
return 0;
1520-
out_err:
1521-
for (++i; i < cc->cluster_size; i++) {
1522-
if (!cc->rpages[i])
1523-
continue;
1524-
redirty_page_for_writepage(wbc, cc->rpages[i]);
1525-
unlock_page(cc->rpages[i]);
1526-
}
1527-
return err;
15281522
}
15291523

15301524
int f2fs_write_multi_pages(struct compress_ctx *cc,

0 commit comments

Comments
 (0)