1 // SPDX-License-Identifier: GPL-2.0
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/lz4.h>
12 #ifndef LZ4_DISTANCE_MAX /* history window size */
13 #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
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)
21 struct z_erofs_decompressor {
23 * if destpages have sparsed pages, fill them with bounce pages.
24 * it also check whether destpages indicate continuous physical memory.
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);
32 static int lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
33 struct list_head *pagepool)
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 };
41 unsigned int i, j, top;
44 for (i = j = 0; i < nr; ++i, ++j) {
45 struct page *const page = rq->out[i];
48 if (j >= LZ4_MAX_DISTANCE_PAGES)
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];
59 __clear_bit(j, bounced);
61 if (kaddr + PAGE_SIZE == page_address(page))
66 kaddr = page_address(page);
71 __set_bit(j, bounced);
74 victim = availables[--top];
77 if (!list_empty(pagepool)) {
78 victim = lru_to_page(pagepool);
79 list_del(&victim->lru);
80 DBG_BUGON(page_ref_count(victim) != 1);
82 victim = alloc_pages(GFP_KERNEL, 0);
86 victim->mapping = Z_EROFS_MAPPING_STAGING;
93 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
94 u8 *src, unsigned int pageofs_in)
97 * if in-place decompression is ongoing, those decompressed
98 * pages should be copied in order to avoid being overlapped.
100 struct page **in = rq->in;
101 u8 *const tmp = erofs_get_pcpubuf(0);
103 unsigned int inlen = rq->inputsize - pageofs_in;
104 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
106 while (tmpp < tmp + inlen) {
108 src = kmap_atomic(*in);
109 memcpy(tmpp, src + pageofs_in, count);
120 static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
122 unsigned int inputmargin, inlen;
124 bool copied, support_0padding;
127 if (rq->inputsize > PAGE_SIZE)
130 src = kmap_atomic(*rq->in);
132 support_0padding = false;
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;
138 while (!src[inputmargin & ~PAGE_MASK])
139 if (!(++inputmargin & ~PAGE_MASK))
142 if (inputmargin >= rq->inputsize) {
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;
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);
166 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
167 inlen, rq->outputsize,
170 errln("%s, failed to decompress, in[%p, %u, %u] out[%p, %u]",
171 __func__, src + inputmargin, inlen, inputmargin,
172 out, rq->outputsize);
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);
182 erofs_put_pcpubuf(src);
188 static struct z_erofs_decompressor decompressors[] = {
189 [Z_EROFS_COMPRESSION_SHIFTED] = {
192 [Z_EROFS_COMPRESSION_LZ4] = {
193 .prepare_destpages = lz4_prepare_destpages,
194 .decompress = lz4_decompress,
199 static void copy_from_pcpubuf(struct page **out, const char *dst,
200 unsigned short pageofs_out,
201 unsigned int outputsize)
203 const char *end = dst + outputsize;
204 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
205 const char *cur = dst - pageofs_out;
208 struct page *const page = *out++;
211 char *buf = kmap_atomic(page);
214 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
217 memcpy(buf + pageofs_out, cur + pageofs_out,
218 min_t(uint, righthalf, end - cur));
226 static int decompress_generic(struct z_erofs_decompress_req *rq,
227 struct list_head *pagepool)
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;
236 if (nrpages_out == 1 && !rq->inplace_io) {
237 DBG_BUGON(!*rq->out);
238 dst = kmap_atomic(*rq->out);
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.
248 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
249 dst = erofs_get_pcpubuf(0);
253 rq->inplace_io = false;
254 ret = alg->decompress(rq, dst);
256 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
259 erofs_put_pcpubuf(dst);
263 ret = alg->prepare_destpages(rq, pagepool);
267 dst = page_address(*rq->out);
272 dst = erofs_vmap(rq->out, nrpages_out);
278 ret = alg->decompress(rq, dst + rq->pageofs_out);
282 else if (dst_maptype == 2)
283 erofs_vunmap(dst, nrpages_out);
287 static int shifted_decompress(const struct z_erofs_decompress_req *rq,
288 struct list_head *pagepool)
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;
295 if (nrpages_out > 2) {
300 if (rq->out[0] == *rq->in) {
301 DBG_BUGON(nrpages_out != 1);
305 src = kmap_atomic(*rq->in);
309 dst = kmap_atomic(rq->out[0]);
310 memcpy(dst + rq->pageofs_out, src, righthalf);
313 if (rq->out[1] == *rq->in) {
314 memmove(src, src + righthalf, rq->pageofs_out);
315 } else if (nrpages_out == 2) {
318 DBG_BUGON(!rq->out[1]);
319 dst = kmap_atomic(rq->out[1]);
320 memcpy(dst, src + righthalf, rq->pageofs_out);
328 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
329 struct list_head *pagepool)
331 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
332 return shifted_decompress(rq, pagepool);
333 return decompress_generic(rq, pagepool);