blob: 93411e9df9b67c2dd9f1c2f959e4dbcfe08da06a [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang7fc45db2019-06-24 15:22:55 +08002/*
Gao Xiang7fc45db2019-06-24 15:22:55 +08003 * Copyright (C) 2019 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang7fc45db2019-06-24 15:22:55 +08005 * Created by Gao Xiang <gaoxiang25@huawei.com>
6 */
7#include "compress.h"
Gao Xiang46c2d142019-07-31 23:57:44 +08008#include <linux/module.h>
Gao Xiang7fc45db2019-06-24 15:22:55 +08009#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
Gao Xiangaf89bce2019-07-03 14:52:09 +080015#define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
Gao Xiang0ffd71b2019-06-24 15:22:56 +080016#ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
17#define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
18#endif
Gao Xiang7fc45db2019-06-24 15:22:55 +080019
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
Huang Jianan5d505382021-03-29 09:23:06 +080031int z_erofs_load_lz4_config(struct super_block *sb,
32 struct erofs_super_block *dsb)
33{
34 u16 distance = le16_to_cpu(dsb->lz4_max_distance);
35
36 EROFS_SB(sb)->lz4.max_distance_pages = distance ?
37 DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
38 LZ4_MAX_DISTANCE_PAGES;
39 return 0;
40}
41
Gao Xiang99634bf2019-09-04 10:09:05 +080042static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
43 struct list_head *pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +080044{
45 const unsigned int nr =
46 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
47 struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
Gao Xiangaf89bce2019-07-03 14:52:09 +080048 unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
49 BITS_PER_LONG)] = { 0 };
Huang Jianan5d505382021-03-29 09:23:06 +080050 unsigned int lz4_max_distance_pages =
51 EROFS_SB(rq->sb)->lz4.max_distance_pages;
Gao Xiang7fc45db2019-06-24 15:22:55 +080052 void *kaddr = NULL;
Gao Xiangaf89bce2019-07-03 14:52:09 +080053 unsigned int i, j, top;
Gao Xiang7fc45db2019-06-24 15:22:55 +080054
Gao Xiangaf89bce2019-07-03 14:52:09 +080055 top = 0;
56 for (i = j = 0; i < nr; ++i, ++j) {
Gao Xiang7fc45db2019-06-24 15:22:55 +080057 struct page *const page = rq->out[i];
Gao Xiangaf89bce2019-07-03 14:52:09 +080058 struct page *victim;
Gao Xiang7fc45db2019-06-24 15:22:55 +080059
Huang Jianan5d505382021-03-29 09:23:06 +080060 if (j >= lz4_max_distance_pages)
Gao Xiangaf89bce2019-07-03 14:52:09 +080061 j = 0;
62
63 /* 'valid' bounced can only be tested after a complete round */
64 if (test_bit(j, bounced)) {
Huang Jianan5d505382021-03-29 09:23:06 +080065 DBG_BUGON(i < lz4_max_distance_pages);
66 DBG_BUGON(top >= lz4_max_distance_pages);
67 availables[top++] = rq->out[i - lz4_max_distance_pages];
Gao Xiangaf89bce2019-07-03 14:52:09 +080068 }
Gao Xiang7fc45db2019-06-24 15:22:55 +080069
70 if (page) {
Gao Xiangaf89bce2019-07-03 14:52:09 +080071 __clear_bit(j, bounced);
Gao Xiang7fc45db2019-06-24 15:22:55 +080072 if (kaddr) {
73 if (kaddr + PAGE_SIZE == page_address(page))
74 kaddr += PAGE_SIZE;
75 else
76 kaddr = NULL;
77 } else if (!i) {
78 kaddr = page_address(page);
79 }
80 continue;
81 }
82 kaddr = NULL;
Gao Xiangaf89bce2019-07-03 14:52:09 +080083 __set_bit(j, bounced);
Gao Xiang7fc45db2019-06-24 15:22:55 +080084
Gao Xiangaf89bce2019-07-03 14:52:09 +080085 if (top) {
86 victim = availables[--top];
87 get_page(victim);
Gao Xiang7fc45db2019-06-24 15:22:55 +080088 } else {
Huang Jiananb4892fa2021-03-16 11:15:14 +080089 victim = erofs_allocpage(pagepool,
90 GFP_KERNEL | __GFP_NOFAIL);
Gao Xiang6aaa7b02020-12-08 17:58:32 +080091 set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
Gao Xiang7fc45db2019-06-24 15:22:55 +080092 }
Gao Xiangaf89bce2019-07-03 14:52:09 +080093 rq->out[i] = victim;
Gao Xiang7fc45db2019-06-24 15:22:55 +080094 }
95 return kaddr ? 1 : 0;
96}
97
98static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
99 u8 *src, unsigned int pageofs_in)
100{
101 /*
102 * if in-place decompression is ongoing, those decompressed
103 * pages should be copied in order to avoid being overlapped.
104 */
105 struct page **in = rq->in;
106 u8 *const tmp = erofs_get_pcpubuf(0);
107 u8 *tmpp = tmp;
108 unsigned int inlen = rq->inputsize - pageofs_in;
109 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
110
111 while (tmpp < tmp + inlen) {
112 if (!src)
113 src = kmap_atomic(*in);
114 memcpy(tmpp, src + pageofs_in, count);
115 kunmap_atomic(src);
116 src = NULL;
117 tmpp += count;
118 pageofs_in = 0;
119 count = PAGE_SIZE;
120 ++in;
121 }
122 return tmp;
123}
124
Gao Xiang99634bf2019-09-04 10:09:05 +0800125static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800126{
127 unsigned int inputmargin, inlen;
128 u8 *src;
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800129 bool copied, support_0padding;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800130 int ret;
131
132 if (rq->inputsize > PAGE_SIZE)
Gao Xiangff784a72019-08-14 18:37:05 +0800133 return -EOPNOTSUPP;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800134
135 src = kmap_atomic(*rq->in);
136 inputmargin = 0;
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800137 support_0padding = false;
138
139 /* decompression inplace is only safe when 0padding is enabled */
Gao Xiangde06a6a2021-03-29 09:23:05 +0800140 if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) {
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800141 support_0padding = true;
142
143 while (!src[inputmargin & ~PAGE_MASK])
144 if (!(++inputmargin & ~PAGE_MASK))
145 break;
146
147 if (inputmargin >= rq->inputsize) {
148 kunmap_atomic(src);
149 return -EIO;
150 }
151 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800152
153 copied = false;
154 inlen = rq->inputsize - inputmargin;
155 if (rq->inplace_io) {
Gao Xiang0ffd71b2019-06-24 15:22:56 +0800156 const uint oend = (rq->pageofs_out +
157 rq->outputsize) & ~PAGE_MASK;
158 const uint nr = PAGE_ALIGN(rq->pageofs_out +
159 rq->outputsize) >> PAGE_SHIFT;
160
161 if (rq->partial_decoding || !support_0padding ||
162 rq->out[nr - 1] != rq->in[0] ||
163 rq->inputsize - oend <
164 LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
165 src = generic_copy_inplace_data(rq, src, inputmargin);
166 inputmargin = 0;
167 copied = true;
168 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800169 }
170
Gao Xiangaf1038a2020-02-26 16:10:07 +0800171 /* legacy format could compress extra data in a pcluster. */
172 if (rq->partial_decoding || !support_0padding)
173 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
174 inlen, rq->outputsize,
175 rq->outputsize);
176 else
177 ret = LZ4_decompress_safe(src + inputmargin, out,
178 inlen, rq->outputsize);
179
Gao Xiangaa99a762020-02-26 16:10:08 +0800180 if (ret != rq->outputsize) {
181 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
182 ret, inlen, inputmargin, rq->outputsize);
183
Gao Xiang7fc45db2019-06-24 15:22:55 +0800184 WARN_ON(1);
185 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
186 16, 1, src + inputmargin, inlen, true);
187 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
188 16, 1, out, rq->outputsize, true);
Gao Xiangaa99a762020-02-26 16:10:08 +0800189
190 if (ret >= 0)
191 memset(out + ret, 0, rq->outputsize - ret);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800192 ret = -EIO;
193 }
194
195 if (copied)
196 erofs_put_pcpubuf(src);
197 else
198 kunmap_atomic(src);
199 return ret;
200}
201
202static struct z_erofs_decompressor decompressors[] = {
203 [Z_EROFS_COMPRESSION_SHIFTED] = {
204 .name = "shifted"
205 },
206 [Z_EROFS_COMPRESSION_LZ4] = {
Gao Xiang99634bf2019-09-04 10:09:05 +0800207 .prepare_destpages = z_erofs_lz4_prepare_destpages,
208 .decompress = z_erofs_lz4_decompress,
Gao Xiang7fc45db2019-06-24 15:22:55 +0800209 .name = "lz4"
210 },
211};
212
213static void copy_from_pcpubuf(struct page **out, const char *dst,
214 unsigned short pageofs_out,
215 unsigned int outputsize)
216{
217 const char *end = dst + outputsize;
218 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
219 const char *cur = dst - pageofs_out;
220
221 while (cur < end) {
222 struct page *const page = *out++;
223
224 if (page) {
225 char *buf = kmap_atomic(page);
226
227 if (cur >= dst) {
228 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
229 end - cur));
230 } else {
231 memcpy(buf + pageofs_out, cur + pageofs_out,
232 min_t(uint, righthalf, end - cur));
233 }
234 kunmap_atomic(buf);
235 }
236 cur += PAGE_SIZE;
237 }
238}
239
Gao Xiang99634bf2019-09-04 10:09:05 +0800240static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
241 struct list_head *pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800242{
243 const unsigned int nrpages_out =
244 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
245 const struct z_erofs_decompressor *alg = decompressors + rq->alg;
246 unsigned int dst_maptype;
247 void *dst;
Gao Xiang73d03932019-09-04 10:09:07 +0800248 int ret, i;
Gao Xiang7fc45db2019-06-24 15:22:55 +0800249
250 if (nrpages_out == 1 && !rq->inplace_io) {
251 DBG_BUGON(!*rq->out);
252 dst = kmap_atomic(*rq->out);
253 dst_maptype = 0;
254 goto dstmap_out;
255 }
256
257 /*
258 * For the case of small output size (especially much less
259 * than PAGE_SIZE), memcpy the decompressed data rather than
260 * compressed data is preferred.
261 */
262 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
263 dst = erofs_get_pcpubuf(0);
264 if (IS_ERR(dst))
265 return PTR_ERR(dst);
266
267 rq->inplace_io = false;
268 ret = alg->decompress(rq, dst);
269 if (!ret)
270 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
271 rq->outputsize);
272
273 erofs_put_pcpubuf(dst);
274 return ret;
275 }
276
277 ret = alg->prepare_destpages(rq, pagepool);
278 if (ret < 0) {
279 return ret;
280 } else if (ret) {
281 dst = page_address(*rq->out);
282 dst_maptype = 1;
283 goto dstmap_out;
284 }
285
Gao Xiang73d03932019-09-04 10:09:07 +0800286 i = 0;
287 while (1) {
Christoph Hellwigd4efd792020-06-01 21:51:27 -0700288 dst = vm_map_ram(rq->out, nrpages_out, -1);
Gao Xiang73d03932019-09-04 10:09:07 +0800289
290 /* retry two more times (totally 3 times) */
291 if (dst || ++i >= 3)
292 break;
293 vm_unmap_aliases();
294 }
295
Gao Xiang7fc45db2019-06-24 15:22:55 +0800296 if (!dst)
297 return -ENOMEM;
Gao Xiang73d03932019-09-04 10:09:07 +0800298
Gao Xiang7fc45db2019-06-24 15:22:55 +0800299 dst_maptype = 2;
300
301dstmap_out:
302 ret = alg->decompress(rq, dst + rq->pageofs_out);
303
304 if (!dst_maptype)
305 kunmap_atomic(dst);
306 else if (dst_maptype == 2)
Gao Xiang73d03932019-09-04 10:09:07 +0800307 vm_unmap_ram(dst, nrpages_out);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800308 return ret;
309}
310
Gao Xiang99634bf2019-09-04 10:09:05 +0800311static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
312 struct list_head *pagepool)
Gao Xiang7fc45db2019-06-24 15:22:55 +0800313{
314 const unsigned int nrpages_out =
315 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
316 const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
317 unsigned char *src, *dst;
318
319 if (nrpages_out > 2) {
320 DBG_BUGON(1);
321 return -EIO;
322 }
323
324 if (rq->out[0] == *rq->in) {
325 DBG_BUGON(nrpages_out != 1);
326 return 0;
327 }
328
329 src = kmap_atomic(*rq->in);
Gao Xiang4d202432020-01-07 10:25:46 +0800330 if (rq->out[0]) {
Gao Xiang7fc45db2019-06-24 15:22:55 +0800331 dst = kmap_atomic(rq->out[0]);
332 memcpy(dst + rq->pageofs_out, src, righthalf);
Gao Xiang4d202432020-01-07 10:25:46 +0800333 kunmap_atomic(dst);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800334 }
335
Gao Xiang4d202432020-01-07 10:25:46 +0800336 if (nrpages_out == 2) {
Gao Xiang7fc45db2019-06-24 15:22:55 +0800337 DBG_BUGON(!rq->out[1]);
Gao Xiang4d202432020-01-07 10:25:46 +0800338 if (rq->out[1] == *rq->in) {
339 memmove(src, src + righthalf, rq->pageofs_out);
340 } else {
341 dst = kmap_atomic(rq->out[1]);
342 memcpy(dst, src + righthalf, rq->pageofs_out);
343 kunmap_atomic(dst);
344 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800345 }
Gao Xiang7fc45db2019-06-24 15:22:55 +0800346 kunmap_atomic(src);
347 return 0;
348}
349
350int z_erofs_decompress(struct z_erofs_decompress_req *rq,
351 struct list_head *pagepool)
352{
353 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
Gao Xiang99634bf2019-09-04 10:09:05 +0800354 return z_erofs_shifted_transform(rq, pagepool);
355 return z_erofs_decompress_generic(rq, pagepool);
Gao Xiang7fc45db2019-06-24 15:22:55 +0800356}
357