Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 3 | * Copyright (C) 2019 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
| 6 | */ |
| 7 | #include "compress.h" |
Gao Xiang | 46c2d14 | 2019-07-31 23:57:44 +0800 | [diff] [blame] | 8 | #include <linux/module.h> |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 9 | #include <linux/lz4.h> |
| 10 | |
| 11 | #ifndef LZ4_DISTANCE_MAX /* history window size */ |
| 12 | #define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ |
| 13 | #endif |
| 14 | |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 15 | #define LZ4_MAX_DISTANCE_PAGES (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1) |
Gao Xiang | 0ffd71b | 2019-06-24 15:22:56 +0800 | [diff] [blame] | 16 | #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN |
| 17 | #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize) (((srcsize) >> 8) + 32) |
| 18 | #endif |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 19 | |
| 20 | struct z_erofs_decompressor { |
| 21 | /* |
| 22 | * if destpages have sparsed pages, fill them with bounce pages. |
| 23 | * it also check whether destpages indicate continuous physical memory. |
| 24 | */ |
| 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); |
| 28 | char *name; |
| 29 | }; |
| 30 | |
Huang Jianan | 5d50538 | 2021-03-29 09:23:06 +0800 | [diff] [blame^] | 31 | int z_erofs_load_lz4_config(struct super_block *sb, |
| 32 | struct erofs_super_block *dsb) |
| 33 | { |
| 34 | u16 distance = le16_to_cpu(dsb->lz4_max_distance); |
| 35 | |
| 36 | EROFS_SB(sb)->lz4.max_distance_pages = distance ? |
| 37 | DIV_ROUND_UP(distance, PAGE_SIZE) + 1 : |
| 38 | LZ4_MAX_DISTANCE_PAGES; |
| 39 | return 0; |
| 40 | } |
| 41 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 42 | static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq, |
| 43 | struct list_head *pagepool) |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 44 | { |
| 45 | const unsigned int nr = |
| 46 | PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; |
| 47 | struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL }; |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 48 | unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES, |
| 49 | BITS_PER_LONG)] = { 0 }; |
Huang Jianan | 5d50538 | 2021-03-29 09:23:06 +0800 | [diff] [blame^] | 50 | unsigned int lz4_max_distance_pages = |
| 51 | EROFS_SB(rq->sb)->lz4.max_distance_pages; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 52 | void *kaddr = NULL; |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 53 | unsigned int i, j, top; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 54 | |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 55 | top = 0; |
| 56 | for (i = j = 0; i < nr; ++i, ++j) { |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 57 | struct page *const page = rq->out[i]; |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 58 | struct page *victim; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 59 | |
Huang Jianan | 5d50538 | 2021-03-29 09:23:06 +0800 | [diff] [blame^] | 60 | if (j >= lz4_max_distance_pages) |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 61 | j = 0; |
| 62 | |
| 63 | /* 'valid' bounced can only be tested after a complete round */ |
| 64 | if (test_bit(j, bounced)) { |
Huang Jianan | 5d50538 | 2021-03-29 09:23:06 +0800 | [diff] [blame^] | 65 | DBG_BUGON(i < lz4_max_distance_pages); |
| 66 | DBG_BUGON(top >= lz4_max_distance_pages); |
| 67 | availables[top++] = rq->out[i - lz4_max_distance_pages]; |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 68 | } |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 69 | |
| 70 | if (page) { |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 71 | __clear_bit(j, bounced); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 72 | if (kaddr) { |
| 73 | if (kaddr + PAGE_SIZE == page_address(page)) |
| 74 | kaddr += PAGE_SIZE; |
| 75 | else |
| 76 | kaddr = NULL; |
| 77 | } else if (!i) { |
| 78 | kaddr = page_address(page); |
| 79 | } |
| 80 | continue; |
| 81 | } |
| 82 | kaddr = NULL; |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 83 | __set_bit(j, bounced); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 84 | |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 85 | if (top) { |
| 86 | victim = availables[--top]; |
| 87 | get_page(victim); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 88 | } else { |
Huang Jianan | b4892fa | 2021-03-16 11:15:14 +0800 | [diff] [blame] | 89 | victim = erofs_allocpage(pagepool, |
| 90 | GFP_KERNEL | __GFP_NOFAIL); |
Gao Xiang | 6aaa7b0 | 2020-12-08 17:58:32 +0800 | [diff] [blame] | 91 | set_page_private(victim, Z_EROFS_SHORTLIVED_PAGE); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 92 | } |
Gao Xiang | af89bce | 2019-07-03 14:52:09 +0800 | [diff] [blame] | 93 | rq->out[i] = victim; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 94 | } |
| 95 | return kaddr ? 1 : 0; |
| 96 | } |
| 97 | |
| 98 | static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq, |
| 99 | u8 *src, unsigned int pageofs_in) |
| 100 | { |
| 101 | /* |
| 102 | * if in-place decompression is ongoing, those decompressed |
| 103 | * pages should be copied in order to avoid being overlapped. |
| 104 | */ |
| 105 | struct page **in = rq->in; |
| 106 | u8 *const tmp = erofs_get_pcpubuf(0); |
| 107 | u8 *tmpp = tmp; |
| 108 | unsigned int inlen = rq->inputsize - pageofs_in; |
| 109 | unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in); |
| 110 | |
| 111 | while (tmpp < tmp + inlen) { |
| 112 | if (!src) |
| 113 | src = kmap_atomic(*in); |
| 114 | memcpy(tmpp, src + pageofs_in, count); |
| 115 | kunmap_atomic(src); |
| 116 | src = NULL; |
| 117 | tmpp += count; |
| 118 | pageofs_in = 0; |
| 119 | count = PAGE_SIZE; |
| 120 | ++in; |
| 121 | } |
| 122 | return tmp; |
| 123 | } |
| 124 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 125 | static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out) |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 126 | { |
| 127 | unsigned int inputmargin, inlen; |
| 128 | u8 *src; |
Gao Xiang | 0ffd71b | 2019-06-24 15:22:56 +0800 | [diff] [blame] | 129 | bool copied, support_0padding; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 130 | int ret; |
| 131 | |
| 132 | if (rq->inputsize > PAGE_SIZE) |
Gao Xiang | ff784a7 | 2019-08-14 18:37:05 +0800 | [diff] [blame] | 133 | return -EOPNOTSUPP; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 134 | |
| 135 | src = kmap_atomic(*rq->in); |
| 136 | inputmargin = 0; |
Gao Xiang | 0ffd71b | 2019-06-24 15:22:56 +0800 | [diff] [blame] | 137 | support_0padding = false; |
| 138 | |
| 139 | /* decompression inplace is only safe when 0padding is enabled */ |
Gao Xiang | de06a6a | 2021-03-29 09:23:05 +0800 | [diff] [blame] | 140 | if (erofs_sb_has_lz4_0padding(EROFS_SB(rq->sb))) { |
Gao Xiang | 0ffd71b | 2019-06-24 15:22:56 +0800 | [diff] [blame] | 141 | support_0padding = true; |
| 142 | |
| 143 | while (!src[inputmargin & ~PAGE_MASK]) |
| 144 | if (!(++inputmargin & ~PAGE_MASK)) |
| 145 | break; |
| 146 | |
| 147 | if (inputmargin >= rq->inputsize) { |
| 148 | kunmap_atomic(src); |
| 149 | return -EIO; |
| 150 | } |
| 151 | } |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 152 | |
| 153 | copied = false; |
| 154 | inlen = rq->inputsize - inputmargin; |
| 155 | if (rq->inplace_io) { |
Gao Xiang | 0ffd71b | 2019-06-24 15:22:56 +0800 | [diff] [blame] | 156 | const uint oend = (rq->pageofs_out + |
| 157 | rq->outputsize) & ~PAGE_MASK; |
| 158 | const uint nr = PAGE_ALIGN(rq->pageofs_out + |
| 159 | rq->outputsize) >> PAGE_SHIFT; |
| 160 | |
| 161 | if (rq->partial_decoding || !support_0padding || |
| 162 | rq->out[nr - 1] != rq->in[0] || |
| 163 | rq->inputsize - oend < |
| 164 | LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) { |
| 165 | src = generic_copy_inplace_data(rq, src, inputmargin); |
| 166 | inputmargin = 0; |
| 167 | copied = true; |
| 168 | } |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Gao Xiang | af1038a | 2020-02-26 16:10:07 +0800 | [diff] [blame] | 171 | /* legacy format could compress extra data in a pcluster. */ |
| 172 | if (rq->partial_decoding || !support_0padding) |
| 173 | ret = LZ4_decompress_safe_partial(src + inputmargin, out, |
| 174 | inlen, rq->outputsize, |
| 175 | rq->outputsize); |
| 176 | else |
| 177 | ret = LZ4_decompress_safe(src + inputmargin, out, |
| 178 | inlen, rq->outputsize); |
| 179 | |
Gao Xiang | aa99a76 | 2020-02-26 16:10:08 +0800 | [diff] [blame] | 180 | if (ret != rq->outputsize) { |
| 181 | erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]", |
| 182 | ret, inlen, inputmargin, rq->outputsize); |
| 183 | |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 184 | WARN_ON(1); |
| 185 | print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET, |
| 186 | 16, 1, src + inputmargin, inlen, true); |
| 187 | print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET, |
| 188 | 16, 1, out, rq->outputsize, true); |
Gao Xiang | aa99a76 | 2020-02-26 16:10:08 +0800 | [diff] [blame] | 189 | |
| 190 | if (ret >= 0) |
| 191 | memset(out + ret, 0, rq->outputsize - ret); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 192 | ret = -EIO; |
| 193 | } |
| 194 | |
| 195 | if (copied) |
| 196 | erofs_put_pcpubuf(src); |
| 197 | else |
| 198 | kunmap_atomic(src); |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static struct z_erofs_decompressor decompressors[] = { |
| 203 | [Z_EROFS_COMPRESSION_SHIFTED] = { |
| 204 | .name = "shifted" |
| 205 | }, |
| 206 | [Z_EROFS_COMPRESSION_LZ4] = { |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 207 | .prepare_destpages = z_erofs_lz4_prepare_destpages, |
| 208 | .decompress = z_erofs_lz4_decompress, |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 209 | .name = "lz4" |
| 210 | }, |
| 211 | }; |
| 212 | |
| 213 | static void copy_from_pcpubuf(struct page **out, const char *dst, |
| 214 | unsigned short pageofs_out, |
| 215 | unsigned int outputsize) |
| 216 | { |
| 217 | const char *end = dst + outputsize; |
| 218 | const unsigned int righthalf = PAGE_SIZE - pageofs_out; |
| 219 | const char *cur = dst - pageofs_out; |
| 220 | |
| 221 | while (cur < end) { |
| 222 | struct page *const page = *out++; |
| 223 | |
| 224 | if (page) { |
| 225 | char *buf = kmap_atomic(page); |
| 226 | |
| 227 | if (cur >= dst) { |
| 228 | memcpy(buf, cur, min_t(uint, PAGE_SIZE, |
| 229 | end - cur)); |
| 230 | } else { |
| 231 | memcpy(buf + pageofs_out, cur + pageofs_out, |
| 232 | min_t(uint, righthalf, end - cur)); |
| 233 | } |
| 234 | kunmap_atomic(buf); |
| 235 | } |
| 236 | cur += PAGE_SIZE; |
| 237 | } |
| 238 | } |
| 239 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 240 | static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq, |
| 241 | struct list_head *pagepool) |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 242 | { |
| 243 | const unsigned int nrpages_out = |
| 244 | PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; |
| 245 | const struct z_erofs_decompressor *alg = decompressors + rq->alg; |
| 246 | unsigned int dst_maptype; |
| 247 | void *dst; |
Gao Xiang | 73d0393 | 2019-09-04 10:09:07 +0800 | [diff] [blame] | 248 | int ret, i; |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 249 | |
| 250 | if (nrpages_out == 1 && !rq->inplace_io) { |
| 251 | DBG_BUGON(!*rq->out); |
| 252 | dst = kmap_atomic(*rq->out); |
| 253 | dst_maptype = 0; |
| 254 | goto dstmap_out; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * For the case of small output size (especially much less |
| 259 | * than PAGE_SIZE), memcpy the decompressed data rather than |
| 260 | * compressed data is preferred. |
| 261 | */ |
| 262 | if (rq->outputsize <= PAGE_SIZE * 7 / 8) { |
| 263 | dst = erofs_get_pcpubuf(0); |
| 264 | if (IS_ERR(dst)) |
| 265 | return PTR_ERR(dst); |
| 266 | |
| 267 | rq->inplace_io = false; |
| 268 | ret = alg->decompress(rq, dst); |
| 269 | if (!ret) |
| 270 | copy_from_pcpubuf(rq->out, dst, rq->pageofs_out, |
| 271 | rq->outputsize); |
| 272 | |
| 273 | erofs_put_pcpubuf(dst); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | ret = alg->prepare_destpages(rq, pagepool); |
| 278 | if (ret < 0) { |
| 279 | return ret; |
| 280 | } else if (ret) { |
| 281 | dst = page_address(*rq->out); |
| 282 | dst_maptype = 1; |
| 283 | goto dstmap_out; |
| 284 | } |
| 285 | |
Gao Xiang | 73d0393 | 2019-09-04 10:09:07 +0800 | [diff] [blame] | 286 | i = 0; |
| 287 | while (1) { |
Christoph Hellwig | d4efd79 | 2020-06-01 21:51:27 -0700 | [diff] [blame] | 288 | dst = vm_map_ram(rq->out, nrpages_out, -1); |
Gao Xiang | 73d0393 | 2019-09-04 10:09:07 +0800 | [diff] [blame] | 289 | |
| 290 | /* retry two more times (totally 3 times) */ |
| 291 | if (dst || ++i >= 3) |
| 292 | break; |
| 293 | vm_unmap_aliases(); |
| 294 | } |
| 295 | |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 296 | if (!dst) |
| 297 | return -ENOMEM; |
Gao Xiang | 73d0393 | 2019-09-04 10:09:07 +0800 | [diff] [blame] | 298 | |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 299 | dst_maptype = 2; |
| 300 | |
| 301 | dstmap_out: |
| 302 | ret = alg->decompress(rq, dst + rq->pageofs_out); |
| 303 | |
| 304 | if (!dst_maptype) |
| 305 | kunmap_atomic(dst); |
| 306 | else if (dst_maptype == 2) |
Gao Xiang | 73d0393 | 2019-09-04 10:09:07 +0800 | [diff] [blame] | 307 | vm_unmap_ram(dst, nrpages_out); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 308 | return ret; |
| 309 | } |
| 310 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 311 | static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq, |
| 312 | struct list_head *pagepool) |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 313 | { |
| 314 | const unsigned int nrpages_out = |
| 315 | PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT; |
| 316 | const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out; |
| 317 | unsigned char *src, *dst; |
| 318 | |
| 319 | if (nrpages_out > 2) { |
| 320 | DBG_BUGON(1); |
| 321 | return -EIO; |
| 322 | } |
| 323 | |
| 324 | if (rq->out[0] == *rq->in) { |
| 325 | DBG_BUGON(nrpages_out != 1); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | src = kmap_atomic(*rq->in); |
Gao Xiang | 4d20243 | 2020-01-07 10:25:46 +0800 | [diff] [blame] | 330 | if (rq->out[0]) { |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 331 | dst = kmap_atomic(rq->out[0]); |
| 332 | memcpy(dst + rq->pageofs_out, src, righthalf); |
Gao Xiang | 4d20243 | 2020-01-07 10:25:46 +0800 | [diff] [blame] | 333 | kunmap_atomic(dst); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 334 | } |
| 335 | |
Gao Xiang | 4d20243 | 2020-01-07 10:25:46 +0800 | [diff] [blame] | 336 | if (nrpages_out == 2) { |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 337 | DBG_BUGON(!rq->out[1]); |
Gao Xiang | 4d20243 | 2020-01-07 10:25:46 +0800 | [diff] [blame] | 338 | if (rq->out[1] == *rq->in) { |
| 339 | memmove(src, src + righthalf, rq->pageofs_out); |
| 340 | } else { |
| 341 | dst = kmap_atomic(rq->out[1]); |
| 342 | memcpy(dst, src + righthalf, rq->pageofs_out); |
| 343 | kunmap_atomic(dst); |
| 344 | } |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 345 | } |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 346 | kunmap_atomic(src); |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int z_erofs_decompress(struct z_erofs_decompress_req *rq, |
| 351 | struct list_head *pagepool) |
| 352 | { |
| 353 | if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 354 | return z_erofs_shifted_transform(rq, pagepool); |
| 355 | return z_erofs_decompress_generic(rq, pagepool); |
Gao Xiang | 7fc45db | 2019-06-24 15:22:55 +0800 | [diff] [blame] | 356 | } |
| 357 | |