xref: /openbmc/linux/fs/erofs/decompressor.c (revision 5d50538f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2019 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Created by Gao Xiang <gaoxiang25@huawei.com>
6  */
7 #include "compress.h"
8 #include <linux/module.h>
9 #include <linux/lz4.h>
10 
11 #ifndef LZ4_DISTANCE_MAX	/* history window size */
12 #define LZ4_DISTANCE_MAX 65535	/* set to maximum value by default */
13 #endif
14 
15 #define LZ4_MAX_DISTANCE_PAGES	(DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
16 #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
17 #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize)  (((srcsize) >> 8) + 32)
18 #endif
19 
20 struct z_erofs_decompressor {
21 	/*
22 	 * if destpages have sparsed pages, fill them with bounce pages.
23 	 * it also check whether destpages indicate continuous physical memory.
24 	 */
25 	int (*prepare_destpages)(struct z_erofs_decompress_req *rq,
26 				 struct list_head *pagepool);
27 	int (*decompress)(struct z_erofs_decompress_req *rq, u8 *out);
28 	char *name;
29 };
30 
31 int z_erofs_load_lz4_config(struct super_block *sb,
32 			    struct erofs_super_block *dsb)
33 {
34 	u16 distance = le16_to_cpu(dsb->lz4_max_distance);
35 
36 	EROFS_SB(sb)->lz4.max_distance_pages = distance ?
37 					DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
38 					LZ4_MAX_DISTANCE_PAGES;
39 	return 0;
40 }
41 
42 static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
43 					 struct list_head *pagepool)
44 {
45 	const unsigned int nr =
46 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
47 	struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
48 	unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
49 					   BITS_PER_LONG)] = { 0 };
50 	unsigned int lz4_max_distance_pages =
51 				EROFS_SB(rq->sb)->lz4.max_distance_pages;
52 	void *kaddr = NULL;
53 	unsigned int i, j, top;
54 
55 	top = 0;
56 	for (i = j = 0; i < nr; ++i, ++j) {
57 		struct page *const page = rq->out[i];
58 		struct page *victim;
59 
60 		if (j >= lz4_max_distance_pages)
61 			j = 0;
62 
63 		/* 'valid' bounced can only be tested after a complete round */
64 		if (test_bit(j, bounced)) {
65 			DBG_BUGON(i < lz4_max_distance_pages);
66 			DBG_BUGON(top >= lz4_max_distance_pages);
67 			availables[top++] = rq->out[i - lz4_max_distance_pages];
68 		}
69 
70 		if (page) {
71 			__clear_bit(j, bounced);
72 			if (kaddr) {
73 				if (kaddr + PAGE_SIZE == page_address(page))
74 					kaddr += PAGE_SIZE;
75 				else
76 					kaddr = NULL;
77 			} else if (!i) {
78 				kaddr = page_address(page);
79 			}
80 			continue;
81 		}
82 		kaddr = NULL;
83 		__set_bit(j, bounced);
84 
85 		if (top) {
86 			victim = availables[--top];
87 			get_page(victim);
88 		} else {
89 			victim = erofs_allocpage(pagepool,
90 						 GFP_KERNEL | __GFP_NOFAIL);
91 			set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
92 		}
93 		rq->out[i] = victim;
94 	}
95 	return kaddr ? 1 : 0;
96 }
97 
98 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
99 				       u8 *src, unsigned int pageofs_in)
100 {
101 	/*
102 	 * if in-place decompression is ongoing, those decompressed
103 	 * pages should be copied in order to avoid being overlapped.
104 	 */
105 	struct page **in = rq->in;
106 	u8 *const tmp = erofs_get_pcpubuf(0);
107 	u8 *tmpp = tmp;
108 	unsigned int inlen = rq->inputsize - pageofs_in;
109 	unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
110 
111 	while (tmpp < tmp + inlen) {
112 		if (!src)
113 			src = kmap_atomic(*in);
114 		memcpy(tmpp, src + pageofs_in, count);
115 		kunmap_atomic(src);
116 		src = NULL;
117 		tmpp += count;
118 		pageofs_in = 0;
119 		count = PAGE_SIZE;
120 		++in;
121 	}
122 	return tmp;
123 }
124 
125 static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
126 {
127 	unsigned int inputmargin, inlen;
128 	u8 *src;
129 	bool copied, support_0padding;
130 	int ret;
131 
132 	if (rq->inputsize > PAGE_SIZE)
133 		return -EOPNOTSUPP;
134 
135 	src = kmap_atomic(*rq->in);
136 	inputmargin = 0;
137 	support_0padding = false;
138 
139 	/* decompression inplace is only safe when 0padding is enabled */
140 	if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) {
141 		support_0padding = true;
142 
143 		while (!src[inputmargin & ~PAGE_MASK])
144 			if (!(++inputmargin & ~PAGE_MASK))
145 				break;
146 
147 		if (inputmargin >= rq->inputsize) {
148 			kunmap_atomic(src);
149 			return -EIO;
150 		}
151 	}
152 
153 	copied = false;
154 	inlen = rq->inputsize - inputmargin;
155 	if (rq->inplace_io) {
156 		const uint oend = (rq->pageofs_out +
157 				   rq->outputsize) & ~PAGE_MASK;
158 		const uint nr = PAGE_ALIGN(rq->pageofs_out +
159 					   rq->outputsize) >> PAGE_SHIFT;
160 
161 		if (rq->partial_decoding || !support_0padding ||
162 		    rq->out[nr - 1] != rq->in[0] ||
163 		    rq->inputsize - oend <
164 		      LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
165 			src = generic_copy_inplace_data(rq, src, inputmargin);
166 			inputmargin = 0;
167 			copied = true;
168 		}
169 	}
170 
171 	/* legacy format could compress extra data in a pcluster. */
172 	if (rq->partial_decoding || !support_0padding)
173 		ret = LZ4_decompress_safe_partial(src + inputmargin, out,
174 						  inlen, rq->outputsize,
175 						  rq->outputsize);
176 	else
177 		ret = LZ4_decompress_safe(src + inputmargin, out,
178 					  inlen, rq->outputsize);
179 
180 	if (ret != rq->outputsize) {
181 		erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
182 			  ret, inlen, inputmargin, rq->outputsize);
183 
184 		WARN_ON(1);
185 		print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
186 			       16, 1, src + inputmargin, inlen, true);
187 		print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
188 			       16, 1, out, rq->outputsize, true);
189 
190 		if (ret >= 0)
191 			memset(out + ret, 0, rq->outputsize - ret);
192 		ret = -EIO;
193 	}
194 
195 	if (copied)
196 		erofs_put_pcpubuf(src);
197 	else
198 		kunmap_atomic(src);
199 	return ret;
200 }
201 
202 static struct z_erofs_decompressor decompressors[] = {
203 	[Z_EROFS_COMPRESSION_SHIFTED] = {
204 		.name = "shifted"
205 	},
206 	[Z_EROFS_COMPRESSION_LZ4] = {
207 		.prepare_destpages = z_erofs_lz4_prepare_destpages,
208 		.decompress = z_erofs_lz4_decompress,
209 		.name = "lz4"
210 	},
211 };
212 
213 static void copy_from_pcpubuf(struct page **out, const char *dst,
214 			      unsigned short pageofs_out,
215 			      unsigned int outputsize)
216 {
217 	const char *end = dst + outputsize;
218 	const unsigned int righthalf = PAGE_SIZE - pageofs_out;
219 	const char *cur = dst - pageofs_out;
220 
221 	while (cur < end) {
222 		struct page *const page = *out++;
223 
224 		if (page) {
225 			char *buf = kmap_atomic(page);
226 
227 			if (cur >= dst) {
228 				memcpy(buf, cur, min_t(uint, PAGE_SIZE,
229 						       end - cur));
230 			} else {
231 				memcpy(buf + pageofs_out, cur + pageofs_out,
232 				       min_t(uint, righthalf, end - cur));
233 			}
234 			kunmap_atomic(buf);
235 		}
236 		cur += PAGE_SIZE;
237 	}
238 }
239 
240 static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
241 				      struct list_head *pagepool)
242 {
243 	const unsigned int nrpages_out =
244 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
245 	const struct z_erofs_decompressor *alg = decompressors + rq->alg;
246 	unsigned int dst_maptype;
247 	void *dst;
248 	int ret, i;
249 
250 	if (nrpages_out == 1 && !rq->inplace_io) {
251 		DBG_BUGON(!*rq->out);
252 		dst = kmap_atomic(*rq->out);
253 		dst_maptype = 0;
254 		goto dstmap_out;
255 	}
256 
257 	/*
258 	 * For the case of small output size (especially much less
259 	 * than PAGE_SIZE), memcpy the decompressed data rather than
260 	 * compressed data is preferred.
261 	 */
262 	if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
263 		dst = erofs_get_pcpubuf(0);
264 		if (IS_ERR(dst))
265 			return PTR_ERR(dst);
266 
267 		rq->inplace_io = false;
268 		ret = alg->decompress(rq, dst);
269 		if (!ret)
270 			copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
271 					  rq->outputsize);
272 
273 		erofs_put_pcpubuf(dst);
274 		return ret;
275 	}
276 
277 	ret = alg->prepare_destpages(rq, pagepool);
278 	if (ret < 0) {
279 		return ret;
280 	} else if (ret) {
281 		dst = page_address(*rq->out);
282 		dst_maptype = 1;
283 		goto dstmap_out;
284 	}
285 
286 	i = 0;
287 	while (1) {
288 		dst = vm_map_ram(rq->out, nrpages_out, -1);
289 
290 		/* retry two more times (totally 3 times) */
291 		if (dst || ++i >= 3)
292 			break;
293 		vm_unmap_aliases();
294 	}
295 
296 	if (!dst)
297 		return -ENOMEM;
298 
299 	dst_maptype = 2;
300 
301 dstmap_out:
302 	ret = alg->decompress(rq, dst + rq->pageofs_out);
303 
304 	if (!dst_maptype)
305 		kunmap_atomic(dst);
306 	else if (dst_maptype == 2)
307 		vm_unmap_ram(dst, nrpages_out);
308 	return ret;
309 }
310 
311 static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
312 				     struct list_head *pagepool)
313 {
314 	const unsigned int nrpages_out =
315 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
316 	const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
317 	unsigned char *src, *dst;
318 
319 	if (nrpages_out > 2) {
320 		DBG_BUGON(1);
321 		return -EIO;
322 	}
323 
324 	if (rq->out[0] == *rq->in) {
325 		DBG_BUGON(nrpages_out != 1);
326 		return 0;
327 	}
328 
329 	src = kmap_atomic(*rq->in);
330 	if (rq->out[0]) {
331 		dst = kmap_atomic(rq->out[0]);
332 		memcpy(dst + rq->pageofs_out, src, righthalf);
333 		kunmap_atomic(dst);
334 	}
335 
336 	if (nrpages_out == 2) {
337 		DBG_BUGON(!rq->out[1]);
338 		if (rq->out[1] == *rq->in) {
339 			memmove(src, src + righthalf, rq->pageofs_out);
340 		} else {
341 			dst = kmap_atomic(rq->out[1]);
342 			memcpy(dst, src + righthalf, rq->pageofs_out);
343 			kunmap_atomic(dst);
344 		}
345 	}
346 	kunmap_atomic(src);
347 	return 0;
348 }
349 
350 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
351 		       struct list_head *pagepool)
352 {
353 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
354 		return z_erofs_shifted_transform(rq, pagepool);
355 	return z_erofs_decompress_generic(rq, pagepool);
356 }
357 
358