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)
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 unused[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
39 BITS_PER_LONG)] = { 0 };
43 for (i = 0; i < nr; ++i) {
44 struct page *const page = rq->out[i];
46 j = i & (LZ4_MAX_DISTANCE_PAGES - 1);
52 if (kaddr + PAGE_SIZE == page_address(page))
57 kaddr = page_address(page);
63 k = find_first_bit(unused, LZ4_MAX_DISTANCE_PAGES);
64 if (k < LZ4_MAX_DISTANCE_PAGES) {
66 get_page(availables[j]);
68 DBG_BUGON(availables[j]);
70 if (!list_empty(pagepool)) {
71 availables[j] = lru_to_page(pagepool);
72 list_del(&availables[j]->lru);
73 DBG_BUGON(page_ref_count(availables[j]) != 1);
75 availables[j] = alloc_pages(GFP_KERNEL, 0);
79 availables[j]->mapping = Z_EROFS_MAPPING_STAGING;
81 rq->out[i] = availables[j];
82 __clear_bit(j, unused);
87 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
88 u8 *src, unsigned int pageofs_in)
91 * if in-place decompression is ongoing, those decompressed
92 * pages should be copied in order to avoid being overlapped.
94 struct page **in = rq->in;
95 u8 *const tmp = erofs_get_pcpubuf(0);
97 unsigned int inlen = rq->inputsize - pageofs_in;
98 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
100 while (tmpp < tmp + inlen) {
102 src = kmap_atomic(*in);
103 memcpy(tmpp, src + pageofs_in, count);
114 static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
116 unsigned int inputmargin, inlen;
118 bool copied, support_0padding;
121 if (rq->inputsize > PAGE_SIZE)
124 src = kmap_atomic(*rq->in);
126 support_0padding = false;
128 /* decompression inplace is only safe when 0padding is enabled */
129 if (EROFS_SB(rq->sb)->requirements & EROFS_REQUIREMENT_LZ4_0PADDING) {
130 support_0padding = true;
132 while (!src[inputmargin & ~PAGE_MASK])
133 if (!(++inputmargin & ~PAGE_MASK))
136 if (inputmargin >= rq->inputsize) {
143 inlen = rq->inputsize - inputmargin;
144 if (rq->inplace_io) {
145 const uint oend = (rq->pageofs_out +
146 rq->outputsize) & ~PAGE_MASK;
147 const uint nr = PAGE_ALIGN(rq->pageofs_out +
148 rq->outputsize) >> PAGE_SHIFT;
150 if (rq->partial_decoding || !support_0padding ||
151 rq->out[nr - 1] != rq->in[0] ||
152 rq->inputsize - oend <
153 LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
154 src = generic_copy_inplace_data(rq, src, inputmargin);
160 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
161 inlen, rq->outputsize,
164 errln("%s, failed to decompress, in[%p, %u, %u] out[%p, %u]",
165 __func__, src + inputmargin, inlen, inputmargin,
166 out, rq->outputsize);
168 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
169 16, 1, src + inputmargin, inlen, true);
170 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
171 16, 1, out, rq->outputsize, true);
176 erofs_put_pcpubuf(src);
182 static struct z_erofs_decompressor decompressors[] = {
183 [Z_EROFS_COMPRESSION_SHIFTED] = {
186 [Z_EROFS_COMPRESSION_LZ4] = {
187 .prepare_destpages = lz4_prepare_destpages,
188 .decompress = lz4_decompress,
193 static void copy_from_pcpubuf(struct page **out, const char *dst,
194 unsigned short pageofs_out,
195 unsigned int outputsize)
197 const char *end = dst + outputsize;
198 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
199 const char *cur = dst - pageofs_out;
202 struct page *const page = *out++;
205 char *buf = kmap_atomic(page);
208 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
211 memcpy(buf + pageofs_out, cur + pageofs_out,
212 min_t(uint, righthalf, end - cur));
220 static int decompress_generic(struct z_erofs_decompress_req *rq,
221 struct list_head *pagepool)
223 const unsigned int nrpages_out =
224 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
225 const struct z_erofs_decompressor *alg = decompressors + rq->alg;
226 unsigned int dst_maptype;
230 if (nrpages_out == 1 && !rq->inplace_io) {
231 DBG_BUGON(!*rq->out);
232 dst = kmap_atomic(*rq->out);
238 * For the case of small output size (especially much less
239 * than PAGE_SIZE), memcpy the decompressed data rather than
240 * compressed data is preferred.
242 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
243 dst = erofs_get_pcpubuf(0);
247 rq->inplace_io = false;
248 ret = alg->decompress(rq, dst);
250 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
253 erofs_put_pcpubuf(dst);
257 ret = alg->prepare_destpages(rq, pagepool);
261 dst = page_address(*rq->out);
266 dst = erofs_vmap(rq->out, nrpages_out);
272 ret = alg->decompress(rq, dst + rq->pageofs_out);
276 else if (dst_maptype == 2)
277 erofs_vunmap(dst, nrpages_out);
281 static int shifted_decompress(const struct z_erofs_decompress_req *rq,
282 struct list_head *pagepool)
284 const unsigned int nrpages_out =
285 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
286 const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
287 unsigned char *src, *dst;
289 if (nrpages_out > 2) {
294 if (rq->out[0] == *rq->in) {
295 DBG_BUGON(nrpages_out != 1);
299 src = kmap_atomic(*rq->in);
303 dst = kmap_atomic(rq->out[0]);
304 memcpy(dst + rq->pageofs_out, src, righthalf);
307 if (rq->out[1] == *rq->in) {
308 memmove(src, src + righthalf, rq->pageofs_out);
309 } else if (nrpages_out == 2) {
312 DBG_BUGON(!rq->out[1]);
313 dst = kmap_atomic(rq->out[1]);
314 memcpy(dst, src + righthalf, rq->pageofs_out);
322 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
323 struct list_head *pagepool)
325 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
326 return shifted_decompress(rq, pagepool);
327 return decompress_generic(rq, pagepool);