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