1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/staging/erofs/decompressor.c
5 * Copyright (C) 2019 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
10 #include <linux/module.h>
11 #include <linux/lz4.h>
13 #ifndef LZ4_DISTANCE_MAX /* history window size */
14 #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
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)
22 struct z_erofs_decompressor {
24 * if destpages have sparsed pages, fill them with bounce pages.
25 * it also check whether destpages indicate continuous physical memory.
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);
34 module_param(use_vmap, bool, 0444);
35 MODULE_PARM_DESC(use_vmap, "Use vmap() instead of vm_map_ram() (default 0)");
37 static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
38 struct list_head *pagepool)
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 };
46 unsigned int i, j, top;
49 for (i = j = 0; i < nr; ++i, ++j) {
50 struct page *const page = rq->out[i];
53 if (j >= LZ4_MAX_DISTANCE_PAGES)
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];
64 __clear_bit(j, bounced);
66 if (kaddr + PAGE_SIZE == page_address(page))
71 kaddr = page_address(page);
76 __set_bit(j, bounced);
79 victim = availables[--top];
82 victim = erofs_allocpage(pagepool, GFP_KERNEL, false);
83 if (unlikely(!victim))
85 victim->mapping = Z_EROFS_MAPPING_STAGING;
92 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
93 u8 *src, unsigned int pageofs_in)
96 * if in-place decompression is ongoing, those decompressed
97 * pages should be copied in order to avoid being overlapped.
99 struct page **in = rq->in;
100 u8 *const tmp = erofs_get_pcpubuf(0);
102 unsigned int inlen = rq->inputsize - pageofs_in;
103 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
105 while (tmpp < tmp + inlen) {
107 src = kmap_atomic(*in);
108 memcpy(tmpp, src + pageofs_in, count);
119 static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
121 unsigned int inputmargin, inlen;
123 bool copied, support_0padding;
126 if (rq->inputsize > PAGE_SIZE)
129 src = kmap_atomic(*rq->in);
131 support_0padding = false;
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;
137 while (!src[inputmargin & ~PAGE_MASK])
138 if (!(++inputmargin & ~PAGE_MASK))
141 if (inputmargin >= rq->inputsize) {
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;
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);
165 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
166 inlen, rq->outputsize,
169 errln("%s, failed to decompress, in[%p, %u, %u] out[%p, %u]",
170 __func__, src + inputmargin, inlen, inputmargin,
171 out, rq->outputsize);
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);
181 erofs_put_pcpubuf(src);
187 static struct z_erofs_decompressor decompressors[] = {
188 [Z_EROFS_COMPRESSION_SHIFTED] = {
191 [Z_EROFS_COMPRESSION_LZ4] = {
192 .prepare_destpages = lz4_prepare_destpages,
193 .decompress = lz4_decompress,
198 static void copy_from_pcpubuf(struct page **out, const char *dst,
199 unsigned short pageofs_out,
200 unsigned int outputsize)
202 const char *end = dst + outputsize;
203 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
204 const char *cur = dst - pageofs_out;
207 struct page *const page = *out++;
210 char *buf = kmap_atomic(page);
213 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
216 memcpy(buf + pageofs_out, cur + pageofs_out,
217 min_t(uint, righthalf, end - cur));
225 static void *erofs_vmap(struct page **pages, unsigned int count)
230 return vmap(pages, count, VM_MAP, PAGE_KERNEL);
233 void *addr = vm_map_ram(pages, count, -1, PAGE_KERNEL);
235 /* retry two more times (totally 3 times) */
236 if (addr || ++i >= 3)
243 static void erofs_vunmap(const void *mem, unsigned int count)
246 vm_unmap_ram(mem, count);
251 static int decompress_generic(struct z_erofs_decompress_req *rq,
252 struct list_head *pagepool)
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;
261 if (nrpages_out == 1 && !rq->inplace_io) {
262 DBG_BUGON(!*rq->out);
263 dst = kmap_atomic(*rq->out);
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.
273 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
274 dst = erofs_get_pcpubuf(0);
278 rq->inplace_io = false;
279 ret = alg->decompress(rq, dst);
281 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
284 erofs_put_pcpubuf(dst);
288 ret = alg->prepare_destpages(rq, pagepool);
292 dst = page_address(*rq->out);
297 dst = erofs_vmap(rq->out, nrpages_out);
303 ret = alg->decompress(rq, dst + rq->pageofs_out);
307 else if (dst_maptype == 2)
308 erofs_vunmap(dst, nrpages_out);
312 static int shifted_decompress(const struct z_erofs_decompress_req *rq,
313 struct list_head *pagepool)
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;
320 if (nrpages_out > 2) {
325 if (rq->out[0] == *rq->in) {
326 DBG_BUGON(nrpages_out != 1);
330 src = kmap_atomic(*rq->in);
334 dst = kmap_atomic(rq->out[0]);
335 memcpy(dst + rq->pageofs_out, src, righthalf);
338 if (rq->out[1] == *rq->in) {
339 memmove(src, src + righthalf, rq->pageofs_out);
340 } else if (nrpages_out == 2) {
343 DBG_BUGON(!rq->out[1]);
344 dst = kmap_atomic(rq->out[1]);
345 memcpy(dst, src + righthalf, rq->pageofs_out);
353 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
354 struct list_head *pagepool)
356 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
357 return shifted_decompress(rq, pagepool);
358 return decompress_generic(rq, pagepool);