Merge tag 'drm-fixes-2019-07-26' of git://anongit.freedesktop.org/drm/drm
[linux-2.6-microblaze.git] / drivers / staging / erofs / decompressor.c
1 // SPDX-License-Identifier: GPL-2.0
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/lz4.h>
11
12 #ifndef LZ4_DISTANCE_MAX        /* history window size */
13 #define LZ4_DISTANCE_MAX 65535  /* set to maximum value by default */
14 #endif
15
16 #define LZ4_MAX_DISTANCE_PAGES  (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
17 #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
18 #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize)  (((srcsize) >> 8) + 32)
19 #endif
20
21 struct z_erofs_decompressor {
22         /*
23          * if destpages have sparsed pages, fill them with bounce pages.
24          * it also check whether destpages indicate continuous physical memory.
25          */
26         int (*prepare_destpages)(struct z_erofs_decompress_req *rq,
27                                  struct list_head *pagepool);
28         int (*decompress)(struct z_erofs_decompress_req *rq, u8 *out);
29         char *name;
30 };
31
32 static int 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 (kaddr) {
61                                 if (kaddr + PAGE_SIZE == page_address(page))
62                                         kaddr += PAGE_SIZE;
63                                 else
64                                         kaddr = NULL;
65                         } else if (!i) {
66                                 kaddr = page_address(page);
67                         }
68                         continue;
69                 }
70                 kaddr = NULL;
71                 __set_bit(j, bounced);
72
73                 if (top) {
74                         victim = availables[--top];
75                         get_page(victim);
76                 } else {
77                         if (!list_empty(pagepool)) {
78                                 victim = lru_to_page(pagepool);
79                                 list_del(&victim->lru);
80                                 DBG_BUGON(page_ref_count(victim) != 1);
81                         } else {
82                                 victim = alloc_pages(GFP_KERNEL, 0);
83                                 if (!victim)
84                                         return -ENOMEM;
85                         }
86                         victim->mapping = Z_EROFS_MAPPING_STAGING;
87                 }
88                 rq->out[i] = victim;
89         }
90         return kaddr ? 1 : 0;
91 }
92
93 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
94                                        u8 *src, unsigned int pageofs_in)
95 {
96         /*
97          * if in-place decompression is ongoing, those decompressed
98          * pages should be copied in order to avoid being overlapped.
99          */
100         struct page **in = rq->in;
101         u8 *const tmp = erofs_get_pcpubuf(0);
102         u8 *tmpp = tmp;
103         unsigned int inlen = rq->inputsize - pageofs_in;
104         unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
105
106         while (tmpp < tmp + inlen) {
107                 if (!src)
108                         src = kmap_atomic(*in);
109                 memcpy(tmpp, src + pageofs_in, count);
110                 kunmap_atomic(src);
111                 src = NULL;
112                 tmpp += count;
113                 pageofs_in = 0;
114                 count = PAGE_SIZE;
115                 ++in;
116         }
117         return tmp;
118 }
119
120 static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
121 {
122         unsigned int inputmargin, inlen;
123         u8 *src;
124         bool copied, support_0padding;
125         int ret;
126
127         if (rq->inputsize > PAGE_SIZE)
128                 return -ENOTSUPP;
129
130         src = kmap_atomic(*rq->in);
131         inputmargin = 0;
132         support_0padding = false;
133
134         /* decompression inplace is only safe when 0padding is enabled */
135         if (EROFS_SB(rq->sb)->requirements & EROFS_REQUIREMENT_LZ4_0PADDING) {
136                 support_0padding = true;
137
138                 while (!src[inputmargin & ~PAGE_MASK])
139                         if (!(++inputmargin & ~PAGE_MASK))
140                                 break;
141
142                 if (inputmargin >= rq->inputsize) {
143                         kunmap_atomic(src);
144                         return -EIO;
145                 }
146         }
147
148         copied = false;
149         inlen = rq->inputsize - inputmargin;
150         if (rq->inplace_io) {
151                 const uint oend = (rq->pageofs_out +
152                                    rq->outputsize) & ~PAGE_MASK;
153                 const uint nr = PAGE_ALIGN(rq->pageofs_out +
154                                            rq->outputsize) >> PAGE_SHIFT;
155
156                 if (rq->partial_decoding || !support_0padding ||
157                     rq->out[nr - 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                 }
164         }
165
166         ret = LZ4_decompress_safe_partial(src + inputmargin, out,
167                                           inlen, rq->outputsize,
168                                           rq->outputsize);
169         if (ret < 0) {
170                 errln("%s, failed to decompress, in[%p, %u, %u] out[%p, %u]",
171                       __func__, src + inputmargin, inlen, inputmargin,
172                       out, rq->outputsize);
173                 WARN_ON(1);
174                 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
175                                16, 1, src + inputmargin, inlen, true);
176                 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
177                                16, 1, out, rq->outputsize, true);
178                 ret = -EIO;
179         }
180
181         if (copied)
182                 erofs_put_pcpubuf(src);
183         else
184                 kunmap_atomic(src);
185         return ret;
186 }
187
188 static struct z_erofs_decompressor decompressors[] = {
189         [Z_EROFS_COMPRESSION_SHIFTED] = {
190                 .name = "shifted"
191         },
192         [Z_EROFS_COMPRESSION_LZ4] = {
193                 .prepare_destpages = lz4_prepare_destpages,
194                 .decompress = lz4_decompress,
195                 .name = "lz4"
196         },
197 };
198
199 static void copy_from_pcpubuf(struct page **out, const char *dst,
200                               unsigned short pageofs_out,
201                               unsigned int outputsize)
202 {
203         const char *end = dst + outputsize;
204         const unsigned int righthalf = PAGE_SIZE - pageofs_out;
205         const char *cur = dst - pageofs_out;
206
207         while (cur < end) {
208                 struct page *const page = *out++;
209
210                 if (page) {
211                         char *buf = kmap_atomic(page);
212
213                         if (cur >= dst) {
214                                 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
215                                                        end - cur));
216                         } else {
217                                 memcpy(buf + pageofs_out, cur + pageofs_out,
218                                        min_t(uint, righthalf, end - cur));
219                         }
220                         kunmap_atomic(buf);
221                 }
222                 cur += PAGE_SIZE;
223         }
224 }
225
226 static int decompress_generic(struct z_erofs_decompress_req *rq,
227                               struct list_head *pagepool)
228 {
229         const unsigned int nrpages_out =
230                 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
231         const struct z_erofs_decompressor *alg = decompressors + rq->alg;
232         unsigned int dst_maptype;
233         void *dst;
234         int ret;
235
236         if (nrpages_out == 1 && !rq->inplace_io) {
237                 DBG_BUGON(!*rq->out);
238                 dst = kmap_atomic(*rq->out);
239                 dst_maptype = 0;
240                 goto dstmap_out;
241         }
242
243         /*
244          * For the case of small output size (especially much less
245          * than PAGE_SIZE), memcpy the decompressed data rather than
246          * compressed data is preferred.
247          */
248         if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
249                 dst = erofs_get_pcpubuf(0);
250                 if (IS_ERR(dst))
251                         return PTR_ERR(dst);
252
253                 rq->inplace_io = false;
254                 ret = alg->decompress(rq, dst);
255                 if (!ret)
256                         copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
257                                           rq->outputsize);
258
259                 erofs_put_pcpubuf(dst);
260                 return ret;
261         }
262
263         ret = alg->prepare_destpages(rq, pagepool);
264         if (ret < 0) {
265                 return ret;
266         } else if (ret) {
267                 dst = page_address(*rq->out);
268                 dst_maptype = 1;
269                 goto dstmap_out;
270         }
271
272         dst = erofs_vmap(rq->out, nrpages_out);
273         if (!dst)
274                 return -ENOMEM;
275         dst_maptype = 2;
276
277 dstmap_out:
278         ret = alg->decompress(rq, dst + rq->pageofs_out);
279
280         if (!dst_maptype)
281                 kunmap_atomic(dst);
282         else if (dst_maptype == 2)
283                 erofs_vunmap(dst, nrpages_out);
284         return ret;
285 }
286
287 static int shifted_decompress(const struct z_erofs_decompress_req *rq,
288                               struct list_head *pagepool)
289 {
290         const unsigned int nrpages_out =
291                 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
292         const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
293         unsigned char *src, *dst;
294
295         if (nrpages_out > 2) {
296                 DBG_BUGON(1);
297                 return -EIO;
298         }
299
300         if (rq->out[0] == *rq->in) {
301                 DBG_BUGON(nrpages_out != 1);
302                 return 0;
303         }
304
305         src = kmap_atomic(*rq->in);
306         if (!rq->out[0]) {
307                 dst = NULL;
308         } else {
309                 dst = kmap_atomic(rq->out[0]);
310                 memcpy(dst + rq->pageofs_out, src, righthalf);
311         }
312
313         if (rq->out[1] == *rq->in) {
314                 memmove(src, src + righthalf, rq->pageofs_out);
315         } else if (nrpages_out == 2) {
316                 if (dst)
317                         kunmap_atomic(dst);
318                 DBG_BUGON(!rq->out[1]);
319                 dst = kmap_atomic(rq->out[1]);
320                 memcpy(dst, src + righthalf, rq->pageofs_out);
321         }
322         if (dst)
323                 kunmap_atomic(dst);
324         kunmap_atomic(src);
325         return 0;
326 }
327
328 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
329                        struct list_head *pagepool)
330 {
331         if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
332                 return shifted_decompress(rq, pagepool);
333         return decompress_generic(rq, pagepool);
334 }
335