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);
31 int z_erofs_load_lz4_config(struct super_block *sb,
32 struct erofs_super_block *dsb,
33 struct z_erofs_lz4_cfgs *lz4, int size)
35 struct erofs_sb_info *sbi = EROFS_SB(sb);
39 if (size < sizeof(struct z_erofs_lz4_cfgs)) {
40 erofs_err(sb, "invalid lz4 cfgs, size=%u", size);
43 distance = le16_to_cpu(lz4->max_distance);
45 sbi->lz4.max_pclusterblks = le16_to_cpu(lz4->max_pclusterblks);
46 if (!sbi->lz4.max_pclusterblks) {
47 sbi->lz4.max_pclusterblks = 1; /* reserved case */
48 } else if (sbi->lz4.max_pclusterblks >
49 Z_EROFS_PCLUSTER_MAX_SIZE / EROFS_BLKSIZ) {
50 erofs_err(sb, "too large lz4 pclusterblks %u",
51 sbi->lz4.max_pclusterblks);
53 } else if (sbi->lz4.max_pclusterblks >= 2) {
54 erofs_info(sb, "EXPERIMENTAL big pcluster feature in use. Use at your own risk!");
57 distance = le16_to_cpu(dsb->u1.lz4_max_distance);
58 sbi->lz4.max_pclusterblks = 1;
61 sbi->lz4.max_distance_pages = distance ?
62 DIV_ROUND_UP(distance, PAGE_SIZE) + 1 :
63 LZ4_MAX_DISTANCE_PAGES;
64 return erofs_pcpubuf_growsize(sbi->lz4.max_pclusterblks);
67 static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
68 struct list_head *pagepool)
70 const unsigned int nr =
71 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
72 struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
73 unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
74 BITS_PER_LONG)] = { 0 };
75 unsigned int lz4_max_distance_pages =
76 EROFS_SB(rq->sb)->lz4.max_distance_pages;
78 unsigned int i, j, top;
81 for (i = j = 0; i < nr; ++i, ++j) {
82 struct page *const page = rq->out[i];
85 if (j >= lz4_max_distance_pages)
88 /* 'valid' bounced can only be tested after a complete round */
89 if (test_bit(j, bounced)) {
90 DBG_BUGON(i < lz4_max_distance_pages);
91 DBG_BUGON(top >= lz4_max_distance_pages);
92 availables[top++] = rq->out[i - lz4_max_distance_pages];
96 __clear_bit(j, bounced);
98 if (kaddr + PAGE_SIZE == page_address(page))
103 kaddr = page_address(page);
108 __set_bit(j, bounced);
111 victim = availables[--top];
114 victim = erofs_allocpage(pagepool,
115 GFP_KERNEL | __GFP_NOFAIL);
116 set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE);
120 return kaddr ? 1 : 0;
123 static void *z_erofs_handle_inplace_io(struct z_erofs_decompress_req *rq,
124 void *inpage, unsigned int *inputmargin, int *maptype,
125 bool support_0padding)
127 unsigned int nrpages_in, nrpages_out;
128 unsigned int ofull, oend, inputsize, total, i, j;
132 inputsize = rq->inputsize;
133 nrpages_in = PAGE_ALIGN(inputsize) >> PAGE_SHIFT;
134 oend = rq->pageofs_out + rq->outputsize;
135 ofull = PAGE_ALIGN(oend);
136 nrpages_out = ofull >> PAGE_SHIFT;
138 if (rq->inplace_io) {
139 if (rq->partial_decoding || !support_0padding ||
140 ofull - oend < LZ4_DECOMPRESS_INPLACE_MARGIN(inputsize))
143 for (i = 0; i < nrpages_in; ++i) {
144 DBG_BUGON(rq->in[i] == NULL);
145 for (j = 0; j < nrpages_out - nrpages_in + i; ++j)
146 if (rq->out[j] == rq->in[i])
151 if (nrpages_in <= 1) {
155 kunmap_atomic(inpage);
157 src = erofs_vm_map_ram(rq->in, nrpages_in);
159 return ERR_PTR(-ENOMEM);
164 /* Or copy compressed data which can be overlapped to per-CPU buffer */
166 src = erofs_get_pcpubuf(nrpages_in);
169 kunmap_atomic(inpage);
170 return ERR_PTR(-EFAULT);
174 total = rq->inputsize;
176 unsigned int page_copycnt =
177 min_t(unsigned int, total, PAGE_SIZE - *inputmargin);
180 inpage = kmap_atomic(*in);
181 memcpy(tmp, inpage + *inputmargin, page_copycnt);
182 kunmap_atomic(inpage);
185 total -= page_copycnt;
193 static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
195 unsigned int inputmargin;
197 bool support_0padding;
200 DBG_BUGON(*rq->in == NULL);
201 headpage = kmap_atomic(*rq->in);
203 support_0padding = false;
205 /* decompression inplace is only safe when 0padding is enabled */
206 if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) {
207 support_0padding = true;
209 while (!headpage[inputmargin & ~PAGE_MASK])
210 if (!(++inputmargin & ~PAGE_MASK))
213 if (inputmargin >= rq->inputsize) {
214 kunmap_atomic(headpage);
219 rq->inputsize -= inputmargin;
220 src = z_erofs_handle_inplace_io(rq, headpage, &inputmargin, &maptype,
225 /* legacy format could compress extra data in a pcluster. */
226 if (rq->partial_decoding || !support_0padding)
227 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
228 rq->inputsize, rq->outputsize, rq->outputsize);
230 ret = LZ4_decompress_safe(src + inputmargin, out,
231 rq->inputsize, rq->outputsize);
233 if (ret != rq->outputsize) {
234 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
235 ret, rq->inputsize, inputmargin, rq->outputsize);
238 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
239 16, 1, src + inputmargin, rq->inputsize, true);
240 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
241 16, 1, out, rq->outputsize, true);
244 memset(out + ret, 0, rq->outputsize - ret);
250 } else if (maptype == 1) {
251 vm_unmap_ram(src, PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT);
252 } else if (maptype == 2) {
253 erofs_put_pcpubuf(src);
261 static struct z_erofs_decompressor decompressors[] = {
262 [Z_EROFS_COMPRESSION_SHIFTED] = {
265 [Z_EROFS_COMPRESSION_LZ4] = {
266 .prepare_destpages = z_erofs_lz4_prepare_destpages,
267 .decompress = z_erofs_lz4_decompress,
272 static void copy_from_pcpubuf(struct page **out, const char *dst,
273 unsigned short pageofs_out,
274 unsigned int outputsize)
276 const char *end = dst + outputsize;
277 const unsigned int righthalf = PAGE_SIZE - pageofs_out;
278 const char *cur = dst - pageofs_out;
281 struct page *const page = *out++;
284 char *buf = kmap_atomic(page);
287 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
290 memcpy(buf + pageofs_out, cur + pageofs_out,
291 min_t(uint, righthalf, end - cur));
299 static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
300 struct list_head *pagepool)
302 const unsigned int nrpages_out =
303 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
304 const struct z_erofs_decompressor *alg = decompressors + rq->alg;
305 unsigned int dst_maptype;
309 /* two optimized fast paths only for non bigpcluster cases yet */
310 if (rq->inputsize <= PAGE_SIZE) {
311 if (nrpages_out == 1 && !rq->inplace_io) {
312 DBG_BUGON(!*rq->out);
313 dst = kmap_atomic(*rq->out);
319 * For the case of small output size (especially much less
320 * than PAGE_SIZE), memcpy the decompressed data rather than
321 * compressed data is preferred.
323 if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
324 dst = erofs_get_pcpubuf(1);
328 rq->inplace_io = false;
329 ret = alg->decompress(rq, dst);
331 copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
334 erofs_put_pcpubuf(dst);
339 /* general decoding path which can be used for all cases */
340 ret = alg->prepare_destpages(rq, pagepool);
344 dst = page_address(*rq->out);
349 dst = erofs_vm_map_ram(rq->out, nrpages_out);
355 ret = alg->decompress(rq, dst + rq->pageofs_out);
359 else if (dst_maptype == 2)
360 vm_unmap_ram(dst, nrpages_out);
364 static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
365 struct list_head *pagepool)
367 const unsigned int nrpages_out =
368 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
369 const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
370 unsigned char *src, *dst;
372 if (nrpages_out > 2) {
377 if (rq->out[0] == *rq->in) {
378 DBG_BUGON(nrpages_out != 1);
382 src = kmap_atomic(*rq->in);
384 dst = kmap_atomic(rq->out[0]);
385 memcpy(dst + rq->pageofs_out, src, righthalf);
389 if (nrpages_out == 2) {
390 DBG_BUGON(!rq->out[1]);
391 if (rq->out[1] == *rq->in) {
392 memmove(src, src + righthalf, rq->pageofs_out);
394 dst = kmap_atomic(rq->out[1]);
395 memcpy(dst, src + righthalf, rq->pageofs_out);
403 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
404 struct list_head *pagepool)
406 if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
407 return z_erofs_shifted_transform(rq, pagepool);
408 return z_erofs_decompress_generic(rq, pagepool);