147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2019 HUAWEI, Inc. 4592e7cd0SAlexander A. Klimov * https://www.huawei.com/ 547e4937aSGao Xiang * Created by Gao Xiang <gaoxiang25@huawei.com> 647e4937aSGao Xiang */ 747e4937aSGao Xiang #include "compress.h" 847e4937aSGao Xiang #include <linux/module.h> 947e4937aSGao Xiang #include <linux/lz4.h> 1047e4937aSGao Xiang 1147e4937aSGao Xiang #ifndef LZ4_DISTANCE_MAX /* history window size */ 1247e4937aSGao Xiang #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ 1347e4937aSGao Xiang #endif 1447e4937aSGao Xiang 1547e4937aSGao Xiang #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1) 1647e4937aSGao Xiang #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN 1747e4937aSGao Xiang #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32) 1847e4937aSGao Xiang #endif 1947e4937aSGao Xiang 2047e4937aSGao Xiang struct z_erofs_decompressor { 2147e4937aSGao Xiang /* 2247e4937aSGao Xiang * if destpages have sparsed pages, fill them with bounce pages. 2347e4937aSGao Xiang * it also check whether destpages indicate continuous physical memory. 2447e4937aSGao Xiang */ 2547e4937aSGao Xiang int (*prepare_destpages)(struct z_erofs_decompress_req *rq, 2647e4937aSGao Xiang struct list_head *pagepool); 2747e4937aSGao Xiang int (*decompress)(struct z_erofs_decompress_req *rq, u8 *out); 2847e4937aSGao Xiang char *name; 2947e4937aSGao Xiang }; 3047e4937aSGao Xiang 3199634bf3SGao Xiang static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq, 3247e4937aSGao Xiang struct list_head *pagepool) 3347e4937aSGao Xiang { 3447e4937aSGao Xiang const unsigned int nr = 3547e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 3647e4937aSGao Xiang struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL }; 3747e4937aSGao Xiang unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES, 3847e4937aSGao Xiang BITS_PER_LONG)] = { 0 }; 3947e4937aSGao Xiang void *kaddr = NULL; 4047e4937aSGao Xiang unsigned int i, j, top; 4147e4937aSGao Xiang 4247e4937aSGao Xiang top = 0; 4347e4937aSGao Xiang for (i = j = 0; i < nr; ++i, ++j) { 4447e4937aSGao Xiang struct page *const page = rq->out[i]; 4547e4937aSGao Xiang struct page *victim; 4647e4937aSGao Xiang 4747e4937aSGao Xiang if (j >= LZ4_MAX_DISTANCE_PAGES) 4847e4937aSGao Xiang j = 0; 4947e4937aSGao Xiang 5047e4937aSGao Xiang /* 'valid' bounced can only be tested after a complete round */ 5147e4937aSGao Xiang if (test_bit(j, bounced)) { 5247e4937aSGao Xiang DBG_BUGON(i < LZ4_MAX_DISTANCE_PAGES); 5347e4937aSGao Xiang DBG_BUGON(top >= LZ4_MAX_DISTANCE_PAGES); 5447e4937aSGao Xiang availables[top++] = rq->out[i - LZ4_MAX_DISTANCE_PAGES]; 5547e4937aSGao Xiang } 5647e4937aSGao Xiang 5747e4937aSGao Xiang if (page) { 5847e4937aSGao Xiang __clear_bit(j, bounced); 5947e4937aSGao Xiang if (kaddr) { 6047e4937aSGao Xiang if (kaddr + PAGE_SIZE == page_address(page)) 6147e4937aSGao Xiang kaddr += PAGE_SIZE; 6247e4937aSGao Xiang else 6347e4937aSGao Xiang kaddr = NULL; 6447e4937aSGao Xiang } else if (!i) { 6547e4937aSGao Xiang kaddr = page_address(page); 6647e4937aSGao Xiang } 6747e4937aSGao Xiang continue; 6847e4937aSGao Xiang } 6947e4937aSGao Xiang kaddr = NULL; 7047e4937aSGao Xiang __set_bit(j, bounced); 7147e4937aSGao Xiang 7247e4937aSGao Xiang if (top) { 7347e4937aSGao Xiang victim = availables[--top]; 7447e4937aSGao Xiang get_page(victim); 7547e4937aSGao Xiang } else { 76b4892fa3SHuang Jianan victim = erofs_allocpage(pagepool, 77b4892fa3SHuang Jianan GFP_KERNEL | __GFP_NOFAIL); 786aaa7b06SGao Xiang set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE); 7947e4937aSGao Xiang } 8047e4937aSGao Xiang rq->out[i] = victim; 8147e4937aSGao Xiang } 8247e4937aSGao Xiang return kaddr ? 1 : 0; 8347e4937aSGao Xiang } 8447e4937aSGao Xiang 8547e4937aSGao Xiang static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq, 8647e4937aSGao Xiang u8 *src, unsigned int pageofs_in) 8747e4937aSGao Xiang { 8847e4937aSGao Xiang /* 8947e4937aSGao Xiang * if in-place decompression is ongoing, those decompressed 9047e4937aSGao Xiang * pages should be copied in order to avoid being overlapped. 9147e4937aSGao Xiang */ 9247e4937aSGao Xiang struct page **in = rq->in; 9347e4937aSGao Xiang u8 *const tmp = erofs_get_pcpubuf(0); 9447e4937aSGao Xiang u8 *tmpp = tmp; 9547e4937aSGao Xiang unsigned int inlen = rq->inputsize - pageofs_in; 9647e4937aSGao Xiang unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in); 9747e4937aSGao Xiang 9847e4937aSGao Xiang while (tmpp < tmp + inlen) { 9947e4937aSGao Xiang if (!src) 10047e4937aSGao Xiang src = kmap_atomic(*in); 10147e4937aSGao Xiang memcpy(tmpp, src + pageofs_in, count); 10247e4937aSGao Xiang kunmap_atomic(src); 10347e4937aSGao Xiang src = NULL; 10447e4937aSGao Xiang tmpp += count; 10547e4937aSGao Xiang pageofs_in = 0; 10647e4937aSGao Xiang count = PAGE_SIZE; 10747e4937aSGao Xiang ++in; 10847e4937aSGao Xiang } 10947e4937aSGao Xiang return tmp; 11047e4937aSGao Xiang } 11147e4937aSGao Xiang 11299634bf3SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out) 11347e4937aSGao Xiang { 11447e4937aSGao Xiang unsigned int inputmargin, inlen; 11547e4937aSGao Xiang u8 *src; 11647e4937aSGao Xiang bool copied, support_0padding; 11747e4937aSGao Xiang int ret; 11847e4937aSGao Xiang 11947e4937aSGao Xiang if (rq->inputsize > PAGE_SIZE) 12047e4937aSGao Xiang return -EOPNOTSUPP; 12147e4937aSGao Xiang 12247e4937aSGao Xiang src = kmap_atomic(*rq->in); 12347e4937aSGao Xiang inputmargin = 0; 12447e4937aSGao Xiang support_0padding = false; 12547e4937aSGao Xiang 12647e4937aSGao Xiang /* decompression inplace is only safe when 0padding is enabled */ 127*de06a6a3SGao Xiang if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) { 12847e4937aSGao Xiang support_0padding = true; 12947e4937aSGao Xiang 13047e4937aSGao Xiang while (!src[inputmargin & ~PAGE_MASK]) 13147e4937aSGao Xiang if (!(++inputmargin & ~PAGE_MASK)) 13247e4937aSGao Xiang break; 13347e4937aSGao Xiang 13447e4937aSGao Xiang if (inputmargin >= rq->inputsize) { 13547e4937aSGao Xiang kunmap_atomic(src); 13647e4937aSGao Xiang return -EIO; 13747e4937aSGao Xiang } 13847e4937aSGao Xiang } 13947e4937aSGao Xiang 14047e4937aSGao Xiang copied = false; 14147e4937aSGao Xiang inlen = rq->inputsize - inputmargin; 14247e4937aSGao Xiang if (rq->inplace_io) { 14347e4937aSGao Xiang const uint oend = (rq->pageofs_out + 14447e4937aSGao Xiang rq->outputsize) & ~PAGE_MASK; 14547e4937aSGao Xiang const uint nr = PAGE_ALIGN(rq->pageofs_out + 14647e4937aSGao Xiang rq->outputsize) >> PAGE_SHIFT; 14747e4937aSGao Xiang 14847e4937aSGao Xiang if (rq->partial_decoding || !support_0padding || 14947e4937aSGao Xiang rq->out[nr - 1] != rq->in[0] || 15047e4937aSGao Xiang rq->inputsize - oend < 15147e4937aSGao Xiang LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) { 15247e4937aSGao Xiang src = generic_copy_inplace_data(rq, src, inputmargin); 15347e4937aSGao Xiang inputmargin = 0; 15447e4937aSGao Xiang copied = true; 15547e4937aSGao Xiang } 15647e4937aSGao Xiang } 15747e4937aSGao Xiang 158af1038abSGao Xiang /* legacy format could compress extra data in a pcluster. */ 159af1038abSGao Xiang if (rq->partial_decoding || !support_0padding) 16047e4937aSGao Xiang ret = LZ4_decompress_safe_partial(src + inputmargin, out, 16147e4937aSGao Xiang inlen, rq->outputsize, 16247e4937aSGao Xiang rq->outputsize); 163af1038abSGao Xiang else 164af1038abSGao Xiang ret = LZ4_decompress_safe(src + inputmargin, out, 165af1038abSGao Xiang inlen, rq->outputsize); 166af1038abSGao Xiang 167aa99a76bSGao Xiang if (ret != rq->outputsize) { 168aa99a76bSGao Xiang erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]", 169aa99a76bSGao Xiang ret, inlen, inputmargin, rq->outputsize); 170aa99a76bSGao Xiang 17147e4937aSGao Xiang WARN_ON(1); 17247e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET, 17347e4937aSGao Xiang 16, 1, src + inputmargin, inlen, true); 17447e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET, 17547e4937aSGao Xiang 16, 1, out, rq->outputsize, true); 176aa99a76bSGao Xiang 177aa99a76bSGao Xiang if (ret >= 0) 178aa99a76bSGao Xiang memset(out + ret, 0, rq->outputsize - ret); 17947e4937aSGao Xiang ret = -EIO; 18047e4937aSGao Xiang } 18147e4937aSGao Xiang 18247e4937aSGao Xiang if (copied) 18347e4937aSGao Xiang erofs_put_pcpubuf(src); 18447e4937aSGao Xiang else 18547e4937aSGao Xiang kunmap_atomic(src); 18647e4937aSGao Xiang return ret; 18747e4937aSGao Xiang } 18847e4937aSGao Xiang 18947e4937aSGao Xiang static struct z_erofs_decompressor decompressors[] = { 19047e4937aSGao Xiang [Z_EROFS_COMPRESSION_SHIFTED] = { 19147e4937aSGao Xiang .name = "shifted" 19247e4937aSGao Xiang }, 19347e4937aSGao Xiang [Z_EROFS_COMPRESSION_LZ4] = { 19499634bf3SGao Xiang .prepare_destpages = z_erofs_lz4_prepare_destpages, 19599634bf3SGao Xiang .decompress = z_erofs_lz4_decompress, 19647e4937aSGao Xiang .name = "lz4" 19747e4937aSGao Xiang }, 19847e4937aSGao Xiang }; 19947e4937aSGao Xiang 20047e4937aSGao Xiang static void copy_from_pcpubuf(struct page **out, const char *dst, 20147e4937aSGao Xiang unsigned short pageofs_out, 20247e4937aSGao Xiang unsigned int outputsize) 20347e4937aSGao Xiang { 20447e4937aSGao Xiang const char *end = dst + outputsize; 20547e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - pageofs_out; 20647e4937aSGao Xiang const char *cur = dst - pageofs_out; 20747e4937aSGao Xiang 20847e4937aSGao Xiang while (cur < end) { 20947e4937aSGao Xiang struct page *const page = *out++; 21047e4937aSGao Xiang 21147e4937aSGao Xiang if (page) { 21247e4937aSGao Xiang char *buf = kmap_atomic(page); 21347e4937aSGao Xiang 21447e4937aSGao Xiang if (cur >= dst) { 21547e4937aSGao Xiang memcpy(buf, cur, min_t(uint, PAGE_SIZE, 21647e4937aSGao Xiang end - cur)); 21747e4937aSGao Xiang } else { 21847e4937aSGao Xiang memcpy(buf + pageofs_out, cur + pageofs_out, 21947e4937aSGao Xiang min_t(uint, righthalf, end - cur)); 22047e4937aSGao Xiang } 22147e4937aSGao Xiang kunmap_atomic(buf); 22247e4937aSGao Xiang } 22347e4937aSGao Xiang cur += PAGE_SIZE; 22447e4937aSGao Xiang } 22547e4937aSGao Xiang } 22647e4937aSGao Xiang 22799634bf3SGao Xiang static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq, 22847e4937aSGao Xiang struct list_head *pagepool) 22947e4937aSGao Xiang { 23047e4937aSGao Xiang const unsigned int nrpages_out = 23147e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 23247e4937aSGao Xiang const struct z_erofs_decompressor *alg = decompressors + rq->alg; 23347e4937aSGao Xiang unsigned int dst_maptype; 23447e4937aSGao Xiang void *dst; 23573d03931SGao Xiang int ret, i; 23647e4937aSGao Xiang 23747e4937aSGao Xiang if (nrpages_out == 1 && !rq->inplace_io) { 23847e4937aSGao Xiang DBG_BUGON(!*rq->out); 23947e4937aSGao Xiang dst = kmap_atomic(*rq->out); 24047e4937aSGao Xiang dst_maptype = 0; 24147e4937aSGao Xiang goto dstmap_out; 24247e4937aSGao Xiang } 24347e4937aSGao Xiang 24447e4937aSGao Xiang /* 24547e4937aSGao Xiang * For the case of small output size (especially much less 24647e4937aSGao Xiang * than PAGE_SIZE), memcpy the decompressed data rather than 24747e4937aSGao Xiang * compressed data is preferred. 24847e4937aSGao Xiang */ 24947e4937aSGao Xiang if (rq->outputsize <= PAGE_SIZE * 7 / 8) { 25047e4937aSGao Xiang dst = erofs_get_pcpubuf(0); 25147e4937aSGao Xiang if (IS_ERR(dst)) 25247e4937aSGao Xiang return PTR_ERR(dst); 25347e4937aSGao Xiang 25447e4937aSGao Xiang rq->inplace_io = false; 25547e4937aSGao Xiang ret = alg->decompress(rq, dst); 25647e4937aSGao Xiang if (!ret) 25747e4937aSGao Xiang copy_from_pcpubuf(rq->out, dst, rq->pageofs_out, 25847e4937aSGao Xiang rq->outputsize); 25947e4937aSGao Xiang 26047e4937aSGao Xiang erofs_put_pcpubuf(dst); 26147e4937aSGao Xiang return ret; 26247e4937aSGao Xiang } 26347e4937aSGao Xiang 26447e4937aSGao Xiang ret = alg->prepare_destpages(rq, pagepool); 26547e4937aSGao Xiang if (ret < 0) { 26647e4937aSGao Xiang return ret; 26747e4937aSGao Xiang } else if (ret) { 26847e4937aSGao Xiang dst = page_address(*rq->out); 26947e4937aSGao Xiang dst_maptype = 1; 27047e4937aSGao Xiang goto dstmap_out; 27147e4937aSGao Xiang } 27247e4937aSGao Xiang 27373d03931SGao Xiang i = 0; 27473d03931SGao Xiang while (1) { 275d4efd79aSChristoph Hellwig dst = vm_map_ram(rq->out, nrpages_out, -1); 27673d03931SGao Xiang 27773d03931SGao Xiang /* retry two more times (totally 3 times) */ 27873d03931SGao Xiang if (dst || ++i >= 3) 27973d03931SGao Xiang break; 28073d03931SGao Xiang vm_unmap_aliases(); 28173d03931SGao Xiang } 28273d03931SGao Xiang 28347e4937aSGao Xiang if (!dst) 28447e4937aSGao Xiang return -ENOMEM; 28573d03931SGao Xiang 28647e4937aSGao Xiang dst_maptype = 2; 28747e4937aSGao Xiang 28847e4937aSGao Xiang dstmap_out: 28947e4937aSGao Xiang ret = alg->decompress(rq, dst + rq->pageofs_out); 29047e4937aSGao Xiang 29147e4937aSGao Xiang if (!dst_maptype) 29247e4937aSGao Xiang kunmap_atomic(dst); 29347e4937aSGao Xiang else if (dst_maptype == 2) 29473d03931SGao Xiang vm_unmap_ram(dst, nrpages_out); 29547e4937aSGao Xiang return ret; 29647e4937aSGao Xiang } 29747e4937aSGao Xiang 29899634bf3SGao Xiang static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq, 29947e4937aSGao Xiang struct list_head *pagepool) 30047e4937aSGao Xiang { 30147e4937aSGao Xiang const unsigned int nrpages_out = 30247e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 30347e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out; 30447e4937aSGao Xiang unsigned char *src, *dst; 30547e4937aSGao Xiang 30647e4937aSGao Xiang if (nrpages_out > 2) { 30747e4937aSGao Xiang DBG_BUGON(1); 30847e4937aSGao Xiang return -EIO; 30947e4937aSGao Xiang } 31047e4937aSGao Xiang 31147e4937aSGao Xiang if (rq->out[0] == *rq->in) { 31247e4937aSGao Xiang DBG_BUGON(nrpages_out != 1); 31347e4937aSGao Xiang return 0; 31447e4937aSGao Xiang } 31547e4937aSGao Xiang 31647e4937aSGao Xiang src = kmap_atomic(*rq->in); 3174d202437SGao Xiang if (rq->out[0]) { 31847e4937aSGao Xiang dst = kmap_atomic(rq->out[0]); 31947e4937aSGao Xiang memcpy(dst + rq->pageofs_out, src, righthalf); 3204d202437SGao Xiang kunmap_atomic(dst); 32147e4937aSGao Xiang } 32247e4937aSGao Xiang 3234d202437SGao Xiang if (nrpages_out == 2) { 3244d202437SGao Xiang DBG_BUGON(!rq->out[1]); 32547e4937aSGao Xiang if (rq->out[1] == *rq->in) { 32647e4937aSGao Xiang memmove(src, src + righthalf, rq->pageofs_out); 3274d202437SGao Xiang } else { 32847e4937aSGao Xiang dst = kmap_atomic(rq->out[1]); 32947e4937aSGao Xiang memcpy(dst, src + righthalf, rq->pageofs_out); 33047e4937aSGao Xiang kunmap_atomic(dst); 3314d202437SGao Xiang } 3324d202437SGao Xiang } 33347e4937aSGao Xiang kunmap_atomic(src); 33447e4937aSGao Xiang return 0; 33547e4937aSGao Xiang } 33647e4937aSGao Xiang 33747e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq, 33847e4937aSGao Xiang struct list_head *pagepool) 33947e4937aSGao Xiang { 34047e4937aSGao Xiang if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) 34199634bf3SGao Xiang return z_erofs_shifted_transform(rq, pagepool); 34299634bf3SGao Xiang return z_erofs_decompress_generic(rq, pagepool); 34347e4937aSGao Xiang } 34447e4937aSGao Xiang 345