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