blob: 8a6260aac26cb52aea987b98271190bc615ed5c1 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2019 HUAWEI, Inc.
Olivier Deprez157378f2022-04-04 15:47:50 +02004 * https://www.huawei.com/
David Brazdil0f672f62019-12-10 10:32:29 +00005 * 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
20struct 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
31static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
32 struct list_head *pagepool)
33{
34 const unsigned int nr =
35 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
36 struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
37 unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
38 BITS_PER_LONG)] = { 0 };
39 void *kaddr = NULL;
40 unsigned int i, j, top;
41
42 top = 0;
43 for (i = j = 0; i < nr; ++i, ++j) {
44 struct page *const page = rq->out[i];
45 struct page *victim;
46
47 if (j >= LZ4_MAX_DISTANCE_PAGES)
48 j = 0;
49
50 /* 'valid' bounced can only be tested after a complete round */
51 if (test_bit(j, bounced)) {
52 DBG_BUGON(i < LZ4_MAX_DISTANCE_PAGES);
53 DBG_BUGON(top >= LZ4_MAX_DISTANCE_PAGES);
54 availables[top++] = rq->out[i - LZ4_MAX_DISTANCE_PAGES];
55 }
56
57 if (page) {
58 __clear_bit(j, bounced);
59 if (kaddr) {
60 if (kaddr + PAGE_SIZE == page_address(page))
61 kaddr += PAGE_SIZE;
62 else
63 kaddr = NULL;
64 } else if (!i) {
65 kaddr = page_address(page);
66 }
67 continue;
68 }
69 kaddr = NULL;
70 __set_bit(j, bounced);
71
72 if (top) {
73 victim = availables[--top];
74 get_page(victim);
75 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +020076 victim = erofs_allocpage(pagepool, GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +000077 if (!victim)
78 return -ENOMEM;
79 victim->mapping = Z_EROFS_MAPPING_STAGING;
80 }
81 rq->out[i] = victim;
82 }
83 return kaddr ? 1 : 0;
84}
85
86static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
87 u8 *src, unsigned int pageofs_in)
88{
89 /*
90 * if in-place decompression is ongoing, those decompressed
91 * pages should be copied in order to avoid being overlapped.
92 */
93 struct page **in = rq->in;
94 u8 *const tmp = erofs_get_pcpubuf(0);
95 u8 *tmpp = tmp;
96 unsigned int inlen = rq->inputsize - pageofs_in;
97 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
98
99 while (tmpp < tmp + inlen) {
100 if (!src)
101 src = kmap_atomic(*in);
102 memcpy(tmpp, src + pageofs_in, count);
103 kunmap_atomic(src);
104 src = NULL;
105 tmpp += count;
106 pageofs_in = 0;
107 count = PAGE_SIZE;
108 ++in;
109 }
110 return tmp;
111}
112
113static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
114{
115 unsigned int inputmargin, inlen;
116 u8 *src;
117 bool copied, support_0padding;
118 int ret;
119
120 if (rq->inputsize > PAGE_SIZE)
121 return -EOPNOTSUPP;
122
123 src = kmap_atomic(*rq->in);
124 inputmargin = 0;
125 support_0padding = false;
126
127 /* decompression inplace is only safe when 0padding is enabled */
128 if (EROFS_SB(rq->sb)->feature_incompat &
129 EROFS_FEATURE_INCOMPAT_LZ4_0PADDING) {
130 support_0padding = true;
131
132 while (!src[inputmargin & ~PAGE_MASK])
133 if (!(++inputmargin & ~PAGE_MASK))
134 break;
135
136 if (inputmargin >= rq->inputsize) {
137 kunmap_atomic(src);
138 return -EIO;
139 }
140 }
141
142 copied = false;
143 inlen = rq->inputsize - inputmargin;
144 if (rq->inplace_io) {
145 const uint oend = (rq->pageofs_out +
146 rq->outputsize) & ~PAGE_MASK;
147 const uint nr = PAGE_ALIGN(rq->pageofs_out +
148 rq->outputsize) >> PAGE_SHIFT;
149
150 if (rq->partial_decoding || !support_0padding ||
151 rq->out[nr - 1] != rq->in[0] ||
152 rq->inputsize - oend <
153 LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
154 src = generic_copy_inplace_data(rq, src, inputmargin);
155 inputmargin = 0;
156 copied = true;
157 }
158 }
159
Olivier Deprez157378f2022-04-04 15:47:50 +0200160 /* legacy format could compress extra data in a pcluster. */
161 if (rq->partial_decoding || !support_0padding)
162 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
163 inlen, rq->outputsize,
164 rq->outputsize);
165 else
166 ret = LZ4_decompress_safe(src + inputmargin, out,
167 inlen, rq->outputsize);
168
169 if (ret != rq->outputsize) {
170 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
171 ret, inlen, inputmargin, rq->outputsize);
172
David Brazdil0f672f62019-12-10 10:32:29 +0000173 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
174 16, 1, src + inputmargin, inlen, true);
175 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
176 16, 1, out, rq->outputsize, true);
Olivier Deprez157378f2022-04-04 15:47:50 +0200177
178 if (ret >= 0)
179 memset(out + ret, 0, rq->outputsize - ret);
David Brazdil0f672f62019-12-10 10:32:29 +0000180 ret = -EIO;
181 }
182
183 if (copied)
184 erofs_put_pcpubuf(src);
185 else
186 kunmap_atomic(src);
187 return ret;
188}
189
190static struct z_erofs_decompressor decompressors[] = {
191 [Z_EROFS_COMPRESSION_SHIFTED] = {
192 .name = "shifted"
193 },
194 [Z_EROFS_COMPRESSION_LZ4] = {
195 .prepare_destpages = z_erofs_lz4_prepare_destpages,
196 .decompress = z_erofs_lz4_decompress,
197 .name = "lz4"
198 },
199};
200
201static void copy_from_pcpubuf(struct page **out, const char *dst,
202 unsigned short pageofs_out,
203 unsigned int outputsize)
204{
205 const char *end = dst + outputsize;
206 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
207 const char *cur = dst - pageofs_out;
208
209 while (cur < end) {
210 struct page *const page = *out++;
211
212 if (page) {
213 char *buf = kmap_atomic(page);
214
215 if (cur >= dst) {
216 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
217 end - cur));
218 } else {
219 memcpy(buf + pageofs_out, cur + pageofs_out,
220 min_t(uint, righthalf, end - cur));
221 }
222 kunmap_atomic(buf);
223 }
224 cur += PAGE_SIZE;
225 }
226}
227
228static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
229 struct list_head *pagepool)
230{
231 const unsigned int nrpages_out =
232 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
233 const struct z_erofs_decompressor *alg = decompressors + rq->alg;
234 unsigned int dst_maptype;
235 void *dst;
236 int ret, i;
237
238 if (nrpages_out == 1 && !rq->inplace_io) {
239 DBG_BUGON(!*rq->out);
240 dst = kmap_atomic(*rq->out);
241 dst_maptype = 0;
242 goto dstmap_out;
243 }
244
245 /*
246 * For the case of small output size (especially much less
247 * than PAGE_SIZE), memcpy the decompressed data rather than
248 * compressed data is preferred.
249 */
250 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
251 dst = erofs_get_pcpubuf(0);
252 if (IS_ERR(dst))
253 return PTR_ERR(dst);
254
255 rq->inplace_io = false;
256 ret = alg->decompress(rq, dst);
257 if (!ret)
258 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
259 rq->outputsize);
260
261 erofs_put_pcpubuf(dst);
262 return ret;
263 }
264
265 ret = alg->prepare_destpages(rq, pagepool);
266 if (ret < 0) {
267 return ret;
268 } else if (ret) {
269 dst = page_address(*rq->out);
270 dst_maptype = 1;
271 goto dstmap_out;
272 }
273
274 i = 0;
275 while (1) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200276 dst = vm_map_ram(rq->out, nrpages_out, -1);
David Brazdil0f672f62019-12-10 10:32:29 +0000277
278 /* retry two more times (totally 3 times) */
279 if (dst || ++i >= 3)
280 break;
281 vm_unmap_aliases();
282 }
283
284 if (!dst)
285 return -ENOMEM;
286
287 dst_maptype = 2;
288
289dstmap_out:
290 ret = alg->decompress(rq, dst + rq->pageofs_out);
291
292 if (!dst_maptype)
293 kunmap_atomic(dst);
294 else if (dst_maptype == 2)
295 vm_unmap_ram(dst, nrpages_out);
296 return ret;
297}
298
299static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
300 struct list_head *pagepool)
301{
302 const unsigned int nrpages_out =
303 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
304 const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
305 unsigned char *src, *dst;
306
307 if (nrpages_out > 2) {
308 DBG_BUGON(1);
309 return -EIO;
310 }
311
312 if (rq->out[0] == *rq->in) {
313 DBG_BUGON(nrpages_out != 1);
314 return 0;
315 }
316
317 src = kmap_atomic(*rq->in);
Olivier Deprez0e641232021-09-23 10:07:05 +0200318 if (rq->out[0]) {
David Brazdil0f672f62019-12-10 10:32:29 +0000319 dst = kmap_atomic(rq->out[0]);
320 memcpy(dst + rq->pageofs_out, src, righthalf);
Olivier Deprez0e641232021-09-23 10:07:05 +0200321 kunmap_atomic(dst);
David Brazdil0f672f62019-12-10 10:32:29 +0000322 }
323
Olivier Deprez0e641232021-09-23 10:07:05 +0200324 if (nrpages_out == 2) {
David Brazdil0f672f62019-12-10 10:32:29 +0000325 DBG_BUGON(!rq->out[1]);
Olivier Deprez0e641232021-09-23 10:07:05 +0200326 if (rq->out[1] == *rq->in) {
327 memmove(src, src + righthalf, rq->pageofs_out);
328 } else {
329 dst = kmap_atomic(rq->out[1]);
330 memcpy(dst, src + righthalf, rq->pageofs_out);
331 kunmap_atomic(dst);
332 }
David Brazdil0f672f62019-12-10 10:32:29 +0000333 }
David Brazdil0f672f62019-12-10 10:32:29 +0000334 kunmap_atomic(src);
335 return 0;
336}
337
338int z_erofs_decompress(struct z_erofs_decompress_req *rq,
339 struct list_head *pagepool)
340{
341 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
342 return z_erofs_shifted_transform(rq, pagepool);
343 return z_erofs_decompress_generic(rq, pagepool);
344}
345