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