Skip to content

Commit be3f603

Browse files
hsiangkaogregkh
authored andcommitted
erofs: fix unsafe pagevec reuse of hooked pclusters
commit 86432a6 upstream. There are pclusters in runtime marked with Z_EROFS_PCLUSTER_TAIL before actual I/O submission. Thus, the decompression chain can be extended if the following pcluster chain hooks such tail pcluster. As the related comment mentioned, if some page is made of a hooked pcluster and another followed pcluster, it can be reused for in-place I/O (since I/O should be submitted anyway): _______________________________________________________________ | tail (partial) page | head (partial) page | |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| However, it's by no means safe to reuse as pagevec since if such PRIMARY_HOOKED pclusters finally move into bypass chain without I/O submission. It's somewhat hard to reproduce with LZ4 and I just found it (general protection fault) by ro_fsstressing a LZMA image for long time. I'm going to actively clean up related code together with multi-page folio adaption in the next few months. Let's address it directly for easier backporting for now. Call trace for reference: z_erofs_decompress_pcluster+0x10a/0x8a0 [erofs] z_erofs_decompress_queue.isra.36+0x3c/0x60 [erofs] z_erofs_runqueue+0x5f3/0x840 [erofs] z_erofs_readahead+0x1e8/0x320 [erofs] read_pages+0x91/0x270 page_cache_ra_unbounded+0x18b/0x240 filemap_get_pages+0x10a/0x5f0 filemap_read+0xa9/0x330 new_sync_read+0x11b/0x1a0 vfs_read+0xf1/0x190 Link: https://lore.kernel.org/r/20211103182006.4040-1-xiang@kernel.org Fixes: 3883a79 ("staging: erofs: introduce VLE decompression support") Cc: <stable@vger.kernel.org> # 4.19+ Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6c1ad56 commit be3f603

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

fs/erofs/zdata.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
278278

279279
/* callers must be with collection lock held */
280280
static int z_erofs_attach_page(struct z_erofs_collector *clt,
281-
struct page *page,
282-
enum z_erofs_page_type type)
281+
struct page *page, enum z_erofs_page_type type,
282+
bool pvec_safereuse)
283283
{
284284
int ret;
285285

@@ -289,9 +289,9 @@ static int z_erofs_attach_page(struct z_erofs_collector *clt,
289289
z_erofs_try_inplace_io(clt, page))
290290
return 0;
291291

292-
ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
292+
ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
293+
pvec_safereuse);
293294
clt->cl->vcnt += (unsigned int)ret;
294-
295295
return ret ? 0 : -EAGAIN;
296296
}
297297

@@ -645,15 +645,16 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
645645
tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
646646

647647
retry:
648-
err = z_erofs_attach_page(clt, page, page_type);
648+
err = z_erofs_attach_page(clt, page, page_type,
649+
clt->mode >= COLLECT_PRIMARY_FOLLOWED);
649650
/* should allocate an additional staging page for pagevec */
650651
if (err == -EAGAIN) {
651652
struct page *const newpage =
652653
alloc_page(GFP_NOFS | __GFP_NOFAIL);
653654

654655
newpage->mapping = Z_EROFS_MAPPING_STAGING;
655656
err = z_erofs_attach_page(clt, newpage,
656-
Z_EROFS_PAGE_TYPE_EXCLUSIVE);
657+
Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
657658
if (!err)
658659
goto retry;
659660
}

fs/erofs/zpvec.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,18 @@ static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor,
107107

108108
static inline bool z_erofs_pagevec_enqueue(struct z_erofs_pagevec_ctor *ctor,
109109
struct page *page,
110-
enum z_erofs_page_type type)
110+
enum z_erofs_page_type type,
111+
bool pvec_safereuse)
111112
{
112-
if (!ctor->next && type)
113-
if (ctor->index + 1 == ctor->nr)
113+
if (!ctor->next) {
114+
/* some pages cannot be reused as pvec safely without I/O */
115+
if (type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && !pvec_safereuse)
116+
type = Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED;
117+
118+
if (type != Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
119+
ctor->index + 1 == ctor->nr)
114120
return false;
121+
}
115122

116123
if (ctor->index >= ctor->nr)
117124
z_erofs_pagevec_ctor_pagedown(ctor, false);

0 commit comments

Comments
 (0)