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