147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2019 HUAWEI, Inc. 447e4937aSGao Xiang * http://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 { 765ddcee1fSGao Xiang victim = erofs_allocpage(pagepool, GFP_KERNEL); 778d8a09b0SGao Xiang if (!victim) 7847e4937aSGao Xiang return -ENOMEM; 7947e4937aSGao Xiang victim->mapping = Z_EROFS_MAPPING_STAGING; 8047e4937aSGao Xiang } 8147e4937aSGao Xiang rq->out[i] = victim; 8247e4937aSGao Xiang } 8347e4937aSGao Xiang return kaddr ? 1 : 0; 8447e4937aSGao Xiang } 8547e4937aSGao Xiang 8647e4937aSGao Xiang static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq, 8747e4937aSGao Xiang u8 *src, unsigned int pageofs_in) 8847e4937aSGao Xiang { 8947e4937aSGao Xiang /* 9047e4937aSGao Xiang * if in-place decompression is ongoing, those decompressed 9147e4937aSGao Xiang * pages should be copied in order to avoid being overlapped. 9247e4937aSGao Xiang */ 9347e4937aSGao Xiang struct page **in = rq->in; 9447e4937aSGao Xiang u8 *const tmp = erofs_get_pcpubuf(0); 9547e4937aSGao Xiang u8 *tmpp = tmp; 9647e4937aSGao Xiang unsigned int inlen = rq->inputsize - pageofs_in; 9747e4937aSGao Xiang unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in); 9847e4937aSGao Xiang 9947e4937aSGao Xiang while (tmpp < tmp + inlen) { 10047e4937aSGao Xiang if (!src) 10147e4937aSGao Xiang src = kmap_atomic(*in); 10247e4937aSGao Xiang memcpy(tmpp, src + pageofs_in, count); 10347e4937aSGao Xiang kunmap_atomic(src); 10447e4937aSGao Xiang src = NULL; 10547e4937aSGao Xiang tmpp += count; 10647e4937aSGao Xiang pageofs_in = 0; 10747e4937aSGao Xiang count = PAGE_SIZE; 10847e4937aSGao Xiang ++in; 10947e4937aSGao Xiang } 11047e4937aSGao Xiang return tmp; 11147e4937aSGao Xiang } 11247e4937aSGao Xiang 11399634bf3SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out) 11447e4937aSGao Xiang { 11547e4937aSGao Xiang unsigned int inputmargin, inlen; 11647e4937aSGao Xiang u8 *src; 11747e4937aSGao Xiang bool copied, support_0padding; 11847e4937aSGao Xiang int ret; 11947e4937aSGao Xiang 12047e4937aSGao Xiang if (rq->inputsize > PAGE_SIZE) 12147e4937aSGao Xiang return -EOPNOTSUPP; 12247e4937aSGao Xiang 12347e4937aSGao Xiang src = kmap_atomic(*rq->in); 12447e4937aSGao Xiang inputmargin = 0; 12547e4937aSGao Xiang support_0padding = false; 12647e4937aSGao Xiang 12747e4937aSGao Xiang /* decompression inplace is only safe when 0padding is enabled */ 128426a9308SGao Xiang if (EROFS_SB(rq->sb)->feature_incompat & 129426a9308SGao Xiang EROFS_FEATURE_INCOMPAT_LZ4_0PADDING) { 13047e4937aSGao Xiang support_0padding = true; 13147e4937aSGao Xiang 13247e4937aSGao Xiang while (!src[inputmargin & ~PAGE_MASK]) 13347e4937aSGao Xiang if (!(++inputmargin & ~PAGE_MASK)) 13447e4937aSGao Xiang break; 13547e4937aSGao Xiang 13647e4937aSGao Xiang if (inputmargin >= rq->inputsize) { 13747e4937aSGao Xiang kunmap_atomic(src); 13847e4937aSGao Xiang return -EIO; 13947e4937aSGao Xiang } 14047e4937aSGao Xiang } 14147e4937aSGao Xiang 14247e4937aSGao Xiang copied = false; 14347e4937aSGao Xiang inlen = rq->inputsize - inputmargin; 14447e4937aSGao Xiang if (rq->inplace_io) { 14547e4937aSGao Xiang const uint oend = (rq->pageofs_out + 14647e4937aSGao Xiang rq->outputsize) & ~PAGE_MASK; 14747e4937aSGao Xiang const uint nr = PAGE_ALIGN(rq->pageofs_out + 14847e4937aSGao Xiang rq->outputsize) >> PAGE_SHIFT; 14947e4937aSGao Xiang 15047e4937aSGao Xiang if (rq->partial_decoding || !support_0padding || 15147e4937aSGao Xiang rq->out[nr - 1] != rq->in[0] || 15247e4937aSGao Xiang rq->inputsize - oend < 15347e4937aSGao Xiang LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) { 15447e4937aSGao Xiang src = generic_copy_inplace_data(rq, src, inputmargin); 15547e4937aSGao Xiang inputmargin = 0; 15647e4937aSGao Xiang copied = true; 15747e4937aSGao Xiang } 15847e4937aSGao Xiang } 15947e4937aSGao Xiang 160af1038abSGao Xiang /* legacy format could compress extra data in a pcluster. */ 161af1038abSGao Xiang if (rq->partial_decoding || !support_0padding) 16247e4937aSGao Xiang ret = LZ4_decompress_safe_partial(src + inputmargin, out, 16347e4937aSGao Xiang inlen, rq->outputsize, 16447e4937aSGao Xiang rq->outputsize); 165af1038abSGao Xiang else 166af1038abSGao Xiang ret = LZ4_decompress_safe(src + inputmargin, out, 167af1038abSGao Xiang inlen, rq->outputsize); 168af1038abSGao Xiang 169aa99a76bSGao Xiang if (ret != rq->outputsize) { 170aa99a76bSGao Xiang erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]", 171aa99a76bSGao Xiang ret, inlen, inputmargin, rq->outputsize); 172aa99a76bSGao Xiang 17347e4937aSGao Xiang WARN_ON(1); 17447e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET, 17547e4937aSGao Xiang 16, 1, src + inputmargin, inlen, true); 17647e4937aSGao Xiang print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET, 17747e4937aSGao Xiang 16, 1, out, rq->outputsize, true); 178aa99a76bSGao Xiang 179aa99a76bSGao Xiang if (ret >= 0) 180aa99a76bSGao Xiang memset(out + ret, 0, rq->outputsize - ret); 18147e4937aSGao Xiang ret = -EIO; 18247e4937aSGao Xiang } 18347e4937aSGao Xiang 18447e4937aSGao Xiang if (copied) 18547e4937aSGao Xiang erofs_put_pcpubuf(src); 18647e4937aSGao Xiang else 18747e4937aSGao Xiang kunmap_atomic(src); 18847e4937aSGao Xiang return ret; 18947e4937aSGao Xiang } 19047e4937aSGao Xiang 19147e4937aSGao Xiang static struct z_erofs_decompressor decompressors[] = { 19247e4937aSGao Xiang [Z_EROFS_COMPRESSION_SHIFTED] = { 19347e4937aSGao Xiang .name = "shifted" 19447e4937aSGao Xiang }, 19547e4937aSGao Xiang [Z_EROFS_COMPRESSION_LZ4] = { 19699634bf3SGao Xiang .prepare_destpages = z_erofs_lz4_prepare_destpages, 19799634bf3SGao Xiang .decompress = z_erofs_lz4_decompress, 19847e4937aSGao Xiang .name = "lz4" 19947e4937aSGao Xiang }, 20047e4937aSGao Xiang }; 20147e4937aSGao Xiang 20247e4937aSGao Xiang static void copy_from_pcpubuf(struct page **out, const char *dst, 20347e4937aSGao Xiang unsigned short pageofs_out, 20447e4937aSGao Xiang unsigned int outputsize) 20547e4937aSGao Xiang { 20647e4937aSGao Xiang const char *end = dst + outputsize; 20747e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - pageofs_out; 20847e4937aSGao Xiang const char *cur = dst - pageofs_out; 20947e4937aSGao Xiang 21047e4937aSGao Xiang while (cur < end) { 21147e4937aSGao Xiang struct page *const page = *out++; 21247e4937aSGao Xiang 21347e4937aSGao Xiang if (page) { 21447e4937aSGao Xiang char *buf = kmap_atomic(page); 21547e4937aSGao Xiang 21647e4937aSGao Xiang if (cur >= dst) { 21747e4937aSGao Xiang memcpy(buf, cur, min_t(uint, PAGE_SIZE, 21847e4937aSGao Xiang end - cur)); 21947e4937aSGao Xiang } else { 22047e4937aSGao Xiang memcpy(buf + pageofs_out, cur + pageofs_out, 22147e4937aSGao Xiang min_t(uint, righthalf, end - cur)); 22247e4937aSGao Xiang } 22347e4937aSGao Xiang kunmap_atomic(buf); 22447e4937aSGao Xiang } 22547e4937aSGao Xiang cur += PAGE_SIZE; 22647e4937aSGao Xiang } 22747e4937aSGao Xiang } 22847e4937aSGao Xiang 22999634bf3SGao Xiang static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq, 23047e4937aSGao Xiang struct list_head *pagepool) 23147e4937aSGao Xiang { 23247e4937aSGao Xiang const unsigned int nrpages_out = 23347e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 23447e4937aSGao Xiang const struct z_erofs_decompressor *alg = decompressors + rq->alg; 23547e4937aSGao Xiang unsigned int dst_maptype; 23647e4937aSGao Xiang void *dst; 23773d03931SGao Xiang int ret, i; 23847e4937aSGao Xiang 23947e4937aSGao Xiang if (nrpages_out == 1 && !rq->inplace_io) { 24047e4937aSGao Xiang DBG_BUGON(!*rq->out); 24147e4937aSGao Xiang dst = kmap_atomic(*rq->out); 24247e4937aSGao Xiang dst_maptype = 0; 24347e4937aSGao Xiang goto dstmap_out; 24447e4937aSGao Xiang } 24547e4937aSGao Xiang 24647e4937aSGao Xiang /* 24747e4937aSGao Xiang * For the case of small output size (especially much less 24847e4937aSGao Xiang * than PAGE_SIZE), memcpy the decompressed data rather than 24947e4937aSGao Xiang * compressed data is preferred. 25047e4937aSGao Xiang */ 25147e4937aSGao Xiang if (rq->outputsize <= PAGE_SIZE * 7 / 8) { 25247e4937aSGao Xiang dst = erofs_get_pcpubuf(0); 25347e4937aSGao Xiang if (IS_ERR(dst)) 25447e4937aSGao Xiang return PTR_ERR(dst); 25547e4937aSGao Xiang 25647e4937aSGao Xiang rq->inplace_io = false; 25747e4937aSGao Xiang ret = alg->decompress(rq, dst); 25847e4937aSGao Xiang if (!ret) 25947e4937aSGao Xiang copy_from_pcpubuf(rq->out, dst, rq->pageofs_out, 26047e4937aSGao Xiang rq->outputsize); 26147e4937aSGao Xiang 26247e4937aSGao Xiang erofs_put_pcpubuf(dst); 26347e4937aSGao Xiang return ret; 26447e4937aSGao Xiang } 26547e4937aSGao Xiang 26647e4937aSGao Xiang ret = alg->prepare_destpages(rq, pagepool); 26747e4937aSGao Xiang if (ret < 0) { 26847e4937aSGao Xiang return ret; 26947e4937aSGao Xiang } else if (ret) { 27047e4937aSGao Xiang dst = page_address(*rq->out); 27147e4937aSGao Xiang dst_maptype = 1; 27247e4937aSGao Xiang goto dstmap_out; 27347e4937aSGao Xiang } 27447e4937aSGao Xiang 27573d03931SGao Xiang i = 0; 27673d03931SGao Xiang while (1) { 277d4efd79aSChristoph Hellwig dst = vm_map_ram(rq->out, nrpages_out, -1); 27873d03931SGao Xiang 27973d03931SGao Xiang /* retry two more times (totally 3 times) */ 28073d03931SGao Xiang if (dst || ++i >= 3) 28173d03931SGao Xiang break; 28273d03931SGao Xiang vm_unmap_aliases(); 28373d03931SGao Xiang } 28473d03931SGao Xiang 28547e4937aSGao Xiang if (!dst) 28647e4937aSGao Xiang return -ENOMEM; 28773d03931SGao Xiang 28847e4937aSGao Xiang dst_maptype = 2; 28947e4937aSGao Xiang 29047e4937aSGao Xiang dstmap_out: 29147e4937aSGao Xiang ret = alg->decompress(rq, dst + rq->pageofs_out); 29247e4937aSGao Xiang 29347e4937aSGao Xiang if (!dst_maptype) 29447e4937aSGao Xiang kunmap_atomic(dst); 29547e4937aSGao Xiang else if (dst_maptype == 2) 29673d03931SGao Xiang vm_unmap_ram(dst, nrpages_out); 29747e4937aSGao Xiang return ret; 29847e4937aSGao Xiang } 29947e4937aSGao Xiang 30099634bf3SGao Xiang static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq, 30147e4937aSGao Xiang struct list_head *pagepool) 30247e4937aSGao Xiang { 30347e4937aSGao Xiang const unsigned int nrpages_out = 30447e4937aSGao Xiang PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; 30547e4937aSGao Xiang const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out; 30647e4937aSGao Xiang unsigned char *src, *dst; 30747e4937aSGao Xiang 30847e4937aSGao Xiang if (nrpages_out > 2) { 30947e4937aSGao Xiang DBG_BUGON(1); 31047e4937aSGao Xiang return -EIO; 31147e4937aSGao Xiang } 31247e4937aSGao Xiang 31347e4937aSGao Xiang if (rq->out[0] == *rq->in) { 31447e4937aSGao Xiang DBG_BUGON(nrpages_out != 1); 31547e4937aSGao Xiang return 0; 31647e4937aSGao Xiang } 31747e4937aSGao Xiang 31847e4937aSGao Xiang src = kmap_atomic(*rq->in); 3194d202437SGao Xiang if (rq->out[0]) { 32047e4937aSGao Xiang dst = kmap_atomic(rq->out[0]); 32147e4937aSGao Xiang memcpy(dst + rq->pageofs_out, src, righthalf); 3224d202437SGao Xiang kunmap_atomic(dst); 32347e4937aSGao Xiang } 32447e4937aSGao Xiang 3254d202437SGao Xiang if (nrpages_out == 2) { 3264d202437SGao Xiang DBG_BUGON(!rq->out[1]); 32747e4937aSGao Xiang if (rq->out[1] == *rq->in) { 32847e4937aSGao Xiang memmove(src, src + righthalf, rq->pageofs_out); 3294d202437SGao Xiang } else { 33047e4937aSGao Xiang dst = kmap_atomic(rq->out[1]); 33147e4937aSGao Xiang memcpy(dst, src + righthalf, rq->pageofs_out); 33247e4937aSGao Xiang kunmap_atomic(dst); 3334d202437SGao Xiang } 3344d202437SGao Xiang } 33547e4937aSGao Xiang kunmap_atomic(src); 33647e4937aSGao Xiang return 0; 33747e4937aSGao Xiang } 33847e4937aSGao Xiang 33947e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq, 34047e4937aSGao Xiang struct list_head *pagepool) 34147e4937aSGao Xiang { 34247e4937aSGao Xiang if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) 34399634bf3SGao Xiang return z_erofs_shifted_transform(rq, pagepool); 34499634bf3SGao Xiang return z_erofs_decompress_generic(rq, pagepool); 34547e4937aSGao Xiang } 34647e4937aSGao Xiang 347