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 pcluster was just linked to a decompression chain by us. It can 511 * also be linked with the remaining pclusters, which means if the 512 * processing page is the tail page of a pcluster, this pcluster can 513 * safely use the whole page (since the previous pcluster is within the 514 * same chain) for in-place I/O, as illustrated below: 515 * ___________________________________________________ 516 * | tail (partial) page | head (partial) page | 517 * | (of the current pcl) | (of the previous pcl) | 518 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____| 519 * 520 * [ (*) the page above can be used as inplace I/O. ] 521 */ 522 Z_EROFS_PCLUSTER_FOLLOWED, 523 }; 524 525 struct z_erofs_decompress_frontend { 526 struct inode *const inode; 527 struct erofs_map_blocks map; 528 struct z_erofs_bvec_iter biter; 529 530 struct page *pagepool; 531 struct page *candidate_bvpage; 532 struct z_erofs_pcluster *pcl; 533 z_erofs_next_pcluster_t owned_head; 534 enum z_erofs_pclustermode mode; 535 536 /* used for applying cache strategy on the fly */ 537 bool backmost; 538 erofs_off_t headoffset; 539 540 /* a pointer used to pick up inplace I/O pages */ 541 unsigned int icur; 542 }; 543 544 #define DECOMPRESS_FRONTEND_INIT(__i) { \ 545 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 546 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true } 547 548 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe) 549 { 550 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy; 551 552 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 553 return false; 554 555 if (fe->backmost) 556 return true; 557 558 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 559 fe->map.m_la < fe->headoffset) 560 return true; 561 562 return false; 563 } 564 565 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe) 566 { 567 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode)); 568 struct z_erofs_pcluster *pcl = fe->pcl; 569 bool shouldalloc = z_erofs_should_alloc_cache(fe); 570 bool standalone = true; 571 /* 572 * optimistic allocation without direct reclaim since inplace I/O 573 * can be used if low memory otherwise. 574 */ 575 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 576 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 577 unsigned int i; 578 579 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED) 580 return; 581 582 for (i = 0; i < pcl->pclusterpages; ++i) { 583 struct page *page; 584 void *t; /* mark pages just found for debugging */ 585 struct page *newpage = NULL; 586 587 /* the compressed page was loaded before */ 588 if (READ_ONCE(pcl->compressed_bvecs[i].page)) 589 continue; 590 591 page = find_get_page(mc, pcl->obj.index + i); 592 593 if (page) { 594 t = (void *)((unsigned long)page | 1); 595 } else { 596 /* I/O is needed, no possible to decompress directly */ 597 standalone = false; 598 if (!shouldalloc) 599 continue; 600 601 /* 602 * try to use cached I/O if page allocation 603 * succeeds or fallback to in-place I/O instead 604 * to avoid any direct reclaim. 605 */ 606 newpage = erofs_allocpage(&fe->pagepool, gfp); 607 if (!newpage) 608 continue; 609 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); 610 t = (void *)((unsigned long)newpage | 1); 611 } 612 613 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t)) 614 continue; 615 616 if (page) 617 put_page(page); 618 else if (newpage) 619 erofs_pagepool_add(&fe->pagepool, newpage); 620 } 621 622 /* 623 * don't do inplace I/O if all compressed pages are available in 624 * managed cache since it can be moved to the bypass queue instead. 625 */ 626 if (standalone) 627 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 628 } 629 630 /* called by erofs_shrinker to get rid of all compressed_pages */ 631 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 632 struct erofs_workgroup *grp) 633 { 634 struct z_erofs_pcluster *const pcl = 635 container_of(grp, struct z_erofs_pcluster, obj); 636 int i; 637 638 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 639 /* 640 * refcount of workgroup is now freezed as 0, 641 * therefore no need to worry about available decompression users. 642 */ 643 for (i = 0; i < pcl->pclusterpages; ++i) { 644 struct page *page = pcl->compressed_bvecs[i].page; 645 646 if (!page) 647 continue; 648 649 /* block other users from reclaiming or migrating the page */ 650 if (!trylock_page(page)) 651 return -EBUSY; 652 653 if (!erofs_page_is_managed(sbi, page)) 654 continue; 655 656 /* barrier is implied in the following 'unlock_page' */ 657 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 658 detach_page_private(page); 659 unlock_page(page); 660 } 661 return 0; 662 } 663 664 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp) 665 { 666 struct z_erofs_pcluster *pcl = folio_get_private(folio); 667 bool ret; 668 int i; 669 670 if (!folio_test_private(folio)) 671 return true; 672 673 ret = false; 674 spin_lock(&pcl->obj.lockref.lock); 675 if (pcl->obj.lockref.count > 0) 676 goto out; 677 678 DBG_BUGON(z_erofs_is_inline_pcluster(pcl)); 679 for (i = 0; i < pcl->pclusterpages; ++i) { 680 if (pcl->compressed_bvecs[i].page == &folio->page) { 681 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 682 ret = true; 683 break; 684 } 685 } 686 if (ret) 687 folio_detach_private(folio); 688 out: 689 spin_unlock(&pcl->obj.lockref.lock); 690 return ret; 691 } 692 693 /* 694 * It will be called only on inode eviction. In case that there are still some 695 * decompression requests in progress, wait with rescheduling for a bit here. 696 * An extra lock could be introduced instead but it seems unnecessary. 697 */ 698 static void z_erofs_cache_invalidate_folio(struct folio *folio, 699 size_t offset, size_t length) 700 { 701 const size_t stop = length + offset; 702 703 /* Check for potential overflow in debug mode */ 704 DBG_BUGON(stop > folio_size(folio) || stop < length); 705 706 if (offset == 0 && stop == folio_size(folio)) 707 while (!z_erofs_cache_release_folio(folio, GFP_NOFS)) 708 cond_resched(); 709 } 710 711 static const struct address_space_operations z_erofs_cache_aops = { 712 .release_folio = z_erofs_cache_release_folio, 713 .invalidate_folio = z_erofs_cache_invalidate_folio, 714 }; 715 716 int erofs_init_managed_cache(struct super_block *sb) 717 { 718 struct inode *const inode = new_inode(sb); 719 720 if (!inode) 721 return -ENOMEM; 722 723 set_nlink(inode, 1); 724 inode->i_size = OFFSET_MAX; 725 inode->i_mapping->a_ops = &z_erofs_cache_aops; 726 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 727 EROFS_SB(sb)->managed_cache = inode; 728 return 0; 729 } 730 731 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe, 732 struct z_erofs_bvec *bvec) 733 { 734 struct z_erofs_pcluster *const pcl = fe->pcl; 735 736 while (fe->icur > 0) { 737 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page, 738 NULL, bvec->page)) { 739 pcl->compressed_bvecs[fe->icur] = *bvec; 740 return true; 741 } 742 } 743 return false; 744 } 745 746 /* callers must be with pcluster lock held */ 747 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe, 748 struct z_erofs_bvec *bvec, bool exclusive) 749 { 750 int ret; 751 752 if (exclusive) { 753 /* give priority for inplaceio to use file pages first */ 754 if (z_erofs_try_inplace_io(fe, bvec)) 755 return 0; 756 /* otherwise, check if it can be used as a bvpage */ 757 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED && 758 !fe->candidate_bvpage) 759 fe->candidate_bvpage = bvec->page; 760 } 761 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage, 762 &fe->pagepool); 763 fe->pcl->vcnt += (ret >= 0); 764 return ret; 765 } 766 767 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f) 768 { 769 struct z_erofs_pcluster *pcl = f->pcl; 770 z_erofs_next_pcluster_t *owned_head = &f->owned_head; 771 772 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 773 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 774 *owned_head) == Z_EROFS_PCLUSTER_NIL) { 775 *owned_head = &pcl->next; 776 /* so we can attach this pcluster to our submission chain. */ 777 f->mode = Z_EROFS_PCLUSTER_FOLLOWED; 778 return; 779 } 780 781 /* type 2, it belongs to an ongoing chain */ 782 f->mode = Z_EROFS_PCLUSTER_INFLIGHT; 783 } 784 785 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe) 786 { 787 struct erofs_map_blocks *map = &fe->map; 788 bool ztailpacking = map->m_flags & EROFS_MAP_META; 789 struct z_erofs_pcluster *pcl; 790 struct erofs_workgroup *grp; 791 int err; 792 793 if (!(map->m_flags & EROFS_MAP_ENCODED) || 794 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) { 795 DBG_BUGON(1); 796 return -EFSCORRUPTED; 797 } 798 799 /* no available pcluster, let's allocate one */ 800 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 : 801 map->m_plen >> PAGE_SHIFT); 802 if (IS_ERR(pcl)) 803 return PTR_ERR(pcl); 804 805 spin_lock_init(&pcl->obj.lockref.lock); 806 pcl->algorithmformat = map->m_algorithmformat; 807 pcl->length = 0; 808 pcl->partial = true; 809 810 /* new pclusters should be claimed as type 1, primary and followed */ 811 pcl->next = fe->owned_head; 812 pcl->pageofs_out = map->m_la & ~PAGE_MASK; 813 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED; 814 815 /* 816 * lock all primary followed works before visible to others 817 * and mutex_trylock *never* fails for a new pcluster. 818 */ 819 mutex_init(&pcl->lock); 820 DBG_BUGON(!mutex_trylock(&pcl->lock)); 821 822 if (ztailpacking) { 823 pcl->obj.index = 0; /* which indicates ztailpacking */ 824 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa); 825 pcl->tailpacking_size = map->m_plen; 826 } else { 827 pcl->obj.index = map->m_pa >> PAGE_SHIFT; 828 829 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj); 830 if (IS_ERR(grp)) { 831 err = PTR_ERR(grp); 832 goto err_out; 833 } 834 835 if (grp != &pcl->obj) { 836 fe->pcl = container_of(grp, 837 struct z_erofs_pcluster, obj); 838 err = -EEXIST; 839 goto err_out; 840 } 841 } 842 fe->owned_head = &pcl->next; 843 fe->pcl = pcl; 844 return 0; 845 846 err_out: 847 mutex_unlock(&pcl->lock); 848 z_erofs_free_pcluster(pcl); 849 return err; 850 } 851 852 static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe) 853 { 854 struct erofs_map_blocks *map = &fe->map; 855 struct erofs_workgroup *grp = NULL; 856 int ret; 857 858 DBG_BUGON(fe->pcl); 859 860 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */ 861 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL); 862 863 if (!(map->m_flags & EROFS_MAP_META)) { 864 grp = erofs_find_workgroup(fe->inode->i_sb, 865 map->m_pa >> PAGE_SHIFT); 866 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) { 867 DBG_BUGON(1); 868 return -EFSCORRUPTED; 869 } 870 871 if (grp) { 872 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj); 873 ret = -EEXIST; 874 } else { 875 ret = z_erofs_register_pcluster(fe); 876 } 877 878 if (ret == -EEXIST) { 879 mutex_lock(&fe->pcl->lock); 880 z_erofs_try_to_claim_pcluster(fe); 881 } else if (ret) { 882 return ret; 883 } 884 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset, 885 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt); 886 /* since file-backed online pages are traversed in reverse order */ 887 fe->icur = z_erofs_pclusterpages(fe->pcl); 888 return 0; 889 } 890 891 /* 892 * keep in mind that no referenced pclusters will be freed 893 * only after a RCU grace period. 894 */ 895 static void z_erofs_rcu_callback(struct rcu_head *head) 896 { 897 z_erofs_free_pcluster(container_of(head, 898 struct z_erofs_pcluster, rcu)); 899 } 900 901 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 902 { 903 struct z_erofs_pcluster *const pcl = 904 container_of(grp, struct z_erofs_pcluster, obj); 905 906 call_rcu(&pcl->rcu, z_erofs_rcu_callback); 907 } 908 909 static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe) 910 { 911 struct z_erofs_pcluster *pcl = fe->pcl; 912 913 if (!pcl) 914 return; 915 916 z_erofs_bvec_iter_end(&fe->biter); 917 mutex_unlock(&pcl->lock); 918 919 if (fe->candidate_bvpage) 920 fe->candidate_bvpage = NULL; 921 922 /* 923 * if all pending pages are added, don't hold its reference 924 * any longer if the pcluster isn't hosted by ourselves. 925 */ 926 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) 927 erofs_workgroup_put(&pcl->obj); 928 929 fe->pcl = NULL; 930 fe->backmost = false; 931 } 932 933 static int z_erofs_read_fragment(struct super_block *sb, struct page *page, 934 unsigned int cur, unsigned int end, erofs_off_t pos) 935 { 936 struct inode *packed_inode = EROFS_SB(sb)->packed_inode; 937 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 938 unsigned int cnt; 939 u8 *src; 940 941 if (!packed_inode) 942 return -EFSCORRUPTED; 943 944 buf.inode = packed_inode; 945 for (; cur < end; cur += cnt, pos += cnt) { 946 cnt = min_t(unsigned int, end - cur, 947 sb->s_blocksize - erofs_blkoff(sb, pos)); 948 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP); 949 if (IS_ERR(src)) { 950 erofs_put_metabuf(&buf); 951 return PTR_ERR(src); 952 } 953 memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt); 954 } 955 erofs_put_metabuf(&buf); 956 return 0; 957 } 958 959 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 960 struct page *page) 961 { 962 struct inode *const inode = fe->inode; 963 struct erofs_map_blocks *const map = &fe->map; 964 const loff_t offset = page_offset(page); 965 bool tight = true, exclusive; 966 unsigned int cur, end, len, spiltted; 967 int err = 0; 968 969 /* register locked file pages as online pages in pack */ 970 z_erofs_onlinepage_init(page); 971 972 spiltted = 0; 973 end = PAGE_SIZE; 974 repeat: 975 cur = end - 1; 976 977 if (offset + cur < map->m_la || 978 offset + cur >= map->m_la + map->m_llen) { 979 z_erofs_pcluster_end(fe); 980 map->m_la = offset + cur; 981 map->m_llen = 0; 982 err = z_erofs_map_blocks_iter(inode, map, 0); 983 if (err) 984 goto out; 985 } else { 986 if (fe->pcl) 987 goto hitted; 988 /* didn't get a valid pcluster previously (very rare) */ 989 } 990 991 if (!(map->m_flags & EROFS_MAP_MAPPED) || 992 map->m_flags & EROFS_MAP_FRAGMENT) 993 goto hitted; 994 995 err = z_erofs_pcluster_begin(fe); 996 if (err) 997 goto out; 998 999 if (z_erofs_is_inline_pcluster(fe->pcl)) { 1000 void *mp; 1001 1002 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb, 1003 erofs_blknr(inode->i_sb, map->m_pa), 1004 EROFS_NO_KMAP); 1005 if (IS_ERR(mp)) { 1006 err = PTR_ERR(mp); 1007 erofs_err(inode->i_sb, 1008 "failed to get inline page, err %d", err); 1009 goto out; 1010 } 1011 get_page(fe->map.buf.page); 1012 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, 1013 fe->map.buf.page); 1014 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE; 1015 } else { 1016 /* bind cache first when cached decompression is preferred */ 1017 z_erofs_bind_cache(fe); 1018 } 1019 hitted: 1020 /* 1021 * Ensure the current partial page belongs to this submit chain rather 1022 * than other concurrent submit chains or the noio(bypass) chain since 1023 * those chains are handled asynchronously thus the page cannot be used 1024 * for inplace I/O or bvpage (should be processed in a strict order.) 1025 */ 1026 tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE); 1027 1028 cur = end - min_t(erofs_off_t, offset + end - map->m_la, end); 1029 if (!(map->m_flags & EROFS_MAP_MAPPED)) { 1030 zero_user_segment(page, cur, end); 1031 goto next_part; 1032 } 1033 if (map->m_flags & EROFS_MAP_FRAGMENT) { 1034 erofs_off_t fpos = offset + cur - map->m_la; 1035 1036 len = min_t(unsigned int, map->m_llen - fpos, end - cur); 1037 err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len, 1038 EROFS_I(inode)->z_fragmentoff + fpos); 1039 if (err) 1040 goto out; 1041 ++spiltted; 1042 tight = false; 1043 goto next_part; 1044 } 1045 1046 exclusive = (!cur && (!spiltted || tight)); 1047 if (cur) 1048 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED); 1049 1050 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) { 1051 .page = page, 1052 .offset = offset - map->m_la, 1053 .end = end, 1054 }), exclusive); 1055 if (err) 1056 goto out; 1057 1058 z_erofs_onlinepage_split(page); 1059 /* bump up the number of spiltted parts of a page */ 1060 ++spiltted; 1061 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK)) 1062 fe->pcl->multibases = true; 1063 if (fe->pcl->length < offset + end - map->m_la) { 1064 fe->pcl->length = offset + end - map->m_la; 1065 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK; 1066 } 1067 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) && 1068 !(map->m_flags & EROFS_MAP_PARTIAL_REF) && 1069 fe->pcl->length == map->m_llen) 1070 fe->pcl->partial = false; 1071 next_part: 1072 /* shorten the remaining extent to update progress */ 1073 map->m_llen = offset + cur - map->m_la; 1074 map->m_flags &= ~EROFS_MAP_FULL_MAPPED; 1075 1076 end = cur; 1077 if (end > 0) 1078 goto repeat; 1079 1080 out: 1081 if (err) 1082 z_erofs_page_mark_eio(page); 1083 z_erofs_onlinepage_endio(page); 1084 return err; 1085 } 1086 1087 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi, 1088 unsigned int readahead_pages) 1089 { 1090 /* auto: enable for read_folio, disable for readahead */ 1091 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) && 1092 !readahead_pages) 1093 return true; 1094 1095 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) && 1096 (readahead_pages <= sbi->opt.max_sync_decompress_pages)) 1097 return true; 1098 1099 return false; 1100 } 1101 1102 static bool z_erofs_page_is_invalidated(struct page *page) 1103 { 1104 return !page->mapping && !z_erofs_is_shortlived_page(page); 1105 } 1106 1107 struct z_erofs_decompress_backend { 1108 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES]; 1109 struct super_block *sb; 1110 struct z_erofs_pcluster *pcl; 1111 1112 /* pages with the longest decompressed length for deduplication */ 1113 struct page **decompressed_pages; 1114 /* pages to keep the compressed data */ 1115 struct page **compressed_pages; 1116 1117 struct list_head decompressed_secondary_bvecs; 1118 struct page **pagepool; 1119 unsigned int onstack_used, nr_pages; 1120 }; 1121 1122 struct z_erofs_bvec_item { 1123 struct z_erofs_bvec bvec; 1124 struct list_head list; 1125 }; 1126 1127 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be, 1128 struct z_erofs_bvec *bvec) 1129 { 1130 struct z_erofs_bvec_item *item; 1131 unsigned int pgnr; 1132 1133 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) && 1134 (bvec->end == PAGE_SIZE || 1135 bvec->offset + bvec->end == be->pcl->length)) { 1136 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT; 1137 DBG_BUGON(pgnr >= be->nr_pages); 1138 if (!be->decompressed_pages[pgnr]) { 1139 be->decompressed_pages[pgnr] = bvec->page; 1140 return; 1141 } 1142 } 1143 1144 /* (cold path) one pcluster is requested multiple times */ 1145 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL); 1146 item->bvec = *bvec; 1147 list_add(&item->list, &be->decompressed_secondary_bvecs); 1148 } 1149 1150 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be, 1151 int err) 1152 { 1153 unsigned int off0 = be->pcl->pageofs_out; 1154 struct list_head *p, *n; 1155 1156 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) { 1157 struct z_erofs_bvec_item *bvi; 1158 unsigned int end, cur; 1159 void *dst, *src; 1160 1161 bvi = container_of(p, struct z_erofs_bvec_item, list); 1162 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0; 1163 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset, 1164 bvi->bvec.end); 1165 dst = kmap_local_page(bvi->bvec.page); 1166 while (cur < end) { 1167 unsigned int pgnr, scur, len; 1168 1169 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT; 1170 DBG_BUGON(pgnr >= be->nr_pages); 1171 1172 scur = bvi->bvec.offset + cur - 1173 ((pgnr << PAGE_SHIFT) - off0); 1174 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur); 1175 if (!be->decompressed_pages[pgnr]) { 1176 err = -EFSCORRUPTED; 1177 cur += len; 1178 continue; 1179 } 1180 src = kmap_local_page(be->decompressed_pages[pgnr]); 1181 memcpy(dst + cur, src + scur, len); 1182 kunmap_local(src); 1183 cur += len; 1184 } 1185 kunmap_local(dst); 1186 if (err) 1187 z_erofs_page_mark_eio(bvi->bvec.page); 1188 z_erofs_onlinepage_endio(bvi->bvec.page); 1189 list_del(p); 1190 kfree(bvi); 1191 } 1192 } 1193 1194 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be) 1195 { 1196 struct z_erofs_pcluster *pcl = be->pcl; 1197 struct z_erofs_bvec_iter biter; 1198 struct page *old_bvpage; 1199 int i; 1200 1201 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0); 1202 for (i = 0; i < pcl->vcnt; ++i) { 1203 struct z_erofs_bvec bvec; 1204 1205 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage); 1206 1207 if (old_bvpage) 1208 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 1209 1210 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page)); 1211 z_erofs_do_decompressed_bvec(be, &bvec); 1212 } 1213 1214 old_bvpage = z_erofs_bvec_iter_end(&biter); 1215 if (old_bvpage) 1216 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage); 1217 } 1218 1219 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be, 1220 bool *overlapped) 1221 { 1222 struct z_erofs_pcluster *pcl = be->pcl; 1223 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1224 int i, err = 0; 1225 1226 *overlapped = false; 1227 for (i = 0; i < pclusterpages; ++i) { 1228 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i]; 1229 struct page *page = bvec->page; 1230 1231 /* compressed pages ought to be present before decompressing */ 1232 if (!page) { 1233 DBG_BUGON(1); 1234 continue; 1235 } 1236 be->compressed_pages[i] = page; 1237 1238 if (z_erofs_is_inline_pcluster(pcl)) { 1239 if (!PageUptodate(page)) 1240 err = -EIO; 1241 continue; 1242 } 1243 1244 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1245 if (!z_erofs_is_shortlived_page(page)) { 1246 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) { 1247 if (!PageUptodate(page)) 1248 err = -EIO; 1249 continue; 1250 } 1251 z_erofs_do_decompressed_bvec(be, bvec); 1252 *overlapped = true; 1253 } 1254 } 1255 1256 if (err) 1257 return err; 1258 return 0; 1259 } 1260 1261 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be, 1262 int err) 1263 { 1264 struct erofs_sb_info *const sbi = EROFS_SB(be->sb); 1265 struct z_erofs_pcluster *pcl = be->pcl; 1266 unsigned int pclusterpages = z_erofs_pclusterpages(pcl); 1267 const struct z_erofs_decompressor *decompressor = 1268 &erofs_decompressors[pcl->algorithmformat]; 1269 unsigned int i, inputsize; 1270 int err2; 1271 struct page *page; 1272 bool overlapped; 1273 1274 mutex_lock(&pcl->lock); 1275 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT; 1276 1277 /* allocate (de)compressed page arrays if cannot be kept on stack */ 1278 be->decompressed_pages = NULL; 1279 be->compressed_pages = NULL; 1280 be->onstack_used = 0; 1281 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) { 1282 be->decompressed_pages = be->onstack_pages; 1283 be->onstack_used = be->nr_pages; 1284 memset(be->decompressed_pages, 0, 1285 sizeof(struct page *) * be->nr_pages); 1286 } 1287 1288 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES) 1289 be->compressed_pages = be->onstack_pages + be->onstack_used; 1290 1291 if (!be->decompressed_pages) 1292 be->decompressed_pages = 1293 kvcalloc(be->nr_pages, sizeof(struct page *), 1294 GFP_KERNEL | __GFP_NOFAIL); 1295 if (!be->compressed_pages) 1296 be->compressed_pages = 1297 kvcalloc(pclusterpages, sizeof(struct page *), 1298 GFP_KERNEL | __GFP_NOFAIL); 1299 1300 z_erofs_parse_out_bvecs(be); 1301 err2 = z_erofs_parse_in_bvecs(be, &overlapped); 1302 if (err2) 1303 err = err2; 1304 if (err) 1305 goto out; 1306 1307 if (z_erofs_is_inline_pcluster(pcl)) 1308 inputsize = pcl->tailpacking_size; 1309 else 1310 inputsize = pclusterpages * PAGE_SIZE; 1311 1312 err = decompressor->decompress(&(struct z_erofs_decompress_req) { 1313 .sb = be->sb, 1314 .in = be->compressed_pages, 1315 .out = be->decompressed_pages, 1316 .pageofs_in = pcl->pageofs_in, 1317 .pageofs_out = pcl->pageofs_out, 1318 .inputsize = inputsize, 1319 .outputsize = pcl->length, 1320 .alg = pcl->algorithmformat, 1321 .inplace_io = overlapped, 1322 .partial_decoding = pcl->partial, 1323 .fillgaps = pcl->multibases, 1324 }, be->pagepool); 1325 1326 out: 1327 /* must handle all compressed pages before actual file pages */ 1328 if (z_erofs_is_inline_pcluster(pcl)) { 1329 page = pcl->compressed_bvecs[0].page; 1330 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); 1331 put_page(page); 1332 } else { 1333 for (i = 0; i < pclusterpages; ++i) { 1334 page = pcl->compressed_bvecs[i].page; 1335 1336 if (erofs_page_is_managed(sbi, page)) 1337 continue; 1338 1339 /* recycle all individual short-lived pages */ 1340 (void)z_erofs_put_shortlivedpage(be->pagepool, page); 1341 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); 1342 } 1343 } 1344 if (be->compressed_pages < be->onstack_pages || 1345 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES) 1346 kvfree(be->compressed_pages); 1347 z_erofs_fill_other_copies(be, err); 1348 1349 for (i = 0; i < be->nr_pages; ++i) { 1350 page = be->decompressed_pages[i]; 1351 if (!page) 1352 continue; 1353 1354 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1355 1356 /* recycle all individual short-lived pages */ 1357 if (z_erofs_put_shortlivedpage(be->pagepool, page)) 1358 continue; 1359 if (err) 1360 z_erofs_page_mark_eio(page); 1361 z_erofs_onlinepage_endio(page); 1362 } 1363 1364 if (be->decompressed_pages != be->onstack_pages) 1365 kvfree(be->decompressed_pages); 1366 1367 pcl->length = 0; 1368 pcl->partial = true; 1369 pcl->multibases = false; 1370 pcl->bvset.nextpage = NULL; 1371 pcl->vcnt = 0; 1372 1373 /* pcluster lock MUST be taken before the following line */ 1374 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 1375 mutex_unlock(&pcl->lock); 1376 return err; 1377 } 1378 1379 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 1380 struct page **pagepool) 1381 { 1382 struct z_erofs_decompress_backend be = { 1383 .sb = io->sb, 1384 .pagepool = pagepool, 1385 .decompressed_secondary_bvecs = 1386 LIST_HEAD_INIT(be.decompressed_secondary_bvecs), 1387 }; 1388 z_erofs_next_pcluster_t owned = io->head; 1389 1390 while (owned != Z_EROFS_PCLUSTER_TAIL) { 1391 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 1392 1393 be.pcl = container_of(owned, struct z_erofs_pcluster, next); 1394 owned = READ_ONCE(be.pcl->next); 1395 1396 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0); 1397 erofs_workgroup_put(&be.pcl->obj); 1398 } 1399 } 1400 1401 static void z_erofs_decompressqueue_work(struct work_struct *work) 1402 { 1403 struct z_erofs_decompressqueue *bgq = 1404 container_of(work, struct z_erofs_decompressqueue, u.work); 1405 struct page *pagepool = NULL; 1406 1407 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL); 1408 z_erofs_decompress_queue(bgq, &pagepool); 1409 erofs_release_pages(&pagepool); 1410 kvfree(bgq); 1411 } 1412 1413 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1414 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work) 1415 { 1416 z_erofs_decompressqueue_work((struct work_struct *)work); 1417 } 1418 #endif 1419 1420 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 1421 int bios) 1422 { 1423 struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 1424 1425 /* wake up the caller thread for sync decompression */ 1426 if (io->sync) { 1427 if (!atomic_add_return(bios, &io->pending_bios)) 1428 complete(&io->u.done); 1429 return; 1430 } 1431 1432 if (atomic_add_return(bios, &io->pending_bios)) 1433 return; 1434 /* Use (kthread_)work and sync decompression for atomic contexts only */ 1435 if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) { 1436 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1437 struct kthread_worker *worker; 1438 1439 rcu_read_lock(); 1440 worker = rcu_dereference( 1441 z_erofs_pcpu_workers[raw_smp_processor_id()]); 1442 if (!worker) { 1443 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work); 1444 queue_work(z_erofs_workqueue, &io->u.work); 1445 } else { 1446 kthread_queue_work(worker, &io->u.kthread_work); 1447 } 1448 rcu_read_unlock(); 1449 #else 1450 queue_work(z_erofs_workqueue, &io->u.work); 1451 #endif 1452 /* enable sync decompression for readahead */ 1453 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) 1454 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON; 1455 return; 1456 } 1457 z_erofs_decompressqueue_work(&io->u.work); 1458 } 1459 1460 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 1461 unsigned int nr, 1462 struct page **pagepool, 1463 struct address_space *mc) 1464 { 1465 const pgoff_t index = pcl->obj.index; 1466 gfp_t gfp = mapping_gfp_mask(mc); 1467 bool tocache = false; 1468 1469 struct address_space *mapping; 1470 struct page *oldpage, *page; 1471 int justfound; 1472 1473 repeat: 1474 page = READ_ONCE(pcl->compressed_bvecs[nr].page); 1475 oldpage = page; 1476 1477 if (!page) 1478 goto out_allocpage; 1479 1480 justfound = (unsigned long)page & 1UL; 1481 page = (struct page *)((unsigned long)page & ~1UL); 1482 1483 /* 1484 * preallocated cached pages, which is used to avoid direct reclaim 1485 * otherwise, it will go inplace I/O path instead. 1486 */ 1487 if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 1488 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 1489 set_page_private(page, 0); 1490 tocache = true; 1491 goto out_tocache; 1492 } 1493 mapping = READ_ONCE(page->mapping); 1494 1495 /* 1496 * file-backed online pages in plcuster are all locked steady, 1497 * therefore it is impossible for `mapping' to be NULL. 1498 */ 1499 if (mapping && mapping != mc) 1500 /* ought to be unmanaged pages */ 1501 goto out; 1502 1503 /* directly return for shortlived page as well */ 1504 if (z_erofs_is_shortlived_page(page)) 1505 goto out; 1506 1507 lock_page(page); 1508 1509 /* only true if page reclaim goes wrong, should never happen */ 1510 DBG_BUGON(justfound && PagePrivate(page)); 1511 1512 /* the page is still in manage cache */ 1513 if (page->mapping == mc) { 1514 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page); 1515 1516 if (!PagePrivate(page)) { 1517 /* 1518 * impossible to be !PagePrivate(page) for 1519 * the current restriction as well if 1520 * the page is already in compressed_bvecs[]. 1521 */ 1522 DBG_BUGON(!justfound); 1523 1524 justfound = 0; 1525 set_page_private(page, (unsigned long)pcl); 1526 SetPagePrivate(page); 1527 } 1528 1529 /* no need to submit io if it is already up-to-date */ 1530 if (PageUptodate(page)) { 1531 unlock_page(page); 1532 page = NULL; 1533 } 1534 goto out; 1535 } 1536 1537 /* 1538 * the managed page has been truncated, it's unsafe to 1539 * reuse this one, let's allocate a new cache-managed page. 1540 */ 1541 DBG_BUGON(page->mapping); 1542 DBG_BUGON(!justfound); 1543 1544 tocache = true; 1545 unlock_page(page); 1546 put_page(page); 1547 out_allocpage: 1548 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 1549 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page, 1550 oldpage, page)) { 1551 erofs_pagepool_add(pagepool, page); 1552 cond_resched(); 1553 goto repeat; 1554 } 1555 out_tocache: 1556 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1557 /* turn into temporary page if fails (1 ref) */ 1558 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1559 goto out; 1560 } 1561 attach_page_private(page, pcl); 1562 /* drop a refcount added by allocpage (then we have 2 refs here) */ 1563 put_page(page); 1564 1565 out: /* the only exit (for tracing and debugging) */ 1566 return page; 1567 } 1568 1569 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb, 1570 struct z_erofs_decompressqueue *fgq, bool *fg) 1571 { 1572 struct z_erofs_decompressqueue *q; 1573 1574 if (fg && !*fg) { 1575 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1576 if (!q) { 1577 *fg = true; 1578 goto fg_out; 1579 } 1580 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD 1581 kthread_init_work(&q->u.kthread_work, 1582 z_erofs_decompressqueue_kthread_work); 1583 #else 1584 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1585 #endif 1586 } else { 1587 fg_out: 1588 q = fgq; 1589 init_completion(&fgq->u.done); 1590 atomic_set(&fgq->pending_bios, 0); 1591 q->eio = false; 1592 q->sync = true; 1593 } 1594 q->sb = sb; 1595 q->head = Z_EROFS_PCLUSTER_TAIL; 1596 return q; 1597 } 1598 1599 /* define decompression jobqueue types */ 1600 enum { 1601 JQ_BYPASS, 1602 JQ_SUBMIT, 1603 NR_JOBQUEUES, 1604 }; 1605 1606 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 1607 z_erofs_next_pcluster_t qtail[], 1608 z_erofs_next_pcluster_t owned_head) 1609 { 1610 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 1611 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 1612 1613 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL); 1614 1615 WRITE_ONCE(*submit_qtail, owned_head); 1616 WRITE_ONCE(*bypass_qtail, &pcl->next); 1617 1618 qtail[JQ_BYPASS] = &pcl->next; 1619 } 1620 1621 static void z_erofs_decompressqueue_endio(struct bio *bio) 1622 { 1623 struct z_erofs_decompressqueue *q = bio->bi_private; 1624 blk_status_t err = bio->bi_status; 1625 struct bio_vec *bvec; 1626 struct bvec_iter_all iter_all; 1627 1628 bio_for_each_segment_all(bvec, bio, iter_all) { 1629 struct page *page = bvec->bv_page; 1630 1631 DBG_BUGON(PageUptodate(page)); 1632 DBG_BUGON(z_erofs_page_is_invalidated(page)); 1633 1634 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 1635 if (!err) 1636 SetPageUptodate(page); 1637 unlock_page(page); 1638 } 1639 } 1640 if (err) 1641 q->eio = true; 1642 z_erofs_decompress_kickoff(q, -1); 1643 bio_put(bio); 1644 } 1645 1646 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f, 1647 struct z_erofs_decompressqueue *fgq, 1648 bool *force_fg, bool readahead) 1649 { 1650 struct super_block *sb = f->inode->i_sb; 1651 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb)); 1652 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1653 struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 1654 z_erofs_next_pcluster_t owned_head = f->owned_head; 1655 /* bio is NULL initially, so no need to initialize last_{index,bdev} */ 1656 pgoff_t last_index; 1657 struct block_device *last_bdev; 1658 unsigned int nr_bios = 0; 1659 struct bio *bio = NULL; 1660 unsigned long pflags; 1661 int memstall = 0; 1662 1663 /* 1664 * if managed cache is enabled, bypass jobqueue is needed, 1665 * no need to read from device for all pclusters in this queue. 1666 */ 1667 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1668 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg); 1669 1670 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1671 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 1672 1673 /* by default, all need io submission */ 1674 q[JQ_SUBMIT]->head = owned_head; 1675 1676 do { 1677 struct erofs_map_dev mdev; 1678 struct z_erofs_pcluster *pcl; 1679 pgoff_t cur, end; 1680 unsigned int i = 0; 1681 bool bypass = true; 1682 1683 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 1684 pcl = container_of(owned_head, struct z_erofs_pcluster, next); 1685 owned_head = READ_ONCE(pcl->next); 1686 1687 if (z_erofs_is_inline_pcluster(pcl)) { 1688 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1689 continue; 1690 } 1691 1692 /* no device id here, thus it will always succeed */ 1693 mdev = (struct erofs_map_dev) { 1694 .m_pa = erofs_pos(sb, pcl->obj.index), 1695 }; 1696 (void)erofs_map_dev(sb, &mdev); 1697 1698 cur = erofs_blknr(sb, mdev.m_pa); 1699 end = cur + pcl->pclusterpages; 1700 1701 do { 1702 struct page *page; 1703 1704 page = pickup_page_for_submission(pcl, i++, 1705 &f->pagepool, mc); 1706 if (!page) 1707 continue; 1708 1709 if (bio && (cur != last_index + 1 || 1710 last_bdev != mdev.m_bdev)) { 1711 submit_bio_retry: 1712 submit_bio(bio); 1713 if (memstall) { 1714 psi_memstall_leave(&pflags); 1715 memstall = 0; 1716 } 1717 bio = NULL; 1718 } 1719 1720 if (unlikely(PageWorkingset(page)) && !memstall) { 1721 psi_memstall_enter(&pflags); 1722 memstall = 1; 1723 } 1724 1725 if (!bio) { 1726 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS, 1727 REQ_OP_READ, GFP_NOIO); 1728 bio->bi_end_io = z_erofs_decompressqueue_endio; 1729 1730 last_bdev = mdev.m_bdev; 1731 bio->bi_iter.bi_sector = (sector_t)cur << 1732 (sb->s_blocksize_bits - 9); 1733 bio->bi_private = q[JQ_SUBMIT]; 1734 if (readahead) 1735 bio->bi_opf |= REQ_RAHEAD; 1736 ++nr_bios; 1737 } 1738 1739 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 1740 goto submit_bio_retry; 1741 1742 last_index = cur; 1743 bypass = false; 1744 } while (++cur < end); 1745 1746 if (!bypass) 1747 qtail[JQ_SUBMIT] = &pcl->next; 1748 else 1749 move_to_bypass_jobqueue(pcl, qtail, owned_head); 1750 } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 1751 1752 if (bio) { 1753 submit_bio(bio); 1754 if (memstall) 1755 psi_memstall_leave(&pflags); 1756 } 1757 1758 /* 1759 * although background is preferred, no one is pending for submission. 1760 * don't issue decompression but drop it directly instead. 1761 */ 1762 if (!*force_fg && !nr_bios) { 1763 kvfree(q[JQ_SUBMIT]); 1764 return; 1765 } 1766 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios); 1767 } 1768 1769 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f, 1770 bool force_fg, bool ra) 1771 { 1772 struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 1773 1774 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL) 1775 return; 1776 z_erofs_submit_queue(f, io, &force_fg, ra); 1777 1778 /* handle bypass queue (no i/o pclusters) immediately */ 1779 z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool); 1780 1781 if (!force_fg) 1782 return; 1783 1784 /* wait until all bios are completed */ 1785 wait_for_completion_io(&io[JQ_SUBMIT].u.done); 1786 1787 /* handle synchronous decompress queue in the caller context */ 1788 z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool); 1789 } 1790 1791 /* 1792 * Since partial uptodate is still unimplemented for now, we have to use 1793 * approximate readmore strategies as a start. 1794 */ 1795 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, 1796 struct readahead_control *rac, bool backmost) 1797 { 1798 struct inode *inode = f->inode; 1799 struct erofs_map_blocks *map = &f->map; 1800 erofs_off_t cur, end, headoffset = f->headoffset; 1801 int err; 1802 1803 if (backmost) { 1804 if (rac) 1805 end = headoffset + readahead_length(rac) - 1; 1806 else 1807 end = headoffset + PAGE_SIZE - 1; 1808 map->m_la = end; 1809 err = z_erofs_map_blocks_iter(inode, map, 1810 EROFS_GET_BLOCKS_READMORE); 1811 if (err) 1812 return; 1813 1814 /* expand ra for the trailing edge if readahead */ 1815 if (rac) { 1816 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE); 1817 readahead_expand(rac, headoffset, cur - headoffset); 1818 return; 1819 } 1820 end = round_up(end, PAGE_SIZE); 1821 } else { 1822 end = round_up(map->m_la, PAGE_SIZE); 1823 1824 if (!map->m_llen) 1825 return; 1826 } 1827 1828 cur = map->m_la + map->m_llen - 1; 1829 while ((cur >= end) && (cur < i_size_read(inode))) { 1830 pgoff_t index = cur >> PAGE_SHIFT; 1831 struct page *page; 1832 1833 page = erofs_grab_cache_page_nowait(inode->i_mapping, index); 1834 if (page) { 1835 if (PageUptodate(page)) 1836 unlock_page(page); 1837 else 1838 (void)z_erofs_do_read_page(f, page); 1839 put_page(page); 1840 } 1841 1842 if (cur < PAGE_SIZE) 1843 break; 1844 cur = (index << PAGE_SHIFT) - 1; 1845 } 1846 } 1847 1848 static int z_erofs_read_folio(struct file *file, struct folio *folio) 1849 { 1850 struct page *page = &folio->page; 1851 struct inode *const inode = page->mapping->host; 1852 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1853 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1854 int err; 1855 1856 trace_erofs_readpage(page, false); 1857 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 1858 1859 z_erofs_pcluster_readmore(&f, NULL, true); 1860 err = z_erofs_do_read_page(&f, page); 1861 z_erofs_pcluster_readmore(&f, NULL, false); 1862 z_erofs_pcluster_end(&f); 1863 1864 /* if some compressed cluster ready, need submit them anyway */ 1865 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false); 1866 1867 if (err && err != -EINTR) 1868 erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu", 1869 err, folio->index, EROFS_I(inode)->nid); 1870 1871 erofs_put_metabuf(&f.map.buf); 1872 erofs_release_pages(&f.pagepool); 1873 return err; 1874 } 1875 1876 static void z_erofs_readahead(struct readahead_control *rac) 1877 { 1878 struct inode *const inode = rac->mapping->host; 1879 struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 1880 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 1881 struct page *head = NULL, *page; 1882 unsigned int nr_pages; 1883 1884 f.headoffset = readahead_pos(rac); 1885 1886 z_erofs_pcluster_readmore(&f, rac, true); 1887 nr_pages = readahead_count(rac); 1888 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 1889 1890 while ((page = readahead_page(rac))) { 1891 set_page_private(page, (unsigned long)head); 1892 head = page; 1893 } 1894 1895 while (head) { 1896 struct page *page = head; 1897 int err; 1898 1899 /* traversal in reverse order */ 1900 head = (void *)page_private(page); 1901 1902 err = z_erofs_do_read_page(&f, page); 1903 if (err && err != -EINTR) 1904 erofs_err(inode->i_sb, "readahead error %d @ %lu of nid %llu", 1905 err, page->index, EROFS_I(inode)->nid); 1906 put_page(page); 1907 } 1908 z_erofs_pcluster_readmore(&f, rac, false); 1909 z_erofs_pcluster_end(&f); 1910 1911 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_pages), true); 1912 erofs_put_metabuf(&f.map.buf); 1913 erofs_release_pages(&f.pagepool); 1914 } 1915 1916 const struct address_space_operations z_erofs_aops = { 1917 .read_folio = z_erofs_read_folio, 1918 .readahead = z_erofs_readahead, 1919 }; 1920