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_pages, 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_collectmode { 231 COLLECT_SECONDARY, 232 COLLECT_PRIMARY, 233 /* 234 * The current collection was the tail of an exist chain, in addition 235 * that the previous processed chained collections are all decided to 236 * be hooked up to it. 237 * A new chain will be created for the remaining collections which are 238 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED, 239 * the next collection cannot reuse the whole page safely in 240 * the following scenario: 241 * ________________________________________________________________ 242 * | tail (partial) page | head (partial) page | 243 * | (belongs to the next cl) | (belongs to the current cl) | 244 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________| 245 */ 246 COLLECT_PRIMARY_HOOKED, 247 /* 248 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it 249 * could be dispatched into bypass queue later due to uptodated managed 250 * pages. All related online pages cannot be reused for inplace I/O (or 251 * pagevec) since it can be directly decoded without I/O submission. 252 */ 253 COLLECT_PRIMARY_FOLLOWED_NOINPLACE, 254 /* 255 * The current collection has been linked with the owned chain, and 256 * could also be linked with the remaining collections, which means 257 * if the processing page is the tail page of the collection, thus 258 * the current collection can safely use the whole page (since 259 * the previous collection is under control) for in-place I/O, as 260 * illustrated below: 261 * ________________________________________________________________ 262 * | tail (partial) page | head (partial) page | 263 * | (of the current cl) | (of the previous collection) | 264 * | PRIMARY_FOLLOWED or | | 265 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| 266 * 267 * [ (*) the above page can be used as inplace I/O. ] 268 */ 269 COLLECT_PRIMARY_FOLLOWED, 270 }; 271 272 struct z_erofs_decompress_frontend { 273 struct inode *const inode; 274 struct erofs_map_blocks map; 275 struct z_erofs_bvec_iter biter; 276 struct z_erofs_pagevec_ctor vector; 277 278 struct page *candidate_bvpage; 279 struct z_erofs_pcluster *pcl, *tailpcl; 280 /* a pointer used to pick up inplace I/O pages */ 281 struct page **icpage_ptr; 282 z_erofs_next_pcluster_t owned_head; 283 284 enum z_erofs_collectmode mode; 285 286 bool readahead; 287 /* used for applying cache strategy on the fly */ 288 bool backmost; 289 erofs_off_t headoffset; 290 }; 291 292 #define DECOMPRESS_FRONTEND_INIT(__i) { \ 293 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 294 .mode = COLLECT_PRIMARY_FOLLOWED, .backmost = true } 295 296 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; 297 static DEFINE_MUTEX(z_pagemap_global_lock); 298 299 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe, 300 enum z_erofs_cache_alloctype type, 301 struct page **pagepool) 302 { 303 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 304 struct z_erofs_pcluster *pcl = fe->pcl; 305 bool standalone = true; 306 /* 307 * optimistic allocation without direct reclaim since inplace I/O 308 * can be used if low memory otherwise. 309 */ 310 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 311 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 312 struct page **pages; 313 pgoff_t index; 314 315 if (fe->mode < COLLECT_PRIMARY_FOLLOWED) 316 return; 317 318 pages = pcl->compressed_pages; 319 index = pcl->obj.index; 320 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) { 321 struct page *page; 322 compressed_page_t t; 323 struct page *newpage = NULL; 324 325 /* the compressed page was loaded before */ 326 if (READ_ONCE(*pages)) 327 continue; 328 329 page = find_get_page(mc, index); 330 331 if (page) { 332 t = tag_compressed_page_justfound(page); 333 } else { 334 /* I/O is needed, no possible to decompress directly */ 335 standalone = false; 336 switch (type) { 337 case TRYALLOC: 338 newpage = erofs_allocpage(pagepool, gfp); 339 if (!newpage) 340 continue; 341 set_page_private(newpage, 342 Z_EROFS_PREALLOCATED_PAGE); 343 t = tag_compressed_page_justfound(newpage); 344 break; 345 default: /* DONTALLOC */ 346 continue; 347 } 348 } 349 350 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) 351 continue; 352 353 if (page) 354 put_page(page); 355 else if (newpage) 356 erofs_pagepool_add(pagepool, newpage); 357 } 358 359 /* 360 * don't do inplace I/O if all compressed pages are available in 361 * managed cache since it can be moved to the bypass queue instead. 362 */ 363 if (standalone) 364 fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 365 } 366 367 /* called by erofs_shrinker to get rid of all compressed_pages */ 368 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 369 struct erofs_workgroup *grp) 370 { 371 struct z_erofs_pcluster *const pcl = 372 container_of(grp, struct z_erofs_pcluster, obj); 373 int i; 374 375 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 376 /* 377 * refcount of workgroup is now freezed as 1, 378 * therefore no need to worry about available decompression users. 379 */ 380 for (i = 0; i < pcl->pclusterpages; ++i) { 381 struct page *page = pcl->compressed_pages[i]; 382 383 if (!page) 384 continue; 385 386 /* block other users from reclaiming or migrating the page */ 387 if (!trylock_page(page)) 388 return -EBUSY; 389 390 if (!erofs_page_is_managed(sbi, page)) 391 continue; 392 393 /* barrier is implied in the following 'unlock_page' */ 394 WRITE_ONCE(pcl->compressed_pages[i], NULL); 395 detach_page_private(page); 396 unlock_page(page); 397 } 398 return 0; 399 } 400 401 int erofs_try_to_free_cached_page(struct page *page) 402 { 403 struct z_erofs_pcluster *const pcl = (void *)page_private(page); 404 int ret = 0; /* 0 - busy */ 405 406 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { 407 unsigned int i; 408 409 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 410 for (i = 0; i < pcl->pclusterpages; ++i) { 411 if (pcl->compressed_pages[i] == page) { 412 WRITE_ONCE(pcl->compressed_pages[i], NULL); 413 ret = 1; 414 break; 415 } 416 } 417 erofs_workgroup_unfreeze(&pcl->obj, 1); 418 419 if (ret) 420 detach_page_private(page); 421 } 422 return ret; 423 } 424 425 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ 426 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 427 struct page *page) 428 { 429 struct z_erofs_pcluster *const pcl = fe->pcl; 430 431 while (fe->icpage_ptr > pcl->compressed_pages) 432 if (!cmpxchg(--fe->icpage_ptr, NULL, page)) 433 return true; 434 return false; 435 } 436 437 /* callers must be with pcluster lock held */ 438 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 439 struct z_erofs_bvec *bvec, 440 enum z_erofs_page_type type) 441 { 442 int ret; 443 444 if (fe->mode >= COLLECT_PRIMARY && 445 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE) { 446 /* give priority for inplaceio to use file pages first */ 447 if (z_erofs_try_inplace_io(fe, bvec->page)) 448 return 0; 449 /* otherwise, check if it can be used as a bvpage */ 450 if (fe->mode >= COLLECT_PRIMARY_FOLLOWED && 451 !fe->candidate_bvpage) 452 fe->candidate_bvpage = bvec->page; 453 } 454 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage); 455 fe->pcl->vcnt += (ret >= 0); 456 return ret; 457 } 458 459 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 460 { 461 struct z_erofs_pcluster *pcl = f->pcl; 462 z_erofs_next_pcluster_t *owned_head = &f->owned_head; 463 464 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 465 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 466 *owned_head) == Z_EROFS_PCLUSTER_NIL) { 467 *owned_head = &pcl->next; 468 /* so we can attach this pcluster to our submission chain. */ 469 f->mode = COLLECT_PRIMARY_FOLLOWED; 470 return; 471 } 472 473 /* 474 * type 2, link to the end of an existing open chain, be careful 475 * that its submission is controlled by the original attached chain. 476 */ 477 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 478 *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 479 *owned_head = Z_EROFS_PCLUSTER_TAIL; 480 f->mode = COLLECT_PRIMARY_HOOKED; 481 f->tailpcl = NULL; 482 return; 483 } 484 /* type 3, it belongs to a chain, but it isn't the end of the chain */ 485 f->mode = COLLECT_PRIMARY; 486 } 487 488 static int z_erofs_lookup_pcluster(struct z_erofs_decompress_frontend *fe) 489 { 490 struct erofs_map_blocks *map = &fe->map; 491 struct z_erofs_pcluster *pcl = fe->pcl; 492 unsigned int length; 493 494 /* to avoid unexpected loop formed by corrupted images */ 495 if (fe->owned_head == &pcl->next || pcl == fe->tailpcl) { 496 DBG_BUGON(1); 497 return -EFSCORRUPTED; 498 } 499 500 if (pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) { 501 DBG_BUGON(1); 502 return -EFSCORRUPTED; 503 } 504 505 length = READ_ONCE(pcl->length); 506 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { 507 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { 508 DBG_BUGON(1); 509 return -EFSCORRUPTED; 510 } 511 } else { 512 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; 513 514 if (map->m_flags & EROFS_MAP_FULL_MAPPED) 515 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; 516 517 while (llen > length && 518 length != cmpxchg_relaxed(&pcl->length, length, llen)) { 519 cpu_relax(); 520 length = READ_ONCE(pcl->length); 521 } 522 } 523 mutex_lock(&pcl->lock); 524 /* used to check tail merging loop due to corrupted images */ 525 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 526 fe->tailpcl = pcl; 527 528 z_erofs_try_to_claim_pcluster(fe); 529 return 0; 530 } 531 532 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 533 { 534 struct erofs_map_blocks *map = &fe->map; 535 bool ztailpacking = map->m_flags & EROFS_MAP_META; 536 struct z_erofs_pcluster *pcl; 537 struct erofs_workgroup *grp; 538 int err; 539 540 if (!(map->m_flags & EROFS_MAP_ENCODED)) { 541 DBG_BUGON(1); 542 return -EFSCORRUPTED; 543 } 544 545 /* no available pcluster, let's allocate one */ 546 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 547 map->m_plen >> PAGE_SHIFT); 548 if (IS_ERR(pcl)) 549 return PTR_ERR(pcl); 550 551 atomic_set(&pcl->obj.refcount, 1); 552 pcl->algorithmformat = map->m_algorithmformat; 553 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | 554 (map->m_flags & EROFS_MAP_FULL_MAPPED ? 555 Z_EROFS_PCLUSTER_FULL_LENGTH : 0); 556 557 /* new pclusters should be claimed as type 1, primary and followed */ 558 pcl->next = fe->owned_head; 559 pcl->pageofs_out = map->m_la & ~PAGE_MASK; 560 fe->mode = COLLECT_PRIMARY_FOLLOWED; 561 562 /* 563 * lock all primary followed works before visible to others 564 * and mutex_trylock *never* fails for a new pcluster. 565 */ 566 mutex_init(&pcl->lock); 567 DBG_BUGON(!mutex_trylock(&pcl->lock)); 568 569 if (ztailpacking) { 570 pcl->obj.index = 0; /* which indicates ztailpacking */ 571 pcl->pageofs_in = erofs_blkoff(map->m_pa); 572 pcl->tailpacking_size = map->m_plen; 573 } else { 574 pcl->obj.index = map->m_pa >> PAGE_SHIFT; 575 576 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 577 if (IS_ERR(grp)) { 578 err = PTR_ERR(grp); 579 goto err_out; 580 } 581 582 if (grp != &pcl->obj) { 583 fe->pcl = container_of(grp, 584 struct z_erofs_pcluster, obj); 585 err = -EEXIST; 586 goto err_out; 587 } 588 } 589 /* used to check tail merging loop due to corrupted images */ 590 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL) 591 fe->tailpcl = pcl; 592 fe->owned_head = &pcl->next; 593 fe->pcl = pcl; 594 return 0; 595 596 err_out: 597 mutex_unlock(&pcl->lock); 598 z_erofs_free_pcluster(pcl); 599 return err; 600 } 601 602 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe) 603 { 604 struct erofs_map_blocks *map = &fe->map; 605 struct erofs_workgroup *grp = NULL; 606 int ret; 607 608 DBG_BUGON(fe->pcl); 609 610 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 611 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 612 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 613 614 if (!(map->m_flags & EROFS_MAP_META)) { 615 grp = erofs_find_workgroup(fe->inode->i_sb, 616 map->m_pa >> PAGE_SHIFT); 617 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 618 DBG_BUGON(1); 619 return -EFSCORRUPTED; 620 } 621 622 if (grp) { 623 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 624 ret = -EEXIST; 625 } else { 626 ret = z_erofs_register_pcluster(fe); 627 } 628 629 if (ret == -EEXIST) { 630 ret = z_erofs_lookup_pcluster(fe); 631 if (ret) { 632 erofs_workgroup_put(&fe->pcl->obj); 633 return ret; 634 } 635 } else if (ret) { 636 return ret; 637 } 638 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 639 Z_EROFS_NR_INLINE_PAGEVECS, fe->pcl->vcnt); 640 /* since file-backed online pages are traversed in reverse order */ 641 fe->icpage_ptr = fe->pcl->compressed_pages + 642 z_erofs_pclusterpages(fe->pcl); 643 return 0; 644 } 645 646 /* 647 * keep in mind that no referenced pclusters will be freed 648 * only after a RCU grace period. 649 */ 650 static void z_erofs_rcu_callback(struct rcu_head *head) 651 { 652 z_erofs_free_pcluster(container_of(head, 653 struct z_erofs_pcluster, rcu)); 654 } 655 656 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 657 { 658 struct z_erofs_pcluster *const pcl = 659 container_of(grp, struct z_erofs_pcluster, obj); 660 661 call_rcu(&pcl->rcu, z_erofs_rcu_callback); 662 } 663 664 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe) 665 { 666 struct z_erofs_pcluster *pcl = fe->pcl; 667 668 if (!pcl) 669 return false; 670 671 z_erofs_bvec_iter_end(&fe->biter); 672 mutex_unlock(&pcl->lock); 673 674 if (fe->candidate_bvpage) { 675 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage)); 676 fe->candidate_bvpage = NULL; 677 } 678 679 /* 680 * if all pending pages are added, don't hold its reference 681 * any longer if the pcluster isn't hosted by ourselves. 682 */ 683 if (fe->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 684 erofs_workgroup_put(&pcl->obj); 685 686 fe->pcl = NULL; 687 return true; 688 } 689 690 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 691 unsigned int cachestrategy, 692 erofs_off_t la) 693 { 694 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 695 return false; 696 697 if (fe->backmost) 698 return true; 699 700 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 701 la < fe->headoffset; 702 } 703 704 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 705 struct page *page, struct page **pagepool) 706 { 707 struct inode *const inode = fe->inode; 708 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 709 struct erofs_map_blocks *const map = &fe->map; 710 const loff_t offset = page_offset(page); 711 bool tight = true; 712 713 enum z_erofs_cache_alloctype cache_strategy; 714 enum z_erofs_page_type page_type; 715 unsigned int cur, end, spiltted, index; 716 int err = 0; 717 718 /* register locked file pages as online pages in pack */ 719 z_erofs_onlinepage_init(page); 720 721 spiltted = 0; 722 end = PAGE_SIZE; 723 repeat: 724 cur = end - 1; 725 726 if (offset + cur < map->m_la || 727 offset + cur >= map->m_la + map->m_llen) { 728 erofs_dbg("out-of-range map @ pos %llu", offset + cur); 729 730 if (z_erofs_collector_end(fe)) 731 fe->backmost = false; 732 map->m_la = offset + cur; 733 map->m_llen = 0; 734 err = z_erofs_map_blocks_iter(inode, map, 0); 735 if (err) 736 goto err_out; 737 } else { 738 if (fe->pcl) 739 goto hitted; 740 /* didn't get a valid pcluster previously (very rare) */ 741 } 742 743 if (!(map->m_flags & EROFS_MAP_MAPPED)) 744 goto hitted; 745 746 err = z_erofs_collector_begin(fe); 747 if (err) 748 goto err_out; 749 750 if (z_erofs_is_inline_pcluster(fe->pcl)) { 751 void *mp; 752 753 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, 754 erofs_blknr(map->m_pa), EROFS_NO_KMAP); 755 if (IS_ERR(mp)) { 756 err = PTR_ERR(mp); 757 erofs_err(inode->i_sb, 758 "failed to get inline page, err %d", err); 759 goto err_out; 760 } 761 get_page(fe->map.buf.page); 762 WRITE_ONCE(fe->pcl->compressed_pages[0], fe->map.buf.page); 763 fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 764 } else { 765 /* bind cache first when cached decompression is preferred */ 766 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, 767 map->m_la)) 768 cache_strategy = TRYALLOC; 769 else 770 cache_strategy = DONTALLOC; 771 772 z_erofs_bind_cache(fe, cache_strategy, pagepool); 773 } 774 hitted: 775 /* 776 * Ensure the current partial page belongs to this submit chain rather 777 * than other concurrent submit chains or the noio(bypass) chain since 778 * those chains are handled asynchronously thus the page cannot be used 779 * for inplace I/O or pagevec (should be processed in strict order.) 780 */ 781 tight &= (fe->mode >= COLLECT_PRIMARY_HOOKED && 782 fe->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); 783 784 cur = end - min_t(unsigned int, offset + end - map->m_la, end); 785 if (!(map->m_flags & EROFS_MAP_MAPPED)) { 786 zero_user_segment(page, cur, end); 787 goto next_part; 788 } 789 790 /* let's derive page type */ 791 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 792 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 793 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 794 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 795 796 if (cur) 797 tight &= (fe->mode >= COLLECT_PRIMARY_FOLLOWED); 798 799 retry: 800 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 801 .page = page, 802 .offset = offset - map->m_la, 803 .end = end, 804 }), page_type); 805 /* should allocate an additional short-lived page for bvset */ 806 if (err == -EAGAIN && !fe->candidate_bvpage) { 807 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL); 808 set_page_private(fe->candidate_bvpage, 809 Z_EROFS_SHORTLIVED_PAGE); 810 goto retry; 811 } 812 813 if (err) { 814 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage); 815 goto err_out; 816 } 817 818 index = page->index - (map->m_la >> PAGE_SHIFT); 819 820 z_erofs_onlinepage_fixup(page, index, true); 821 822 /* bump up the number of spiltted parts of a page */ 823 ++spiltted; 824 /* also update nr_pages */ 825 fe->pcl->nr_pages = max_t(pgoff_t, fe->pcl->nr_pages, index + 1); 826 next_part: 827 /* can be used for verification */ 828 map->m_llen = offset + cur - map->m_la; 829 830 end = cur; 831 if (end > 0) 832 goto repeat; 833 834 out: 835 z_erofs_onlinepage_endio(page); 836 837 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 838 __func__, page, spiltted, map->m_llen); 839 return err; 840 841 /* if some error occurred while processing this page */ 842 err_out: 843 SetPageError(page); 844 goto out; 845 } 846 847 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi, 848 unsigned int readahead_pages) 849 { 850 /* auto: enable for read_folio, disable for readahead */ 851 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 852 !readahead_pages) 853 return true; 854 855 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 856 (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 857 return true; 858 859 return false; 860 } 861 862 static bool z_erofs_page_is_invalidated(struct page *page) 863 { 864 return !page->mapping && !z_erofs_is_shortlived_page(page); 865 } 866 867 static int z_erofs_parse_out_bvecs(struct z_erofs_pcluster *pcl, 868 struct page **pages, struct page **pagepool) 869 { 870 struct z_erofs_bvec_iter biter; 871 struct page *old_bvpage; 872 int i, err = 0; 873 874 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, 875 Z_EROFS_NR_INLINE_PAGEVECS, 0); 876 for (i = 0; i < pcl->vcnt; ++i) { 877 struct z_erofs_bvec bvec; 878 unsigned int pagenr; 879 880 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 881 882 if (old_bvpage) 883 z_erofs_put_shortlivedpage(pagepool, old_bvpage); 884 885 pagenr = (bvec.offset + pcl->pageofs_out) >> PAGE_SHIFT; 886 DBG_BUGON(pagenr >= pcl->nr_pages); 887 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 888 /* 889 * currently EROFS doesn't support multiref(dedup), 890 * so here erroring out one multiref page. 891 */ 892 if (pages[pagenr]) { 893 DBG_BUGON(1); 894 SetPageError(pages[pagenr]); 895 z_erofs_onlinepage_endio(pages[pagenr]); 896 err = -EFSCORRUPTED; 897 } 898 pages[pagenr] = bvec.page; 899 } 900 901 old_bvpage = z_erofs_bvec_iter_end(&biter); 902 if (old_bvpage) 903 z_erofs_put_shortlivedpage(pagepool, old_bvpage); 904 return err; 905 } 906 907 static int z_erofs_decompress_pcluster(struct super_block *sb, 908 struct z_erofs_pcluster *pcl, 909 struct page **pagepool) 910 { 911 struct erofs_sb_info *const sbi = EROFS_SB(sb); 912 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 913 unsigned int i, inputsize, outputsize, llen, nr_pages; 914 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 915 struct page **pages, **compressed_pages, *page; 916 917 bool overlapped, partial; 918 int err; 919 920 might_sleep(); 921 DBG_BUGON(!READ_ONCE(pcl->nr_pages)); 922 923 mutex_lock(&pcl->lock); 924 nr_pages = pcl->nr_pages; 925 926 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { 927 pages = pages_onstack; 928 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 929 mutex_trylock(&z_pagemap_global_lock)) { 930 pages = z_pagemap_global; 931 } else { 932 gfp_t gfp_flags = GFP_KERNEL; 933 934 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 935 gfp_flags |= __GFP_NOFAIL; 936 937 pages = kvmalloc_array(nr_pages, sizeof(struct page *), 938 gfp_flags); 939 940 /* fallback to global pagemap for the lowmem scenario */ 941 if (!pages) { 942 mutex_lock(&z_pagemap_global_lock); 943 pages = z_pagemap_global; 944 } 945 } 946 947 for (i = 0; i < nr_pages; ++i) 948 pages[i] = NULL; 949 950 err = z_erofs_parse_out_bvecs(pcl, pages, pagepool); 951 952 overlapped = false; 953 compressed_pages = pcl->compressed_pages; 954 955 for (i = 0; i < pclusterpages; ++i) { 956 unsigned int pagenr; 957 958 page = compressed_pages[i]; 959 /* all compressed pages ought to be valid */ 960 DBG_BUGON(!page); 961 962 if (z_erofs_is_inline_pcluster(pcl)) { 963 if (!PageUptodate(page)) 964 err = -EIO; 965 continue; 966 } 967 968 DBG_BUGON(z_erofs_page_is_invalidated(page)); 969 if (!z_erofs_is_shortlived_page(page)) { 970 if (erofs_page_is_managed(sbi, page)) { 971 if (!PageUptodate(page)) 972 err = -EIO; 973 continue; 974 } 975 976 /* 977 * only if non-head page can be selected 978 * for inplace decompression 979 */ 980 pagenr = z_erofs_onlinepage_index(page); 981 982 DBG_BUGON(pagenr >= nr_pages); 983 if (pages[pagenr]) { 984 DBG_BUGON(1); 985 SetPageError(pages[pagenr]); 986 z_erofs_onlinepage_endio(pages[pagenr]); 987 err = -EFSCORRUPTED; 988 } 989 pages[pagenr] = page; 990 991 overlapped = true; 992 } 993 994 /* PG_error needs checking for all non-managed pages */ 995 if (PageError(page)) { 996 DBG_BUGON(PageUptodate(page)); 997 err = -EIO; 998 } 999 } 1000 1001 if (err) 1002 goto out; 1003 1004 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 1005 if (nr_pages << PAGE_SHIFT >= pcl->pageofs_out + llen) { 1006 outputsize = llen; 1007 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 1008 } else { 1009 outputsize = (nr_pages << PAGE_SHIFT) - pcl->pageofs_out; 1010 partial = true; 1011 } 1012 1013 if (z_erofs_is_inline_pcluster(pcl)) 1014 inputsize = pcl->tailpacking_size; 1015 else 1016 inputsize = pclusterpages * PAGE_SIZE; 1017 1018 err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 1019 .sb = sb, 1020 .in = compressed_pages, 1021 .out = pages, 1022 .pageofs_in = pcl->pageofs_in, 1023 .pageofs_out = pcl->pageofs_out, 1024 .inputsize = inputsize, 1025 .outputsize = outputsize, 1026 .alg = pcl->algorithmformat, 1027 .inplace_io = overlapped, 1028 .partial_decoding = partial 1029 }, pagepool); 1030 1031 out: 1032 /* must handle all compressed pages before actual file pages */ 1033 if (z_erofs_is_inline_pcluster(pcl)) { 1034 page = compressed_pages[0]; 1035 WRITE_ONCE(compressed_pages[0], NULL); 1036 put_page(page); 1037 } else { 1038 for (i = 0; i < pclusterpages; ++i) { 1039 page = compressed_pages[i]; 1040 1041 if (erofs_page_is_managed(sbi, page)) 1042 continue; 1043 1044 /* recycle all individual short-lived pages */ 1045 (void)z_erofs_put_shortlivedpage(pagepool, page); 1046 WRITE_ONCE(compressed_pages[i], NULL); 1047 } 1048 } 1049 1050 for (i = 0; i < nr_pages; ++i) { 1051 page = pages[i]; 1052 if (!page) 1053 continue; 1054 1055 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1056 1057 /* recycle all individual short-lived pages */ 1058 if (z_erofs_put_shortlivedpage(pagepool, page)) 1059 continue; 1060 1061 if (err < 0) 1062 SetPageError(page); 1063 1064 z_erofs_onlinepage_endio(page); 1065 } 1066 1067 if (pages == z_pagemap_global) 1068 mutex_unlock(&z_pagemap_global_lock); 1069 else if (pages != pages_onstack) 1070 kvfree(pages); 1071 1072 pcl->nr_pages = 0; 1073 pcl->bvset.nextpage = NULL; 1074 pcl->vcnt = 0; 1075 1076 /* pcluster lock MUST be taken before the following line */ 1077 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 1078 mutex_unlock(&pcl->lock); 1079 return err; 1080 } 1081 1082 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1083 struct page **pagepool) 1084 { 1085 z_erofs_next_pcluster_t owned = io->head; 1086 1087 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 1088 struct z_erofs_pcluster *pcl; 1089 1090 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 1091 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 1092 1093 /* no possible that 'owned' equals NULL */ 1094 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 1095 1096 pcl = container_of(owned, struct z_erofs_pcluster, next); 1097 owned = READ_ONCE(pcl->next); 1098 1099 z_erofs_decompress_pcluster(io->sb, pcl, pagepool); 1100 erofs_workgroup_put(&pcl->obj); 1101 } 1102 } 1103 1104 static void z_erofs_decompressqueue_work(struct work_struct *work) 1105 { 1106 struct z_erofs_decompressqueue *bgq = 1107 container_of(work, struct z_erofs_decompressqueue, u.work); 1108 struct page *pagepool = NULL; 1109 1110 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1111 z_erofs_decompress_queue(bgq, &pagepool); 1112 1113 erofs_release_pages(&pagepool); 1114 kvfree(bgq); 1115 } 1116 1117 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 1118 bool sync, int bios) 1119 { 1120 struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 1121 1122 /* wake up the caller thread for sync decompression */ 1123 if (sync) { 1124 if (!atomic_add_return(bios, &io->pending_bios)) 1125 complete(&io->u.done); 1126 1127 return; 1128 } 1129 1130 if (atomic_add_return(bios, &io->pending_bios)) 1131 return; 1132 /* Use workqueue and sync decompression for atomic contexts only */ 1133 if (in_atomic() || irqs_disabled()) { 1134 queue_work(z_erofs_workqueue, &io->u.work); 1135 /* enable sync decompression for readahead */ 1136 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 1137 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 1138 return; 1139 } 1140 z_erofs_decompressqueue_work(&io->u.work); 1141 } 1142 1143 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 1144 unsigned int nr, 1145 struct page **pagepool, 1146 struct address_space *mc) 1147 { 1148 const pgoff_t index = pcl->obj.index; 1149 gfp_t gfp = mapping_gfp_mask(mc); 1150 bool tocache = false; 1151 1152 struct address_space *mapping; 1153 struct page *oldpage, *page; 1154 1155 compressed_page_t t; 1156 int justfound; 1157 1158 repeat: 1159 page = READ_ONCE(pcl->compressed_pages[nr]); 1160 oldpage = page; 1161 1162 if (!page) 1163 goto out_allocpage; 1164 1165 /* process the target tagged pointer */ 1166 t = tagptr_init(compressed_page_t, page); 1167 justfound = tagptr_unfold_tags(t); 1168 page = tagptr_unfold_ptr(t); 1169 1170 /* 1171 * preallocated cached pages, which is used to avoid direct reclaim 1172 * otherwise, it will go inplace I/O path instead. 1173 */ 1174 if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1175 WRITE_ONCE(pcl->compressed_pages[nr], page); 1176 set_page_private(page, 0); 1177 tocache = true; 1178 goto out_tocache; 1179 } 1180 mapping = READ_ONCE(page->mapping); 1181 1182 /* 1183 * file-backed online pages in plcuster are all locked steady, 1184 * therefore it is impossible for `mapping' to be NULL. 1185 */ 1186 if (mapping && mapping != mc) 1187 /* ought to be unmanaged pages */ 1188 goto out; 1189 1190 /* directly return for shortlived page as well */ 1191 if (z_erofs_is_shortlived_page(page)) 1192 goto out; 1193 1194 lock_page(page); 1195 1196 /* only true if page reclaim goes wrong, should never happen */ 1197 DBG_BUGON(justfound && PagePrivate(page)); 1198 1199 /* the page is still in manage cache */ 1200 if (page->mapping == mc) { 1201 WRITE_ONCE(pcl->compressed_pages[nr], page); 1202 1203 ClearPageError(page); 1204 if (!PagePrivate(page)) { 1205 /* 1206 * impossible to be !PagePrivate(page) for 1207 * the current restriction as well if 1208 * the page is already in compressed_pages[]. 1209 */ 1210 DBG_BUGON(!justfound); 1211 1212 justfound = 0; 1213 set_page_private(page, (unsigned long)pcl); 1214 SetPagePrivate(page); 1215 } 1216 1217 /* no need to submit io if it is already up-to-date */ 1218 if (PageUptodate(page)) { 1219 unlock_page(page); 1220 page = NULL; 1221 } 1222 goto out; 1223 } 1224 1225 /* 1226 * the managed page has been truncated, it's unsafe to 1227 * reuse this one, let's allocate a new cache-managed page. 1228 */ 1229 DBG_BUGON(page->mapping); 1230 DBG_BUGON(!justfound); 1231 1232 tocache = true; 1233 unlock_page(page); 1234 put_page(page); 1235 out_allocpage: 1236 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1237 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 1238 erofs_pagepool_add(pagepool, page); 1239 cond_resched(); 1240 goto repeat; 1241 } 1242 out_tocache: 1243 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1244 /* turn into temporary page if fails (1 ref) */ 1245 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1246 goto out; 1247 } 1248 attach_page_private(page, pcl); 1249 /* drop a refcount added by allocpage (then we have 2 refs here) */ 1250 put_page(page); 1251 1252 out: /* the only exit (for tracing and debugging) */ 1253 return page; 1254 } 1255 1256 static struct z_erofs_decompressqueue * 1257 jobqueue_init(struct super_block *sb, 1258 struct z_erofs_decompressqueue *fgq, bool *fg) 1259 { 1260 struct z_erofs_decompressqueue *q; 1261 1262 if (fg && !*fg) { 1263 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1264 if (!q) { 1265 *fg = true; 1266 goto fg_out; 1267 } 1268 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1269 } else { 1270 fg_out: 1271 q = fgq; 1272 init_completion(&fgq->u.done); 1273 atomic_set(&fgq->pending_bios, 0); 1274 } 1275 q->sb = sb; 1276 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1277 return q; 1278 } 1279 1280 /* define decompression jobqueue types */ 1281 enum { 1282 JQ_BYPASS, 1283 JQ_SUBMIT, 1284 NR_JOBQUEUES, 1285 }; 1286 1287 static void *jobqueueset_init(struct super_block *sb, 1288 struct z_erofs_decompressqueue *q[], 1289 struct z_erofs_decompressqueue *fgq, bool *fg) 1290 { 1291 /* 1292 * if managed cache is enabled, bypass jobqueue is needed, 1293 * no need to read from device for all pclusters in this queue. 1294 */ 1295 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1296 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 1297 1298 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 1299 } 1300 1301 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 1302 z_erofs_next_pcluster_t qtail[], 1303 z_erofs_next_pcluster_t owned_head) 1304 { 1305 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 1306 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 1307 1308 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1309 if (owned_head == Z_EROFS_PCLUSTER_TAIL) 1310 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1311 1312 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 1313 1314 WRITE_ONCE(*submit_qtail, owned_head); 1315 WRITE_ONCE(*bypass_qtail, &pcl->next); 1316 1317 qtail[JQ_BYPASS] = &pcl->next; 1318 } 1319 1320 static void z_erofs_decompressqueue_endio(struct bio *bio) 1321 { 1322 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 1323 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 1324 blk_status_t err = bio->bi_status; 1325 struct bio_vec *bvec; 1326 struct bvec_iter_all iter_all; 1327 1328 bio_for_each_segment_all(bvec, bio, iter_all) { 1329 struct page *page = bvec->bv_page; 1330 1331 DBG_BUGON(PageUptodate(page)); 1332 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1333 1334 if (err) 1335 SetPageError(page); 1336 1337 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 1338 if (!err) 1339 SetPageUptodate(page); 1340 unlock_page(page); 1341 } 1342 } 1343 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 1344 bio_put(bio); 1345 } 1346 1347 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1348 struct page **pagepool, 1349 struct z_erofs_decompressqueue *fgq, 1350 bool *force_fg) 1351 { 1352 struct super_block *sb = f->inode->i_sb; 1353 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 1354 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1355 struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 1356 void *bi_private; 1357 z_erofs_next_pcluster_t owned_head = f->owned_head; 1358 /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 1359 pgoff_t last_index; 1360 struct block_device *last_bdev; 1361 unsigned int nr_bios = 0; 1362 struct bio *bio = NULL; 1363 1364 bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1365 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1366 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 1367 1368 /* by default, all need io submission */ 1369 q[JQ_SUBMIT]->head = owned_head; 1370 1371 do { 1372 struct erofs_map_dev mdev; 1373 struct z_erofs_pcluster *pcl; 1374 pgoff_t cur, end; 1375 unsigned int i = 0; 1376 bool bypass = true; 1377 1378 /* no possible 'owned_head' equals the following */ 1379 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 1380 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 1381 1382 pcl = container_of(owned_head, struct z_erofs_pcluster, next); 1383 1384 /* close the main owned chain at first */ 1385 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 1386 Z_EROFS_PCLUSTER_TAIL_CLOSED); 1387 if (z_erofs_is_inline_pcluster(pcl)) { 1388 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1389 continue; 1390 } 1391 1392 /* no device id here, thus it will always succeed */ 1393 mdev = (struct erofs_map_dev) { 1394 .m_pa = blknr_to_addr(pcl->obj.index), 1395 }; 1396 (void)erofs_map_dev(sb, &mdev); 1397 1398 cur = erofs_blknr(mdev.m_pa); 1399 end = cur + pcl->pclusterpages; 1400 1401 do { 1402 struct page *page; 1403 1404 page = pickup_page_for_submission(pcl, i++, pagepool, 1405 mc); 1406 if (!page) 1407 continue; 1408 1409 if (bio && (cur != last_index + 1 || 1410 last_bdev != mdev.m_bdev)) { 1411 submit_bio_retry: 1412 submit_bio(bio); 1413 bio = NULL; 1414 } 1415 1416 if (!bio) { 1417 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 1418 REQ_OP_READ, GFP_NOIO); 1419 bio->bi_end_io = z_erofs_decompressqueue_endio; 1420 1421 last_bdev = mdev.m_bdev; 1422 bio->bi_iter.bi_sector = (sector_t)cur << 1423 LOG_SECTORS_PER_BLOCK; 1424 bio->bi_private = bi_private; 1425 if (f->readahead) 1426 bio->bi_opf |= REQ_RAHEAD; 1427 ++nr_bios; 1428 } 1429 1430 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 1431 goto submit_bio_retry; 1432 1433 last_index = cur; 1434 bypass = false; 1435 } while (++cur < end); 1436 1437 if (!bypass) 1438 qtail[JQ_SUBMIT] = &pcl->next; 1439 else 1440 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1441 } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 1442 1443 if (bio) 1444 submit_bio(bio); 1445 1446 /* 1447 * although background is preferred, no one is pending for submission. 1448 * don't issue workqueue for decompression but drop it directly instead. 1449 */ 1450 if (!*force_fg && !nr_bios) { 1451 kvfree(q[JQ_SUBMIT]); 1452 return; 1453 } 1454 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 1455 } 1456 1457 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, 1458 struct page **pagepool, bool force_fg) 1459 { 1460 struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 1461 1462 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) 1463 return; 1464 z_erofs_submit_queue(f, pagepool, io, &force_fg); 1465 1466 /* handle bypass queue (no i/o pclusters) immediately */ 1467 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 1468 1469 if (!force_fg) 1470 return; 1471 1472 /* wait until all bios are completed */ 1473 wait_for_completion_io(&io[JQ_SUBMIT].u.done); 1474 1475 /* handle synchronous decompress queue in the caller context */ 1476 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 1477 } 1478 1479 /* 1480 * Since partial uptodate is still unimplemented for now, we have to use 1481 * approximate readmore strategies as a start. 1482 */ 1483 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, 1484 struct readahead_control *rac, 1485 erofs_off_t end, 1486 struct page **pagepool, 1487 bool backmost) 1488 { 1489 struct inode *inode = f->inode; 1490 struct erofs_map_blocks *map = &f->map; 1491 erofs_off_t cur; 1492 int err; 1493 1494 if (backmost) { 1495 map->m_la = end; 1496 err = z_erofs_map_blocks_iter(inode, map, 1497 EROFS_GET_BLOCKS_READMORE); 1498 if (err) 1499 return; 1500 1501 /* expend ra for the trailing edge if readahead */ 1502 if (rac) { 1503 loff_t newstart = readahead_pos(rac); 1504 1505 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 1506 readahead_expand(rac, newstart, cur - newstart); 1507 return; 1508 } 1509 end = round_up(end, PAGE_SIZE); 1510 } else { 1511 end = round_up(map->m_la, PAGE_SIZE); 1512 1513 if (!map->m_llen) 1514 return; 1515 } 1516 1517 cur = map->m_la + map->m_llen - 1; 1518 while (cur >= end) { 1519 pgoff_t index = cur >> PAGE_SHIFT; 1520 struct page *page; 1521 1522 page = erofs_grab_cache_page_nowait(inode->i_mapping, index); 1523 if (page) { 1524 if (PageUptodate(page)) { 1525 unlock_page(page); 1526 } else { 1527 err = z_erofs_do_read_page(f, page, pagepool); 1528 if (err) 1529 erofs_err(inode->i_sb, 1530 "readmore error at page %lu @ nid %llu", 1531 index, EROFS_I(inode)->nid); 1532 } 1533 put_page(page); 1534 } 1535 1536 if (cur < PAGE_SIZE) 1537 break; 1538 cur = (index << PAGE_SHIFT) - 1; 1539 } 1540 } 1541 1542 static int z_erofs_read_folio(struct file *file, struct folio *folio) 1543 { 1544 struct page *page = &folio->page; 1545 struct inode *const inode = page->mapping->host; 1546 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1547 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1548 struct page *pagepool = NULL; 1549 int err; 1550 1551 trace_erofs_readpage(page, false); 1552 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 1553 1554 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1, 1555 &pagepool, true); 1556 err = z_erofs_do_read_page(&f, page, &pagepool); 1557 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false); 1558 1559 (void)z_erofs_collector_end(&f); 1560 1561 /* if some compressed cluster ready, need submit them anyway */ 1562 z_erofs_runqueue(&f, &pagepool, 1563 z_erofs_get_sync_decompress_policy(sbi, 0)); 1564 1565 if (err) 1566 erofs_err(inode->i_sb, "failed to read, err [%d]", err); 1567 1568 erofs_put_metabuf(&f.map.buf); 1569 erofs_release_pages(&pagepool); 1570 return err; 1571 } 1572 1573 static void z_erofs_readahead(struct readahead_control *rac) 1574 { 1575 struct inode *const inode = rac->mapping->host; 1576 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1577 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1578 struct page *pagepool = NULL, *head = NULL, *page; 1579 unsigned int nr_pages; 1580 1581 f.readahead = true; 1582 f.headoffset = readahead_pos(rac); 1583 1584 z_erofs_pcluster_readmore(&f, rac, f.headoffset + 1585 readahead_length(rac) - 1, &pagepool, true); 1586 nr_pages = readahead_count(rac); 1587 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 1588 1589 while ((page = readahead_page(rac))) { 1590 set_page_private(page, (unsigned long)head); 1591 head = page; 1592 } 1593 1594 while (head) { 1595 struct page *page = head; 1596 int err; 1597 1598 /* traversal in reverse order */ 1599 head = (void *)page_private(page); 1600 1601 err = z_erofs_do_read_page(&f, page, &pagepool); 1602 if (err) 1603 erofs_err(inode->i_sb, 1604 "readahead error at page %lu @ nid %llu", 1605 page->index, EROFS_I(inode)->nid); 1606 put_page(page); 1607 } 1608 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false); 1609 (void)z_erofs_collector_end(&f); 1610 1611 z_erofs_runqueue(&f, &pagepool, 1612 z_erofs_get_sync_decompress_policy(sbi, nr_pages)); 1613 erofs_put_metabuf(&f.map.buf); 1614 erofs_release_pages(&pagepool); 1615 } 1616 1617 const struct address_space_operations z_erofs_aops = { 1618 .read_folio = z_erofs_read_folio, 1619 .readahead = z_erofs_readahead, 1620 }; 1621