Commit af89bcef authored by Gao Xiang's avatar Gao Xiang Committed by Greg Kroah-Hartman

staging: erofs: fix LZ4 limited bounced page mis-reuse

Like all lz77-based algrithms, lz4 has a dynamically populated
("sliding window") dictionary and the maximum lookback distance
is 65535. Therefore the number of bounced pages could be limited
by erofs based on this property.

However, just now we observed some lz4 sequences in the extreme
case cannot be decompressed correctly after this feature is enabled,
the root causes after analysis are clear as follows:
1) max bounced pages should be 17 rather than 16 pages;
2) considering the following case, the broken implementation
   could reuse unsafely in advance (in other words, reuse it
   less than a safe distance),
   0 1 2 ... 16 17 18 ... 33 34
   b             p  b         b
   note that the bounce page that we are concerned was allocated
   at 0, and it reused at 18 since page 17 exists, but it mis-reused
   at 34 in advance again, which causes decompress failure.

This patch resolves the issue by introducing a bitmap to mark
whether the page in the same position of last round is a bounced
page or not, and a micro stack data structure to store all
available bounced pages.

Fixes: 7fc45dbc ("staging: erofs: introduce generic decompression backend")
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarGao Xiang <gaoxiang25@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 268d8281
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
#endif #endif
#define LZ4_MAX_DISTANCE_PAGES DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
#ifndef LZ4_DECOMPRESS_INPLACE_MARGIN #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
#define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32) #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
#endif #endif
...@@ -35,19 +35,28 @@ static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq, ...@@ -35,19 +35,28 @@ static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
const unsigned int nr = const unsigned int nr =
PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL }; struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
unsigned long unused[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES, unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
BITS_PER_LONG)] = { 0 }; BITS_PER_LONG)] = { 0 };
void *kaddr = NULL; void *kaddr = NULL;
unsigned int i, j, k; unsigned int i, j, top;
for (i = 0; i < nr; ++i) { top = 0;
for (i = j = 0; i < nr; ++i, ++j) {
struct page *const page = rq->out[i]; struct page *const page = rq->out[i];
struct page *victim;
j = i & (LZ4_MAX_DISTANCE_PAGES - 1); if (j >= LZ4_MAX_DISTANCE_PAGES)
if (availables[j]) j = 0;
__set_bit(j, unused);
/* 'valid' bounced can only be tested after a complete round */
if (test_bit(j, bounced)) {
DBG_BUGON(i < LZ4_MAX_DISTANCE_PAGES);
DBG_BUGON(top >= LZ4_MAX_DISTANCE_PAGES);
availables[top++] = rq->out[i - LZ4_MAX_DISTANCE_PAGES];
}
if (page) { if (page) {
__clear_bit(j, bounced);
if (kaddr) { if (kaddr) {
if (kaddr + PAGE_SIZE == page_address(page)) if (kaddr + PAGE_SIZE == page_address(page))
kaddr += PAGE_SIZE; kaddr += PAGE_SIZE;
...@@ -59,27 +68,24 @@ static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq, ...@@ -59,27 +68,24 @@ static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
continue; continue;
} }
kaddr = NULL; kaddr = NULL;
__set_bit(j, bounced);
k = find_first_bit(unused, LZ4_MAX_DISTANCE_PAGES); if (top) {
if (k < LZ4_MAX_DISTANCE_PAGES) { victim = availables[--top];
j = k; get_page(victim);
get_page(availables[j]);
} else { } else {
DBG_BUGON(availables[j]);
if (!list_empty(pagepool)) { if (!list_empty(pagepool)) {
availables[j] = lru_to_page(pagepool); victim = lru_to_page(pagepool);
list_del(&availables[j]->lru); list_del(&victim->lru);
DBG_BUGON(page_ref_count(availables[j]) != 1); DBG_BUGON(page_ref_count(victim) != 1);
} else { } else {
availables[j] = alloc_pages(GFP_KERNEL, 0); victim = alloc_pages(GFP_KERNEL, 0);
if (!availables[j]) if (!victim)
return -ENOMEM; return -ENOMEM;
} }
availables[j]->mapping = Z_EROFS_MAPPING_STAGING; victim->mapping = Z_EROFS_MAPPING_STAGING;
} }
rq->out[i] = availables[j]; rq->out[i] = victim;
__clear_bit(j, unused);
} }
return kaddr ? 1 : 0; return kaddr ? 1 : 0;
} }
......
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