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 /* L: whether extra buffer allocations are best-effort */
88 /* A: compressed bvecs (can be cached or inplaced pages) */
89 struct z_erofs_bvec compressed_bvecs[];
92 /* the end of a chain of pclusters */
93 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
94 #define Z_EROFS_PCLUSTER_NIL (NULL)
96 struct z_erofs_decompressqueue {
97 struct super_block *sb;
98 atomic_t pending_bios;
99 z_erofs_next_pcluster_t head;
102 struct completion done;
103 struct work_struct work;
104 struct kthread_work kthread_work;
109 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
111 return !pcl->obj.index;
114 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
116 return PAGE_ALIGN(pcl->pclustersize) >> PAGE_SHIFT;
120 * bit 30: I/O error occurred on this page
121 * bit 0 - 29: remaining parts to complete this page
123 #define Z_EROFS_PAGE_EIO (1 << 30)
125 static inline void z_erofs_onlinepage_init(struct page *page)
130 } u = { .o = ATOMIC_INIT(1) };
132 set_page_private(page, u.v);
134 SetPagePrivate(page);
137 static inline void z_erofs_onlinepage_split(struct page *page)
139 atomic_inc((atomic_t *)&page->private);
142 static void z_erofs_onlinepage_endio(struct page *page, int err)
146 DBG_BUGON(!PagePrivate(page));
149 orig = atomic_read((atomic_t *)&page->private);
150 v = (orig - 1) | (err ? Z_EROFS_PAGE_EIO : 0);
151 } while (atomic_cmpxchg((atomic_t *)&page->private, orig, v) != orig);
153 if (!(v & ~Z_EROFS_PAGE_EIO)) {
154 set_page_private(page, 0);
155 ClearPagePrivate(page);
156 if (!(v & Z_EROFS_PAGE_EIO))
157 SetPageUptodate(page);
162 #define Z_EROFS_ONSTACK_PAGES 32
165 * since pclustersize is variable for big pcluster feature, introduce slab
166 * pools implementation for different pcluster sizes.
168 struct z_erofs_pcluster_slab {
169 struct kmem_cache *slab;
170 unsigned int maxpages;
174 #define _PCLP(n) { .maxpages = n }
176 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
177 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
178 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
181 struct z_erofs_bvec_iter {
183 struct z_erofs_bvset *bvset;
184 unsigned int nr, cur;
187 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
190 kunmap_local(iter->bvset);
194 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
196 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
197 /* have to access nextpage in advance, otherwise it will be unmapped */
198 struct page *nextpage = iter->bvset->nextpage;
199 struct page *oldpage;
201 DBG_BUGON(!nextpage);
202 oldpage = z_erofs_bvec_iter_end(iter);
203 iter->bvpage = nextpage;
204 iter->bvset = kmap_local_page(nextpage);
205 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
210 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
211 struct z_erofs_bvset_inline *bvset,
212 unsigned int bootstrap_nr,
215 *iter = (struct z_erofs_bvec_iter) {
217 .bvset = (struct z_erofs_bvset *)bvset,
220 while (cur > iter->nr) {
222 z_erofs_bvset_flip(iter);
227 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
228 struct z_erofs_bvec *bvec,
229 struct page **candidate_bvpage,
230 struct page **pagepool)
232 if (iter->cur >= iter->nr) {
233 struct page *nextpage = *candidate_bvpage;
236 nextpage = erofs_allocpage(pagepool, GFP_KERNEL);
239 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
241 DBG_BUGON(iter->bvset->nextpage);
242 iter->bvset->nextpage = nextpage;
243 z_erofs_bvset_flip(iter);
245 iter->bvset->nextpage = NULL;
246 *candidate_bvpage = NULL;
248 iter->bvset->bvec[iter->cur++] = *bvec;
252 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
253 struct z_erofs_bvec *bvec,
254 struct page **old_bvpage)
256 if (iter->cur == iter->nr)
257 *old_bvpage = z_erofs_bvset_flip(iter);
260 *bvec = iter->bvset->bvec[iter->cur++];
263 static void z_erofs_destroy_pcluster_pool(void)
267 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
268 if (!pcluster_pool[i].slab)
270 kmem_cache_destroy(pcluster_pool[i].slab);
271 pcluster_pool[i].slab = NULL;
275 static int z_erofs_create_pcluster_pool(void)
277 struct z_erofs_pcluster_slab *pcs;
278 struct z_erofs_pcluster *a;
281 for (pcs = pcluster_pool;
282 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
283 size = struct_size(a, compressed_bvecs, pcs->maxpages);
285 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
286 pcs->slab = kmem_cache_create(pcs->name, size, 0,
287 SLAB_RECLAIM_ACCOUNT, NULL);
291 z_erofs_destroy_pcluster_pool();
297 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
299 unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
300 struct z_erofs_pcluster_slab *pcs = pcluster_pool;
302 for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
303 struct z_erofs_pcluster *pcl;
305 if (nrpages > pcs->maxpages)
308 pcl = kmem_cache_zalloc(pcs->slab, GFP_KERNEL);
310 return ERR_PTR(-ENOMEM);
311 pcl->pclustersize = size;
314 return ERR_PTR(-EINVAL);
317 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
319 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
322 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
323 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
325 if (pclusterpages > pcs->maxpages)
328 kmem_cache_free(pcs->slab, pcl);
334 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
336 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
337 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
339 static void erofs_destroy_percpu_workers(void)
341 struct kthread_worker *worker;
344 for_each_possible_cpu(cpu) {
345 worker = rcu_dereference_protected(
346 z_erofs_pcpu_workers[cpu], 1);
347 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
349 kthread_destroy_worker(worker);
351 kfree(z_erofs_pcpu_workers);
354 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
356 struct kthread_worker *worker =
357 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
361 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
362 sched_set_fifo_low(worker->task);
366 static int erofs_init_percpu_workers(void)
368 struct kthread_worker *worker;
371 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
372 sizeof(struct kthread_worker *), GFP_ATOMIC);
373 if (!z_erofs_pcpu_workers)
376 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
377 worker = erofs_init_percpu_worker(cpu);
379 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
384 static inline void erofs_destroy_percpu_workers(void) {}
385 static inline int erofs_init_percpu_workers(void) { return 0; }
388 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
389 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
390 static enum cpuhp_state erofs_cpuhp_state;
392 static int erofs_cpu_online(unsigned int cpu)
394 struct kthread_worker *worker, *old;
396 worker = erofs_init_percpu_worker(cpu);
398 return PTR_ERR(worker);
400 spin_lock(&z_erofs_pcpu_worker_lock);
401 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
402 lockdep_is_held(&z_erofs_pcpu_worker_lock));
404 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
405 spin_unlock(&z_erofs_pcpu_worker_lock);
407 kthread_destroy_worker(worker);
411 static int erofs_cpu_offline(unsigned int cpu)
413 struct kthread_worker *worker;
415 spin_lock(&z_erofs_pcpu_worker_lock);
416 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
417 lockdep_is_held(&z_erofs_pcpu_worker_lock));
418 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
419 spin_unlock(&z_erofs_pcpu_worker_lock);
423 kthread_destroy_worker(worker);
427 static int erofs_cpu_hotplug_init(void)
431 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
432 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
436 erofs_cpuhp_state = state;
440 static void erofs_cpu_hotplug_destroy(void)
442 if (erofs_cpuhp_state)
443 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
445 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
446 static inline int erofs_cpu_hotplug_init(void) { return 0; }
447 static inline void erofs_cpu_hotplug_destroy(void) {}
450 void z_erofs_exit_zip_subsystem(void)
452 erofs_cpu_hotplug_destroy();
453 erofs_destroy_percpu_workers();
454 destroy_workqueue(z_erofs_workqueue);
455 z_erofs_destroy_pcluster_pool();
458 int __init z_erofs_init_zip_subsystem(void)
460 int err = z_erofs_create_pcluster_pool();
463 goto out_error_pcluster_pool;
465 z_erofs_workqueue = alloc_workqueue("erofs_worker",
466 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
467 if (!z_erofs_workqueue) {
469 goto out_error_workqueue_init;
472 err = erofs_init_percpu_workers();
474 goto out_error_pcpu_worker;
476 err = erofs_cpu_hotplug_init();
478 goto out_error_cpuhp_init;
481 out_error_cpuhp_init:
482 erofs_destroy_percpu_workers();
483 out_error_pcpu_worker:
484 destroy_workqueue(z_erofs_workqueue);
485 out_error_workqueue_init:
486 z_erofs_destroy_pcluster_pool();
487 out_error_pcluster_pool:
491 enum z_erofs_pclustermode {
492 Z_EROFS_PCLUSTER_INFLIGHT,
494 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
495 * could be dispatched into bypass queue later due to uptodated managed
496 * pages. All related online pages cannot be reused for inplace I/O (or
497 * bvpage) since it can be directly decoded without I/O submission.
499 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
501 * The pcluster was just linked to a decompression chain by us. It can
502 * also be linked with the remaining pclusters, which means if the
503 * processing page is the tail page of a pcluster, this pcluster can
504 * safely use the whole page (since the previous pcluster is within the
505 * same chain) for in-place I/O, as illustrated below:
506 * ___________________________________________________
507 * | tail (partial) page | head (partial) page |
508 * | (of the current pcl) | (of the previous pcl) |
509 * |___PCLUSTER_FOLLOWED___|_____PCLUSTER_FOLLOWED_____|
511 * [ (*) the page above can be used as inplace I/O. ]
513 Z_EROFS_PCLUSTER_FOLLOWED,
516 struct z_erofs_decompress_frontend {
517 struct inode *const inode;
518 struct erofs_map_blocks map;
519 struct z_erofs_bvec_iter biter;
521 struct page *pagepool;
522 struct page *candidate_bvpage;
523 struct z_erofs_pcluster *pcl;
524 z_erofs_next_pcluster_t owned_head;
525 enum z_erofs_pclustermode mode;
527 erofs_off_t headoffset;
529 /* a pointer used to pick up inplace I/O pages */
533 #define DECOMPRESS_FRONTEND_INIT(__i) { \
534 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
535 .mode = Z_EROFS_PCLUSTER_FOLLOWED }
537 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
539 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
541 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
544 if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
547 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
548 fe->map.m_la < fe->headoffset)
554 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
556 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
557 struct z_erofs_pcluster *pcl = fe->pcl;
558 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
559 bool shouldalloc = z_erofs_should_alloc_cache(fe);
560 bool standalone = true;
562 * optimistic allocation without direct reclaim since inplace I/O
563 * can be used if low memory otherwise.
565 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
566 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
569 if (i_blocksize(fe->inode) != PAGE_SIZE ||
570 fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
573 for (i = 0; i < pclusterpages; ++i) {
574 struct page *page, *newpage;
575 void *t; /* mark pages just found for debugging */
577 /* Inaccurate check w/o locking to avoid unneeded lookups */
578 if (READ_ONCE(pcl->compressed_bvecs[i].page))
581 page = find_get_page(mc, pcl->obj.index + i);
583 t = (void *)((unsigned long)page | 1);
586 /* I/O is needed, no possible to decompress directly */
592 * Try cached I/O if allocation succeeds or fallback to
593 * in-place I/O instead to avoid any direct reclaim.
595 newpage = erofs_allocpage(&fe->pagepool, gfp);
598 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
599 t = (void *)((unsigned long)newpage | 1);
601 spin_lock(&pcl->obj.lockref.lock);
602 if (!pcl->compressed_bvecs[i].page) {
603 pcl->compressed_bvecs[i].page = t;
604 spin_unlock(&pcl->obj.lockref.lock);
607 spin_unlock(&pcl->obj.lockref.lock);
612 erofs_pagepool_add(&fe->pagepool, newpage);
616 * don't do inplace I/O if all compressed pages are available in
617 * managed cache since it can be moved to the bypass queue instead.
620 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
623 /* called by erofs_shrinker to get rid of all compressed_pages */
624 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
625 struct erofs_workgroup *grp)
627 struct z_erofs_pcluster *const pcl =
628 container_of(grp, struct z_erofs_pcluster, obj);
629 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
632 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
634 * refcount of workgroup is now freezed as 0,
635 * therefore no need to worry about available decompression users.
637 for (i = 0; i < pclusterpages; ++i) {
638 struct page *page = pcl->compressed_bvecs[i].page;
643 /* block other users from reclaiming or migrating the page */
644 if (!trylock_page(page))
647 if (!erofs_page_is_managed(sbi, page))
650 /* barrier is implied in the following 'unlock_page' */
651 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
652 detach_page_private(page);
658 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
660 struct z_erofs_pcluster *pcl = folio_get_private(folio);
661 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
665 if (!folio_test_private(folio))
669 spin_lock(&pcl->obj.lockref.lock);
670 if (pcl->obj.lockref.count > 0)
673 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
674 for (i = 0; i < pclusterpages; ++i) {
675 if (pcl->compressed_bvecs[i].page == &folio->page) {
676 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
682 folio_detach_private(folio);
684 spin_unlock(&pcl->obj.lockref.lock);
689 * It will be called only on inode eviction. In case that there are still some
690 * decompression requests in progress, wait with rescheduling for a bit here.
691 * An extra lock could be introduced instead but it seems unnecessary.
693 static void z_erofs_cache_invalidate_folio(struct folio *folio,
694 size_t offset, size_t length)
696 const size_t stop = length + offset;
698 /* Check for potential overflow in debug mode */
699 DBG_BUGON(stop > folio_size(folio) || stop < length);
701 if (offset == 0 && stop == folio_size(folio))
702 while (!z_erofs_cache_release_folio(folio, 0))
706 static const struct address_space_operations z_erofs_cache_aops = {
707 .release_folio = z_erofs_cache_release_folio,
708 .invalidate_folio = z_erofs_cache_invalidate_folio,
711 int erofs_init_managed_cache(struct super_block *sb)
713 struct inode *const inode = new_inode(sb);
719 inode->i_size = OFFSET_MAX;
720 inode->i_mapping->a_ops = &z_erofs_cache_aops;
721 mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL);
722 EROFS_SB(sb)->managed_cache = inode;
726 /* callers must be with pcluster lock held */
727 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
728 struct z_erofs_bvec *bvec, bool exclusive)
730 struct z_erofs_pcluster *pcl = fe->pcl;
734 /* give priority for inplaceio to use file pages first */
735 spin_lock(&pcl->obj.lockref.lock);
736 while (fe->icur > 0) {
737 if (pcl->compressed_bvecs[--fe->icur].page)
739 pcl->compressed_bvecs[fe->icur] = *bvec;
740 spin_unlock(&pcl->obj.lockref.lock);
743 spin_unlock(&pcl->obj.lockref.lock);
745 /* otherwise, check if it can be used as a bvpage */
746 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
747 !fe->candidate_bvpage)
748 fe->candidate_bvpage = bvec->page;
750 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
752 fe->pcl->vcnt += (ret >= 0);
756 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
758 struct z_erofs_pcluster *pcl = f->pcl;
759 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
761 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
762 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
763 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
764 *owned_head = &pcl->next;
765 /* so we can attach this pcluster to our submission chain. */
766 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
770 /* type 2, it belongs to an ongoing chain */
771 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
774 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
776 struct erofs_map_blocks *map = &fe->map;
777 struct super_block *sb = fe->inode->i_sb;
778 bool ztailpacking = map->m_flags & EROFS_MAP_META;
779 struct z_erofs_pcluster *pcl;
780 struct erofs_workgroup *grp;
783 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
784 (!ztailpacking && !erofs_blknr(sb, map->m_pa))) {
786 return -EFSCORRUPTED;
789 /* no available pcluster, let's allocate one */
790 pcl = z_erofs_alloc_pcluster(map->m_plen);
794 spin_lock_init(&pcl->obj.lockref.lock);
795 pcl->obj.lockref.count = 1; /* one ref for this request */
796 pcl->algorithmformat = map->m_algorithmformat;
800 /* new pclusters should be claimed as type 1, primary and followed */
801 pcl->next = fe->owned_head;
802 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
803 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
806 * lock all primary followed works before visible to others
807 * and mutex_trylock *never* fails for a new pcluster.
809 mutex_init(&pcl->lock);
810 DBG_BUGON(!mutex_trylock(&pcl->lock));
813 pcl->obj.index = 0; /* which indicates ztailpacking */
815 pcl->obj.index = erofs_blknr(sb, map->m_pa);
817 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
823 if (grp != &pcl->obj) {
824 fe->pcl = container_of(grp,
825 struct z_erofs_pcluster, obj);
830 fe->owned_head = &pcl->next;
835 mutex_unlock(&pcl->lock);
836 z_erofs_free_pcluster(pcl);
840 static int z_erofs_pcluster_begin(struct z_erofs_decompress_frontend *fe)
842 struct erofs_map_blocks *map = &fe->map;
843 struct super_block *sb = fe->inode->i_sb;
844 erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
845 struct erofs_workgroup *grp = NULL;
850 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
851 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
853 if (!(map->m_flags & EROFS_MAP_META)) {
854 grp = erofs_find_workgroup(sb, blknr);
855 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
857 return -EFSCORRUPTED;
861 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
864 ret = z_erofs_register_pcluster(fe);
867 if (ret == -EEXIST) {
868 mutex_lock(&fe->pcl->lock);
869 z_erofs_try_to_claim_pcluster(fe);
874 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
875 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
876 if (!z_erofs_is_inline_pcluster(fe->pcl)) {
877 /* bind cache first when cached decompression is preferred */
878 z_erofs_bind_cache(fe);
882 mptr = erofs_read_metabuf(&map->buf, sb, blknr, EROFS_NO_KMAP);
885 erofs_err(sb, "failed to get inline data %d", ret);
888 get_page(map->buf.page);
889 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
890 fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
891 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
893 /* file-backed inplace I/O pages are traversed in reverse order */
894 fe->icur = z_erofs_pclusterpages(fe->pcl);
899 * keep in mind that no referenced pclusters will be freed
900 * only after a RCU grace period.
902 static void z_erofs_rcu_callback(struct rcu_head *head)
904 z_erofs_free_pcluster(container_of(head,
905 struct z_erofs_pcluster, rcu));
908 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
910 struct z_erofs_pcluster *const pcl =
911 container_of(grp, struct z_erofs_pcluster, obj);
913 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
916 static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
918 struct z_erofs_pcluster *pcl = fe->pcl;
923 z_erofs_bvec_iter_end(&fe->biter);
924 mutex_unlock(&pcl->lock);
926 if (fe->candidate_bvpage)
927 fe->candidate_bvpage = NULL;
930 * if all pending pages are added, don't hold its reference
931 * any longer if the pcluster isn't hosted by ourselves.
933 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
934 erofs_workgroup_put(&pcl->obj);
939 static int z_erofs_read_fragment(struct super_block *sb, struct page *page,
940 unsigned int cur, unsigned int end, erofs_off_t pos)
942 struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
943 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
948 return -EFSCORRUPTED;
950 buf.inode = packed_inode;
951 for (; cur < end; cur += cnt, pos += cnt) {
952 cnt = min_t(unsigned int, end - cur,
953 sb->s_blocksize - erofs_blkoff(sb, pos));
954 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
956 erofs_put_metabuf(&buf);
959 memcpy_to_page(page, cur, src + erofs_blkoff(sb, pos), cnt);
961 erofs_put_metabuf(&buf);
965 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
966 struct page *page, bool ra)
968 struct inode *const inode = fe->inode;
969 struct erofs_map_blocks *const map = &fe->map;
970 const loff_t offset = page_offset(page);
971 const unsigned int bs = i_blocksize(inode);
972 bool tight = true, exclusive;
973 unsigned int cur, end, len, split;
976 z_erofs_onlinepage_init(page);
980 if (offset + end - 1 < map->m_la ||
981 offset + end - 1 >= map->m_la + map->m_llen) {
982 z_erofs_pcluster_end(fe);
983 map->m_la = offset + end - 1;
985 err = z_erofs_map_blocks_iter(inode, map, 0);
990 cur = offset > map->m_la ? 0 : map->m_la - offset;
991 /* bump split parts first to avoid several separate cases */
994 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
995 zero_user_segment(page, cur, end);
1000 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1001 erofs_off_t fpos = offset + cur - map->m_la;
1003 len = min_t(unsigned int, map->m_llen - fpos, end - cur);
1004 err = z_erofs_read_fragment(inode->i_sb, page, cur, cur + len,
1005 EROFS_I(inode)->z_fragmentoff + fpos);
1013 err = z_erofs_pcluster_begin(fe);
1016 fe->pcl->besteffort |= !ra;
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,
1283 .gfp = pcl->besteffort ?
1284 GFP_KERNEL | __GFP_NOFAIL :
1285 GFP_NOWAIT | __GFP_NORETRY
1288 /* must handle all compressed pages before actual file pages */
1289 if (z_erofs_is_inline_pcluster(pcl)) {
1290 page = pcl->compressed_bvecs[0].page;
1291 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1294 for (i = 0; i < pclusterpages; ++i) {
1295 /* consider shortlived pages added when decompressing */
1296 page = be->compressed_pages[i];
1298 if (!page || erofs_page_is_managed(sbi, page))
1300 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1301 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1304 if (be->compressed_pages < be->onstack_pages ||
1305 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1306 kvfree(be->compressed_pages);
1307 z_erofs_fill_other_copies(be, err);
1309 for (i = 0; i < be->nr_pages; ++i) {
1310 page = be->decompressed_pages[i];
1314 DBG_BUGON(z_erofs_page_is_invalidated(page));
1316 /* recycle all individual short-lived pages */
1317 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1319 z_erofs_onlinepage_endio(page, err);
1322 if (be->decompressed_pages != be->onstack_pages)
1323 kvfree(be->decompressed_pages);
1326 pcl->partial = true;
1327 pcl->multibases = false;
1328 pcl->besteffort = false;
1329 pcl->bvset.nextpage = NULL;
1332 /* pcluster lock MUST be taken before the following line */
1333 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1334 mutex_unlock(&pcl->lock);
1338 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1339 struct page **pagepool)
1341 struct z_erofs_decompress_backend be = {
1343 .pagepool = pagepool,
1344 .decompressed_secondary_bvecs =
1345 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1347 z_erofs_next_pcluster_t owned = io->head;
1349 while (owned != Z_EROFS_PCLUSTER_TAIL) {
1350 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1352 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1353 owned = READ_ONCE(be.pcl->next);
1355 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1356 if (z_erofs_is_inline_pcluster(be.pcl))
1357 z_erofs_free_pcluster(be.pcl);
1359 erofs_workgroup_put(&be.pcl->obj);
1363 static void z_erofs_decompressqueue_work(struct work_struct *work)
1365 struct z_erofs_decompressqueue *bgq =
1366 container_of(work, struct z_erofs_decompressqueue, u.work);
1367 struct page *pagepool = NULL;
1369 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1370 z_erofs_decompress_queue(bgq, &pagepool);
1371 erofs_release_pages(&pagepool);
1375 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1376 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1378 z_erofs_decompressqueue_work((struct work_struct *)work);
1382 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1385 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1387 /* wake up the caller thread for sync decompression */
1389 if (!atomic_add_return(bios, &io->pending_bios))
1390 complete(&io->u.done);
1394 if (atomic_add_return(bios, &io->pending_bios))
1396 /* Use (kthread_)work and sync decompression for atomic contexts only */
1397 if (!in_task() || irqs_disabled() || rcu_read_lock_any_held()) {
1398 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1399 struct kthread_worker *worker;
1402 worker = rcu_dereference(
1403 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1405 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1406 queue_work(z_erofs_workqueue, &io->u.work);
1408 kthread_queue_work(worker, &io->u.kthread_work);
1412 queue_work(z_erofs_workqueue, &io->u.work);
1414 /* enable sync decompression for readahead */
1415 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1416 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1419 z_erofs_decompressqueue_work(&io->u.work);
1422 static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
1423 struct z_erofs_decompress_frontend *f,
1424 struct z_erofs_pcluster *pcl,
1426 struct address_space *mc)
1428 gfp_t gfp = mapping_gfp_mask(mc);
1429 bool tocache = false;
1430 struct z_erofs_bvec zbv;
1431 struct address_space *mapping;
1433 int justfound, bs = i_blocksize(f->inode);
1435 /* Except for inplace pages, the entire page can be used for I/Os */
1436 bvec->bv_offset = 0;
1437 bvec->bv_len = PAGE_SIZE;
1439 spin_lock(&pcl->obj.lockref.lock);
1440 zbv = pcl->compressed_bvecs[nr];
1442 justfound = (unsigned long)page & 1UL;
1443 page = (struct page *)((unsigned long)page & ~1UL);
1444 pcl->compressed_bvecs[nr].page = page;
1445 spin_unlock(&pcl->obj.lockref.lock);
1449 bvec->bv_page = page;
1450 DBG_BUGON(z_erofs_is_shortlived_page(page));
1452 * Handle preallocated cached pages. We tried to allocate such pages
1453 * without triggering direct reclaim. If allocation failed, inplace
1454 * file-backed pages will be used instead.
1456 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1457 set_page_private(page, 0);
1462 mapping = READ_ONCE(page->mapping);
1464 * File-backed pages for inplace I/Os are all locked steady,
1465 * therefore it is impossible for `mapping` to be NULL.
1467 if (mapping && mapping != mc) {
1469 bvec->bv_offset = round_up(-zbv.offset, bs);
1470 bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset;
1475 /* only true if page reclaim goes wrong, should never happen */
1476 DBG_BUGON(justfound && PagePrivate(page));
1478 /* the cached page is still in managed cache */
1479 if (page->mapping == mc) {
1481 * The cached page is still available but without a valid
1482 * `->private` pcluster hint. Let's reconnect them.
1484 if (!PagePrivate(page)) {
1485 DBG_BUGON(!justfound);
1486 /* compressed_bvecs[] already takes a ref */
1487 attach_page_private(page, pcl);
1491 /* no need to submit if it is already up-to-date */
1492 if (PageUptodate(page)) {
1494 bvec->bv_page = NULL;
1500 * It has been truncated, so it's unsafe to reuse this one. Let's
1501 * allocate a new page for compressed data.
1503 DBG_BUGON(page->mapping);
1504 DBG_BUGON(!justfound);
1510 page = erofs_allocpage(&f->pagepool, gfp | __GFP_NOFAIL);
1511 spin_lock(&pcl->obj.lockref.lock);
1512 if (pcl->compressed_bvecs[nr].page) {
1513 erofs_pagepool_add(&f->pagepool, page);
1514 spin_unlock(&pcl->obj.lockref.lock);
1518 pcl->compressed_bvecs[nr].page = page;
1519 spin_unlock(&pcl->obj.lockref.lock);
1520 bvec->bv_page = page;
1522 if (!tocache || bs != PAGE_SIZE ||
1523 add_to_page_cache_lru(page, mc, pcl->obj.index + nr, gfp)) {
1524 /* turn into a temporary shortlived page (1 ref) */
1525 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1528 attach_page_private(page, pcl);
1529 /* drop a refcount added by allocpage (then 2 refs in total here) */
1533 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1534 struct z_erofs_decompressqueue *fgq, bool *fg)
1536 struct z_erofs_decompressqueue *q;
1539 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1544 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1545 kthread_init_work(&q->u.kthread_work,
1546 z_erofs_decompressqueue_kthread_work);
1548 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1553 init_completion(&fgq->u.done);
1554 atomic_set(&fgq->pending_bios, 0);
1559 q->head = Z_EROFS_PCLUSTER_TAIL;
1563 /* define decompression jobqueue types */
1570 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1571 z_erofs_next_pcluster_t qtail[],
1572 z_erofs_next_pcluster_t owned_head)
1574 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1575 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1577 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1579 WRITE_ONCE(*submit_qtail, owned_head);
1580 WRITE_ONCE(*bypass_qtail, &pcl->next);
1582 qtail[JQ_BYPASS] = &pcl->next;
1585 static void z_erofs_submissionqueue_endio(struct bio *bio)
1587 struct z_erofs_decompressqueue *q = bio->bi_private;
1588 blk_status_t err = bio->bi_status;
1589 struct bio_vec *bvec;
1590 struct bvec_iter_all iter_all;
1592 bio_for_each_segment_all(bvec, bio, iter_all) {
1593 struct page *page = bvec->bv_page;
1595 DBG_BUGON(PageUptodate(page));
1596 DBG_BUGON(z_erofs_page_is_invalidated(page));
1597 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1599 SetPageUptodate(page);
1605 z_erofs_decompress_kickoff(q, -1);
1609 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1610 struct z_erofs_decompressqueue *fgq,
1611 bool *force_fg, bool readahead)
1613 struct super_block *sb = f->inode->i_sb;
1614 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1615 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1616 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1617 z_erofs_next_pcluster_t owned_head = f->owned_head;
1618 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1619 erofs_off_t last_pa;
1620 struct block_device *last_bdev;
1621 unsigned int nr_bios = 0;
1622 struct bio *bio = NULL;
1623 unsigned long pflags;
1626 /* No need to read from device for pclusters in the bypass queue. */
1627 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1628 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1630 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1631 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1633 /* by default, all need io submission */
1634 q[JQ_SUBMIT]->head = owned_head;
1637 struct erofs_map_dev mdev;
1638 struct z_erofs_pcluster *pcl;
1639 erofs_off_t cur, end;
1640 struct bio_vec bvec;
1644 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1645 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1646 owned_head = READ_ONCE(pcl->next);
1648 if (z_erofs_is_inline_pcluster(pcl)) {
1649 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1653 /* no device id here, thus it will always succeed */
1654 mdev = (struct erofs_map_dev) {
1655 .m_pa = erofs_pos(sb, pcl->obj.index),
1657 (void)erofs_map_dev(sb, &mdev);
1660 end = cur + pcl->pclustersize;
1662 z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
1666 if (bio && (cur != last_pa ||
1667 last_bdev != mdev.m_bdev)) {
1671 psi_memstall_leave(&pflags);
1677 if (unlikely(PageWorkingset(bvec.bv_page)) &&
1679 psi_memstall_enter(&pflags);
1684 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1685 REQ_OP_READ, GFP_NOIO);
1686 bio->bi_end_io = z_erofs_submissionqueue_endio;
1687 bio->bi_iter.bi_sector = cur >> 9;
1688 bio->bi_private = q[JQ_SUBMIT];
1690 bio->bi_opf |= REQ_RAHEAD;
1692 last_bdev = mdev.m_bdev;
1695 if (cur + bvec.bv_len > end)
1696 bvec.bv_len = end - cur;
1697 DBG_BUGON(bvec.bv_len < sb->s_blocksize);
1698 if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
1700 goto submit_bio_retry;
1702 last_pa = cur + bvec.bv_len;
1704 } while ((cur += bvec.bv_len) < end);
1707 qtail[JQ_SUBMIT] = &pcl->next;
1709 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1710 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1715 psi_memstall_leave(&pflags);
1719 * although background is preferred, no one is pending for submission.
1720 * don't issue decompression but drop it directly instead.
1722 if (!*force_fg && !nr_bios) {
1723 kvfree(q[JQ_SUBMIT]);
1726 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1729 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1730 bool force_fg, bool ra)
1732 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1734 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1736 z_erofs_submit_queue(f, io, &force_fg, ra);
1738 /* handle bypass queue (no i/o pclusters) immediately */
1739 z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1744 /* wait until all bios are completed */
1745 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1747 /* handle synchronous decompress queue in the caller context */
1748 z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
1752 * Since partial uptodate is still unimplemented for now, we have to use
1753 * approximate readmore strategies as a start.
1755 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1756 struct readahead_control *rac, bool backmost)
1758 struct inode *inode = f->inode;
1759 struct erofs_map_blocks *map = &f->map;
1760 erofs_off_t cur, end, headoffset = f->headoffset;
1765 end = headoffset + readahead_length(rac) - 1;
1767 end = headoffset + PAGE_SIZE - 1;
1769 err = z_erofs_map_blocks_iter(inode, map,
1770 EROFS_GET_BLOCKS_READMORE);
1774 /* expand ra for the trailing edge if readahead */
1776 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1777 readahead_expand(rac, headoffset, cur - headoffset);
1780 end = round_up(end, PAGE_SIZE);
1782 end = round_up(map->m_la, PAGE_SIZE);
1788 cur = map->m_la + map->m_llen - 1;
1789 while ((cur >= end) && (cur < i_size_read(inode))) {
1790 pgoff_t index = cur >> PAGE_SHIFT;
1793 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1795 if (PageUptodate(page))
1798 (void)z_erofs_do_read_page(f, page, !!rac);
1802 if (cur < PAGE_SIZE)
1804 cur = (index << PAGE_SHIFT) - 1;
1808 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1810 struct inode *const inode = folio->mapping->host;
1811 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1812 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1815 trace_erofs_read_folio(folio, false);
1816 f.headoffset = (erofs_off_t)folio->index << PAGE_SHIFT;
1818 z_erofs_pcluster_readmore(&f, NULL, true);
1819 err = z_erofs_do_read_page(&f, &folio->page, false);
1820 z_erofs_pcluster_readmore(&f, NULL, false);
1821 z_erofs_pcluster_end(&f);
1823 /* if some compressed cluster ready, need submit them anyway */
1824 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
1826 if (err && err != -EINTR)
1827 erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1828 err, folio->index, EROFS_I(inode)->nid);
1830 erofs_put_metabuf(&f.map.buf);
1831 erofs_release_pages(&f.pagepool);
1835 static void z_erofs_readahead(struct readahead_control *rac)
1837 struct inode *const inode = rac->mapping->host;
1838 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1839 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1840 struct folio *head = NULL, *folio;
1841 unsigned int nr_folios;
1844 f.headoffset = readahead_pos(rac);
1846 z_erofs_pcluster_readmore(&f, rac, true);
1847 nr_folios = readahead_count(rac);
1848 trace_erofs_readpages(inode, readahead_index(rac), nr_folios, false);
1850 while ((folio = readahead_folio(rac))) {
1851 folio->private = head;
1855 /* traverse in reverse order for best metadata I/O performance */
1858 head = folio_get_private(folio);
1860 err = z_erofs_do_read_page(&f, &folio->page, true);
1861 if (err && err != -EINTR)
1862 erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
1863 folio->index, EROFS_I(inode)->nid);
1865 z_erofs_pcluster_readmore(&f, rac, false);
1866 z_erofs_pcluster_end(&f);
1868 z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_folios), true);
1869 erofs_put_metabuf(&f.map.buf);
1870 erofs_release_pages(&f.pagepool);
1873 const struct address_space_operations z_erofs_aops = {
1874 .read_folio = z_erofs_read_folio,
1875 .readahead = z_erofs_readahead,