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