1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
8 #include <linux/module.h>
11 #ifndef LZ4_DISTANCE_MAX /* history window size */
12 #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
15 #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
16 #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
17 #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32)
20 struct z_erofs_decompressor {
22 * if destpages have sparsed pages, fill them with bounce pages.
23 * it also check whether destpages indicate continuous physical memory.
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,
32 static int z_erofs_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);
60 if (!PageHighMem(page)) {
62 kaddr = page_address(page);
66 kaddr + PAGE_SIZE == page_address(page)) {
75 __set_bit(j, bounced);
78 victim = availables[--top];
81 victim = erofs_allocpage(pagepool, GFP_KERNEL);
84 victim->mapping = Z_EROFS_MAPPING_STAGING;
91 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
92 u8 *src, unsigned int pageofs_in)
95 * if in-place decompression is ongoing, those decompressed
96 * pages should be copied in order to avoid being overlapped.
98 struct page **in = rq->in;
99 u8 *const tmp = erofs_get_pcpubuf(0);
101 unsigned int inlen = rq->inputsize - pageofs_in;
102 unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
104 while (tmpp < tmp + inlen) {
106 src = kmap_atomic(*in);
107 memcpy(tmpp, src + pageofs_in, count);
118 static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out,
121 const uint nrpages_out = PAGE_ALIGN(rq->pageofs_out +
122 rq->outputsize) >> PAGE_SHIFT;
123 unsigned int inputmargin, inlen;
125 bool copied, support_0padding;
128 if (rq->inputsize > PAGE_SIZE)
131 src = kmap_atomic(*rq->in);
134 support_0padding = false;
136 /* decompression inplace is only safe when 0padding is enabled */
137 if (EROFS_SB(rq->sb)->feature_incompat &
138 EROFS_FEATURE_INCOMPAT_LZ4_0PADDING) {
139 support_0padding = true;
141 while (!src[inputmargin & ~PAGE_MASK])
142 if (!(++inputmargin & ~PAGE_MASK))
145 if (inputmargin >= rq->inputsize) {
152 inlen = rq->inputsize - inputmargin;
153 if (rq->inplace_io) {
154 const uint oend = (rq->pageofs_out +
155 rq->outputsize) & ~PAGE_MASK;
156 if (rq->partial_decoding || !support_0padding ||
157 rq->out[nrpages_out - 1] != rq->in[0] ||
158 rq->inputsize - oend <
159 LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
160 src = generic_copy_inplace_data(rq, src, inputmargin);
164 src = obase + ((nrpages_out - 1) << PAGE_SHIFT);
168 /* legacy format could compress extra data in a pcluster. */
169 if (rq->partial_decoding || !support_0padding)
170 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
171 inlen, rq->outputsize,
174 ret = LZ4_decompress_safe(src + inputmargin, out,
175 inlen, rq->outputsize);
177 if (ret != rq->outputsize) {
178 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
179 ret, inlen, inputmargin, rq->outputsize);
181 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
182 16, 1, src + inputmargin, inlen, true);
183 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
184 16, 1, out, rq->outputsize, true);
187 memset(out + ret, 0, rq->outputsize - ret);
192 erofs_put_pcpubuf(src);
198 static struct z_erofs_decompressor decompressors[] = {
199 [Z_EROFS_COMPRESSION_SHIFTED] = {
202 [Z_EROFS_COMPRESSION_LZ4] = {
203 .prepare_destpages = z_erofs_lz4_prepare_destpages,
204 .decompress = z_erofs_lz4_decompress,
209 static void copy_from_pcpubuf(struct page **out, const char *dst,
210 unsigned short pageofs_out,
211 unsigned int outputsize)
213 const char *end = dst + outputsize;
214 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
215 const char *cur = dst - pageofs_out;
218 struct page *const page = *out++;
221 char *buf = kmap_atomic(page);
224 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
227 memcpy(buf + pageofs_out, cur + pageofs_out,
228 min_t(uint, righthalf, end - cur));
236 static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
237 struct list_head *pagepool)
239 const unsigned int nrpages_out =
240 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
241 const struct z_erofs_decompressor *alg = decompressors + rq->alg;
242 unsigned int dst_maptype;
246 if (nrpages_out == 1 && !rq->inplace_io) {
247 DBG_BUGON(!*rq->out);
248 dst = kmap_atomic(*rq->out);
254 * For the case of small output size (especially much less
255 * than PAGE_SIZE), memcpy the decompressed data rather than
256 * compressed data is preferred.
258 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
259 dst = erofs_get_pcpubuf(0);
263 rq->inplace_io = false;
264 ret = alg->decompress(rq, dst, NULL);
266 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
269 erofs_put_pcpubuf(dst);
273 ret = alg->prepare_destpages(rq, pagepool);
277 dst = page_address(*rq->out);
284 dst = vm_map_ram(rq->out, nrpages_out, -1);
286 /* retry two more times (totally 3 times) */
298 ret = alg->decompress(rq, dst + rq->pageofs_out, dst);
302 else if (dst_maptype == 2)
303 vm_unmap_ram(dst, nrpages_out);
307 static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
308 struct list_head *pagepool)
310 const unsigned int nrpages_out =
311 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
312 const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
313 unsigned char *src, *dst;
315 if (nrpages_out > 2) {
320 if (rq->out[0] == *rq->in) {
321 DBG_BUGON(nrpages_out != 1);
325 src = kmap_atomic(*rq->in);
327 dst = kmap_atomic(rq->out[0]);
328 memcpy(dst + rq->pageofs_out, src, righthalf);
332 if (nrpages_out == 2) {
333 DBG_BUGON(!rq->out[1]);
334 if (rq->out[1] == *rq->in) {
335 memmove(src, src + righthalf, rq->pageofs_out);
337 dst = kmap_atomic(rq->out[1]);
338 memcpy(dst, src + righthalf, rq->pageofs_out);
346 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
347 struct list_head *pagepool)
349 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
350 return z_erofs_shifted_transform(rq, pagepool);
351 return z_erofs_decompress_generic(rq, pagepool);