xref: /linux/fs/erofs/decompressor.c (revision 46249cded18ac0c4ffb7b177219510a133a51c00)
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 
315d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb,
32*46249cdeSGao Xiang 			    struct erofs_super_block *dsb,
33*46249cdeSGao Xiang 			    struct z_erofs_lz4_cfgs *lz4, int size)
345d50538fSHuang Jianan {
35*46249cdeSGao Xiang 	u16 distance;
36*46249cdeSGao Xiang 
37*46249cdeSGao Xiang 	if (lz4) {
38*46249cdeSGao Xiang 		if (size < sizeof(struct z_erofs_lz4_cfgs)) {
39*46249cdeSGao Xiang 			erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
40*46249cdeSGao Xiang 			return -EINVAL;
41*46249cdeSGao Xiang 		}
42*46249cdeSGao Xiang 		distance = le16_to_cpu(lz4->max_distance);
43*46249cdeSGao Xiang 	} else {
44*46249cdeSGao Xiang 		distance = le16_to_cpu(dsb->lz4_max_distance);
45*46249cdeSGao Xiang 	}
465d50538fSHuang Jianan 
475d50538fSHuang Jianan 	EROFS_SB(sb)->lz4.max_distance_pages = distance ?
485d50538fSHuang Jianan 					DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
495d50538fSHuang Jianan 					LZ4_MAX_DISTANCE_PAGES;
505d50538fSHuang Jianan 	return 0;
515d50538fSHuang Jianan }
525d50538fSHuang Jianan 
5399634bf3SGao Xiang static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
5447e4937aSGao Xiang 					 struct list_head *pagepool)
5547e4937aSGao Xiang {
5647e4937aSGao Xiang 	const unsigned int nr =
5747e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
5847e4937aSGao Xiang 	struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
5947e4937aSGao Xiang 	unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
6047e4937aSGao Xiang 					   BITS_PER_LONG)] = { 0 };
615d50538fSHuang Jianan 	unsigned int lz4_max_distance_pages =
625d50538fSHuang Jianan 				EROFS_SB(rq->sb)->lz4.max_distance_pages;
6347e4937aSGao Xiang 	void *kaddr = NULL;
6447e4937aSGao Xiang 	unsigned int i, j, top;
6547e4937aSGao Xiang 
6647e4937aSGao Xiang 	top = 0;
6747e4937aSGao Xiang 	for (i = j = 0; i < nr; ++i, ++j) {
6847e4937aSGao Xiang 		struct page *const page = rq->out[i];
6947e4937aSGao Xiang 		struct page *victim;
7047e4937aSGao Xiang 
715d50538fSHuang Jianan 		if (j >= lz4_max_distance_pages)
7247e4937aSGao Xiang 			j = 0;
7347e4937aSGao Xiang 
7447e4937aSGao Xiang 		/* 'valid' bounced can only be tested after a complete round */
7547e4937aSGao Xiang 		if (test_bit(j, bounced)) {
765d50538fSHuang Jianan 			DBG_BUGON(i < lz4_max_distance_pages);
775d50538fSHuang Jianan 			DBG_BUGON(top >= lz4_max_distance_pages);
785d50538fSHuang Jianan 			availables[top++] = rq->out[i - lz4_max_distance_pages];
7947e4937aSGao Xiang 		}
8047e4937aSGao Xiang 
8147e4937aSGao Xiang 		if (page) {
8247e4937aSGao Xiang 			__clear_bit(j, bounced);
8347e4937aSGao Xiang 			if (kaddr) {
8447e4937aSGao Xiang 				if (kaddr + PAGE_SIZE == page_address(page))
8547e4937aSGao Xiang 					kaddr += PAGE_SIZE;
8647e4937aSGao Xiang 				else
8747e4937aSGao Xiang 					kaddr = NULL;
8847e4937aSGao Xiang 			} else if (!i) {
8947e4937aSGao Xiang 				kaddr = page_address(page);
9047e4937aSGao Xiang 			}
9147e4937aSGao Xiang 			continue;
9247e4937aSGao Xiang 		}
9347e4937aSGao Xiang 		kaddr = NULL;
9447e4937aSGao Xiang 		__set_bit(j, bounced);
9547e4937aSGao Xiang 
9647e4937aSGao Xiang 		if (top) {
9747e4937aSGao Xiang 			victim = availables[--top];
9847e4937aSGao Xiang 			get_page(victim);
9947e4937aSGao Xiang 		} else {
100b4892fa3SHuang Jianan 			victim = erofs_allocpage(pagepool,
101b4892fa3SHuang Jianan 						 GFP_KERNEL | __GFP_NOFAIL);
1026aaa7b06SGao Xiang 			set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
10347e4937aSGao Xiang 		}
10447e4937aSGao Xiang 		rq->out[i] = victim;
10547e4937aSGao Xiang 	}
10647e4937aSGao Xiang 	return kaddr ? 1 : 0;
10747e4937aSGao Xiang }
10847e4937aSGao Xiang 
10947e4937aSGao Xiang static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
11047e4937aSGao Xiang 				       u8 *src, unsigned int pageofs_in)
11147e4937aSGao Xiang {
11247e4937aSGao Xiang 	/*
11347e4937aSGao Xiang 	 * if in-place decompression is ongoing, those decompressed
11447e4937aSGao Xiang 	 * pages should be copied in order to avoid being overlapped.
11547e4937aSGao Xiang 	 */
11647e4937aSGao Xiang 	struct page **in = rq->in;
11747e4937aSGao Xiang 	u8 *const tmp = erofs_get_pcpubuf(0);
11847e4937aSGao Xiang 	u8 *tmpp = tmp;
11947e4937aSGao Xiang 	unsigned int inlen = rq->inputsize - pageofs_in;
12047e4937aSGao Xiang 	unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
12147e4937aSGao Xiang 
12247e4937aSGao Xiang 	while (tmpp < tmp + inlen) {
12347e4937aSGao Xiang 		if (!src)
12447e4937aSGao Xiang 			src = kmap_atomic(*in);
12547e4937aSGao Xiang 		memcpy(tmpp, src + pageofs_in, count);
12647e4937aSGao Xiang 		kunmap_atomic(src);
12747e4937aSGao Xiang 		src = NULL;
12847e4937aSGao Xiang 		tmpp += count;
12947e4937aSGao Xiang 		pageofs_in = 0;
13047e4937aSGao Xiang 		count = PAGE_SIZE;
13147e4937aSGao Xiang 		++in;
13247e4937aSGao Xiang 	}
13347e4937aSGao Xiang 	return tmp;
13447e4937aSGao Xiang }
13547e4937aSGao Xiang 
13699634bf3SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
13747e4937aSGao Xiang {
13847e4937aSGao Xiang 	unsigned int inputmargin, inlen;
13947e4937aSGao Xiang 	u8 *src;
14047e4937aSGao Xiang 	bool copied, support_0padding;
14147e4937aSGao Xiang 	int ret;
14247e4937aSGao Xiang 
14347e4937aSGao Xiang 	if (rq->inputsize > PAGE_SIZE)
14447e4937aSGao Xiang 		return -EOPNOTSUPP;
14547e4937aSGao Xiang 
14647e4937aSGao Xiang 	src = kmap_atomic(*rq->in);
14747e4937aSGao Xiang 	inputmargin = 0;
14847e4937aSGao Xiang 	support_0padding = false;
14947e4937aSGao Xiang 
15047e4937aSGao Xiang 	/* decompression inplace is only safe when 0padding is enabled */
151de06a6a3SGao Xiang 	if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) {
15247e4937aSGao Xiang 		support_0padding = true;
15347e4937aSGao Xiang 
15447e4937aSGao Xiang 		while (!src[inputmargin & ~PAGE_MASK])
15547e4937aSGao Xiang 			if (!(++inputmargin & ~PAGE_MASK))
15647e4937aSGao Xiang 				break;
15747e4937aSGao Xiang 
15847e4937aSGao Xiang 		if (inputmargin >= rq->inputsize) {
15947e4937aSGao Xiang 			kunmap_atomic(src);
16047e4937aSGao Xiang 			return -EIO;
16147e4937aSGao Xiang 		}
16247e4937aSGao Xiang 	}
16347e4937aSGao Xiang 
16447e4937aSGao Xiang 	copied = false;
16547e4937aSGao Xiang 	inlen = rq->inputsize - inputmargin;
16647e4937aSGao Xiang 	if (rq->inplace_io) {
16747e4937aSGao Xiang 		const uint oend = (rq->pageofs_out +
16847e4937aSGao Xiang 				   rq->outputsize) & ~PAGE_MASK;
16947e4937aSGao Xiang 		const uint nr = PAGE_ALIGN(rq->pageofs_out +
17047e4937aSGao Xiang 					   rq->outputsize) >> PAGE_SHIFT;
17147e4937aSGao Xiang 
17247e4937aSGao Xiang 		if (rq->partial_decoding || !support_0padding ||
17347e4937aSGao Xiang 		    rq->out[nr - 1] != rq->in[0] ||
17447e4937aSGao Xiang 		    rq->inputsize - oend <
17547e4937aSGao Xiang 		      LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
17647e4937aSGao Xiang 			src = generic_copy_inplace_data(rq, src, inputmargin);
17747e4937aSGao Xiang 			inputmargin = 0;
17847e4937aSGao Xiang 			copied = true;
17947e4937aSGao Xiang 		}
18047e4937aSGao Xiang 	}
18147e4937aSGao Xiang 
182af1038abSGao Xiang 	/* legacy format could compress extra data in a pcluster. */
183af1038abSGao Xiang 	if (rq->partial_decoding || !support_0padding)
18447e4937aSGao Xiang 		ret = LZ4_decompress_safe_partial(src + inputmargin, out,
18547e4937aSGao Xiang 						  inlen, rq->outputsize,
18647e4937aSGao Xiang 						  rq->outputsize);
187af1038abSGao Xiang 	else
188af1038abSGao Xiang 		ret = LZ4_decompress_safe(src + inputmargin, out,
189af1038abSGao Xiang 					  inlen, rq->outputsize);
190af1038abSGao Xiang 
191aa99a76bSGao Xiang 	if (ret != rq->outputsize) {
192aa99a76bSGao Xiang 		erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
193aa99a76bSGao Xiang 			  ret, inlen, inputmargin, rq->outputsize);
194aa99a76bSGao Xiang 
19547e4937aSGao Xiang 		WARN_ON(1);
19647e4937aSGao Xiang 		print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
19747e4937aSGao Xiang 			       16, 1, src + inputmargin, inlen, true);
19847e4937aSGao Xiang 		print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
19947e4937aSGao Xiang 			       16, 1, out, rq->outputsize, true);
200aa99a76bSGao Xiang 
201aa99a76bSGao Xiang 		if (ret >= 0)
202aa99a76bSGao Xiang 			memset(out + ret, 0, rq->outputsize - ret);
20347e4937aSGao Xiang 		ret = -EIO;
20447e4937aSGao Xiang 	}
20547e4937aSGao Xiang 
20647e4937aSGao Xiang 	if (copied)
20747e4937aSGao Xiang 		erofs_put_pcpubuf(src);
20847e4937aSGao Xiang 	else
20947e4937aSGao Xiang 		kunmap_atomic(src);
21047e4937aSGao Xiang 	return ret;
21147e4937aSGao Xiang }
21247e4937aSGao Xiang 
21347e4937aSGao Xiang static struct z_erofs_decompressor decompressors[] = {
21447e4937aSGao Xiang 	[Z_EROFS_COMPRESSION_SHIFTED] = {
21547e4937aSGao Xiang 		.name = "shifted"
21647e4937aSGao Xiang 	},
21747e4937aSGao Xiang 	[Z_EROFS_COMPRESSION_LZ4] = {
21899634bf3SGao Xiang 		.prepare_destpages = z_erofs_lz4_prepare_destpages,
21999634bf3SGao Xiang 		.decompress = z_erofs_lz4_decompress,
22047e4937aSGao Xiang 		.name = "lz4"
22147e4937aSGao Xiang 	},
22247e4937aSGao Xiang };
22347e4937aSGao Xiang 
22447e4937aSGao Xiang static void copy_from_pcpubuf(struct page **out, const char *dst,
22547e4937aSGao Xiang 			      unsigned short pageofs_out,
22647e4937aSGao Xiang 			      unsigned int outputsize)
22747e4937aSGao Xiang {
22847e4937aSGao Xiang 	const char *end = dst + outputsize;
22947e4937aSGao Xiang 	const unsigned int righthalf = PAGE_SIZE - pageofs_out;
23047e4937aSGao Xiang 	const char *cur = dst - pageofs_out;
23147e4937aSGao Xiang 
23247e4937aSGao Xiang 	while (cur < end) {
23347e4937aSGao Xiang 		struct page *const page = *out++;
23447e4937aSGao Xiang 
23547e4937aSGao Xiang 		if (page) {
23647e4937aSGao Xiang 			char *buf = kmap_atomic(page);
23747e4937aSGao Xiang 
23847e4937aSGao Xiang 			if (cur >= dst) {
23947e4937aSGao Xiang 				memcpy(buf, cur, min_t(uint, PAGE_SIZE,
24047e4937aSGao Xiang 						       end - cur));
24147e4937aSGao Xiang 			} else {
24247e4937aSGao Xiang 				memcpy(buf + pageofs_out, cur + pageofs_out,
24347e4937aSGao Xiang 				       min_t(uint, righthalf, end - cur));
24447e4937aSGao Xiang 			}
24547e4937aSGao Xiang 			kunmap_atomic(buf);
24647e4937aSGao Xiang 		}
24747e4937aSGao Xiang 		cur += PAGE_SIZE;
24847e4937aSGao Xiang 	}
24947e4937aSGao Xiang }
25047e4937aSGao Xiang 
25199634bf3SGao Xiang static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
25247e4937aSGao Xiang 				      struct list_head *pagepool)
25347e4937aSGao Xiang {
25447e4937aSGao Xiang 	const unsigned int nrpages_out =
25547e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
25647e4937aSGao Xiang 	const struct z_erofs_decompressor *alg = decompressors + rq->alg;
25747e4937aSGao Xiang 	unsigned int dst_maptype;
25847e4937aSGao Xiang 	void *dst;
25973d03931SGao Xiang 	int ret, i;
26047e4937aSGao Xiang 
26147e4937aSGao Xiang 	if (nrpages_out == 1 && !rq->inplace_io) {
26247e4937aSGao Xiang 		DBG_BUGON(!*rq->out);
26347e4937aSGao Xiang 		dst = kmap_atomic(*rq->out);
26447e4937aSGao Xiang 		dst_maptype = 0;
26547e4937aSGao Xiang 		goto dstmap_out;
26647e4937aSGao Xiang 	}
26747e4937aSGao Xiang 
26847e4937aSGao Xiang 	/*
26947e4937aSGao Xiang 	 * For the case of small output size (especially much less
27047e4937aSGao Xiang 	 * than PAGE_SIZE), memcpy the decompressed data rather than
27147e4937aSGao Xiang 	 * compressed data is preferred.
27247e4937aSGao Xiang 	 */
27347e4937aSGao Xiang 	if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
27447e4937aSGao Xiang 		dst = erofs_get_pcpubuf(0);
27547e4937aSGao Xiang 		if (IS_ERR(dst))
27647e4937aSGao Xiang 			return PTR_ERR(dst);
27747e4937aSGao Xiang 
27847e4937aSGao Xiang 		rq->inplace_io = false;
27947e4937aSGao Xiang 		ret = alg->decompress(rq, dst);
28047e4937aSGao Xiang 		if (!ret)
28147e4937aSGao Xiang 			copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
28247e4937aSGao Xiang 					  rq->outputsize);
28347e4937aSGao Xiang 
28447e4937aSGao Xiang 		erofs_put_pcpubuf(dst);
28547e4937aSGao Xiang 		return ret;
28647e4937aSGao Xiang 	}
28747e4937aSGao Xiang 
28847e4937aSGao Xiang 	ret = alg->prepare_destpages(rq, pagepool);
28947e4937aSGao Xiang 	if (ret < 0) {
29047e4937aSGao Xiang 		return ret;
29147e4937aSGao Xiang 	} else if (ret) {
29247e4937aSGao Xiang 		dst = page_address(*rq->out);
29347e4937aSGao Xiang 		dst_maptype = 1;
29447e4937aSGao Xiang 		goto dstmap_out;
29547e4937aSGao Xiang 	}
29647e4937aSGao Xiang 
29773d03931SGao Xiang 	i = 0;
29873d03931SGao Xiang 	while (1) {
299d4efd79aSChristoph Hellwig 		dst = vm_map_ram(rq->out, nrpages_out, -1);
30073d03931SGao Xiang 
30173d03931SGao Xiang 		/* retry two more times (totally 3 times) */
30273d03931SGao Xiang 		if (dst || ++i >= 3)
30373d03931SGao Xiang 			break;
30473d03931SGao Xiang 		vm_unmap_aliases();
30573d03931SGao Xiang 	}
30673d03931SGao Xiang 
30747e4937aSGao Xiang 	if (!dst)
30847e4937aSGao Xiang 		return -ENOMEM;
30973d03931SGao Xiang 
31047e4937aSGao Xiang 	dst_maptype = 2;
31147e4937aSGao Xiang 
31247e4937aSGao Xiang dstmap_out:
31347e4937aSGao Xiang 	ret = alg->decompress(rq, dst + rq->pageofs_out);
31447e4937aSGao Xiang 
31547e4937aSGao Xiang 	if (!dst_maptype)
31647e4937aSGao Xiang 		kunmap_atomic(dst);
31747e4937aSGao Xiang 	else if (dst_maptype == 2)
31873d03931SGao Xiang 		vm_unmap_ram(dst, nrpages_out);
31947e4937aSGao Xiang 	return ret;
32047e4937aSGao Xiang }
32147e4937aSGao Xiang 
32299634bf3SGao Xiang static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
32347e4937aSGao Xiang 				     struct list_head *pagepool)
32447e4937aSGao Xiang {
32547e4937aSGao Xiang 	const unsigned int nrpages_out =
32647e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
32747e4937aSGao Xiang 	const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
32847e4937aSGao Xiang 	unsigned char *src, *dst;
32947e4937aSGao Xiang 
33047e4937aSGao Xiang 	if (nrpages_out > 2) {
33147e4937aSGao Xiang 		DBG_BUGON(1);
33247e4937aSGao Xiang 		return -EIO;
33347e4937aSGao Xiang 	}
33447e4937aSGao Xiang 
33547e4937aSGao Xiang 	if (rq->out[0] == *rq->in) {
33647e4937aSGao Xiang 		DBG_BUGON(nrpages_out != 1);
33747e4937aSGao Xiang 		return 0;
33847e4937aSGao Xiang 	}
33947e4937aSGao Xiang 
34047e4937aSGao Xiang 	src = kmap_atomic(*rq->in);
3414d202437SGao Xiang 	if (rq->out[0]) {
34247e4937aSGao Xiang 		dst = kmap_atomic(rq->out[0]);
34347e4937aSGao Xiang 		memcpy(dst + rq->pageofs_out, src, righthalf);
3444d202437SGao Xiang 		kunmap_atomic(dst);
34547e4937aSGao Xiang 	}
34647e4937aSGao Xiang 
3474d202437SGao Xiang 	if (nrpages_out == 2) {
3484d202437SGao Xiang 		DBG_BUGON(!rq->out[1]);
34947e4937aSGao Xiang 		if (rq->out[1] == *rq->in) {
35047e4937aSGao Xiang 			memmove(src, src + righthalf, rq->pageofs_out);
3514d202437SGao Xiang 		} else {
35247e4937aSGao Xiang 			dst = kmap_atomic(rq->out[1]);
35347e4937aSGao Xiang 			memcpy(dst, src + righthalf, rq->pageofs_out);
35447e4937aSGao Xiang 			kunmap_atomic(dst);
3554d202437SGao Xiang 		}
3564d202437SGao Xiang 	}
35747e4937aSGao Xiang 	kunmap_atomic(src);
35847e4937aSGao Xiang 	return 0;
35947e4937aSGao Xiang }
36047e4937aSGao Xiang 
36147e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq,
36247e4937aSGao Xiang 		       struct list_head *pagepool)
36347e4937aSGao Xiang {
36447e4937aSGao Xiang 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
36599634bf3SGao Xiang 		return z_erofs_shifted_transform(rq, pagepool);
36699634bf3SGao Xiang 	return z_erofs_decompress_generic(rq, pagepool);
36747e4937aSGao Xiang }
36847e4937aSGao Xiang 
369