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