xref: /openbmc/linux/fs/erofs/decompressor.c (revision 99634bf3)
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 
3147e4937aSGao Xiang static bool use_vmap;
3247e4937aSGao Xiang module_param(use_vmap, bool, 0444);
3347e4937aSGao Xiang MODULE_PARM_DESC(use_vmap, "Use vmap() instead of vm_map_ram() (default 0)");
3447e4937aSGao Xiang 
3599634bf3SGao Xiang static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
3647e4937aSGao Xiang 					 struct list_head *pagepool)
3747e4937aSGao Xiang {
3847e4937aSGao Xiang 	const unsigned int nr =
3947e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
4047e4937aSGao Xiang 	struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
4147e4937aSGao Xiang 	unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
4247e4937aSGao Xiang 					   BITS_PER_LONG)] = { 0 };
4347e4937aSGao Xiang 	void *kaddr = NULL;
4447e4937aSGao Xiang 	unsigned int i, j, top;
4547e4937aSGao Xiang 
4647e4937aSGao Xiang 	top = 0;
4747e4937aSGao Xiang 	for (i = j = 0; i < nr; ++i, ++j) {
4847e4937aSGao Xiang 		struct page *const page = rq->out[i];
4947e4937aSGao Xiang 		struct page *victim;
5047e4937aSGao Xiang 
5147e4937aSGao Xiang 		if (j >= LZ4_MAX_DISTANCE_PAGES)
5247e4937aSGao Xiang 			j = 0;
5347e4937aSGao Xiang 
5447e4937aSGao Xiang 		/* 'valid' bounced can only be tested after a complete round */
5547e4937aSGao Xiang 		if (test_bit(j, bounced)) {
5647e4937aSGao Xiang 			DBG_BUGON(i < LZ4_MAX_DISTANCE_PAGES);
5747e4937aSGao Xiang 			DBG_BUGON(top >= LZ4_MAX_DISTANCE_PAGES);
5847e4937aSGao Xiang 			availables[top++] = rq->out[i - LZ4_MAX_DISTANCE_PAGES];
5947e4937aSGao Xiang 		}
6047e4937aSGao Xiang 
6147e4937aSGao Xiang 		if (page) {
6247e4937aSGao Xiang 			__clear_bit(j, bounced);
6347e4937aSGao Xiang 			if (kaddr) {
6447e4937aSGao Xiang 				if (kaddr + PAGE_SIZE == page_address(page))
6547e4937aSGao Xiang 					kaddr += PAGE_SIZE;
6647e4937aSGao Xiang 				else
6747e4937aSGao Xiang 					kaddr = NULL;
6847e4937aSGao Xiang 			} else if (!i) {
6947e4937aSGao Xiang 				kaddr = page_address(page);
7047e4937aSGao Xiang 			}
7147e4937aSGao Xiang 			continue;
7247e4937aSGao Xiang 		}
7347e4937aSGao Xiang 		kaddr = NULL;
7447e4937aSGao Xiang 		__set_bit(j, bounced);
7547e4937aSGao Xiang 
7647e4937aSGao Xiang 		if (top) {
7747e4937aSGao Xiang 			victim = availables[--top];
7847e4937aSGao Xiang 			get_page(victim);
7947e4937aSGao Xiang 		} else {
8047e4937aSGao Xiang 			victim = erofs_allocpage(pagepool, GFP_KERNEL, false);
818d8a09b0SGao Xiang 			if (!victim)
8247e4937aSGao Xiang 				return -ENOMEM;
8347e4937aSGao Xiang 			victim->mapping = Z_EROFS_MAPPING_STAGING;
8447e4937aSGao Xiang 		}
8547e4937aSGao Xiang 		rq->out[i] = victim;
8647e4937aSGao Xiang 	}
8747e4937aSGao Xiang 	return kaddr ? 1 : 0;
8847e4937aSGao Xiang }
8947e4937aSGao Xiang 
9047e4937aSGao Xiang static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
9147e4937aSGao Xiang 				       u8 *src, unsigned int pageofs_in)
9247e4937aSGao Xiang {
9347e4937aSGao Xiang 	/*
9447e4937aSGao Xiang 	 * if in-place decompression is ongoing, those decompressed
9547e4937aSGao Xiang 	 * pages should be copied in order to avoid being overlapped.
9647e4937aSGao Xiang 	 */
9747e4937aSGao Xiang 	struct page **in = rq->in;
9847e4937aSGao Xiang 	u8 *const tmp = erofs_get_pcpubuf(0);
9947e4937aSGao Xiang 	u8 *tmpp = tmp;
10047e4937aSGao Xiang 	unsigned int inlen = rq->inputsize - pageofs_in;
10147e4937aSGao Xiang 	unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
10247e4937aSGao Xiang 
10347e4937aSGao Xiang 	while (tmpp < tmp + inlen) {
10447e4937aSGao Xiang 		if (!src)
10547e4937aSGao Xiang 			src = kmap_atomic(*in);
10647e4937aSGao Xiang 		memcpy(tmpp, src + pageofs_in, count);
10747e4937aSGao Xiang 		kunmap_atomic(src);
10847e4937aSGao Xiang 		src = NULL;
10947e4937aSGao Xiang 		tmpp += count;
11047e4937aSGao Xiang 		pageofs_in = 0;
11147e4937aSGao Xiang 		count = PAGE_SIZE;
11247e4937aSGao Xiang 		++in;
11347e4937aSGao Xiang 	}
11447e4937aSGao Xiang 	return tmp;
11547e4937aSGao Xiang }
11647e4937aSGao Xiang 
11799634bf3SGao Xiang static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
11847e4937aSGao Xiang {
11947e4937aSGao Xiang 	unsigned int inputmargin, inlen;
12047e4937aSGao Xiang 	u8 *src;
12147e4937aSGao Xiang 	bool copied, support_0padding;
12247e4937aSGao Xiang 	int ret;
12347e4937aSGao Xiang 
12447e4937aSGao Xiang 	if (rq->inputsize > PAGE_SIZE)
12547e4937aSGao Xiang 		return -EOPNOTSUPP;
12647e4937aSGao Xiang 
12747e4937aSGao Xiang 	src = kmap_atomic(*rq->in);
12847e4937aSGao Xiang 	inputmargin = 0;
12947e4937aSGao Xiang 	support_0padding = false;
13047e4937aSGao Xiang 
13147e4937aSGao Xiang 	/* decompression inplace is only safe when 0padding is enabled */
132426a9308SGao Xiang 	if (EROFS_SB(rq->sb)->feature_incompat &
133426a9308SGao Xiang 	    EROFS_FEATURE_INCOMPAT_LZ4_0PADDING) {
13447e4937aSGao Xiang 		support_0padding = true;
13547e4937aSGao Xiang 
13647e4937aSGao Xiang 		while (!src[inputmargin & ~PAGE_MASK])
13747e4937aSGao Xiang 			if (!(++inputmargin & ~PAGE_MASK))
13847e4937aSGao Xiang 				break;
13947e4937aSGao Xiang 
14047e4937aSGao Xiang 		if (inputmargin >= rq->inputsize) {
14147e4937aSGao Xiang 			kunmap_atomic(src);
14247e4937aSGao Xiang 			return -EIO;
14347e4937aSGao Xiang 		}
14447e4937aSGao Xiang 	}
14547e4937aSGao Xiang 
14647e4937aSGao Xiang 	copied = false;
14747e4937aSGao Xiang 	inlen = rq->inputsize - inputmargin;
14847e4937aSGao Xiang 	if (rq->inplace_io) {
14947e4937aSGao Xiang 		const uint oend = (rq->pageofs_out +
15047e4937aSGao Xiang 				   rq->outputsize) & ~PAGE_MASK;
15147e4937aSGao Xiang 		const uint nr = PAGE_ALIGN(rq->pageofs_out +
15247e4937aSGao Xiang 					   rq->outputsize) >> PAGE_SHIFT;
15347e4937aSGao Xiang 
15447e4937aSGao Xiang 		if (rq->partial_decoding || !support_0padding ||
15547e4937aSGao Xiang 		    rq->out[nr - 1] != rq->in[0] ||
15647e4937aSGao Xiang 		    rq->inputsize - oend <
15747e4937aSGao Xiang 		      LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
15847e4937aSGao Xiang 			src = generic_copy_inplace_data(rq, src, inputmargin);
15947e4937aSGao Xiang 			inputmargin = 0;
16047e4937aSGao Xiang 			copied = true;
16147e4937aSGao Xiang 		}
16247e4937aSGao Xiang 	}
16347e4937aSGao Xiang 
16447e4937aSGao Xiang 	ret = LZ4_decompress_safe_partial(src + inputmargin, out,
16547e4937aSGao Xiang 					  inlen, rq->outputsize,
16647e4937aSGao Xiang 					  rq->outputsize);
16747e4937aSGao Xiang 	if (ret < 0) {
16847e4937aSGao Xiang 		errln("%s, failed to decompress, in[%p, %u, %u] out[%p, %u]",
16947e4937aSGao Xiang 		      __func__, src + inputmargin, inlen, inputmargin,
17047e4937aSGao Xiang 		      out, rq->outputsize);
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);
17647e4937aSGao Xiang 		ret = -EIO;
17747e4937aSGao Xiang 	}
17847e4937aSGao Xiang 
17947e4937aSGao Xiang 	if (copied)
18047e4937aSGao Xiang 		erofs_put_pcpubuf(src);
18147e4937aSGao Xiang 	else
18247e4937aSGao Xiang 		kunmap_atomic(src);
18347e4937aSGao Xiang 	return ret;
18447e4937aSGao Xiang }
18547e4937aSGao Xiang 
18647e4937aSGao Xiang static struct z_erofs_decompressor decompressors[] = {
18747e4937aSGao Xiang 	[Z_EROFS_COMPRESSION_SHIFTED] = {
18847e4937aSGao Xiang 		.name = "shifted"
18947e4937aSGao Xiang 	},
19047e4937aSGao Xiang 	[Z_EROFS_COMPRESSION_LZ4] = {
19199634bf3SGao Xiang 		.prepare_destpages = z_erofs_lz4_prepare_destpages,
19299634bf3SGao Xiang 		.decompress = z_erofs_lz4_decompress,
19347e4937aSGao Xiang 		.name = "lz4"
19447e4937aSGao Xiang 	},
19547e4937aSGao Xiang };
19647e4937aSGao Xiang 
19747e4937aSGao Xiang static void copy_from_pcpubuf(struct page **out, const char *dst,
19847e4937aSGao Xiang 			      unsigned short pageofs_out,
19947e4937aSGao Xiang 			      unsigned int outputsize)
20047e4937aSGao Xiang {
20147e4937aSGao Xiang 	const char *end = dst + outputsize;
20247e4937aSGao Xiang 	const unsigned int righthalf = PAGE_SIZE - pageofs_out;
20347e4937aSGao Xiang 	const char *cur = dst - pageofs_out;
20447e4937aSGao Xiang 
20547e4937aSGao Xiang 	while (cur < end) {
20647e4937aSGao Xiang 		struct page *const page = *out++;
20747e4937aSGao Xiang 
20847e4937aSGao Xiang 		if (page) {
20947e4937aSGao Xiang 			char *buf = kmap_atomic(page);
21047e4937aSGao Xiang 
21147e4937aSGao Xiang 			if (cur >= dst) {
21247e4937aSGao Xiang 				memcpy(buf, cur, min_t(uint, PAGE_SIZE,
21347e4937aSGao Xiang 						       end - cur));
21447e4937aSGao Xiang 			} else {
21547e4937aSGao Xiang 				memcpy(buf + pageofs_out, cur + pageofs_out,
21647e4937aSGao Xiang 				       min_t(uint, righthalf, end - cur));
21747e4937aSGao Xiang 			}
21847e4937aSGao Xiang 			kunmap_atomic(buf);
21947e4937aSGao Xiang 		}
22047e4937aSGao Xiang 		cur += PAGE_SIZE;
22147e4937aSGao Xiang 	}
22247e4937aSGao Xiang }
22347e4937aSGao Xiang 
22447e4937aSGao Xiang static void *erofs_vmap(struct page **pages, unsigned int count)
22547e4937aSGao Xiang {
22647e4937aSGao Xiang 	int i = 0;
22747e4937aSGao Xiang 
22847e4937aSGao Xiang 	if (use_vmap)
22947e4937aSGao Xiang 		return vmap(pages, count, VM_MAP, PAGE_KERNEL);
23047e4937aSGao Xiang 
23147e4937aSGao Xiang 	while (1) {
23247e4937aSGao Xiang 		void *addr = vm_map_ram(pages, count, -1, PAGE_KERNEL);
23347e4937aSGao Xiang 
23447e4937aSGao Xiang 		/* retry two more times (totally 3 times) */
23547e4937aSGao Xiang 		if (addr || ++i >= 3)
23647e4937aSGao Xiang 			return addr;
23747e4937aSGao Xiang 		vm_unmap_aliases();
23847e4937aSGao Xiang 	}
23947e4937aSGao Xiang 	return NULL;
24047e4937aSGao Xiang }
24147e4937aSGao Xiang 
24247e4937aSGao Xiang static void erofs_vunmap(const void *mem, unsigned int count)
24347e4937aSGao Xiang {
24447e4937aSGao Xiang 	if (!use_vmap)
24547e4937aSGao Xiang 		vm_unmap_ram(mem, count);
24647e4937aSGao Xiang 	else
24747e4937aSGao Xiang 		vunmap(mem);
24847e4937aSGao Xiang }
24947e4937aSGao Xiang 
25099634bf3SGao Xiang static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
25147e4937aSGao Xiang 				      struct list_head *pagepool)
25247e4937aSGao Xiang {
25347e4937aSGao Xiang 	const unsigned int nrpages_out =
25447e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
25547e4937aSGao Xiang 	const struct z_erofs_decompressor *alg = decompressors + rq->alg;
25647e4937aSGao Xiang 	unsigned int dst_maptype;
25747e4937aSGao Xiang 	void *dst;
25847e4937aSGao Xiang 	int ret;
25947e4937aSGao Xiang 
26047e4937aSGao Xiang 	if (nrpages_out == 1 && !rq->inplace_io) {
26147e4937aSGao Xiang 		DBG_BUGON(!*rq->out);
26247e4937aSGao Xiang 		dst = kmap_atomic(*rq->out);
26347e4937aSGao Xiang 		dst_maptype = 0;
26447e4937aSGao Xiang 		goto dstmap_out;
26547e4937aSGao Xiang 	}
26647e4937aSGao Xiang 
26747e4937aSGao Xiang 	/*
26847e4937aSGao Xiang 	 * For the case of small output size (especially much less
26947e4937aSGao Xiang 	 * than PAGE_SIZE), memcpy the decompressed data rather than
27047e4937aSGao Xiang 	 * compressed data is preferred.
27147e4937aSGao Xiang 	 */
27247e4937aSGao Xiang 	if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
27347e4937aSGao Xiang 		dst = erofs_get_pcpubuf(0);
27447e4937aSGao Xiang 		if (IS_ERR(dst))
27547e4937aSGao Xiang 			return PTR_ERR(dst);
27647e4937aSGao Xiang 
27747e4937aSGao Xiang 		rq->inplace_io = false;
27847e4937aSGao Xiang 		ret = alg->decompress(rq, dst);
27947e4937aSGao Xiang 		if (!ret)
28047e4937aSGao Xiang 			copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
28147e4937aSGao Xiang 					  rq->outputsize);
28247e4937aSGao Xiang 
28347e4937aSGao Xiang 		erofs_put_pcpubuf(dst);
28447e4937aSGao Xiang 		return ret;
28547e4937aSGao Xiang 	}
28647e4937aSGao Xiang 
28747e4937aSGao Xiang 	ret = alg->prepare_destpages(rq, pagepool);
28847e4937aSGao Xiang 	if (ret < 0) {
28947e4937aSGao Xiang 		return ret;
29047e4937aSGao Xiang 	} else if (ret) {
29147e4937aSGao Xiang 		dst = page_address(*rq->out);
29247e4937aSGao Xiang 		dst_maptype = 1;
29347e4937aSGao Xiang 		goto dstmap_out;
29447e4937aSGao Xiang 	}
29547e4937aSGao Xiang 
29647e4937aSGao Xiang 	dst = erofs_vmap(rq->out, nrpages_out);
29747e4937aSGao Xiang 	if (!dst)
29847e4937aSGao Xiang 		return -ENOMEM;
29947e4937aSGao Xiang 	dst_maptype = 2;
30047e4937aSGao Xiang 
30147e4937aSGao Xiang dstmap_out:
30247e4937aSGao Xiang 	ret = alg->decompress(rq, dst + rq->pageofs_out);
30347e4937aSGao Xiang 
30447e4937aSGao Xiang 	if (!dst_maptype)
30547e4937aSGao Xiang 		kunmap_atomic(dst);
30647e4937aSGao Xiang 	else if (dst_maptype == 2)
30747e4937aSGao Xiang 		erofs_vunmap(dst, nrpages_out);
30847e4937aSGao Xiang 	return ret;
30947e4937aSGao Xiang }
31047e4937aSGao Xiang 
31199634bf3SGao Xiang static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
31247e4937aSGao Xiang 				     struct list_head *pagepool)
31347e4937aSGao Xiang {
31447e4937aSGao Xiang 	const unsigned int nrpages_out =
31547e4937aSGao Xiang 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
31647e4937aSGao Xiang 	const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
31747e4937aSGao Xiang 	unsigned char *src, *dst;
31847e4937aSGao Xiang 
31947e4937aSGao Xiang 	if (nrpages_out > 2) {
32047e4937aSGao Xiang 		DBG_BUGON(1);
32147e4937aSGao Xiang 		return -EIO;
32247e4937aSGao Xiang 	}
32347e4937aSGao Xiang 
32447e4937aSGao Xiang 	if (rq->out[0] == *rq->in) {
32547e4937aSGao Xiang 		DBG_BUGON(nrpages_out != 1);
32647e4937aSGao Xiang 		return 0;
32747e4937aSGao Xiang 	}
32847e4937aSGao Xiang 
32947e4937aSGao Xiang 	src = kmap_atomic(*rq->in);
33047e4937aSGao Xiang 	if (!rq->out[0]) {
33147e4937aSGao Xiang 		dst = NULL;
33247e4937aSGao Xiang 	} else {
33347e4937aSGao Xiang 		dst = kmap_atomic(rq->out[0]);
33447e4937aSGao Xiang 		memcpy(dst + rq->pageofs_out, src, righthalf);
33547e4937aSGao Xiang 	}
33647e4937aSGao Xiang 
33747e4937aSGao Xiang 	if (rq->out[1] == *rq->in) {
33847e4937aSGao Xiang 		memmove(src, src + righthalf, rq->pageofs_out);
33947e4937aSGao Xiang 	} else if (nrpages_out == 2) {
34047e4937aSGao Xiang 		if (dst)
34147e4937aSGao Xiang 			kunmap_atomic(dst);
34247e4937aSGao Xiang 		DBG_BUGON(!rq->out[1]);
34347e4937aSGao Xiang 		dst = kmap_atomic(rq->out[1]);
34447e4937aSGao Xiang 		memcpy(dst, src + righthalf, rq->pageofs_out);
34547e4937aSGao Xiang 	}
34647e4937aSGao Xiang 	if (dst)
34747e4937aSGao Xiang 		kunmap_atomic(dst);
34847e4937aSGao Xiang 	kunmap_atomic(src);
34947e4937aSGao Xiang 	return 0;
35047e4937aSGao Xiang }
35147e4937aSGao Xiang 
35247e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq,
35347e4937aSGao Xiang 		       struct list_head *pagepool)
35447e4937aSGao Xiang {
35547e4937aSGao Xiang 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
35699634bf3SGao Xiang 		return z_erofs_shifted_transform(rq, pagepool);
35799634bf3SGao Xiang 	return z_erofs_decompress_generic(rq, pagepool);
35847e4937aSGao Xiang }
35947e4937aSGao Xiang 
360