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: pcluster size (compressed size) in bytes */
60 unsigned int pclustersize;
62 /* I: page offset of start position of decompression */
63 unsigned short pageofs_out;
65 /* I: page offset of inline compressed data */
66 unsigned short pageofs_in;
69 /* L: inline a certain number of bvec for bootstrap */
70 struct z_erofs_bvset_inline bvset;
72 /* I: can be used to free the pcluster by RCU. */
76 /* I: compression algorithm format */
77 unsigned char algorithmformat;
79 /* L: whether partial decompression or not */
82 /* L: indicate several pageofs_outs or not */
85 /* A: compressed bvecs (can be cached or inplaced pages) */
86 struct z_erofs_bvec compressed_bvecs[];
89 /* the end of a chain of pclusters */
90 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
91 #define Z_EROFS_PCLUSTER_NIL (NULL)
93 struct z_erofs_decompressqueue {
94 struct super_block *sb;
95 atomic_t pending_bios;
96 z_erofs_next_pcluster_t head;
99 struct completion done;
100 struct work_struct work;
101 struct kthread_work kthread_work;
106 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
108 return !pcl->obj.index;
111 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
113 return PAGE_ALIGN(pcl->pclustersize) >> PAGE_SHIFT;
117 * bit 30: I/O error occurred on this page
118 * bit 0 - 29: remaining parts to complete this page
120 #define Z_EROFS_PAGE_EIO (1 << 30)
122 static inline void z_erofs_onlinepage_init(struct page *page)
127 } u = { .o = ATOMIC_INIT(1) };
129 set_page_private(page, u.v);
131 SetPagePrivate(page);
134 static inline void z_erofs_onlinepage_split(struct page *page)
136 atomic_inc((atomic_t *)&page->private);
139 static void z_erofs_onlinepage_endio(struct page *page, int err)
143 DBG_BUGON(!PagePrivate(page));
146 orig = atomic_read((atomic_t *)&page->private);
147 v = (orig - 1) | (err ? Z_EROFS_PAGE_EIO : 0);
148 } while (atomic_cmpxchg((atomic_t *)&page->private, orig, v) != orig);
150 if (!(v & ~Z_EROFS_PAGE_EIO)) {
151 set_page_private(page, 0);
152 ClearPagePrivate(page);
153 if (!(v & Z_EROFS_PAGE_EIO))
154 SetPageUptodate(page);
159 #define Z_EROFS_ONSTACK_PAGES 32
162 * since pclustersize is variable for big pcluster feature, introduce slab
163 * pools implementation for different pcluster sizes.
165 struct z_erofs_pcluster_slab {
166 struct kmem_cache *slab;
167 unsigned int maxpages;
171 #define _PCLP(n) { .maxpages = n }
173 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
174 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
175 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
178 struct z_erofs_bvec_iter {
180 struct z_erofs_bvset *bvset;
181 unsigned int nr, cur;
184 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
187 kunmap_local(iter->bvset);
191 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
193 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
194 /* have to access nextpage in advance, otherwise it will be unmapped */
195 struct page *nextpage = iter->bvset->nextpage;
196 struct page *oldpage;
198 DBG_BUGON(!nextpage);
199 oldpage = z_erofs_bvec_iter_end(iter);
200 iter->bvpage = nextpage;
201 iter->bvset = kmap_local_page(nextpage);
202 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
207 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
208 struct z_erofs_bvset_inline *bvset,
209 unsigned int bootstrap_nr,
212 *iter = (struct z_erofs_bvec_iter) {
214 .bvset = (struct z_erofs_bvset *)bvset,
217 while (cur > iter->nr) {
219 z_erofs_bvset_flip(iter);
224 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
225 struct z_erofs_bvec *bvec,
226 struct page **candidate_bvpage,
227 struct page **pagepool)
229 if (iter->cur >= iter->nr) {
230 struct page *nextpage = *candidate_bvpage;
233 nextpage = erofs_allocpage(pagepool, GFP_NOFS);
236 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
238 DBG_BUGON(iter->bvset->nextpage);
239 iter->bvset->nextpage = nextpage;
240 z_erofs_bvset_flip(iter);
242 iter->bvset->nextpage = NULL;
243 *candidate_bvpage = NULL;
245 iter->bvset->bvec[iter->cur++] = *bvec;
249 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
250 struct z_erofs_bvec *bvec,
251 struct page **old_bvpage)
253 if (iter->cur == iter->nr)
254 *old_bvpage = z_erofs_bvset_flip(iter);
257 *bvec = iter->bvset->bvec[iter->cur++];
260 static void z_erofs_destroy_pcluster_pool(void)
264 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
265 if (!pcluster_pool[i].slab)
267 kmem_cache_destroy(pcluster_pool[i].slab);
268 pcluster_pool[i].slab = NULL;
272 static int z_erofs_create_pcluster_pool(void)
274 struct z_erofs_pcluster_slab *pcs;
275 struct z_erofs_pcluster *a;
278 for (pcs = pcluster_pool;
279 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
280 size = struct_size(a, compressed_bvecs, pcs->maxpages);
282 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
283 pcs->slab = kmem_cache_create(pcs->name, size, 0,
284 SLAB_RECLAIM_ACCOUNT, NULL);
288 z_erofs_destroy_pcluster_pool();
294 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
296 unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
297 struct z_erofs_pcluster_slab *pcs = pcluster_pool;
299 for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
300 struct z_erofs_pcluster *pcl;
302 if (nrpages > pcs->maxpages)
305 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
307 return ERR_PTR(-ENOMEM);
308 pcl->pclustersize = size;
311 return ERR_PTR(-EINVAL);
314 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
316 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
319 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
320 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
322 if (pclusterpages > pcs->maxpages)
325 kmem_cache_free(pcs->slab, pcl);
331 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
333 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
334 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
336 static void erofs_destroy_percpu_workers(void)
338 struct kthread_worker *worker;
341 for_each_possible_cpu(cpu) {
342 worker = rcu_dereference_protected(
343 z_erofs_pcpu_workers[cpu], 1);
344 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
346 kthread_destroy_worker(worker);
348 kfree(z_erofs_pcpu_workers);
351 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
353 struct kthread_worker *worker =
354 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
358 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
359 sched_set_fifo_low(worker->task);
363 static int erofs_init_percpu_workers(void)
365 struct kthread_worker *worker;
368 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
369 sizeof(struct kthread_worker *), GFP_ATOMIC);
370 if (!z_erofs_pcpu_workers)
373 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
374 worker = erofs_init_percpu_worker(cpu);
376 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
381 static inline void erofs_destroy_percpu_workers(void) {}
382 static inline int erofs_init_percpu_workers(void) { return 0; }
385 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
386 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
387 static enum cpuhp_state erofs_cpuhp_state;
389 static int erofs_cpu_online(unsigned int cpu)
391 struct kthread_worker *worker, *old;
393 worker = erofs_init_percpu_worker(cpu);
395 return PTR_ERR(worker);
397 spin_lock(&z_erofs_pcpu_worker_lock);
398 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
399 lockdep_is_held(&z_erofs_pcpu_worker_lock));
401 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
402 spin_unlock(&z_erofs_pcpu_worker_lock);
404 kthread_destroy_worker(worker);
408 static int erofs_cpu_offline(unsigned int cpu)
410 struct kthread_worker *worker;
412 spin_lock(&z_erofs_pcpu_worker_lock);
413 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
414 lockdep_is_held(&z_erofs_pcpu_worker_lock));
415 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
416 spin_unlock(&z_erofs_pcpu_worker_lock);
420 kthread_destroy_worker(worker);
424 static int erofs_cpu_hotplug_init(void)
428 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
429 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
433 erofs_cpuhp_state = state;
437 static void erofs_cpu_hotplug_destroy(void)
439 if (erofs_cpuhp_state)
440 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
442 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
443 static inline int erofs_cpu_hotplug_init(void) { return 0; }
444 static inline void erofs_cpu_hotplug_destroy(void) {}
447 void z_erofs_exit_zip_subsystem(void)
449 erofs_cpu_hotplug_destroy();
450 erofs_destroy_percpu_workers();
451 destroy_workqueue(z_erofs_workqueue);
452 z_erofs_destroy_pcluster_pool();
455 int __init z_erofs_init_zip_subsystem(void)
457 int err = z_erofs_create_pcluster_pool();
460 goto out_error_pcluster_pool;
462 z_erofs_workqueue = alloc_workqueue("erofs_worker",
463 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
464 if (!z_erofs_workqueue) {
466 goto out_error_workqueue_init;
469 err = erofs_init_percpu_workers();
471 goto out_error_pcpu_worker;
473 err = erofs_cpu_hotplug_init();
475 goto out_error_cpuhp_init;
478 out_error_cpuhp_init:
479 erofs_destroy_percpu_workers();
480 out_error_pcpu_worker:
481 destroy_workqueue(z_erofs_workqueue);
482 out_error_workqueue_init:
483 z_erofs_destroy_pcluster_pool();
484 out_error_pcluster_pool:
488 enum z_erofs_pclustermode {
489 Z_EROFS_PCLUSTER_INFLIGHT,
491 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
492 * could be dispatched into bypass queue later due to uptodated managed
493 * pages. All related online pages cannot be reused for inplace I/O (or
494 * bvpage) since it can be directly decoded without I/O submission.
496 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
498 * The pcluster was just linked to a decompression chain by us. It can
499 * also be linked with the remaining pclusters, which means if the
500 * processing page is the tail page of a pcluster, this pcluster can
501 * safely use the whole page (since the previous pcluster is within the
502 * same chain) for in-place I/O, as illustrated below:
503 * ___________________________________________________
504 * | tail (partial) page | head (partial) page |
505 * | (of the current pcl) | (of the previous pcl) |
506 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____|
508 * [ (*) the page above can be used as inplace I/O. ]
510 Z_EROFS_PCLUSTER_FOLLOWED,
513 struct z_erofs_decompress_frontend {
514 struct inode *const inode;
515 struct erofs_map_blocks map;
516 struct z_erofs_bvec_iter biter;
518 struct page *pagepool;
519 struct page *candidate_bvpage;
520 struct z_erofs_pcluster *pcl;
521 z_erofs_next_pcluster_t owned_head;
522 enum z_erofs_pclustermode mode;
524 erofs_off_t headoffset;
526 /* a pointer used to pick up inplace I/O pages */
530 #define DECOMPRESS_FRONTEND_INIT(__i) { \
531 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
532 .mode = Z_EROFS_PCLUSTER_FOLLOWED }
534 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
536 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
538 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
541 if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
544 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
545 fe->map.m_la < fe->headoffset)
551 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
553 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
554 struct z_erofs_pcluster *pcl = fe->pcl;
555 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
556 bool shouldalloc = z_erofs_should_alloc_cache(fe);
557 bool standalone = true;
559 * optimistic allocation without direct reclaim since inplace I/O
560 * can be used if low memory otherwise.
562 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
563 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
566 if (i_blocksize(fe->inode) != PAGE_SIZE)
568 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
571 for (i = 0; i < pclusterpages; ++i) {
572 struct page *page, *newpage;
573 void *t; /* mark pages just found for debugging */
575 /* the compressed page was loaded before */
576 if (READ_ONCE(pcl->compressed_bvecs[i].page))
579 page = find_get_page(mc, pcl->obj.index + i);
582 t = (void *)((unsigned long)page | 1);
585 /* I/O is needed, no possible to decompress directly */
591 * Try cached I/O if allocation succeeds or fallback to
592 * in-place I/O instead to avoid any direct reclaim.
594 newpage = erofs_allocpage(&fe->pagepool, gfp);
597 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
598 t = (void *)((unsigned long)newpage | 1);
601 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
607 erofs_pagepool_add(&fe->pagepool, newpage);
611 * don't do inplace I/O if all compressed pages are available in
612 * managed cache since it can be moved to the bypass queue instead.
615 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
618 /* called by erofs_shrinker to get rid of all compressed_pages */
619 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
620 struct erofs_workgroup *grp)
622 struct z_erofs_pcluster *const pcl =
623 container_of(grp, struct z_erofs_pcluster, obj);
624 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
627 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
629 * refcount of workgroup is now freezed as 0,
630 * therefore no need to worry about available decompression users.
632 for (i = 0; i < pclusterpages; ++i) {
633 struct page *page = pcl->compressed_bvecs[i].page;
638 /* block other users from reclaiming or migrating the page */
639 if (!trylock_page(page))
642 if (!erofs_page_is_managed(sbi, page))
645 /* barrier is implied in the following 'unlock_page' */
646 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
647 detach_page_private(page);
653 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
655 struct z_erofs_pcluster *pcl = folio_get_private(folio);
656 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
660 if (!folio_test_private(folio))
664 spin_lock(&pcl->obj.lockref.lock);
665 if (pcl->obj.lockref.count > 0)
668 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
669 for (i = 0; i < pclusterpages; ++i) {
670 if (pcl->compressed_bvecs[i].page == &folio->page) {
671 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
677 folio_detach_private(folio);
679 spin_unlock(&pcl->obj.lockref.lock);
684 * It will be called only on inode eviction. In case that there are still some
685 * decompression requests in progress, wait with rescheduling for a bit here.
686 * An extra lock could be introduced instead but it seems unnecessary.
688 static void z_erofs_cache_invalidate_folio(struct folio *folio,
689 size_t offset, size_t length)
691 const size_t stop = length + offset;
693 /* Check for potential overflow in debug mode */
694 DBG_BUGON(stop > folio_size(folio) || stop < length);
696 if (offset == 0 && stop == folio_size(folio))
697 while (!z_erofs_cache_release_folio(folio, GFP_NOFS))
701 static const struct address_space_operations z_erofs_cache_aops = {
702 .release_folio = z_erofs_cache_release_folio,
703 .invalidate_folio = z_erofs_cache_invalidate_folio,
706 int erofs_init_managed_cache(struct super_block *sb)
708 struct inode *const inode = new_inode(sb);
714 inode->i_size = OFFSET_MAX;
715 inode->i_mapping->a_ops = &z_erofs_cache_aops;
716 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
717 EROFS_SB(sb)->managed_cache = inode;
721 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
722 struct z_erofs_bvec *bvec)
724 struct z_erofs_pcluster *const pcl = fe->pcl;
726 while (fe->icur > 0) {
727 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
729 pcl->compressed_bvecs[fe->icur] = *bvec;
736 /* callers must be with pcluster lock held */
737 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
738 struct z_erofs_bvec *bvec, bool exclusive)
743 /* give priority for inplaceio to use file pages first */
744 if (z_erofs_try_inplace_io(fe, bvec))
746 /* otherwise, check if it can be used as a bvpage */
747 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
748 !fe->candidate_bvpage)
749 fe->candidate_bvpage = bvec->page;
751 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
753 fe->pcl->vcnt += (ret >= 0);
757 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
759 struct z_erofs_pcluster *pcl = f->pcl;
760 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
762 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
763 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
764 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
765 *owned_head = &pcl->next;
766 /* so we can attach this pcluster to our submission chain. */
767 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
771 /* type 2, it belongs to an ongoing chain */
772 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
775 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
777 struct erofs_map_blocks *map = &fe->map;
778 struct super_block *sb = fe->inode->i_sb;
779 bool ztailpacking = map->m_flags & EROFS_MAP_META;
780 struct z_erofs_pcluster *pcl;
781 struct erofs_workgroup *grp;
784 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
785 (!ztailpacking && !erofs_blknr(sb, map->m_pa))) {
787 return -EFSCORRUPTED;
790 /* no available pcluster, let's allocate one */
791 pcl = z_erofs_alloc_pcluster(map->m_plen);
795 spin_lock_init(&pcl->obj.lockref.lock);
796 pcl->obj.lockref.count = 1; /* one ref for this request */
797 pcl->algorithmformat = map->m_algorithmformat;
801 /* new pclusters should be claimed as type 1, primary and followed */
802 pcl->next = fe->owned_head;
803 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
804 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
807 * lock all primary followed works before visible to others
808 * and mutex_trylock *never* fails for a new pcluster.
810 mutex_init(&pcl->lock);
811 DBG_BUGON(!mutex_trylock(&pcl->lock));
814 pcl->obj.index = 0; /* which indicates ztailpacking */
816 pcl->obj.index = erofs_blknr(sb, map->m_pa);
818 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
824 if (grp != &pcl->obj) {
825 fe->pcl = container_of(grp,
826 struct z_erofs_pcluster, obj);
831 fe->owned_head = &pcl->next;
836 mutex_unlock(&pcl->lock);
837 z_erofs_free_pcluster(pcl);
841 static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
843 struct erofs_map_blocks *map = &fe->map;
844 struct super_block *sb = fe->inode->i_sb;
845 erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
846 struct erofs_workgroup *grp = NULL;
851 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
852 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
854 if (!(map->m_flags & EROFS_MAP_META)) {
855 grp = erofs_find_workgroup(sb, blknr);
856 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
858 return -EFSCORRUPTED;
862 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
865 ret = z_erofs_register_pcluster(fe);
868 if (ret == -EEXIST) {
869 mutex_lock(&fe->pcl->lock);
870 z_erofs_try_to_claim_pcluster(fe);
875 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
876 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
877 if (!z_erofs_is_inline_pcluster(fe->pcl)) {
878 /* bind cache first when cached decompression is preferred */
879 z_erofs_bind_cache(fe);
883 mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP);
886 erofs_err(sb, "failed to get inline data %d", ret);
889 get_page(map->buf.page);
890 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
891 fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
892 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
894 /* file-backed inplace I/O pages are traversed in reverse order */
895 fe->icur = z_erofs_pclusterpages(fe->pcl);
900 * keep in mind that no referenced pclusters will be freed
901 * only after a RCU grace period.
903 static void z_erofs_rcu_callback(struct rcu_head *head)
905 z_erofs_free_pcluster(container_of(head,
906 struct z_erofs_pcluster, rcu));
909 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
911 struct z_erofs_pcluster *const pcl =
912 container_of(grp, struct z_erofs_pcluster, obj);
914 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
917 static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
919 struct z_erofs_pcluster *pcl = fe->pcl;
924 z_erofs_bvec_iter_end(&fe->biter);
925 mutex_unlock(&pcl->lock);
927 if (fe->candidate_bvpage)
928 fe->candidate_bvpage = NULL;
931 * if all pending pages are added, don't hold its reference
932 * any longer if the pcluster isn't hosted by ourselves.
934 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
935 erofs_workgroup_put(&pcl->obj);
940 static int z_erofs_read_fragment(struct super_block *sb, struct page *page,
941 unsigned int cur, unsigned int end, erofs_off_t pos)
943 struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
944 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
949 return -EFSCORRUPTED;
951 buf.inode = packed_inode;
952 for (; cur < end; cur += cnt, pos += cnt) {
953 cnt = min_t(unsigned int, end - cur,
954 sb->s_blocksize - erofs_blkoff(sb, pos));
955 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
957 erofs_put_metabuf(&buf);
960 memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt);
962 erofs_put_metabuf(&buf);
966 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
969 struct inode *const inode = fe->inode;
970 struct erofs_map_blocks *const map = &fe->map;
971 const loff_t offset = page_offset(page);
972 const unsigned int bs = i_blocksize(inode);
973 bool tight = true, exclusive;
974 unsigned int cur, end, len, split;
977 z_erofs_onlinepage_init(page);
981 if (offset + end - 1 < map->m_la ||
982 offset + end - 1 >= map->m_la + map->m_llen) {
983 z_erofs_pcluster_end(fe);
984 map->m_la = offset + end - 1;
986 err = z_erofs_map_blocks_iter(inode, map, 0);
991 cur = offset > map->m_la ? 0 : map->m_la - offset;
992 /* bump split parts first to avoid several separate cases */
995 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
996 zero_user_segment(page, cur, end);
1001 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1002 erofs_off_t fpos = offset + cur - map->m_la;
1004 len = min_t(unsigned int, map->m_llen - fpos, end - cur);
1005 err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len,
1006 EROFS_I(inode)->z_fragmentoff + fpos);
1014 err = z_erofs_pcluster_begin(fe);
1020 * Ensure the current partial page belongs to this submit chain rather
1021 * than other concurrent submit chains or the noio(bypass) chain since
1022 * those chains are handled asynchronously thus the page cannot be used
1023 * for inplace I/O or bvpage (should be processed in a strict order.)
1025 tight &= (fe->mode > Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1026 exclusive = (!cur && ((split <= 1) || (tight && bs == PAGE_SIZE)));
1028 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1030 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1032 .offset = offset - map->m_la,
1038 z_erofs_onlinepage_split(page);
1039 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1040 fe->pcl->multibases = true;
1041 if (fe->pcl->length < offset + end - map->m_la) {
1042 fe->pcl->length = offset + end - map->m_la;
1043 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1045 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1046 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1047 fe->pcl->length == map->m_llen)
1048 fe->pcl->partial = false;
1050 /* shorten the remaining extent to update progress */
1051 map->m_llen = offset + cur - map->m_la;
1052 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1059 z_erofs_onlinepage_endio(page, err);
1063 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1064 unsigned int readahead_pages)
1066 /* auto: enable for read_folio, disable for readahead */
1067 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1071 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1072 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1078 static bool z_erofs_page_is_invalidated(struct page *page)
1080 return !page->mapping && !z_erofs_is_shortlived_page(page);
1083 struct z_erofs_decompress_backend {
1084 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1085 struct super_block *sb;
1086 struct z_erofs_pcluster *pcl;
1088 /* pages with the longest decompressed length for deduplication */
1089 struct page **decompressed_pages;
1090 /* pages to keep the compressed data */
1091 struct page **compressed_pages;
1093 struct list_head decompressed_secondary_bvecs;
1094 struct page **pagepool;
1095 unsigned int onstack_used, nr_pages;
1098 struct z_erofs_bvec_item {
1099 struct z_erofs_bvec bvec;
1100 struct list_head list;
1103 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1104 struct z_erofs_bvec *bvec)
1106 struct z_erofs_bvec_item *item;
1109 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK) &&
1110 (bvec->end == PAGE_SIZE ||
1111 bvec->offset + bvec->end == be->pcl->length)) {
1112 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1113 DBG_BUGON(pgnr >= be->nr_pages);
1114 if (!be->decompressed_pages[pgnr]) {
1115 be->decompressed_pages[pgnr] = bvec->page;
1120 /* (cold path) one pcluster is requested multiple times */
1121 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1123 list_add(&item->list, &be->decompressed_secondary_bvecs);
1126 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1129 unsigned int off0 = be->pcl->pageofs_out;
1130 struct list_head *p, *n;
1132 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1133 struct z_erofs_bvec_item *bvi;
1134 unsigned int end, cur;
1137 bvi = container_of(p, struct z_erofs_bvec_item, list);
1138 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1139 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1141 dst = kmap_local_page(bvi->bvec.page);
1143 unsigned int pgnr, scur, len;
1145 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1146 DBG_BUGON(pgnr >= be->nr_pages);
1148 scur = bvi->bvec.offset + cur -
1149 ((pgnr << PAGE_SHIFT) - off0);
1150 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1151 if (!be->decompressed_pages[pgnr]) {
1152 err = -EFSCORRUPTED;
1156 src = kmap_local_page(be->decompressed_pages[pgnr]);
1157 memcpy(dst + cur, src + scur, len);
1162 z_erofs_onlinepage_endio(bvi->bvec.page, err);
1168 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1170 struct z_erofs_pcluster *pcl = be->pcl;
1171 struct z_erofs_bvec_iter biter;
1172 struct page *old_bvpage;
1175 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1176 for (i = 0; i < pcl->vcnt; ++i) {
1177 struct z_erofs_bvec bvec;
1179 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1182 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1184 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1185 z_erofs_do_decompressed_bvec(be, &bvec);
1188 old_bvpage = z_erofs_bvec_iter_end(&biter);
1190 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1193 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1196 struct z_erofs_pcluster *pcl = be->pcl;
1197 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1200 *overlapped = false;
1201 for (i = 0; i < pclusterpages; ++i) {
1202 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1203 struct page *page = bvec->page;
1205 /* compressed data ought to be valid before decompressing */
1210 be->compressed_pages[i] = page;
1212 if (z_erofs_is_inline_pcluster(pcl) ||
1213 erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1214 if (!PageUptodate(page))
1219 DBG_BUGON(z_erofs_page_is_invalidated(page));
1220 if (z_erofs_is_shortlived_page(page))
1222 z_erofs_do_decompressed_bvec(be, bvec);
1228 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1231 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1232 struct z_erofs_pcluster *pcl = be->pcl;
1233 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1234 const struct z_erofs_decompressor *decomp =
1235 &erofs_decompressors[pcl->algorithmformat];
1240 mutex_lock(&pcl->lock);
1241 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1243 /* allocate (de)compressed page arrays if cannot be kept on stack */
1244 be->decompressed_pages = NULL;
1245 be->compressed_pages = NULL;
1246 be->onstack_used = 0;
1247 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1248 be->decompressed_pages = be->onstack_pages;
1249 be->onstack_used = be->nr_pages;
1250 memset(be->decompressed_pages, 0,
1251 sizeof(struct page *) * be->nr_pages);
1254 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1255 be->compressed_pages = be->onstack_pages + be->onstack_used;
1257 if (!be->decompressed_pages)
1258 be->decompressed_pages =
1259 kvcalloc(be->nr_pages, sizeof(struct page *),
1260 GFP_KERNEL | __GFP_NOFAIL);
1261 if (!be->compressed_pages)
1262 be->compressed_pages =
1263 kvcalloc(pclusterpages, sizeof(struct page *),
1264 GFP_KERNEL | __GFP_NOFAIL);
1266 z_erofs_parse_out_bvecs(be);
1267 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1271 err = decomp->decompress(&(struct z_erofs_decompress_req) {
1273 .in = be->compressed_pages,
1274 .out = be->decompressed_pages,
1275 .pageofs_in = pcl->pageofs_in,
1276 .pageofs_out = pcl->pageofs_out,
1277 .inputsize = pcl->pclustersize,
1278 .outputsize = pcl->length,
1279 .alg = pcl->algorithmformat,
1280 .inplace_io = overlapped,
1281 .partial_decoding = pcl->partial,
1282 .fillgaps = pcl->multibases,
1285 /* must handle all compressed pages before actual file pages */
1286 if (z_erofs_is_inline_pcluster(pcl)) {
1287 page = pcl->compressed_bvecs[0].page;
1288 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1291 for (i = 0; i < pclusterpages; ++i) {
1292 /* consider shortlived pages added when decompressing */
1293 page = be->compressed_pages[i];
1295 if (!page || erofs_page_is_managed(sbi, page))
1297 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1298 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1301 if (be->compressed_pages < be->onstack_pages ||
1302 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1303 kvfree(be->compressed_pages);
1304 z_erofs_fill_other_copies(be, err);
1306 for (i = 0; i < be->nr_pages; ++i) {
1307 page = be->decompressed_pages[i];
1311 DBG_BUGON(z_erofs_page_is_invalidated(page));
1313 /* recycle all individual short-lived pages */
1314 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1316 z_erofs_onlinepage_endio(page, err);
1319 if (be->decompressed_pages != be->onstack_pages)
1320 kvfree(be->decompressed_pages);
1323 pcl->partial = true;
1324 pcl->multibases = false;
1325 pcl->bvset.nextpage = NULL;
1328 /* pcluster lock MUST be taken before the following line */
1329 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1330 mutex_unlock(&pcl->lock);
1334 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1335 struct page **pagepool)
1337 struct z_erofs_decompress_backend be = {
1339 .pagepool = pagepool,
1340 .decompressed_secondary_bvecs =
1341 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1343 z_erofs_next_pcluster_t owned = io->head;
1345 while (owned != Z_EROFS_PCLUSTER_TAIL) {
1346 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1348 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1349 owned = READ_ONCE(be.pcl->next);
1351 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1352 if (z_erofs_is_inline_pcluster(be.pcl))
1353 z_erofs_free_pcluster(be.pcl);
1355 erofs_workgroup_put(&be.pcl->obj);
1359 static void z_erofs_decompressqueue_work(struct work_struct *work)
1361 struct z_erofs_decompressqueue *bgq =
1362 container_of(work, struct z_erofs_decompressqueue, u.work);
1363 struct page *pagepool = NULL;
1365 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1366 z_erofs_decompress_queue(bgq, &pagepool);
1367 erofs_release_pages(&pagepool);
1371 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1372 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1374 z_erofs_decompressqueue_work((struct work_struct *)work);
1378 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1381 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1383 /* wake up the caller thread for sync decompression */
1385 if (!atomic_add_return(bios, &io->pending_bios))
1386 complete(&io->u.done);
1390 if (atomic_add_return(bios, &io->pending_bios))
1392 /* Use (kthread_)work and sync decompression for atomic contexts only */
1393 if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
1394 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1395 struct kthread_worker *worker;
1398 worker = rcu_dereference(
1399 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1401 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1402 queue_work(z_erofs_workqueue, &io->u.work);
1404 kthread_queue_work(worker, &io->u.kthread_work);
1408 queue_work(z_erofs_workqueue, &io->u.work);
1410 /* enable sync decompression for readahead */
1411 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1412 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1415 z_erofs_decompressqueue_work(&io->u.work);
1418 static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
1419 struct z_erofs_decompress_frontend *f,
1420 struct z_erofs_pcluster *pcl,
1422 struct address_space *mc)
1424 gfp_t gfp = mapping_gfp_mask(mc);
1425 bool tocache = false;
1426 struct z_erofs_bvec *zbv = pcl->compressed_bvecs + nr;
1427 struct address_space *mapping;
1428 struct page *page, *oldpage;
1429 int justfound, bs = i_blocksize(f->inode);
1431 /* Except for inplace pages, the entire page can be used for I/Os */
1432 bvec->bv_offset = 0;
1433 bvec->bv_len = PAGE_SIZE;
1435 oldpage = READ_ONCE(zbv->page);
1439 justfound = (unsigned long)oldpage & 1UL;
1440 page = (struct page *)((unsigned long)oldpage & ~1UL);
1441 bvec->bv_page = page;
1443 DBG_BUGON(z_erofs_is_shortlived_page(page));
1445 * Handle preallocated cached pages. We tried to allocate such pages
1446 * without triggering direct reclaim. If allocation failed, inplace
1447 * file-backed pages will be used instead.
1449 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1450 set_page_private(page, 0);
1451 WRITE_ONCE(zbv->page, page);
1456 mapping = READ_ONCE(page->mapping);
1458 * File-backed pages for inplace I/Os are all locked steady,
1459 * therefore it is impossible for `mapping` to be NULL.
1461 if (mapping && mapping != mc) {
1462 if (zbv->offset < 0)
1463 bvec->bv_offset = round_up(-zbv->offset, bs);
1464 bvec->bv_len = round_up(zbv->end, bs) - bvec->bv_offset;
1469 /* only true if page reclaim goes wrong, should never happen */
1470 DBG_BUGON(justfound && PagePrivate(page));
1472 /* the cached page is still in managed cache */
1473 if (page->mapping == mc) {
1474 WRITE_ONCE(zbv->page, page);
1476 * The cached page is still available but without a valid
1477 * `->private` pcluster hint. Let's reconnect them.
1479 if (!PagePrivate(page)) {
1480 DBG_BUGON(!justfound);
1481 /* compressed_bvecs[] already takes a ref */
1482 attach_page_private(page, pcl);
1486 /* no need to submit if it is already up-to-date */
1487 if (PageUptodate(page)) {
1489 bvec->bv_page = NULL;
1495 * It has been truncated, so it's unsafe to reuse this one. Let's
1496 * allocate a new page for compressed data.
1498 DBG_BUGON(page->mapping);
1499 DBG_BUGON(!justfound);
1505 page = erofs_allocpage(&f->pagepool, gfp | __GFP_NOFAIL);
1506 if (oldpage != cmpxchg(&zbv->page, oldpage, page)) {
1507 erofs_pagepool_add(&f->pagepool, page);
1511 bvec->bv_page = page;
1513 if (!tocache || bs != PAGE_SIZE ||
1514 add_to_page_cache_lru(page, mc, pcl->obj.index + nr, gfp)) {
1515 /* turn into a temporary shortlived page (1 ref) */
1516 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1519 attach_page_private(page, pcl);
1520 /* drop a refcount added by allocpage (then 2 refs in total here) */
1524 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1525 struct z_erofs_decompressqueue *fgq, bool *fg)
1527 struct z_erofs_decompressqueue *q;
1530 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1535 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1536 kthread_init_work(&q->u.kthread_work,
1537 z_erofs_decompressqueue_kthread_work);
1539 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1544 init_completion(&fgq->u.done);
1545 atomic_set(&fgq->pending_bios, 0);
1550 q->head = Z_EROFS_PCLUSTER_TAIL;
1554 /* define decompression jobqueue types */
1561 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1562 z_erofs_next_pcluster_t qtail[],
1563 z_erofs_next_pcluster_t owned_head)
1565 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1566 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1568 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1570 WRITE_ONCE(*submit_qtail, owned_head);
1571 WRITE_ONCE(*bypass_qtail, &pcl->next);
1573 qtail[JQ_BYPASS] = &pcl->next;
1576 static void z_erofs_submissionqueue_endio(struct bio *bio)
1578 struct z_erofs_decompressqueue *q = bio->bi_private;
1579 blk_status_t err = bio->bi_status;
1580 struct bio_vec *bvec;
1581 struct bvec_iter_all iter_all;
1583 bio_for_each_segment_all(bvec, bio, iter_all) {
1584 struct page *page = bvec->bv_page;
1586 DBG_BUGON(PageUptodate(page));
1587 DBG_BUGON(z_erofs_page_is_invalidated(page));
1588 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1590 SetPageUptodate(page);
1596 z_erofs_decompress_kickoff(q, -1);
1600 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1601 struct z_erofs_decompressqueue *fgq,
1602 bool *force_fg, bool readahead)
1604 struct super_block *sb = f->inode->i_sb;
1605 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1606 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1607 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1608 z_erofs_next_pcluster_t owned_head = f->owned_head;
1609 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1610 erofs_off_t last_pa;
1611 struct block_device *last_bdev;
1612 unsigned int nr_bios = 0;
1613 struct bio *bio = NULL;
1614 unsigned long pflags;
1617 /* No need to read from device for pclusters in the bypass queue. */
1618 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1619 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1621 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1622 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1624 /* by default, all need io submission */
1625 q[JQ_SUBMIT]->head = owned_head;
1628 struct erofs_map_dev mdev;
1629 struct z_erofs_pcluster *pcl;
1630 erofs_off_t cur, end;
1631 struct bio_vec bvec;
1635 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1636 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1637 owned_head = READ_ONCE(pcl->next);
1639 if (z_erofs_is_inline_pcluster(pcl)) {
1640 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1644 /* no device id here, thus it will always succeed */
1645 mdev = (struct erofs_map_dev) {
1646 .m_pa = erofs_pos(sb, pcl->obj.index),
1648 (void)erofs_map_dev(sb, &mdev);
1651 end = cur + pcl->pclustersize;
1653 z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
1657 if (bio && (cur != last_pa ||
1658 last_bdev != mdev.m_bdev)) {
1662 psi_memstall_leave(&pflags);
1668 if (unlikely(PageWorkingset(bvec.bv_page)) &&
1670 psi_memstall_enter(&pflags);
1675 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1676 REQ_OP_READ, GFP_NOIO);
1677 bio->bi_end_io = z_erofs_submissionqueue_endio;
1678 bio->bi_iter.bi_sector = cur >> 9;
1679 bio->bi_private = q[JQ_SUBMIT];
1681 bio->bi_opf |= REQ_RAHEAD;
1683 last_bdev = mdev.m_bdev;
1686 if (cur + bvec.bv_len > end)
1687 bvec.bv_len = end - cur;
1688 if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
1690 goto submit_bio_retry;
1692 last_pa = cur + bvec.bv_len;
1694 } while ((cur += bvec.bv_len) < end);
1697 qtail[JQ_SUBMIT] = &pcl->next;
1699 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1700 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1705 psi_memstall_leave(&pflags);
1709 * although background is preferred, no one is pending for submission.
1710 * don't issue decompression but drop it directly instead.
1712 if (!*force_fg && !nr_bios) {
1713 kvfree(q[JQ_SUBMIT]);
1716 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1719 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1720 bool force_fg, bool ra)
1722 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1724 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1726 z_erofs_submit_queue(f, io, &force_fg, ra);
1728 /* handle bypass queue (no i/o pclusters) immediately */
1729 z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1734 /* wait until all bios are completed */
1735 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1737 /* handle synchronous decompress queue in the caller context */
1738 z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
1742 * Since partial uptodate is still unimplemented for now, we have to use
1743 * approximate readmore strategies as a start.
1745 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1746 struct readahead_control *rac, bool backmost)
1748 struct inode *inode = f->inode;
1749 struct erofs_map_blocks *map = &f->map;
1750 erofs_off_t cur, end, headoffset = f->headoffset;
1755 end = headoffset + readahead_length(rac) - 1;
1757 end = headoffset + PAGE_SIZE - 1;
1759 err = z_erofs_map_blocks_iter(inode, map,
1760 EROFS_GET_BLOCKS_READMORE);
1764 /* expand ra for the trailing edge if readahead */
1766 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1767 readahead_expand(rac, headoffset, cur - headoffset);
1770 end = round_up(end, PAGE_SIZE);
1772 end = round_up(map->m_la, PAGE_SIZE);
1778 cur = map->m_la + map->m_llen - 1;
1779 while ((cur >= end) && (cur < i_size_read(inode))) {
1780 pgoff_t index = cur >> PAGE_SHIFT;
1783 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1785 if (PageUptodate(page))
1788 (void)z_erofs_do_read_page(f, page);
1792 if (cur < PAGE_SIZE)
1794 cur = (index << PAGE_SHIFT) - 1;
1798 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1800 struct inode *const inode = folio->mapping->host;
1801 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1802 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1805 trace_erofs_read_folio(folio, false);
1806 f.headoffset = (erofs_off_t)folio->index << PAGE_SHIFT;
1808 z_erofs_pcluster_readmore(&f, NULL, true);
1809 err = z_erofs_do_read_page(&f, &folio->page);
1810 z_erofs_pcluster_readmore(&f, NULL, false);
1811 z_erofs_pcluster_end(&f);
1813 /* if some compressed cluster ready, need submit them anyway */
1814 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
1816 if (err && err != -EINTR)
1817 erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1818 err, folio->index, EROFS_I(inode)->nid);
1820 erofs_put_metabuf(&f.map.buf);
1821 erofs_release_pages(&f.pagepool);
1825 static void z_erofs_readahead(struct readahead_control *rac)
1827 struct inode *const inode = rac->mapping->host;
1828 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1829 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1830 struct folio *head = NULL, *folio;
1831 unsigned int nr_folios;
1834 f.headoffset = readahead_pos(rac);
1836 z_erofs_pcluster_readmore(&f, rac, true);
1837 nr_folios = readahead_count(rac);
1838 trace_erofs_readpages(inode, readahead_index(rac), nr_folios, false);
1840 while ((folio = readahead_folio(rac))) {
1841 folio->private = head;
1845 /* traverse in reverse order for best metadata I/O performance */
1848 head = folio_get_private(folio);
1850 err = z_erofs_do_read_page(&f, &folio->page);
1851 if (err && err != -EINTR)
1852 erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
1853 folio->index, EROFS_I(inode)->nid);
1855 z_erofs_pcluster_readmore(&f, rac, false);
1856 z_erofs_pcluster_end(&f);
1858 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_folios), true);
1859 erofs_put_metabuf(&f.map.buf);
1860 erofs_release_pages(&f.pagepool);
1863 const struct address_space_operations z_erofs_aops = {
1864 .read_folio = z_erofs_read_folio,
1865 .readahead = z_erofs_readahead,