1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2022 Alibaba Cloud 6 */ 7 #include "zdata.h" 8 #include "compress.h" 9 #include <linux/prefetch.h> 10 11 #include <trace/events/erofs.h> 12 13 /* 14 * since pclustersize is variable for big pcluster feature, introduce slab 15 * pools implementation for different pcluster sizes. 16 */ 17 struct z_erofs_pcluster_slab { 18 struct kmem_cache *slab; 19 unsigned int maxpages; 20 char name[48]; 21 }; 22 23 #define _PCLP(n) { .maxpages = n } 24 25 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 26 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 27 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) 28 }; 29 30 struct z_erofs_bvec_iter { 31 struct page *bvpage; 32 struct z_erofs_bvset *bvset; 33 unsigned int nr, cur; 34 }; 35 36 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter) 37 { 38 if (iter->bvpage) 39 kunmap_local(iter->bvset); 40 return iter->bvpage; 41 } 42 43 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter) 44 { 45 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec; 46 /* have to access nextpage in advance, otherwise it will be unmapped */ 47 struct page *nextpage = iter->bvset->nextpage; 48 struct page *oldpage; 49 50 DBG_BUGON(!nextpage); 51 oldpage = z_erofs_bvec_iter_end(iter); 52 iter->bvpage = nextpage; 53 iter->bvset = kmap_local_page(nextpage); 54 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec); 55 iter->cur = 0; 56 return oldpage; 57 } 58 59 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter, 60 struct z_erofs_bvset_inline *bvset, 61 unsigned int bootstrap_nr, 62 unsigned int cur) 63 { 64 *iter = (struct z_erofs_bvec_iter) { 65 .nr = bootstrap_nr, 66 .bvset = (struct z_erofs_bvset *)bvset, 67 }; 68 69 while (cur > iter->nr) { 70 cur -= iter->nr; 71 z_erofs_bvset_flip(iter); 72 } 73 iter->cur = cur; 74 } 75 76 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter, 77 struct z_erofs_bvec *bvec, 78 struct page **candidate_bvpage) 79 { 80 if (iter->cur == iter->nr) { 81 if (!*candidate_bvpage) 82 return -EAGAIN; 83 84 DBG_BUGON(iter->bvset->nextpage); 85 iter->bvset->nextpage = *candidate_bvpage; 86 z_erofs_bvset_flip(iter); 87 88 iter->bvset->nextpage = NULL; 89 *candidate_bvpage = NULL; 90 } 91 iter->bvset->bvec[iter->cur++] = *bvec; 92 return 0; 93 } 94 95 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter, 96 struct z_erofs_bvec *bvec, 97 struct page **old_bvpage) 98 { 99 if (iter->cur == iter->nr) 100 *old_bvpage = z_erofs_bvset_flip(iter); 101 else 102 *old_bvpage = NULL; 103 *bvec = iter->bvset->bvec[iter->cur++]; 104 } 105 106 static void z_erofs_destroy_pcluster_pool(void) 107 { 108 int i; 109 110 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 111 if (!pcluster_pool[i].slab) 112 continue; 113 kmem_cache_destroy(pcluster_pool[i].slab); 114 pcluster_pool[i].slab = NULL; 115 } 116 } 117 118 static int z_erofs_create_pcluster_pool(void) 119 { 120 struct z_erofs_pcluster_slab *pcs; 121 struct z_erofs_pcluster *a; 122 unsigned int size; 123 124 for (pcs = pcluster_pool; 125 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 126 size = struct_size(a, compressed_bvecs, pcs->maxpages); 127 128 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 129 pcs->slab = kmem_cache_create(pcs->name, size, 0, 130 SLAB_RECLAIM_ACCOUNT, NULL); 131 if (pcs->slab) 132 continue; 133 134 z_erofs_destroy_pcluster_pool(); 135 return -ENOMEM; 136 } 137 return 0; 138 } 139 140 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) 141 { 142 int i; 143 144 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 145 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 146 struct z_erofs_pcluster *pcl; 147 148 if (nrpages > pcs->maxpages) 149 continue; 150 151 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); 152 if (!pcl) 153 return ERR_PTR(-ENOMEM); 154 pcl->pclusterpages = nrpages; 155 return pcl; 156 } 157 return ERR_PTR(-EINVAL); 158 } 159 160 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 161 { 162 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 163 int i; 164 165 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 166 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 167 168 if (pclusterpages > pcs->maxpages) 169 continue; 170 171 kmem_cache_free(pcs->slab, pcl); 172 return; 173 } 174 DBG_BUGON(1); 175 } 176 177 /* how to allocate cached pages for a pcluster */ 178 enum z_erofs_cache_alloctype { 179 DONTALLOC, /* don't allocate any cached pages */ 180 /* 181 * try to use cached I/O if page allocation succeeds or fallback 182 * to in-place I/O instead to avoid any direct reclaim. 183 */ 184 TRYALLOC, 185 }; 186 187 /* 188 * tagged pointer with 1-bit tag for all compressed pages 189 * tag 0 - the page is just found with an extra page reference 190 */ 191 typedef tagptr1_t compressed_page_t; 192 193 #define tag_compressed_page_justfound(page) \ 194 tagptr_fold(compressed_page_t, page, 1) 195 196 static struct workqueue_struct *z_erofs_workqueue __read_mostly; 197 198 void z_erofs_exit_zip_subsystem(void) 199 { 200 destroy_workqueue(z_erofs_workqueue); 201 z_erofs_destroy_pcluster_pool(); 202 } 203 204 static inline int z_erofs_init_workqueue(void) 205 { 206 const unsigned int onlinecpus = num_possible_cpus(); 207 208 /* 209 * no need to spawn too many threads, limiting threads could minimum 210 * scheduling overhead, perhaps per-CPU threads should be better? 211 */ 212 z_erofs_workqueue = alloc_workqueue("erofs_unzipd", 213 WQ_UNBOUND | WQ_HIGHPRI, 214 onlinecpus + onlinecpus / 4); 215 return z_erofs_workqueue ? 0 : -ENOMEM; 216 } 217 218 int __init z_erofs_init_zip_subsystem(void) 219 { 220 int err = z_erofs_create_pcluster_pool(); 221 222 if (err) 223 return err; 224 err = z_erofs_init_workqueue(); 225 if (err) 226 z_erofs_destroy_pcluster_pool(); 227 return err; 228 } 229 230 enum z_erofs_pclustermode { 231 Z_EROFS_PCLUSTER_INFLIGHT, 232 /* 233 * The current pclusters was the tail of an exist chain, in addition 234 * that the previous processed chained pclusters are all decided to 235 * be hooked up to it. 236 * A new chain will be created for the remaining pclusters which are 237 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED, 238 * the next pcluster cannot reuse the whole page safely for inplace I/O 239 * in the following scenario: 240 * ________________________________________________________________ 241 * | tail (partial) page | head (partial) page | 242 * | (belongs to the next pcl) | (belongs to the current pcl) | 243 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________| 244 */ 245 Z_EROFS_PCLUSTER_HOOKED, 246 /* 247 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it 248 * could be dispatched into bypass queue later due to uptodated managed 249 * pages. All related online pages cannot be reused for inplace I/O (or 250 * bvpage) since it can be directly decoded without I/O submission. 251 */ 252 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE, 253 /* 254 * The current collection has been linked with the owned chain, and 255 * could also be linked with the remaining collections, which means 256 * if the processing page is the tail page of the collection, thus 257 * the current collection can safely use the whole page (since 258 * the previous collection is under control) for in-place I/O, as 259 * illustrated below: 260 * ________________________________________________________________ 261 * | tail (partial) page | head (partial) page | 262 * | (of the current cl) | (of the previous collection) | 263 * | PCLUSTER_FOLLOWED or | | 264 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________| 265 * 266 * [ (*) the above page can be used as inplace I/O. ] 267 */ 268 Z_EROFS_PCLUSTER_FOLLOWED, 269 }; 270 271 struct z_erofs_decompress_frontend { 272 struct inode *const inode; 273 struct erofs_map_blocks map; 274 struct z_erofs_bvec_iter biter; 275 276 struct page *candidate_bvpage; 277 struct z_erofs_pcluster *pcl, *tailpcl; 278 z_erofs_next_pcluster_t owned_head; 279 enum z_erofs_pclustermode mode; 280 281 bool readahead; 282 /* used for applying cache strategy on the fly */ 283 bool backmost; 284 erofs_off_t headoffset; 285 286 /* a pointer used to pick up inplace I/O pages */ 287 unsigned int icur; 288 }; 289 290 #define DECOMPRESS_FRONTEND_INIT(__i) { \ 291 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 292 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } 293 294 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe, 295 enum z_erofs_cache_alloctype type, 296 struct page **pagepool) 297 { 298 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 299 struct z_erofs_pcluster *pcl = fe->pcl; 300 bool standalone = true; 301 /* 302 * optimistic allocation without direct reclaim since inplace I/O 303 * can be used if low memory otherwise. 304 */ 305 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 306 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 307 unsigned int i; 308 309 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 310 return; 311 312 for (i = 0; i < pcl->pclusterpages; ++i) { 313 struct page *page; 314 compressed_page_t t; 315 struct page *newpage = NULL; 316 317 /* the compressed page was loaded before */ 318 if (READ_ONCE(pcl->compressed_bvecs[i].page)) 319 continue; 320 321 page = find_get_page(mc, pcl->obj.index + i); 322 323 if (page) { 324 t = tag_compressed_page_justfound(page); 325 } else { 326 /* I/O is needed, no possible to decompress directly */ 327 standalone = false; 328 switch (type) { 329 case TRYALLOC: 330 newpage = erofs_allocpage(pagepool, gfp); 331 if (!newpage) 332 continue; 333 set_page_private(newpage, 334 Z_EROFS_PREALLOCATED_PAGE); 335 t = tag_compressed_page_justfound(newpage); 336 break; 337 default: /* DONTALLOC */ 338 continue; 339 } 340 } 341 342 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, 343 tagptr_cast_ptr(t))) 344 continue; 345 346 if (page) 347 put_page(page); 348 else if (newpage) 349 erofs_pagepool_add(pagepool, newpage); 350 } 351 352 /* 353 * don't do inplace I/O if all compressed pages are available in 354 * managed cache since it can be moved to the bypass queue instead. 355 */ 356 if (standalone) 357 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 358 } 359 360 /* called by erofs_shrinker to get rid of all compressed_pages */ 361 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 362 struct erofs_workgroup *grp) 363 { 364 struct z_erofs_pcluster *const pcl = 365 container_of(grp, struct z_erofs_pcluster, obj); 366 int i; 367 368 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 369 /* 370 * refcount of workgroup is now freezed as 1, 371 * therefore no need to worry about available decompression users. 372 */ 373 for (i = 0; i < pcl->pclusterpages; ++i) { 374 struct page *page = pcl->compressed_bvecs[i].page; 375 376 if (!page) 377 continue; 378 379 /* block other users from reclaiming or migrating the page */ 380 if (!trylock_page(page)) 381 return -EBUSY; 382 383 if (!erofs_page_is_managed(sbi, page)) 384 continue; 385 386 /* barrier is implied in the following 'unlock_page' */ 387 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 388 detach_page_private(page); 389 unlock_page(page); 390 } 391 return 0; 392 } 393 394 int erofs_try_to_free_cached_page(struct page *page) 395 { 396 struct z_erofs_pcluster *const pcl = (void *)page_private(page); 397 int ret, i; 398 399 if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1)) 400 return 0; 401 402 ret = 0; 403 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 404 for (i = 0; i < pcl->pclusterpages; ++i) { 405 if (pcl->compressed_bvecs[i].page == page) { 406 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 407 ret = 1; 408 break; 409 } 410 } 411 erofs_workgroup_unfreeze(&pcl->obj, 1); 412 if (ret) 413 detach_page_private(page); 414 return ret; 415 } 416 417 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 418 struct z_erofs_bvec *bvec) 419 { 420 struct z_erofs_pcluster *const pcl = fe->pcl; 421 422 while (fe->icur > 0) { 423 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, 424 NULL, bvec->page)) { 425 pcl->compressed_bvecs[fe->icur] = *bvec; 426 return true; 427 } 428 } 429 return false; 430 } 431 432 /* callers must be with pcluster lock held */ 433 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 434 struct z_erofs_bvec *bvec, bool exclusive) 435 { 436 int ret; 437 438 if (exclusive) { 439 /* give priority for inplaceio to use file pages first */ 440 if (z_erofs_try_inplace_io(fe, bvec)) 441 return 0; 442 /* otherwise, check if it can be used as a bvpage */ 443 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 444 !fe->candidate_bvpage) 445 fe->candidate_bvpage = bvec->page; 446 } 447 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage); 448 fe->pcl->vcnt += (ret >= 0); 449 return ret; 450 } 451 452 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 453 { 454 struct z_erofs_pcluster *pcl = f->pcl; 455 z_erofs_next_pcluster_t *owned_head = &f->owned_head; 456 457 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 458 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 459 *owned_head) == Z_EROFS_PCLUSTER_NIL) { 460 *owned_head = &pcl->next; 461 /* so we can attach this pcluster to our submission chain. */ 462 f->mode = Z_EROFS_PCLUSTER_FOLLOWED; 463 return; 464 } 465 466 /* 467 * type 2, link to the end of an existing open chain, be careful 468 * that its submission is controlled by the original attached chain. 469 */ 470 if (*owned_head != &pcl->next && pcl != f->tailpcl && 471 cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 472 *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 473 *owned_head = Z_EROFS_PCLUSTER_TAIL; 474 f->mode = Z_EROFS_PCLUSTER_HOOKED; 475 f->tailpcl = NULL; 476 return; 477 } 478 /* type 3, it belongs to a chain, but it isn't the end of the chain */ 479 f->mode = Z_EROFS_PCLUSTER_INFLIGHT; 480 } 481 482 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 483 { 484 struct erofs_map_blocks *map = &fe->map; 485 bool ztailpacking = map->m_flags & EROFS_MAP_META; 486 struct z_erofs_pcluster *pcl; 487 struct erofs_workgroup *grp; 488 int err; 489 490 if (!(map->m_flags & EROFS_MAP_ENCODED)) { 491 DBG_BUGON(1); 492 return -EFSCORRUPTED; 493 } 494 495 /* no available pcluster, let's allocate one */ 496 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 497 map->m_plen >> PAGE_SHIFT); 498 if (IS_ERR(pcl)) 499 return PTR_ERR(pcl); 500 501 atomic_set(&pcl->obj.refcount, 1); 502 pcl->algorithmformat = map->m_algorithmformat; 503 pcl->length = 0; 504 pcl->partial = true; 505 506 /* new pclusters should be claimed as type 1, primary and followed */ 507 pcl->next = fe->owned_head; 508 pcl->pageofs_out = map->m_la & ~PAGE_MASK; 509 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 510 511 /* 512 * lock all primary followed works before visible to others 513 * and mutex_trylock *never* fails for a new pcluster. 514 */ 515 mutex_init(&pcl->lock); 516 DBG_BUGON(!mutex_trylock(&pcl->lock)); 517 518 if (ztailpacking) { 519 pcl->obj.index = 0; /* which indicates ztailpacking */ 520 pcl->pageofs_in = erofs_blkoff(map->m_pa); 521 pcl->tailpacking_size = map->m_plen; 522 } else { 523 pcl->obj.index = map->m_pa >> PAGE_SHIFT; 524 525 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 526 if (IS_ERR(grp)) { 527 err = PTR_ERR(grp); 528 goto err_out; 529 } 530 531 if (grp != &pcl->obj) { 532 fe->pcl = container_of(grp, 533 struct z_erofs_pcluster, obj); 534 err = -EEXIST; 535 goto err_out; 536 } 537 } 538 /* used to check tail merging loop due to corrupted images */ 539 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 540 fe->tailpcl = pcl; 541 fe->owned_head = &pcl->next; 542 fe->pcl = pcl; 543 return 0; 544 545 err_out: 546 mutex_unlock(&pcl->lock); 547 z_erofs_free_pcluster(pcl); 548 return err; 549 } 550 551 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe) 552 { 553 struct erofs_map_blocks *map = &fe->map; 554 struct erofs_workgroup *grp = NULL; 555 int ret; 556 557 DBG_BUGON(fe->pcl); 558 559 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 560 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 561 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 562 563 if (!(map->m_flags & EROFS_MAP_META)) { 564 grp = erofs_find_workgroup(fe->inode->i_sb, 565 map->m_pa >> PAGE_SHIFT); 566 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 567 DBG_BUGON(1); 568 return -EFSCORRUPTED; 569 } 570 571 if (grp) { 572 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 573 ret = -EEXIST; 574 } else { 575 ret = z_erofs_register_pcluster(fe); 576 } 577 578 if (ret == -EEXIST) { 579 mutex_lock(&fe->pcl->lock); 580 /* used to check tail merging loop due to corrupted images */ 581 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 582 fe->tailpcl = fe->pcl; 583 584 z_erofs_try_to_claim_pcluster(fe); 585 } else if (ret) { 586 return ret; 587 } 588 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 589 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 590 /* since file-backed online pages are traversed in reverse order */ 591 fe->icur = z_erofs_pclusterpages(fe->pcl); 592 return 0; 593 } 594 595 /* 596 * keep in mind that no referenced pclusters will be freed 597 * only after a RCU grace period. 598 */ 599 static void z_erofs_rcu_callback(struct rcu_head *head) 600 { 601 z_erofs_free_pcluster(container_of(head, 602 struct z_erofs_pcluster, rcu)); 603 } 604 605 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 606 { 607 struct z_erofs_pcluster *const pcl = 608 container_of(grp, struct z_erofs_pcluster, obj); 609 610 call_rcu(&pcl->rcu, z_erofs_rcu_callback); 611 } 612 613 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe) 614 { 615 struct z_erofs_pcluster *pcl = fe->pcl; 616 617 if (!pcl) 618 return false; 619 620 z_erofs_bvec_iter_end(&fe->biter); 621 mutex_unlock(&pcl->lock); 622 623 if (fe->candidate_bvpage) { 624 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage)); 625 fe->candidate_bvpage = NULL; 626 } 627 628 /* 629 * if all pending pages are added, don't hold its reference 630 * any longer if the pcluster isn't hosted by ourselves. 631 */ 632 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 633 erofs_workgroup_put(&pcl->obj); 634 635 fe->pcl = NULL; 636 return true; 637 } 638 639 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 640 unsigned int cachestrategy, 641 erofs_off_t la) 642 { 643 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 644 return false; 645 646 if (fe->backmost) 647 return true; 648 649 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 650 la < fe->headoffset; 651 } 652 653 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos, 654 struct page *page, unsigned int pageofs, 655 unsigned int len) 656 { 657 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode; 658 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 659 u8 *src, *dst; 660 unsigned int i, cnt; 661 662 pos += EROFS_I(inode)->z_fragmentoff; 663 for (i = 0; i < len; i += cnt) { 664 cnt = min_t(unsigned int, len - i, 665 EROFS_BLKSIZ - erofs_blkoff(pos)); 666 src = erofs_bread(&buf, packed_inode, 667 erofs_blknr(pos), EROFS_KMAP); 668 if (IS_ERR(src)) { 669 erofs_put_metabuf(&buf); 670 return PTR_ERR(src); 671 } 672 673 dst = kmap_local_page(page); 674 memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt); 675 kunmap_local(dst); 676 pos += cnt; 677 } 678 erofs_put_metabuf(&buf); 679 return 0; 680 } 681 682 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 683 struct page *page, struct page **pagepool) 684 { 685 struct inode *const inode = fe->inode; 686 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 687 struct erofs_map_blocks *const map = &fe->map; 688 const loff_t offset = page_offset(page); 689 bool tight = true, exclusive; 690 691 enum z_erofs_cache_alloctype cache_strategy; 692 unsigned int cur, end, spiltted; 693 int err = 0; 694 695 /* register locked file pages as online pages in pack */ 696 z_erofs_onlinepage_init(page); 697 698 spiltted = 0; 699 end = PAGE_SIZE; 700 repeat: 701 cur = end - 1; 702 703 if (offset + cur < map->m_la || 704 offset + cur >= map->m_la + map->m_llen) { 705 erofs_dbg("out-of-range map @ pos %llu", offset + cur); 706 707 if (z_erofs_collector_end(fe)) 708 fe->backmost = false; 709 map->m_la = offset + cur; 710 map->m_llen = 0; 711 err = z_erofs_map_blocks_iter(inode, map, 0); 712 if (err) 713 goto out; 714 } else { 715 if (fe->pcl) 716 goto hitted; 717 /* didn't get a valid pcluster previously (very rare) */ 718 } 719 720 if (!(map->m_flags & EROFS_MAP_MAPPED) || 721 map->m_flags & EROFS_MAP_FRAGMENT) 722 goto hitted; 723 724 err = z_erofs_collector_begin(fe); 725 if (err) 726 goto out; 727 728 if (z_erofs_is_inline_pcluster(fe->pcl)) { 729 void *mp; 730 731 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, 732 erofs_blknr(map->m_pa), EROFS_NO_KMAP); 733 if (IS_ERR(mp)) { 734 err = PTR_ERR(mp); 735 erofs_err(inode->i_sb, 736 "failed to get inline page, err %d", err); 737 goto out; 738 } 739 get_page(fe->map.buf.page); 740 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, 741 fe->map.buf.page); 742 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 743 } else { 744 /* bind cache first when cached decompression is preferred */ 745 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, 746 map->m_la)) 747 cache_strategy = TRYALLOC; 748 else 749 cache_strategy = DONTALLOC; 750 751 z_erofs_bind_cache(fe, cache_strategy, pagepool); 752 } 753 hitted: 754 /* 755 * Ensure the current partial page belongs to this submit chain rather 756 * than other concurrent submit chains or the noio(bypass) chain since 757 * those chains are handled asynchronously thus the page cannot be used 758 * for inplace I/O or bvpage (should be processed in a strict order.) 759 */ 760 tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED && 761 fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); 762 763 cur = end - min_t(unsigned int, offset + end - map->m_la, end); 764 if (!(map->m_flags & EROFS_MAP_MAPPED)) { 765 zero_user_segment(page, cur, end); 766 goto next_part; 767 } 768 if (map->m_flags & EROFS_MAP_FRAGMENT) { 769 unsigned int pageofs, skip, len; 770 771 if (offset > map->m_la) { 772 pageofs = 0; 773 skip = offset - map->m_la; 774 } else { 775 pageofs = map->m_la & ~PAGE_MASK; 776 skip = 0; 777 } 778 len = min_t(unsigned int, map->m_llen - skip, end - cur); 779 err = z_erofs_read_fragment(inode, skip, page, pageofs, len); 780 if (err) 781 goto out; 782 ++spiltted; 783 tight = false; 784 goto next_part; 785 } 786 787 exclusive = (!cur && (!spiltted || tight)); 788 if (cur) 789 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 790 791 retry: 792 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 793 .page = page, 794 .offset = offset - map->m_la, 795 .end = end, 796 }), exclusive); 797 /* should allocate an additional short-lived page for bvset */ 798 if (err == -EAGAIN && !fe->candidate_bvpage) { 799 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL); 800 set_page_private(fe->candidate_bvpage, 801 Z_EROFS_SHORTLIVED_PAGE); 802 goto retry; 803 } 804 805 if (err) { 806 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage); 807 goto out; 808 } 809 810 z_erofs_onlinepage_split(page); 811 /* bump up the number of spiltted parts of a page */ 812 ++spiltted; 813 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) 814 fe->pcl->multibases = true; 815 816 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && 817 !(map->m_flags & EROFS_MAP_PARTIAL_REF) && 818 fe->pcl->length == map->m_llen) 819 fe->pcl->partial = false; 820 if (fe->pcl->length < offset + end - map->m_la) { 821 fe->pcl->length = offset + end - map->m_la; 822 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 823 } 824 next_part: 825 /* shorten the remaining extent to update progress */ 826 map->m_llen = offset + cur - map->m_la; 827 map->m_flags &= ~EROFS_MAP_FULL_MAPPED; 828 829 end = cur; 830 if (end > 0) 831 goto repeat; 832 833 out: 834 if (err) 835 z_erofs_page_mark_eio(page); 836 z_erofs_onlinepage_endio(page); 837 838 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 839 __func__, page, spiltted, map->m_llen); 840 return err; 841 } 842 843 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi, 844 unsigned int readahead_pages) 845 { 846 /* auto: enable for read_folio, disable for readahead */ 847 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 848 !readahead_pages) 849 return true; 850 851 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 852 (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 853 return true; 854 855 return false; 856 } 857 858 static bool z_erofs_page_is_invalidated(struct page *page) 859 { 860 return !page->mapping && !z_erofs_is_shortlived_page(page); 861 } 862 863 struct z_erofs_decompress_backend { 864 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 865 struct super_block *sb; 866 struct z_erofs_pcluster *pcl; 867 868 /* pages with the longest decompressed length for deduplication */ 869 struct page **decompressed_pages; 870 /* pages to keep the compressed data */ 871 struct page **compressed_pages; 872 873 struct list_head decompressed_secondary_bvecs; 874 struct page **pagepool; 875 unsigned int onstack_used, nr_pages; 876 }; 877 878 struct z_erofs_bvec_item { 879 struct z_erofs_bvec bvec; 880 struct list_head list; 881 }; 882 883 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, 884 struct z_erofs_bvec *bvec) 885 { 886 struct z_erofs_bvec_item *item; 887 888 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) { 889 unsigned int pgnr; 890 struct page *oldpage; 891 892 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; 893 DBG_BUGON(pgnr >= be->nr_pages); 894 oldpage = be->decompressed_pages[pgnr]; 895 be->decompressed_pages[pgnr] = bvec->page; 896 897 if (!oldpage) 898 return; 899 } 900 901 /* (cold path) one pcluster is requested multiple times */ 902 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); 903 item->bvec = *bvec; 904 list_add(&item->list, &be->decompressed_secondary_bvecs); 905 } 906 907 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, 908 int err) 909 { 910 unsigned int off0 = be->pcl->pageofs_out; 911 struct list_head *p, *n; 912 913 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 914 struct z_erofs_bvec_item *bvi; 915 unsigned int end, cur; 916 void *dst, *src; 917 918 bvi = container_of(p, struct z_erofs_bvec_item, list); 919 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 920 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 921 bvi->bvec.end); 922 dst = kmap_local_page(bvi->bvec.page); 923 while (cur < end) { 924 unsigned int pgnr, scur, len; 925 926 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 927 DBG_BUGON(pgnr >= be->nr_pages); 928 929 scur = bvi->bvec.offset + cur - 930 ((pgnr << PAGE_SHIFT) - off0); 931 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 932 if (!be->decompressed_pages[pgnr]) { 933 err = -EFSCORRUPTED; 934 cur += len; 935 continue; 936 } 937 src = kmap_local_page(be->decompressed_pages[pgnr]); 938 memcpy(dst + cur, src + scur, len); 939 kunmap_local(src); 940 cur += len; 941 } 942 kunmap_local(dst); 943 if (err) 944 z_erofs_page_mark_eio(bvi->bvec.page); 945 z_erofs_onlinepage_endio(bvi->bvec.page); 946 list_del(p); 947 kfree(bvi); 948 } 949 } 950 951 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) 952 { 953 struct z_erofs_pcluster *pcl = be->pcl; 954 struct z_erofs_bvec_iter biter; 955 struct page *old_bvpage; 956 int i; 957 958 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 959 for (i = 0; i < pcl->vcnt; ++i) { 960 struct z_erofs_bvec bvec; 961 962 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 963 964 if (old_bvpage) 965 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 966 967 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 968 z_erofs_do_decompressed_bvec(be, &bvec); 969 } 970 971 old_bvpage = z_erofs_bvec_iter_end(&biter); 972 if (old_bvpage) 973 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 974 } 975 976 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, 977 bool *overlapped) 978 { 979 struct z_erofs_pcluster *pcl = be->pcl; 980 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 981 int i, err = 0; 982 983 *overlapped = false; 984 for (i = 0; i < pclusterpages; ++i) { 985 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 986 struct page *page = bvec->page; 987 988 /* compressed pages ought to be present before decompressing */ 989 if (!page) { 990 DBG_BUGON(1); 991 continue; 992 } 993 be->compressed_pages[i] = page; 994 995 if (z_erofs_is_inline_pcluster(pcl)) { 996 if (!PageUptodate(page)) 997 err = -EIO; 998 continue; 999 } 1000 1001 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1002 if (!z_erofs_is_shortlived_page(page)) { 1003 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { 1004 if (!PageUptodate(page)) 1005 err = -EIO; 1006 continue; 1007 } 1008 z_erofs_do_decompressed_bvec(be, bvec); 1009 *overlapped = true; 1010 } 1011 } 1012 1013 if (err) 1014 return err; 1015 return 0; 1016 } 1017 1018 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, 1019 int err) 1020 { 1021 struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 1022 struct z_erofs_pcluster *pcl = be->pcl; 1023 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1024 unsigned int i, inputsize; 1025 int err2; 1026 struct page *page; 1027 bool overlapped; 1028 1029 mutex_lock(&pcl->lock); 1030 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 1031 1032 /* allocate (de)compressed page arrays if cannot be kept on stack */ 1033 be->decompressed_pages = NULL; 1034 be->compressed_pages = NULL; 1035 be->onstack_used = 0; 1036 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 1037 be->decompressed_pages = be->onstack_pages; 1038 be->onstack_used = be->nr_pages; 1039 memset(be->decompressed_pages, 0, 1040 sizeof(struct page *) * be->nr_pages); 1041 } 1042 1043 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1044 be->compressed_pages = be->onstack_pages + be->onstack_used; 1045 1046 if (!be->decompressed_pages) 1047 be->decompressed_pages = 1048 kvcalloc(be->nr_pages, sizeof(struct page *), 1049 GFP_KERNEL | __GFP_NOFAIL); 1050 if (!be->compressed_pages) 1051 be->compressed_pages = 1052 kvcalloc(pclusterpages, sizeof(struct page *), 1053 GFP_KERNEL | __GFP_NOFAIL); 1054 1055 z_erofs_parse_out_bvecs(be); 1056 err2 = z_erofs_parse_in_bvecs(be, &overlapped); 1057 if (err2) 1058 err = err2; 1059 if (err) 1060 goto out; 1061 1062 if (z_erofs_is_inline_pcluster(pcl)) 1063 inputsize = pcl->tailpacking_size; 1064 else 1065 inputsize = pclusterpages * PAGE_SIZE; 1066 1067 err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 1068 .sb = be->sb, 1069 .in = be->compressed_pages, 1070 .out = be->decompressed_pages, 1071 .pageofs_in = pcl->pageofs_in, 1072 .pageofs_out = pcl->pageofs_out, 1073 .inputsize = inputsize, 1074 .outputsize = pcl->length, 1075 .alg = pcl->algorithmformat, 1076 .inplace_io = overlapped, 1077 .partial_decoding = pcl->partial, 1078 .fillgaps = pcl->multibases, 1079 }, be->pagepool); 1080 1081 out: 1082 /* must handle all compressed pages before actual file pages */ 1083 if (z_erofs_is_inline_pcluster(pcl)) { 1084 page = pcl->compressed_bvecs[0].page; 1085 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1086 put_page(page); 1087 } else { 1088 for (i = 0; i < pclusterpages; ++i) { 1089 page = pcl->compressed_bvecs[i].page; 1090 1091 if (erofs_page_is_managed(sbi, page)) 1092 continue; 1093 1094 /* recycle all individual short-lived pages */ 1095 (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1096 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 1097 } 1098 } 1099 if (be->compressed_pages < be->onstack_pages || 1100 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1101 kvfree(be->compressed_pages); 1102 z_erofs_fill_other_copies(be, err); 1103 1104 for (i = 0; i < be->nr_pages; ++i) { 1105 page = be->decompressed_pages[i]; 1106 if (!page) 1107 continue; 1108 1109 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1110 1111 /* recycle all individual short-lived pages */ 1112 if (z_erofs_put_shortlivedpage(be->pagepool, page)) 1113 continue; 1114 if (err) 1115 z_erofs_page_mark_eio(page); 1116 z_erofs_onlinepage_endio(page); 1117 } 1118 1119 if (be->decompressed_pages != be->onstack_pages) 1120 kvfree(be->decompressed_pages); 1121 1122 pcl->length = 0; 1123 pcl->partial = true; 1124 pcl->multibases = false; 1125 pcl->bvset.nextpage = NULL; 1126 pcl->vcnt = 0; 1127 1128 /* pcluster lock MUST be taken before the following line */ 1129 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 1130 mutex_unlock(&pcl->lock); 1131 return err; 1132 } 1133 1134 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1135 struct page **pagepool) 1136 { 1137 struct z_erofs_decompress_backend be = { 1138 .sb = io->sb, 1139 .pagepool = pagepool, 1140 .decompressed_secondary_bvecs = 1141 LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 1142 }; 1143 z_erofs_next_pcluster_t owned = io->head; 1144 1145 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 1146 /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 1147 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 1148 /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */ 1149 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 1150 1151 be.pcl = container_of(owned, struct z_erofs_pcluster, next); 1152 owned = READ_ONCE(be.pcl->next); 1153 1154 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); 1155 erofs_workgroup_put(&be.pcl->obj); 1156 } 1157 } 1158 1159 static void z_erofs_decompressqueue_work(struct work_struct *work) 1160 { 1161 struct z_erofs_decompressqueue *bgq = 1162 container_of(work, struct z_erofs_decompressqueue, u.work); 1163 struct page *pagepool = NULL; 1164 1165 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1166 z_erofs_decompress_queue(bgq, &pagepool); 1167 1168 erofs_release_pages(&pagepool); 1169 kvfree(bgq); 1170 } 1171 1172 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 1173 bool sync, int bios) 1174 { 1175 struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 1176 1177 /* wake up the caller thread for sync decompression */ 1178 if (sync) { 1179 if (!atomic_add_return(bios, &io->pending_bios)) 1180 complete(&io->u.done); 1181 return; 1182 } 1183 1184 if (atomic_add_return(bios, &io->pending_bios)) 1185 return; 1186 /* Use workqueue and sync decompression for atomic contexts only */ 1187 if (in_atomic() || irqs_disabled()) { 1188 queue_work(z_erofs_workqueue, &io->u.work); 1189 /* enable sync decompression for readahead */ 1190 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 1191 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 1192 return; 1193 } 1194 z_erofs_decompressqueue_work(&io->u.work); 1195 } 1196 1197 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 1198 unsigned int nr, 1199 struct page **pagepool, 1200 struct address_space *mc) 1201 { 1202 const pgoff_t index = pcl->obj.index; 1203 gfp_t gfp = mapping_gfp_mask(mc); 1204 bool tocache = false; 1205 1206 struct address_space *mapping; 1207 struct page *oldpage, *page; 1208 1209 compressed_page_t t; 1210 int justfound; 1211 1212 repeat: 1213 page = READ_ONCE(pcl->compressed_bvecs[nr].page); 1214 oldpage = page; 1215 1216 if (!page) 1217 goto out_allocpage; 1218 1219 /* process the target tagged pointer */ 1220 t = tagptr_init(compressed_page_t, page); 1221 justfound = tagptr_unfold_tags(t); 1222 page = tagptr_unfold_ptr(t); 1223 1224 /* 1225 * preallocated cached pages, which is used to avoid direct reclaim 1226 * otherwise, it will go inplace I/O path instead. 1227 */ 1228 if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1229 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 1230 set_page_private(page, 0); 1231 tocache = true; 1232 goto out_tocache; 1233 } 1234 mapping = READ_ONCE(page->mapping); 1235 1236 /* 1237 * file-backed online pages in plcuster are all locked steady, 1238 * therefore it is impossible for `mapping' to be NULL. 1239 */ 1240 if (mapping && mapping != mc) 1241 /* ought to be unmanaged pages */ 1242 goto out; 1243 1244 /* directly return for shortlived page as well */ 1245 if (z_erofs_is_shortlived_page(page)) 1246 goto out; 1247 1248 lock_page(page); 1249 1250 /* only true if page reclaim goes wrong, should never happen */ 1251 DBG_BUGON(justfound && PagePrivate(page)); 1252 1253 /* the page is still in manage cache */ 1254 if (page->mapping == mc) { 1255 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 1256 1257 if (!PagePrivate(page)) { 1258 /* 1259 * impossible to be !PagePrivate(page) for 1260 * the current restriction as well if 1261 * the page is already in compressed_bvecs[]. 1262 */ 1263 DBG_BUGON(!justfound); 1264 1265 justfound = 0; 1266 set_page_private(page, (unsigned long)pcl); 1267 SetPagePrivate(page); 1268 } 1269 1270 /* no need to submit io if it is already up-to-date */ 1271 if (PageUptodate(page)) { 1272 unlock_page(page); 1273 page = NULL; 1274 } 1275 goto out; 1276 } 1277 1278 /* 1279 * the managed page has been truncated, it's unsafe to 1280 * reuse this one, let's allocate a new cache-managed page. 1281 */ 1282 DBG_BUGON(page->mapping); 1283 DBG_BUGON(!justfound); 1284 1285 tocache = true; 1286 unlock_page(page); 1287 put_page(page); 1288 out_allocpage: 1289 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1290 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, 1291 oldpage, page)) { 1292 erofs_pagepool_add(pagepool, page); 1293 cond_resched(); 1294 goto repeat; 1295 } 1296 out_tocache: 1297 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1298 /* turn into temporary page if fails (1 ref) */ 1299 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1300 goto out; 1301 } 1302 attach_page_private(page, pcl); 1303 /* drop a refcount added by allocpage (then we have 2 refs here) */ 1304 put_page(page); 1305 1306 out: /* the only exit (for tracing and debugging) */ 1307 return page; 1308 } 1309 1310 static struct z_erofs_decompressqueue * 1311 jobqueue_init(struct super_block *sb, 1312 struct z_erofs_decompressqueue *fgq, bool *fg) 1313 { 1314 struct z_erofs_decompressqueue *q; 1315 1316 if (fg && !*fg) { 1317 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1318 if (!q) { 1319 *fg = true; 1320 goto fg_out; 1321 } 1322 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1323 } else { 1324 fg_out: 1325 q = fgq; 1326 init_completion(&fgq->u.done); 1327 atomic_set(&fgq->pending_bios, 0); 1328 q->eio = false; 1329 } 1330 q->sb = sb; 1331 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1332 return q; 1333 } 1334 1335 /* define decompression jobqueue types */ 1336 enum { 1337 JQ_BYPASS, 1338 JQ_SUBMIT, 1339 NR_JOBQUEUES, 1340 }; 1341 1342 static void *jobqueueset_init(struct super_block *sb, 1343 struct z_erofs_decompressqueue *q[], 1344 struct z_erofs_decompressqueue *fgq, bool *fg) 1345 { 1346 /* 1347 * if managed cache is enabled, bypass jobqueue is needed, 1348 * no need to read from device for all pclusters in this queue. 1349 */ 1350 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1351 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 1352 1353 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 1354 } 1355 1356 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 1357 z_erofs_next_pcluster_t qtail[], 1358 z_erofs_next_pcluster_t owned_head) 1359 { 1360 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 1361 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 1362 1363 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1364 if (owned_head == Z_EROFS_PCLUSTER_TAIL) 1365 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1366 1367 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 1368 1369 WRITE_ONCE(*submit_qtail, owned_head); 1370 WRITE_ONCE(*bypass_qtail, &pcl->next); 1371 1372 qtail[JQ_BYPASS] = &pcl->next; 1373 } 1374 1375 static void z_erofs_decompressqueue_endio(struct bio *bio) 1376 { 1377 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 1378 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 1379 blk_status_t err = bio->bi_status; 1380 struct bio_vec *bvec; 1381 struct bvec_iter_all iter_all; 1382 1383 bio_for_each_segment_all(bvec, bio, iter_all) { 1384 struct page *page = bvec->bv_page; 1385 1386 DBG_BUGON(PageUptodate(page)); 1387 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1388 1389 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 1390 if (!err) 1391 SetPageUptodate(page); 1392 unlock_page(page); 1393 } 1394 } 1395 if (err) 1396 q->eio = true; 1397 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 1398 bio_put(bio); 1399 } 1400 1401 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1402 struct page **pagepool, 1403 struct z_erofs_decompressqueue *fgq, 1404 bool *force_fg) 1405 { 1406 struct super_block *sb = f->inode->i_sb; 1407 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 1408 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1409 struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 1410 void *bi_private; 1411 z_erofs_next_pcluster_t owned_head = f->owned_head; 1412 /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 1413 pgoff_t last_index; 1414 struct block_device *last_bdev; 1415 unsigned int nr_bios = 0; 1416 struct bio *bio = NULL; 1417 1418 bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1419 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1420 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 1421 1422 /* by default, all need io submission */ 1423 q[JQ_SUBMIT]->head = owned_head; 1424 1425 do { 1426 struct erofs_map_dev mdev; 1427 struct z_erofs_pcluster *pcl; 1428 pgoff_t cur, end; 1429 unsigned int i = 0; 1430 bool bypass = true; 1431 1432 /* no possible 'owned_head' equals the following */ 1433 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1434 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 1435 1436 pcl = container_of(owned_head, struct z_erofs_pcluster, next); 1437 1438 /* close the main owned chain at first */ 1439 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 1440 Z_EROFS_PCLUSTER_TAIL_CLOSED); 1441 if (z_erofs_is_inline_pcluster(pcl)) { 1442 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1443 continue; 1444 } 1445 1446 /* no device id here, thus it will always succeed */ 1447 mdev = (struct erofs_map_dev) { 1448 .m_pa = blknr_to_addr(pcl->obj.index), 1449 }; 1450 (void)erofs_map_dev(sb, &mdev); 1451 1452 cur = erofs_blknr(mdev.m_pa); 1453 end = cur + pcl->pclusterpages; 1454 1455 do { 1456 struct page *page; 1457 1458 page = pickup_page_for_submission(pcl, i++, pagepool, 1459 mc); 1460 if (!page) 1461 continue; 1462 1463 if (bio && (cur != last_index + 1 || 1464 last_bdev != mdev.m_bdev)) { 1465 submit_bio_retry: 1466 submit_bio(bio); 1467 bio = NULL; 1468 } 1469 1470 if (!bio) { 1471 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 1472 REQ_OP_READ, GFP_NOIO); 1473 bio->bi_end_io = z_erofs_decompressqueue_endio; 1474 1475 last_bdev = mdev.m_bdev; 1476 bio->bi_iter.bi_sector = (sector_t)cur << 1477 LOG_SECTORS_PER_BLOCK; 1478 bio->bi_private = bi_private; 1479 if (f->readahead) 1480 bio->bi_opf |= REQ_RAHEAD; 1481 ++nr_bios; 1482 } 1483 1484 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 1485 goto submit_bio_retry; 1486 1487 last_index = cur; 1488 bypass = false; 1489 } while (++cur < end); 1490 1491 if (!bypass) 1492 qtail[JQ_SUBMIT] = &pcl->next; 1493 else 1494 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1495 } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 1496 1497 if (bio) 1498 submit_bio(bio); 1499 1500 /* 1501 * although background is preferred, no one is pending for submission. 1502 * don't issue workqueue for decompression but drop it directly instead. 1503 */ 1504 if (!*force_fg && !nr_bios) { 1505 kvfree(q[JQ_SUBMIT]); 1506 return; 1507 } 1508 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 1509 } 1510 1511 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, 1512 struct page **pagepool, bool force_fg) 1513 { 1514 struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 1515 1516 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) 1517 return; 1518 z_erofs_submit_queue(f, pagepool, io, &force_fg); 1519 1520 /* handle bypass queue (no i/o pclusters) immediately */ 1521 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 1522 1523 if (!force_fg) 1524 return; 1525 1526 /* wait until all bios are completed */ 1527 wait_for_completion_io(&io[JQ_SUBMIT].u.done); 1528 1529 /* handle synchronous decompress queue in the caller context */ 1530 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 1531 } 1532 1533 /* 1534 * Since partial uptodate is still unimplemented for now, we have to use 1535 * approximate readmore strategies as a start. 1536 */ 1537 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, 1538 struct readahead_control *rac, 1539 erofs_off_t end, 1540 struct page **pagepool, 1541 bool backmost) 1542 { 1543 struct inode *inode = f->inode; 1544 struct erofs_map_blocks *map = &f->map; 1545 erofs_off_t cur; 1546 int err; 1547 1548 if (backmost) { 1549 map->m_la = end; 1550 err = z_erofs_map_blocks_iter(inode, map, 1551 EROFS_GET_BLOCKS_READMORE); 1552 if (err) 1553 return; 1554 1555 /* expend ra for the trailing edge if readahead */ 1556 if (rac) { 1557 loff_t newstart = readahead_pos(rac); 1558 1559 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 1560 readahead_expand(rac, newstart, cur - newstart); 1561 return; 1562 } 1563 end = round_up(end, PAGE_SIZE); 1564 } else { 1565 end = round_up(map->m_la, PAGE_SIZE); 1566 1567 if (!map->m_llen) 1568 return; 1569 } 1570 1571 cur = map->m_la + map->m_llen - 1; 1572 while (cur >= end) { 1573 pgoff_t index = cur >> PAGE_SHIFT; 1574 struct page *page; 1575 1576 page = erofs_grab_cache_page_nowait(inode->i_mapping, index); 1577 if (page) { 1578 if (PageUptodate(page)) { 1579 unlock_page(page); 1580 } else { 1581 err = z_erofs_do_read_page(f, page, pagepool); 1582 if (err) 1583 erofs_err(inode->i_sb, 1584 "readmore error at page %lu @ nid %llu", 1585 index, EROFS_I(inode)->nid); 1586 } 1587 put_page(page); 1588 } 1589 1590 if (cur < PAGE_SIZE) 1591 break; 1592 cur = (index << PAGE_SHIFT) - 1; 1593 } 1594 } 1595 1596 static int z_erofs_read_folio(struct file *file, struct folio *folio) 1597 { 1598 struct page *page = &folio->page; 1599 struct inode *const inode = page->mapping->host; 1600 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1601 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1602 struct page *pagepool = NULL; 1603 int err; 1604 1605 trace_erofs_readpage(page, false); 1606 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 1607 1608 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1, 1609 &pagepool, true); 1610 err = z_erofs_do_read_page(&f, page, &pagepool); 1611 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false); 1612 1613 (void)z_erofs_collector_end(&f); 1614 1615 /* if some compressed cluster ready, need submit them anyway */ 1616 z_erofs_runqueue(&f, &pagepool, 1617 z_erofs_get_sync_decompress_policy(sbi, 0)); 1618 1619 if (err) 1620 erofs_err(inode->i_sb, "failed to read, err [%d]", err); 1621 1622 erofs_put_metabuf(&f.map.buf); 1623 erofs_release_pages(&pagepool); 1624 return err; 1625 } 1626 1627 static void z_erofs_readahead(struct readahead_control *rac) 1628 { 1629 struct inode *const inode = rac->mapping->host; 1630 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1631 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1632 struct page *pagepool = NULL, *head = NULL, *page; 1633 unsigned int nr_pages; 1634 1635 f.readahead = true; 1636 f.headoffset = readahead_pos(rac); 1637 1638 z_erofs_pcluster_readmore(&f, rac, f.headoffset + 1639 readahead_length(rac) - 1, &pagepool, true); 1640 nr_pages = readahead_count(rac); 1641 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 1642 1643 while ((page = readahead_page(rac))) { 1644 set_page_private(page, (unsigned long)head); 1645 head = page; 1646 } 1647 1648 while (head) { 1649 struct page *page = head; 1650 int err; 1651 1652 /* traversal in reverse order */ 1653 head = (void *)page_private(page); 1654 1655 err = z_erofs_do_read_page(&f, page, &pagepool); 1656 if (err) 1657 erofs_err(inode->i_sb, 1658 "readahead error at page %lu @ nid %llu", 1659 page->index, EROFS_I(inode)->nid); 1660 put_page(page); 1661 } 1662 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false); 1663 (void)z_erofs_collector_end(&f); 1664 1665 z_erofs_runqueue(&f, &pagepool, 1666 z_erofs_get_sync_decompress_policy(sbi, nr_pages)); 1667 erofs_put_metabuf(&f.map.buf); 1668 erofs_release_pages(&pagepool); 1669 } 1670 1671 const struct address_space_operations z_erofs_aops = { 1672 .read_folio = z_erofs_read_folio, 1673 .readahead = z_erofs_readahead, 1674 }; 1675