Skip to content

Commit 5639b73

Browse files
chaseyugregkh
authored andcommitted
f2fs: compress: fix race condition of overwrite vs truncate
[ Upstream commit a949dc5 ] pos_fsstress testcase complains a panic as belew: ------------[ cut here ]------------ kernel BUG at fs/f2fs/compress.c:1082! invalid opcode: 0000 [raspberrypi#1] SMP PTI CPU: 4 PID: 2753477 Comm: kworker/u16:2 Tainted: G OE 5.12.0-rc1-custom raspberrypi#1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 Workqueue: writeback wb_workfn (flush-252:16) RIP: 0010:prepare_compress_overwrite+0x4c0/0x760 [f2fs] Call Trace: f2fs_prepare_compress_overwrite+0x5f/0x80 [f2fs] f2fs_write_cache_pages+0x468/0x8a0 [f2fs] f2fs_write_data_pages+0x2a4/0x2f0 [f2fs] do_writepages+0x38/0xc0 __writeback_single_inode+0x44/0x2a0 writeback_sb_inodes+0x223/0x4d0 __writeback_inodes_wb+0x56/0xf0 wb_writeback+0x1dd/0x290 wb_workfn+0x309/0x500 process_one_work+0x220/0x3c0 worker_thread+0x53/0x420 kthread+0x12f/0x150 ret_from_fork+0x22/0x30 The root cause is truncate() may race with overwrite as below, so that one reference count left in page can not guarantee the page attaching in mapping tree all the time, after truncation, later find_lock_page() may return NULL pointer. - prepare_compress_overwrite - f2fs_pagecache_get_page - unlock_page - f2fs_setattr - truncate_setsize - truncate_inode_page - delete_from_page_cache - find_lock_page Fix this by avoiding referencing updated page. Fixes: 4c8ff70 ("f2fs: support data compression") Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 72b0f30 commit 5639b73

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

‎fs/f2fs/compress.c‎

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,6 @@ static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
123123
f2fs_drop_rpages(cc, len, true);
124124
}
125125

126-
static void f2fs_put_rpages_mapping(struct address_space *mapping,
127-
pgoff_t start, int len)
128-
{
129-
int i;
130-
131-
for (i = 0; i < len; i++) {
132-
struct page *page = find_get_page(mapping, start + i);
133-
134-
put_page(page);
135-
put_page(page);
136-
}
137-
}
138-
139126
static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
140127
struct writeback_control *wbc, bool redirty, int unlock)
141128
{
@@ -986,7 +973,7 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
986973
}
987974

988975
if (PageUptodate(page))
989-
unlock_page(page);
976+
f2fs_put_page(page, 1);
990977
else
991978
f2fs_compress_ctx_add_page(cc, page);
992979
}
@@ -996,32 +983,34 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
996983

997984
ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
998985
&last_block_in_bio, false, true);
986+
f2fs_put_rpages(cc);
999987
f2fs_destroy_compress_ctx(cc);
1000988
if (ret)
1001-
goto release_pages;
989+
goto out;
1002990
if (bio)
1003991
f2fs_submit_bio(sbi, bio, DATA);
1004992

1005993
ret = f2fs_init_compress_ctx(cc);
1006994
if (ret)
1007-
goto release_pages;
995+
goto out;
1008996
}
1009997

1010998
for (i = 0; i < cc->cluster_size; i++) {
1011999
f2fs_bug_on(sbi, cc->rpages[i]);
10121000

10131001
page = find_lock_page(mapping, start_idx + i);
1014-
f2fs_bug_on(sbi, !page);
1002+
if (!page) {
1003+
/* page can be truncated */
1004+
goto release_and_retry;
1005+
}
10151006

10161007
f2fs_wait_on_page_writeback(page, DATA, true, true);
1017-
10181008
f2fs_compress_ctx_add_page(cc, page);
1019-
f2fs_put_page(page, 0);
10201009

10211010
if (!PageUptodate(page)) {
1011+
release_and_retry:
1012+
f2fs_put_rpages(cc);
10221013
f2fs_unlock_rpages(cc, i + 1);
1023-
f2fs_put_rpages_mapping(mapping, start_idx,
1024-
cc->cluster_size);
10251014
f2fs_destroy_compress_ctx(cc);
10261015
goto retry;
10271016
}
@@ -1053,10 +1042,10 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
10531042
}
10541043

10551044
unlock_pages:
1045+
f2fs_put_rpages(cc);
10561046
f2fs_unlock_rpages(cc, i);
1057-
release_pages:
1058-
f2fs_put_rpages_mapping(mapping, start_idx, i);
10591047
f2fs_destroy_compress_ctx(cc);
1048+
out:
10601049
return ret;
10611050
}
10621051

0 commit comments

Comments
 (0)