1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
12 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS 2
16 * let's leave a type here in case of introducing
17 * another tagged pointer later.
19 typedef void *z_erofs_next_pcluster_t;
27 #define __Z_EROFS_BVSET(name, total) \
29 /* point to the next page which contains the following bvecs */ \
30 struct page *nextpage; \
31 struct z_erofs_bvec bvec[total]; \
33 __Z_EROFS_BVSET(z_erofs_bvset,);
34 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
37 * Structure fields follow one of the following exclusion rules.
39 * I: Modifiable by initialization/destruction paths and read-only
42 * L: Field should be protected by the pcluster lock;
44 * A: Field should be accessed / updated in atomic for parallelized code.
46 struct z_erofs_pcluster {
47 struct erofs_workgroup obj;
50 /* A: point to next chained pcluster or TAILs */
51 z_erofs_next_pcluster_t next;
53 /* L: the maximum decompression size of this round */
56 /* L: total number of bvecs */
59 /* I: page offset of start position of decompression */
60 unsigned short pageofs_out;
62 /* I: page offset of inline compressed data */
63 unsigned short pageofs_in;
66 /* L: inline a certain number of bvec for bootstrap */
67 struct z_erofs_bvset_inline bvset;
69 /* I: can be used to free the pcluster by RCU. */
74 /* I: physical cluster size in pages */
75 unsigned short pclusterpages;
77 /* I: tailpacking inline compressed size */
78 unsigned short tailpacking_size;
81 /* I: compression algorithm format */
82 unsigned char algorithmformat;
84 /* L: whether partial decompression or not */
87 /* L: indicate several pageofs_outs or not */
90 /* A: compressed bvecs (can be cached or inplaced pages) */
91 struct z_erofs_bvec compressed_bvecs[];
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)
98 struct z_erofs_decompressqueue {
99 struct super_block *sb;
100 atomic_t pending_bios;
101 z_erofs_next_pcluster_t head;
104 struct completion done;
105 struct work_struct work;
106 struct kthread_work kthread_work;
111 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
113 return !pcl->obj.index;
116 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
118 if (z_erofs_is_inline_pcluster(pcl))
120 return pcl->pclusterpages;
124 * bit 30: I/O error occurred on this page
125 * bit 0 - 29: remaining parts to complete this page
127 #define Z_EROFS_PAGE_EIO (1 << 30)
129 static inline void z_erofs_onlinepage_init(struct page *page)
134 } u = { .o = ATOMIC_INIT(1) };
136 set_page_private(page, u.v);
138 SetPagePrivate(page);
141 static inline void z_erofs_onlinepage_split(struct page *page)
143 atomic_inc((atomic_t *)&page->private);
146 static inline void z_erofs_page_mark_eio(struct page *page)
151 orig = atomic_read((atomic_t *)&page->private);
152 } while (atomic_cmpxchg((atomic_t *)&page->private, orig,
153 orig | Z_EROFS_PAGE_EIO) != orig);
156 static inline void z_erofs_onlinepage_endio(struct page *page)
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);
171 #define Z_EROFS_ONSTACK_PAGES 32
174 * since pclustersize is variable for big pcluster feature, introduce slab
175 * pools implementation for different pcluster sizes.
177 struct z_erofs_pcluster_slab {
178 struct kmem_cache *slab;
179 unsigned int maxpages;
183 #define _PCLP(n) { .maxpages = n }
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)
190 struct z_erofs_bvec_iter {
192 struct z_erofs_bvset *bvset;
193 unsigned int nr, cur;
196 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
199 kunmap_local(iter->bvset);
203 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
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;
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);
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,
224 *iter = (struct z_erofs_bvec_iter) {
226 .bvset = (struct z_erofs_bvset *)bvset,
229 while (cur > iter->nr) {
231 z_erofs_bvset_flip(iter);
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)
241 if (iter->cur >= iter->nr) {
242 struct page *nextpage = *candidate_bvpage;
245 nextpage = erofs_allocpage(pagepool, GFP_NOFS);
248 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
250 DBG_BUGON(iter->bvset->nextpage);
251 iter->bvset->nextpage = nextpage;
252 z_erofs_bvset_flip(iter);
254 iter->bvset->nextpage = NULL;
255 *candidate_bvpage = NULL;
257 iter->bvset->bvec[iter->cur++] = *bvec;
261 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
262 struct z_erofs_bvec *bvec,
263 struct page **old_bvpage)
265 if (iter->cur == iter->nr)
266 *old_bvpage = z_erofs_bvset_flip(iter);
269 *bvec = iter->bvset->bvec[iter->cur++];
272 static void z_erofs_destroy_pcluster_pool(void)
276 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
277 if (!pcluster_pool[i].slab)
279 kmem_cache_destroy(pcluster_pool[i].slab);
280 pcluster_pool[i].slab = NULL;
284 static int z_erofs_create_pcluster_pool(void)
286 struct z_erofs_pcluster_slab *pcs;
287 struct z_erofs_pcluster *a;
290 for (pcs = pcluster_pool;
291 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
292 size = struct_size(a, compressed_bvecs, pcs->maxpages);
294 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
295 pcs->slab = kmem_cache_create(pcs->name, size, 0,
296 SLAB_RECLAIM_ACCOUNT, NULL);
300 z_erofs_destroy_pcluster_pool();
306 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
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;
314 if (nrpages > pcs->maxpages)
317 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
319 return ERR_PTR(-ENOMEM);
320 pcl->pclusterpages = nrpages;
323 return ERR_PTR(-EINVAL);
326 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
328 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
331 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
332 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
334 if (pclusterpages > pcs->maxpages)
337 kmem_cache_free(pcs->slab, pcl);
343 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
345 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
346 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
348 static void erofs_destroy_percpu_workers(void)
350 struct kthread_worker *worker;
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);
358 kthread_destroy_worker(worker);
360 kfree(z_erofs_pcpu_workers);
363 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
365 struct kthread_worker *worker =
366 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
370 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
371 sched_set_fifo_low(worker->task);
375 static int erofs_init_percpu_workers(void)
377 struct kthread_worker *worker;
380 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
381 sizeof(struct kthread_worker *), GFP_ATOMIC);
382 if (!z_erofs_pcpu_workers)
385 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
386 worker = erofs_init_percpu_worker(cpu);
388 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
393 static inline void erofs_destroy_percpu_workers(void) {}
394 static inline int erofs_init_percpu_workers(void) { return 0; }
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;
401 static int erofs_cpu_online(unsigned int cpu)
403 struct kthread_worker *worker, *old;
405 worker = erofs_init_percpu_worker(cpu);
407 return PTR_ERR(worker);
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));
413 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
414 spin_unlock(&z_erofs_pcpu_worker_lock);
416 kthread_destroy_worker(worker);
420 static int erofs_cpu_offline(unsigned int cpu)
422 struct kthread_worker *worker;
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);
432 kthread_destroy_worker(worker);
436 static int erofs_cpu_hotplug_init(void)
440 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
441 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
445 erofs_cpuhp_state = state;
449 static void erofs_cpu_hotplug_destroy(void)
451 if (erofs_cpuhp_state)
452 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
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) {}
459 void z_erofs_exit_zip_subsystem(void)
461 erofs_cpu_hotplug_destroy();
462 erofs_destroy_percpu_workers();
463 destroy_workqueue(z_erofs_workqueue);
464 z_erofs_destroy_pcluster_pool();
467 int __init z_erofs_init_zip_subsystem(void)
469 int err = z_erofs_create_pcluster_pool();
472 goto out_error_pcluster_pool;
474 z_erofs_workqueue = alloc_workqueue("erofs_worker",
475 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
476 if (!z_erofs_workqueue) {
478 goto out_error_workqueue_init;
481 err = erofs_init_percpu_workers();
483 goto out_error_pcpu_worker;
485 err = erofs_cpu_hotplug_init();
487 goto out_error_cpuhp_init;
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:
500 enum z_erofs_pclustermode {
501 Z_EROFS_PCLUSTER_INFLIGHT,
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.
508 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
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
516 * ________________________________________________________________
517 * | tail (partial) page | head (partial) page |
518 * | (of the current cl) | (of the previous collection) |
520 * |__PCLUSTER_FOLLOWED___|___________PCLUSTER_FOLLOWED____________|
522 * [ (*) the above page can be used as inplace I/O. ]
524 Z_EROFS_PCLUSTER_FOLLOWED,
527 struct z_erofs_decompress_frontend {
528 struct inode *const inode;
529 struct erofs_map_blocks map;
530 struct z_erofs_bvec_iter biter;
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;
538 /* used for applying cache strategy on the fly */
540 erofs_off_t headoffset;
542 /* a pointer used to pick up inplace I/O pages */
546 #define DECOMPRESS_FRONTEND_INIT(__i) { \
547 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
548 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
550 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
552 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
554 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
560 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
561 fe->map.m_la < fe->headoffset)
567 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
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;
574 * optimistic allocation without direct reclaim since inplace I/O
575 * can be used if low memory otherwise.
577 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
578 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
581 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
584 for (i = 0; i < pcl->pclusterpages; ++i) {
586 void *t; /* mark pages just found for debugging */
587 struct page *newpage = NULL;
589 /* the compressed page was loaded before */
590 if (READ_ONCE(pcl->compressed_bvecs[i].page))
593 page = find_get_page(mc, pcl->obj.index + i);
596 t = (void *)((unsigned long)page | 1);
598 /* I/O is needed, no possible to decompress directly */
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.
608 newpage = erofs_allocpage(&fe->pagepool, gfp);
611 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
612 t = (void *)((unsigned long)newpage | 1);
615 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
621 erofs_pagepool_add(&fe->pagepool, newpage);
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.
629 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
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)
636 struct z_erofs_pcluster *const pcl =
637 container_of(grp, struct z_erofs_pcluster, obj);
640 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
642 * refcount of workgroup is now freezed as 0,
643 * therefore no need to worry about available decompression users.
645 for (i = 0; i < pcl->pclusterpages; ++i) {
646 struct page *page = pcl->compressed_bvecs[i].page;
651 /* block other users from reclaiming or migrating the page */
652 if (!trylock_page(page))
655 if (!erofs_page_is_managed(sbi, page))
658 /* barrier is implied in the following 'unlock_page' */
659 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
660 detach_page_private(page);
666 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
668 struct z_erofs_pcluster *pcl = folio_get_private(folio);
672 if (!folio_test_private(folio))
676 spin_lock(&pcl->obj.lockref.lock);
677 if (pcl->obj.lockref.count > 0)
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);
689 folio_detach_private(folio);
691 spin_unlock(&pcl->obj.lockref.lock);
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.
700 static void z_erofs_cache_invalidate_folio(struct folio *folio,
701 size_t offset, size_t length)
703 const size_t stop = length + offset;
705 /* Check for potential overflow in debug mode */
706 DBG_BUGON(stop > folio_size(folio) || stop < length);
708 if (offset == 0 && stop == folio_size(folio))
709 while (!z_erofs_cache_release_folio(folio, GFP_NOFS))
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,
718 int erofs_init_managed_cache(struct super_block *sb)
720 struct inode *const inode = new_inode(sb);
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;
733 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
734 struct z_erofs_bvec *bvec)
736 struct z_erofs_pcluster *const pcl = fe->pcl;
738 while (fe->icur > 0) {
739 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
741 pcl->compressed_bvecs[fe->icur] = *bvec;
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)
755 /* give priority for inplaceio to use file pages first */
756 if (z_erofs_try_inplace_io(fe, bvec))
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;
763 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
765 fe->pcl->vcnt += (ret >= 0);
769 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
771 struct z_erofs_pcluster *pcl = f->pcl;
772 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
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;
783 /* type 2, it belongs to an ongoing chain */
784 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
787 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
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;
795 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
796 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
798 return -EFSCORRUPTED;
801 /* no available pcluster, let's allocate one */
802 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
803 map->m_plen >> PAGE_SHIFT);
807 spin_lock_init(&pcl->obj.lockref.lock);
808 pcl->algorithmformat = map->m_algorithmformat;
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;
818 * lock all primary followed works before visible to others
819 * and mutex_trylock *never* fails for a new pcluster.
821 mutex_init(&pcl->lock);
822 DBG_BUGON(!mutex_trylock(&pcl->lock));
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;
829 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
831 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
837 if (grp != &pcl->obj) {
838 fe->pcl = container_of(grp,
839 struct z_erofs_pcluster, obj);
844 fe->owned_head = &pcl->next;
849 mutex_unlock(&pcl->lock);
850 z_erofs_free_pcluster(pcl);
854 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
856 struct erofs_map_blocks *map = &fe->map;
857 struct erofs_workgroup *grp = NULL;
862 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
863 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
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) {
870 return -EFSCORRUPTED;
874 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
877 ret = z_erofs_register_pcluster(fe);
880 if (ret == -EEXIST) {
881 mutex_lock(&fe->pcl->lock);
882 z_erofs_try_to_claim_pcluster(fe);
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);
894 * keep in mind that no referenced pclusters will be freed
895 * only after a RCU grace period.
897 static void z_erofs_rcu_callback(struct rcu_head *head)
899 z_erofs_free_pcluster(container_of(head,
900 struct z_erofs_pcluster, rcu));
903 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
905 struct z_erofs_pcluster *const pcl =
906 container_of(grp, struct z_erofs_pcluster, obj);
908 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
911 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
913 struct z_erofs_pcluster *pcl = fe->pcl;
918 z_erofs_bvec_iter_end(&fe->biter);
919 mutex_unlock(&pcl->lock);
921 if (fe->candidate_bvpage)
922 fe->candidate_bvpage = NULL;
925 * if all pending pages are added, don't hold its reference
926 * any longer if the pcluster isn't hosted by ourselves.
928 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
929 erofs_workgroup_put(&pcl->obj);
935 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
936 struct page *page, unsigned int pageofs,
939 struct super_block *sb = inode->i_sb;
940 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
941 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
946 return -EFSCORRUPTED;
948 buf.inode = packed_inode;
949 pos += EROFS_I(inode)->z_fragmentoff;
950 for (i = 0; i < len; i += cnt) {
951 cnt = min_t(unsigned int, len - i,
952 sb->s_blocksize - erofs_blkoff(sb, pos));
953 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
955 erofs_put_metabuf(&buf);
959 dst = kmap_local_page(page);
960 memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
964 erofs_put_metabuf(&buf);
968 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
971 struct inode *const inode = fe->inode;
972 struct erofs_map_blocks *const map = &fe->map;
973 const loff_t offset = page_offset(page);
974 bool tight = true, exclusive;
975 unsigned int cur, end, spiltted;
978 /* register locked file pages as online pages in pack */
979 z_erofs_onlinepage_init(page);
986 if (offset + cur < map->m_la ||
987 offset + cur >= map->m_la + map->m_llen) {
988 if (z_erofs_collector_end(fe))
989 fe->backmost = false;
990 map->m_la = offset + cur;
992 err = z_erofs_map_blocks_iter(inode, map, 0);
998 /* didn't get a valid pcluster previously (very rare) */
1001 if (!(map->m_flags & EROFS_MAP_MAPPED) ||
1002 map->m_flags & EROFS_MAP_FRAGMENT)
1005 err = z_erofs_collector_begin(fe);
1009 if (z_erofs_is_inline_pcluster(fe->pcl)) {
1012 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
1013 erofs_blknr(inode->i_sb, map->m_pa),
1017 erofs_err(inode->i_sb,
1018 "failed to get inline page, err %d", err);
1021 get_page(fe->map.buf.page);
1022 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1024 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1026 /* bind cache first when cached decompression is preferred */
1027 z_erofs_bind_cache(fe);
1031 * Ensure the current partial page belongs to this submit chain rather
1032 * than other concurrent submit chains or the noio(bypass) chain since
1033 * those chains are handled asynchronously thus the page cannot be used
1034 * for inplace I/O or bvpage (should be processed in a strict order.)
1036 tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1038 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
1039 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1040 zero_user_segment(page, cur, end);
1043 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1044 unsigned int pageofs, skip, len;
1046 if (offset > map->m_la) {
1048 skip = offset - map->m_la;
1050 pageofs = map->m_la & ~PAGE_MASK;
1053 len = min_t(unsigned int, map->m_llen - skip, end - cur);
1054 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1062 exclusive = (!cur && (!spiltted || tight));
1064 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1066 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1068 .offset = offset - map->m_la,
1074 z_erofs_onlinepage_split(page);
1075 /* bump up the number of spiltted parts of a page */
1077 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1078 fe->pcl->multibases = true;
1079 if (fe->pcl->length < offset + end - map->m_la) {
1080 fe->pcl->length = offset + end - map->m_la;
1081 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1083 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1084 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1085 fe->pcl->length == map->m_llen)
1086 fe->pcl->partial = false;
1088 /* shorten the remaining extent to update progress */
1089 map->m_llen = offset + cur - map->m_la;
1090 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1098 z_erofs_page_mark_eio(page);
1099 z_erofs_onlinepage_endio(page);
1103 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1104 unsigned int readahead_pages)
1106 /* auto: enable for read_folio, disable for readahead */
1107 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1111 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1112 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1118 static bool z_erofs_page_is_invalidated(struct page *page)
1120 return !page->mapping && !z_erofs_is_shortlived_page(page);
1123 struct z_erofs_decompress_backend {
1124 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1125 struct super_block *sb;
1126 struct z_erofs_pcluster *pcl;
1128 /* pages with the longest decompressed length for deduplication */
1129 struct page **decompressed_pages;
1130 /* pages to keep the compressed data */
1131 struct page **compressed_pages;
1133 struct list_head decompressed_secondary_bvecs;
1134 struct page **pagepool;
1135 unsigned int onstack_used, nr_pages;
1138 struct z_erofs_bvec_item {
1139 struct z_erofs_bvec bvec;
1140 struct list_head list;
1143 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1144 struct z_erofs_bvec *bvec)
1146 struct z_erofs_bvec_item *item;
1148 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1151 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1152 DBG_BUGON(pgnr >= be->nr_pages);
1153 if (!be->decompressed_pages[pgnr]) {
1154 be->decompressed_pages[pgnr] = bvec->page;
1159 /* (cold path) one pcluster is requested multiple times */
1160 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1162 list_add(&item->list, &be->decompressed_secondary_bvecs);
1165 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1168 unsigned int off0 = be->pcl->pageofs_out;
1169 struct list_head *p, *n;
1171 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1172 struct z_erofs_bvec_item *bvi;
1173 unsigned int end, cur;
1176 bvi = container_of(p, struct z_erofs_bvec_item, list);
1177 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1178 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1180 dst = kmap_local_page(bvi->bvec.page);
1182 unsigned int pgnr, scur, len;
1184 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1185 DBG_BUGON(pgnr >= be->nr_pages);
1187 scur = bvi->bvec.offset + cur -
1188 ((pgnr << PAGE_SHIFT) - off0);
1189 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1190 if (!be->decompressed_pages[pgnr]) {
1191 err = -EFSCORRUPTED;
1195 src = kmap_local_page(be->decompressed_pages[pgnr]);
1196 memcpy(dst + cur, src + scur, len);
1202 z_erofs_page_mark_eio(bvi->bvec.page);
1203 z_erofs_onlinepage_endio(bvi->bvec.page);
1209 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1211 struct z_erofs_pcluster *pcl = be->pcl;
1212 struct z_erofs_bvec_iter biter;
1213 struct page *old_bvpage;
1216 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1217 for (i = 0; i < pcl->vcnt; ++i) {
1218 struct z_erofs_bvec bvec;
1220 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1223 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1225 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1226 z_erofs_do_decompressed_bvec(be, &bvec);
1229 old_bvpage = z_erofs_bvec_iter_end(&biter);
1231 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1234 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1237 struct z_erofs_pcluster *pcl = be->pcl;
1238 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1241 *overlapped = false;
1242 for (i = 0; i < pclusterpages; ++i) {
1243 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1244 struct page *page = bvec->page;
1246 /* compressed pages ought to be present before decompressing */
1251 be->compressed_pages[i] = page;
1253 if (z_erofs_is_inline_pcluster(pcl)) {
1254 if (!PageUptodate(page))
1259 DBG_BUGON(z_erofs_page_is_invalidated(page));
1260 if (!z_erofs_is_shortlived_page(page)) {
1261 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1262 if (!PageUptodate(page))
1266 z_erofs_do_decompressed_bvec(be, bvec);
1276 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1279 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1280 struct z_erofs_pcluster *pcl = be->pcl;
1281 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1282 const struct z_erofs_decompressor *decompressor =
1283 &erofs_decompressors[pcl->algorithmformat];
1284 unsigned int i, inputsize;
1289 mutex_lock(&pcl->lock);
1290 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1292 /* allocate (de)compressed page arrays if cannot be kept on stack */
1293 be->decompressed_pages = NULL;
1294 be->compressed_pages = NULL;
1295 be->onstack_used = 0;
1296 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1297 be->decompressed_pages = be->onstack_pages;
1298 be->onstack_used = be->nr_pages;
1299 memset(be->decompressed_pages, 0,
1300 sizeof(struct page *) * be->nr_pages);
1303 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1304 be->compressed_pages = be->onstack_pages + be->onstack_used;
1306 if (!be->decompressed_pages)
1307 be->decompressed_pages =
1308 kvcalloc(be->nr_pages, sizeof(struct page *),
1309 GFP_KERNEL | __GFP_NOFAIL);
1310 if (!be->compressed_pages)
1311 be->compressed_pages =
1312 kvcalloc(pclusterpages, sizeof(struct page *),
1313 GFP_KERNEL | __GFP_NOFAIL);
1315 z_erofs_parse_out_bvecs(be);
1316 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1322 if (z_erofs_is_inline_pcluster(pcl))
1323 inputsize = pcl->tailpacking_size;
1325 inputsize = pclusterpages * PAGE_SIZE;
1327 err = decompressor->decompress(&(struct z_erofs_decompress_req) {
1329 .in = be->compressed_pages,
1330 .out = be->decompressed_pages,
1331 .pageofs_in = pcl->pageofs_in,
1332 .pageofs_out = pcl->pageofs_out,
1333 .inputsize = inputsize,
1334 .outputsize = pcl->length,
1335 .alg = pcl->algorithmformat,
1336 .inplace_io = overlapped,
1337 .partial_decoding = pcl->partial,
1338 .fillgaps = pcl->multibases,
1342 /* must handle all compressed pages before actual file pages */
1343 if (z_erofs_is_inline_pcluster(pcl)) {
1344 page = pcl->compressed_bvecs[0].page;
1345 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1348 for (i = 0; i < pclusterpages; ++i) {
1349 page = pcl->compressed_bvecs[i].page;
1351 if (erofs_page_is_managed(sbi, page))
1354 /* recycle all individual short-lived pages */
1355 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1356 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1359 if (be->compressed_pages < be->onstack_pages ||
1360 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1361 kvfree(be->compressed_pages);
1362 z_erofs_fill_other_copies(be, err);
1364 for (i = 0; i < be->nr_pages; ++i) {
1365 page = be->decompressed_pages[i];
1369 DBG_BUGON(z_erofs_page_is_invalidated(page));
1371 /* recycle all individual short-lived pages */
1372 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1375 z_erofs_page_mark_eio(page);
1376 z_erofs_onlinepage_endio(page);
1379 if (be->decompressed_pages != be->onstack_pages)
1380 kvfree(be->decompressed_pages);
1383 pcl->partial = true;
1384 pcl->multibases = false;
1385 pcl->bvset.nextpage = NULL;
1388 /* pcluster lock MUST be taken before the following line */
1389 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1390 mutex_unlock(&pcl->lock);
1394 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1395 struct page **pagepool)
1397 struct z_erofs_decompress_backend be = {
1399 .pagepool = pagepool,
1400 .decompressed_secondary_bvecs =
1401 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1403 z_erofs_next_pcluster_t owned = io->head;
1405 while (owned != Z_EROFS_PCLUSTER_TAIL) {
1406 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1408 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1409 owned = READ_ONCE(be.pcl->next);
1411 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1412 erofs_workgroup_put(&be.pcl->obj);
1416 static void z_erofs_decompressqueue_work(struct work_struct *work)
1418 struct z_erofs_decompressqueue *bgq =
1419 container_of(work, struct z_erofs_decompressqueue, u.work);
1420 struct page *pagepool = NULL;
1422 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1423 z_erofs_decompress_queue(bgq, &pagepool);
1424 erofs_release_pages(&pagepool);
1428 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1429 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1431 z_erofs_decompressqueue_work((struct work_struct *)work);
1435 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1438 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1440 /* wake up the caller thread for sync decompression */
1442 if (!atomic_add_return(bios, &io->pending_bios))
1443 complete(&io->u.done);
1447 if (atomic_add_return(bios, &io->pending_bios))
1449 /* Use (kthread_)work and sync decompression for atomic contexts only */
1450 if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
1451 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1452 struct kthread_worker *worker;
1455 worker = rcu_dereference(
1456 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1458 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1459 queue_work(z_erofs_workqueue, &io->u.work);
1461 kthread_queue_work(worker, &io->u.kthread_work);
1465 queue_work(z_erofs_workqueue, &io->u.work);
1467 /* enable sync decompression for readahead */
1468 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1469 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1472 z_erofs_decompressqueue_work(&io->u.work);
1475 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1477 struct page **pagepool,
1478 struct address_space *mc)
1480 const pgoff_t index = pcl->obj.index;
1481 gfp_t gfp = mapping_gfp_mask(mc);
1482 bool tocache = false;
1484 struct address_space *mapping;
1485 struct page *oldpage, *page;
1489 page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1495 justfound = (unsigned long)page & 1UL;
1496 page = (struct page *)((unsigned long)page & ~1UL);
1499 * preallocated cached pages, which is used to avoid direct reclaim
1500 * otherwise, it will go inplace I/O path instead.
1502 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1503 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1504 set_page_private(page, 0);
1508 mapping = READ_ONCE(page->mapping);
1511 * file-backed online pages in plcuster are all locked steady,
1512 * therefore it is impossible for `mapping' to be NULL.
1514 if (mapping && mapping != mc)
1515 /* ought to be unmanaged pages */
1518 /* directly return for shortlived page as well */
1519 if (z_erofs_is_shortlived_page(page))
1524 /* only true if page reclaim goes wrong, should never happen */
1525 DBG_BUGON(justfound && PagePrivate(page));
1527 /* the page is still in manage cache */
1528 if (page->mapping == mc) {
1529 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1531 if (!PagePrivate(page)) {
1533 * impossible to be !PagePrivate(page) for
1534 * the current restriction as well if
1535 * the page is already in compressed_bvecs[].
1537 DBG_BUGON(!justfound);
1540 set_page_private(page, (unsigned long)pcl);
1541 SetPagePrivate(page);
1544 /* no need to submit io if it is already up-to-date */
1545 if (PageUptodate(page)) {
1553 * the managed page has been truncated, it's unsafe to
1554 * reuse this one, let's allocate a new cache-managed page.
1556 DBG_BUGON(page->mapping);
1557 DBG_BUGON(!justfound);
1563 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1564 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1566 erofs_pagepool_add(pagepool, page);
1571 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1572 /* turn into temporary page if fails (1 ref) */
1573 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1576 attach_page_private(page, pcl);
1577 /* drop a refcount added by allocpage (then we have 2 refs here) */
1580 out: /* the only exit (for tracing and debugging) */
1584 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1585 struct z_erofs_decompressqueue *fgq, bool *fg)
1587 struct z_erofs_decompressqueue *q;
1590 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1595 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1596 kthread_init_work(&q->u.kthread_work,
1597 z_erofs_decompressqueue_kthread_work);
1599 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1604 init_completion(&fgq->u.done);
1605 atomic_set(&fgq->pending_bios, 0);
1610 q->head = Z_EROFS_PCLUSTER_TAIL;
1614 /* define decompression jobqueue types */
1621 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1622 z_erofs_next_pcluster_t qtail[],
1623 z_erofs_next_pcluster_t owned_head)
1625 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1626 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1628 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1630 WRITE_ONCE(*submit_qtail, owned_head);
1631 WRITE_ONCE(*bypass_qtail, &pcl->next);
1633 qtail[JQ_BYPASS] = &pcl->next;
1636 static void z_erofs_decompressqueue_endio(struct bio *bio)
1638 struct z_erofs_decompressqueue *q = bio->bi_private;
1639 blk_status_t err = bio->bi_status;
1640 struct bio_vec *bvec;
1641 struct bvec_iter_all iter_all;
1643 bio_for_each_segment_all(bvec, bio, iter_all) {
1644 struct page *page = bvec->bv_page;
1646 DBG_BUGON(PageUptodate(page));
1647 DBG_BUGON(z_erofs_page_is_invalidated(page));
1649 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1651 SetPageUptodate(page);
1657 z_erofs_decompress_kickoff(q, -1);
1661 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1662 struct z_erofs_decompressqueue *fgq,
1663 bool *force_fg, bool readahead)
1665 struct super_block *sb = f->inode->i_sb;
1666 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1667 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1668 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1669 z_erofs_next_pcluster_t owned_head = f->owned_head;
1670 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1672 struct block_device *last_bdev;
1673 unsigned int nr_bios = 0;
1674 struct bio *bio = NULL;
1675 unsigned long pflags;
1679 * if managed cache is enabled, bypass jobqueue is needed,
1680 * no need to read from device for all pclusters in this queue.
1682 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1683 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1685 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1686 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1688 /* by default, all need io submission */
1689 q[JQ_SUBMIT]->head = owned_head;
1692 struct erofs_map_dev mdev;
1693 struct z_erofs_pcluster *pcl;
1698 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1699 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1700 owned_head = READ_ONCE(pcl->next);
1702 if (z_erofs_is_inline_pcluster(pcl)) {
1703 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1707 /* no device id here, thus it will always succeed */
1708 mdev = (struct erofs_map_dev) {
1709 .m_pa = erofs_pos(sb, pcl->obj.index),
1711 (void)erofs_map_dev(sb, &mdev);
1713 cur = erofs_blknr(sb, mdev.m_pa);
1714 end = cur + pcl->pclusterpages;
1719 page = pickup_page_for_submission(pcl, i++,
1724 if (bio && (cur != last_index + 1 ||
1725 last_bdev != mdev.m_bdev)) {
1729 psi_memstall_leave(&pflags);
1735 if (unlikely(PageWorkingset(page)) && !memstall) {
1736 psi_memstall_enter(&pflags);
1741 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1742 REQ_OP_READ, GFP_NOIO);
1743 bio->bi_end_io = z_erofs_decompressqueue_endio;
1745 last_bdev = mdev.m_bdev;
1746 bio->bi_iter.bi_sector = (sector_t)cur <<
1747 (sb->s_blocksize_bits - 9);
1748 bio->bi_private = q[JQ_SUBMIT];
1750 bio->bi_opf |= REQ_RAHEAD;
1754 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1755 goto submit_bio_retry;
1759 } while (++cur < end);
1762 qtail[JQ_SUBMIT] = &pcl->next;
1764 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1765 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1770 psi_memstall_leave(&pflags);
1774 * although background is preferred, no one is pending for submission.
1775 * don't issue decompression but drop it directly instead.
1777 if (!*force_fg && !nr_bios) {
1778 kvfree(q[JQ_SUBMIT]);
1781 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1784 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1785 bool force_fg, bool ra)
1787 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1789 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1791 z_erofs_submit_queue(f, io, &force_fg, ra);
1793 /* handle bypass queue (no i/o pclusters) immediately */
1794 z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1799 /* wait until all bios are completed */
1800 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1802 /* handle synchronous decompress queue in the caller context */
1803 z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
1807 * Since partial uptodate is still unimplemented for now, we have to use
1808 * approximate readmore strategies as a start.
1810 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1811 struct readahead_control *rac, bool backmost)
1813 struct inode *inode = f->inode;
1814 struct erofs_map_blocks *map = &f->map;
1815 erofs_off_t cur, end, headoffset = f->headoffset;
1820 end = headoffset + readahead_length(rac) - 1;
1822 end = headoffset + PAGE_SIZE - 1;
1824 err = z_erofs_map_blocks_iter(inode, map,
1825 EROFS_GET_BLOCKS_READMORE);
1829 /* expand ra for the trailing edge if readahead */
1831 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1832 readahead_expand(rac, headoffset, cur - headoffset);
1835 end = round_up(end, PAGE_SIZE);
1837 end = round_up(map->m_la, PAGE_SIZE);
1843 cur = map->m_la + map->m_llen - 1;
1844 while (cur >= end) {
1845 pgoff_t index = cur >> PAGE_SHIFT;
1848 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1850 if (PageUptodate(page)) {
1853 err = z_erofs_do_read_page(f, page);
1855 erofs_err(inode->i_sb,
1856 "readmore error at page %lu @ nid %llu",
1857 index, EROFS_I(inode)->nid);
1862 if (cur < PAGE_SIZE)
1864 cur = (index << PAGE_SHIFT) - 1;
1868 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1870 struct page *page = &folio->page;
1871 struct inode *const inode = page->mapping->host;
1872 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1873 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1876 trace_erofs_readpage(page, false);
1877 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1879 z_erofs_pcluster_readmore(&f, NULL, true);
1880 err = z_erofs_do_read_page(&f, page);
1881 z_erofs_pcluster_readmore(&f, NULL, false);
1882 (void)z_erofs_collector_end(&f);
1884 /* if some compressed cluster ready, need submit them anyway */
1885 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
1888 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1890 erofs_put_metabuf(&f.map.buf);
1891 erofs_release_pages(&f.pagepool);
1895 static void z_erofs_readahead(struct readahead_control *rac)
1897 struct inode *const inode = rac->mapping->host;
1898 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1899 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1900 struct page *head = NULL, *page;
1901 unsigned int nr_pages;
1903 f.headoffset = readahead_pos(rac);
1905 z_erofs_pcluster_readmore(&f, rac, true);
1906 nr_pages = readahead_count(rac);
1907 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1909 while ((page = readahead_page(rac))) {
1910 set_page_private(page, (unsigned long)head);
1915 struct page *page = head;
1918 /* traversal in reverse order */
1919 head = (void *)page_private(page);
1921 err = z_erofs_do_read_page(&f, page);
1923 erofs_err(inode->i_sb,
1924 "readahead error at page %lu @ nid %llu",
1925 page->index, EROFS_I(inode)->nid);
1928 z_erofs_pcluster_readmore(&f, rac, false);
1929 (void)z_erofs_collector_end(&f);
1931 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_pages), true);
1932 erofs_put_metabuf(&f.map.buf);
1933 erofs_release_pages(&f.pagepool);
1936 const struct address_space_operations z_erofs_aops = {
1937 .read_folio = z_erofs_read_folio,
1938 .readahead = z_erofs_readahead,