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
8 #include <linux/prefetch.h>
10 #include <linux/cpuhotplug.h>
11 #include <trace/events/erofs.h>
13 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
14 #define Z_EROFS_INLINE_BVECS 2
17 * let's leave a type here in case of introducing
18 * another tagged pointer later.
20 typedef void *z_erofs_next_pcluster_t;
28 #define __Z_EROFS_BVSET(name, total) \
30 /* point to the next page which contains the following bvecs */ \
31 struct page *nextpage; \
32 struct z_erofs_bvec bvec[total]; \
34 __Z_EROFS_BVSET(z_erofs_bvset,);
35 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
38 * Structure fields follow one of the following exclusion rules.
40 * I: Modifiable by initialization/destruction paths and read-only
43 * L: Field should be protected by the pcluster lock;
45 * A: Field should be accessed / updated in atomic for parallelized code.
47 struct z_erofs_pcluster {
48 struct erofs_workgroup obj;
51 /* A: point to next chained pcluster or TAILs */
52 z_erofs_next_pcluster_t next;
54 /* L: the maximum decompression size of this round */
57 /* L: total number of bvecs */
60 /* I: page offset of start position of decompression */
61 unsigned short pageofs_out;
63 /* I: page offset of inline compressed data */
64 unsigned short pageofs_in;
67 /* L: inline a certain number of bvec for bootstrap */
68 struct z_erofs_bvset_inline bvset;
70 /* I: can be used to free the pcluster by RCU. */
75 /* I: physical cluster size in pages */
76 unsigned short pclusterpages;
78 /* I: tailpacking inline compressed size */
79 unsigned short tailpacking_size;
82 /* I: compression algorithm format */
83 unsigned char algorithmformat;
85 /* L: whether partial decompression or not */
88 /* L: indicate several pageofs_outs or not */
91 /* A: compressed bvecs (can be cached or inplaced pages) */
92 struct z_erofs_bvec compressed_bvecs[];
95 /* let's avoid the valid 32-bit kernel addresses */
97 /* the chained workgroup has't submitted io (still open) */
98 #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
99 /* the chained workgroup has already submitted io */
100 #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
102 #define Z_EROFS_PCLUSTER_NIL (NULL)
104 struct z_erofs_decompressqueue {
105 struct super_block *sb;
106 atomic_t pending_bios;
107 z_erofs_next_pcluster_t head;
110 struct completion done;
111 struct work_struct work;
112 struct kthread_work kthread_work;
117 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
119 return !pcl->obj.index;
122 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
124 if (z_erofs_is_inline_pcluster(pcl))
126 return pcl->pclusterpages;
130 * bit 30: I/O error occurred on this page
131 * bit 0 - 29: remaining parts to complete this page
133 #define Z_EROFS_PAGE_EIO (1 << 30)
135 static inline void z_erofs_onlinepage_init(struct page *page)
140 } u = { .o = ATOMIC_INIT(1) };
142 set_page_private(page, u.v);
144 SetPagePrivate(page);
147 static inline void z_erofs_onlinepage_split(struct page *page)
149 atomic_inc((atomic_t *)&page->private);
152 static inline void z_erofs_page_mark_eio(struct page *page)
157 orig = atomic_read((atomic_t *)&page->private);
158 } while (atomic_cmpxchg((atomic_t *)&page->private, orig,
159 orig | Z_EROFS_PAGE_EIO) != orig);
162 static inline void z_erofs_onlinepage_endio(struct page *page)
166 DBG_BUGON(!PagePrivate(page));
167 v = atomic_dec_return((atomic_t *)&page->private);
168 if (!(v & ~Z_EROFS_PAGE_EIO)) {
169 set_page_private(page, 0);
170 ClearPagePrivate(page);
171 if (!(v & Z_EROFS_PAGE_EIO))
172 SetPageUptodate(page);
177 #define Z_EROFS_ONSTACK_PAGES 32
180 * since pclustersize is variable for big pcluster feature, introduce slab
181 * pools implementation for different pcluster sizes.
183 struct z_erofs_pcluster_slab {
184 struct kmem_cache *slab;
185 unsigned int maxpages;
189 #define _PCLP(n) { .maxpages = n }
191 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
192 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
193 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
196 struct z_erofs_bvec_iter {
198 struct z_erofs_bvset *bvset;
199 unsigned int nr, cur;
202 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
205 kunmap_local(iter->bvset);
209 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
211 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
212 /* have to access nextpage in advance, otherwise it will be unmapped */
213 struct page *nextpage = iter->bvset->nextpage;
214 struct page *oldpage;
216 DBG_BUGON(!nextpage);
217 oldpage = z_erofs_bvec_iter_end(iter);
218 iter->bvpage = nextpage;
219 iter->bvset = kmap_local_page(nextpage);
220 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
225 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
226 struct z_erofs_bvset_inline *bvset,
227 unsigned int bootstrap_nr,
230 *iter = (struct z_erofs_bvec_iter) {
232 .bvset = (struct z_erofs_bvset *)bvset,
235 while (cur > iter->nr) {
237 z_erofs_bvset_flip(iter);
242 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
243 struct z_erofs_bvec *bvec,
244 struct page **candidate_bvpage)
246 if (iter->cur == iter->nr) {
247 if (!*candidate_bvpage)
250 DBG_BUGON(iter->bvset->nextpage);
251 iter->bvset->nextpage = *candidate_bvpage;
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);
373 sched_set_normal(worker->task, 0);
377 static int erofs_init_percpu_workers(void)
379 struct kthread_worker *worker;
382 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
383 sizeof(struct kthread_worker *), GFP_ATOMIC);
384 if (!z_erofs_pcpu_workers)
387 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
388 worker = erofs_init_percpu_worker(cpu);
390 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
395 static inline void erofs_destroy_percpu_workers(void) {}
396 static inline int erofs_init_percpu_workers(void) { return 0; }
399 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
400 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
401 static enum cpuhp_state erofs_cpuhp_state;
403 static int erofs_cpu_online(unsigned int cpu)
405 struct kthread_worker *worker, *old;
407 worker = erofs_init_percpu_worker(cpu);
409 return PTR_ERR(worker);
411 spin_lock(&z_erofs_pcpu_worker_lock);
412 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
413 lockdep_is_held(&z_erofs_pcpu_worker_lock));
415 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
416 spin_unlock(&z_erofs_pcpu_worker_lock);
418 kthread_destroy_worker(worker);
422 static int erofs_cpu_offline(unsigned int cpu)
424 struct kthread_worker *worker;
426 spin_lock(&z_erofs_pcpu_worker_lock);
427 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
428 lockdep_is_held(&z_erofs_pcpu_worker_lock));
429 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
430 spin_unlock(&z_erofs_pcpu_worker_lock);
434 kthread_destroy_worker(worker);
438 static int erofs_cpu_hotplug_init(void)
442 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
443 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
447 erofs_cpuhp_state = state;
451 static void erofs_cpu_hotplug_destroy(void)
453 if (erofs_cpuhp_state)
454 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
456 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
457 static inline int erofs_cpu_hotplug_init(void) { return 0; }
458 static inline void erofs_cpu_hotplug_destroy(void) {}
461 void z_erofs_exit_zip_subsystem(void)
463 erofs_cpu_hotplug_destroy();
464 erofs_destroy_percpu_workers();
465 destroy_workqueue(z_erofs_workqueue);
466 z_erofs_destroy_pcluster_pool();
469 int __init z_erofs_init_zip_subsystem(void)
471 int err = z_erofs_create_pcluster_pool();
474 goto out_error_pcluster_pool;
476 z_erofs_workqueue = alloc_workqueue("erofs_worker",
477 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
478 if (!z_erofs_workqueue) {
480 goto out_error_workqueue_init;
483 err = erofs_init_percpu_workers();
485 goto out_error_pcpu_worker;
487 err = erofs_cpu_hotplug_init();
489 goto out_error_cpuhp_init;
492 out_error_cpuhp_init:
493 erofs_destroy_percpu_workers();
494 out_error_pcpu_worker:
495 destroy_workqueue(z_erofs_workqueue);
496 out_error_workqueue_init:
497 z_erofs_destroy_pcluster_pool();
498 out_error_pcluster_pool:
502 enum z_erofs_pclustermode {
503 Z_EROFS_PCLUSTER_INFLIGHT,
505 * The current pclusters was the tail of an exist chain, in addition
506 * that the previous processed chained pclusters are all decided to
507 * be hooked up to it.
508 * A new chain will be created for the remaining pclusters which are
509 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
510 * the next pcluster cannot reuse the whole page safely for inplace I/O
511 * in the following scenario:
512 * ________________________________________________________________
513 * | tail (partial) page | head (partial) page |
514 * | (belongs to the next pcl) | (belongs to the current pcl) |
515 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
517 Z_EROFS_PCLUSTER_HOOKED,
519 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
520 * could be dispatched into bypass queue later due to uptodated managed
521 * pages. All related online pages cannot be reused for inplace I/O (or
522 * bvpage) since it can be directly decoded without I/O submission.
524 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
526 * The current collection has been linked with the owned chain, and
527 * could also be linked with the remaining collections, which means
528 * if the processing page is the tail page of the collection, thus
529 * the current collection can safely use the whole page (since
530 * the previous collection is under control) for in-place I/O, as
532 * ________________________________________________________________
533 * | tail (partial) page | head (partial) page |
534 * | (of the current cl) | (of the previous collection) |
535 * | PCLUSTER_FOLLOWED or | |
536 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
538 * [ (*) the above page can be used as inplace I/O. ]
540 Z_EROFS_PCLUSTER_FOLLOWED,
543 struct z_erofs_decompress_frontend {
544 struct inode *const inode;
545 struct erofs_map_blocks map;
546 struct z_erofs_bvec_iter biter;
548 struct page *candidate_bvpage;
549 struct z_erofs_pcluster *pcl, *tailpcl;
550 z_erofs_next_pcluster_t owned_head;
551 enum z_erofs_pclustermode mode;
554 /* used for applying cache strategy on the fly */
556 erofs_off_t headoffset;
558 /* a pointer used to pick up inplace I/O pages */
562 #define DECOMPRESS_FRONTEND_INIT(__i) { \
563 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
564 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
566 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
568 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
570 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
576 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
577 fe->map.m_la < fe->headoffset)
583 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
584 struct page **pagepool)
586 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
587 struct z_erofs_pcluster *pcl = fe->pcl;
588 bool shouldalloc = z_erofs_should_alloc_cache(fe);
589 bool standalone = true;
591 * optimistic allocation without direct reclaim since inplace I/O
592 * can be used if low memory otherwise.
594 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
595 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
598 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
601 for (i = 0; i < pcl->pclusterpages; ++i) {
603 void *t; /* mark pages just found for debugging */
604 struct page *newpage = NULL;
606 /* the compressed page was loaded before */
607 if (READ_ONCE(pcl->compressed_bvecs[i].page))
610 page = find_get_page(mc, pcl->obj.index + i);
613 t = (void *)((unsigned long)page | 1);
615 /* I/O is needed, no possible to decompress directly */
621 * try to use cached I/O if page allocation
622 * succeeds or fallback to in-place I/O instead
623 * to avoid any direct reclaim.
625 newpage = erofs_allocpage(pagepool, gfp);
628 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
629 t = (void *)((unsigned long)newpage | 1);
632 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
638 erofs_pagepool_add(pagepool, newpage);
642 * don't do inplace I/O if all compressed pages are available in
643 * managed cache since it can be moved to the bypass queue instead.
646 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
649 /* called by erofs_shrinker to get rid of all compressed_pages */
650 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
651 struct erofs_workgroup *grp)
653 struct z_erofs_pcluster *const pcl =
654 container_of(grp, struct z_erofs_pcluster, obj);
657 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
659 * refcount of workgroup is now freezed as 1,
660 * therefore no need to worry about available decompression users.
662 for (i = 0; i < pcl->pclusterpages; ++i) {
663 struct page *page = pcl->compressed_bvecs[i].page;
668 /* block other users from reclaiming or migrating the page */
669 if (!trylock_page(page))
672 if (!erofs_page_is_managed(sbi, page))
675 /* barrier is implied in the following 'unlock_page' */
676 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
677 detach_page_private(page);
683 int erofs_try_to_free_cached_page(struct page *page)
685 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
688 if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
692 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
693 for (i = 0; i < pcl->pclusterpages; ++i) {
694 if (pcl->compressed_bvecs[i].page == page) {
695 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
700 erofs_workgroup_unfreeze(&pcl->obj, 1);
702 detach_page_private(page);
706 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
707 struct z_erofs_bvec *bvec)
709 struct z_erofs_pcluster *const pcl = fe->pcl;
711 while (fe->icur > 0) {
712 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
714 pcl->compressed_bvecs[fe->icur] = *bvec;
721 /* callers must be with pcluster lock held */
722 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
723 struct z_erofs_bvec *bvec, bool exclusive)
728 /* give priority for inplaceio to use file pages first */
729 if (z_erofs_try_inplace_io(fe, bvec))
731 /* otherwise, check if it can be used as a bvpage */
732 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
733 !fe->candidate_bvpage)
734 fe->candidate_bvpage = bvec->page;
736 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
737 fe->pcl->vcnt += (ret >= 0);
741 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
743 struct z_erofs_pcluster *pcl = f->pcl;
744 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
746 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
747 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
748 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
749 *owned_head = &pcl->next;
750 /* so we can attach this pcluster to our submission chain. */
751 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
756 * type 2, link to the end of an existing open chain, be careful
757 * that its submission is controlled by the original attached chain.
759 if (*owned_head != &pcl->next && pcl != f->tailpcl &&
760 cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
761 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
762 *owned_head = Z_EROFS_PCLUSTER_TAIL;
763 f->mode = Z_EROFS_PCLUSTER_HOOKED;
767 /* type 3, it belongs to a chain, but it isn't the end of the chain */
768 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
771 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
773 struct erofs_map_blocks *map = &fe->map;
774 bool ztailpacking = map->m_flags & EROFS_MAP_META;
775 struct z_erofs_pcluster *pcl;
776 struct erofs_workgroup *grp;
779 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
780 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
782 return -EFSCORRUPTED;
785 /* no available pcluster, let's allocate one */
786 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
787 map->m_plen >> PAGE_SHIFT);
791 atomic_set(&pcl->obj.refcount, 1);
792 pcl->algorithmformat = map->m_algorithmformat;
796 /* new pclusters should be claimed as type 1, primary and followed */
797 pcl->next = fe->owned_head;
798 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
799 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
802 * lock all primary followed works before visible to others
803 * and mutex_trylock *never* fails for a new pcluster.
805 mutex_init(&pcl->lock);
806 DBG_BUGON(!mutex_trylock(&pcl->lock));
809 pcl->obj.index = 0; /* which indicates ztailpacking */
810 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
811 pcl->tailpacking_size = map->m_plen;
813 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
815 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
821 if (grp != &pcl->obj) {
822 fe->pcl = container_of(grp,
823 struct z_erofs_pcluster, obj);
828 /* used to check tail merging loop due to corrupted images */
829 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
831 fe->owned_head = &pcl->next;
836 mutex_unlock(&pcl->lock);
837 z_erofs_free_pcluster(pcl);
841 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
843 struct erofs_map_blocks *map = &fe->map;
844 struct erofs_workgroup *grp = NULL;
849 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
850 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
851 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
853 if (!(map->m_flags & EROFS_MAP_META)) {
854 grp = erofs_find_workgroup(fe->inode->i_sb,
855 map->m_pa >> PAGE_SHIFT);
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 /* used to check tail merging loop due to corrupted images */
871 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
872 fe->tailpcl = fe->pcl;
874 z_erofs_try_to_claim_pcluster(fe);
878 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
879 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
880 /* since file-backed online pages are traversed in reverse order */
881 fe->icur = z_erofs_pclusterpages(fe->pcl);
886 * keep in mind that no referenced pclusters will be freed
887 * only after a RCU grace period.
889 static void z_erofs_rcu_callback(struct rcu_head *head)
891 z_erofs_free_pcluster(container_of(head,
892 struct z_erofs_pcluster, rcu));
895 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
897 struct z_erofs_pcluster *const pcl =
898 container_of(grp, struct z_erofs_pcluster, obj);
900 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
903 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
905 struct z_erofs_pcluster *pcl = fe->pcl;
910 z_erofs_bvec_iter_end(&fe->biter);
911 mutex_unlock(&pcl->lock);
913 if (fe->candidate_bvpage) {
914 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
915 fe->candidate_bvpage = NULL;
919 * if all pending pages are added, don't hold its reference
920 * any longer if the pcluster isn't hosted by ourselves.
922 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
923 erofs_workgroup_put(&pcl->obj);
929 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
930 struct page *page, unsigned int pageofs,
933 struct super_block *sb = inode->i_sb;
934 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
935 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
940 return -EFSCORRUPTED;
942 buf.inode = packed_inode;
943 pos += EROFS_I(inode)->z_fragmentoff;
944 for (i = 0; i < len; i += cnt) {
945 cnt = min_t(unsigned int, len - i,
946 sb->s_blocksize - erofs_blkoff(sb, pos));
947 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
949 erofs_put_metabuf(&buf);
953 dst = kmap_local_page(page);
954 memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
958 erofs_put_metabuf(&buf);
962 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
963 struct page *page, struct page **pagepool)
965 struct inode *const inode = fe->inode;
966 struct erofs_map_blocks *const map = &fe->map;
967 const loff_t offset = page_offset(page);
968 bool tight = true, exclusive;
969 unsigned int cur, end, spiltted;
972 /* register locked file pages as online pages in pack */
973 z_erofs_onlinepage_init(page);
980 if (offset + cur < map->m_la ||
981 offset + cur >= map->m_la + map->m_llen) {
982 if (z_erofs_collector_end(fe))
983 fe->backmost = false;
984 map->m_la = offset + cur;
986 err = z_erofs_map_blocks_iter(inode, map, 0);
992 /* didn't get a valid pcluster previously (very rare) */
995 if (!(map->m_flags & EROFS_MAP_MAPPED) ||
996 map->m_flags & EROFS_MAP_FRAGMENT)
999 err = z_erofs_collector_begin(fe);
1003 if (z_erofs_is_inline_pcluster(fe->pcl)) {
1006 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
1007 erofs_blknr(inode->i_sb, map->m_pa),
1011 erofs_err(inode->i_sb,
1012 "failed to get inline page, err %d", err);
1015 get_page(fe->map.buf.page);
1016 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1018 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1020 /* bind cache first when cached decompression is preferred */
1021 z_erofs_bind_cache(fe, pagepool);
1025 * Ensure the current partial page belongs to this submit chain rather
1026 * than other concurrent submit chains or the noio(bypass) chain since
1027 * those chains are handled asynchronously thus the page cannot be used
1028 * for inplace I/O or bvpage (should be processed in a strict order.)
1030 tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1031 fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1033 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
1034 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1035 zero_user_segment(page, cur, end);
1038 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1039 unsigned int pageofs, skip, len;
1041 if (offset > map->m_la) {
1043 skip = offset - map->m_la;
1045 pageofs = map->m_la & ~PAGE_MASK;
1048 len = min_t(unsigned int, map->m_llen - skip, end - cur);
1049 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1057 exclusive = (!cur && (!spiltted || tight));
1059 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1062 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1064 .offset = offset - map->m_la,
1067 /* should allocate an additional short-lived page for bvset */
1068 if (err == -EAGAIN && !fe->candidate_bvpage) {
1069 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
1070 set_page_private(fe->candidate_bvpage,
1071 Z_EROFS_SHORTLIVED_PAGE);
1076 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
1080 z_erofs_onlinepage_split(page);
1081 /* bump up the number of spiltted parts of a page */
1083 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1084 fe->pcl->multibases = true;
1085 if (fe->pcl->length < offset + end - map->m_la) {
1086 fe->pcl->length = offset + end - map->m_la;
1087 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1089 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1090 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1091 fe->pcl->length == map->m_llen)
1092 fe->pcl->partial = false;
1094 /* shorten the remaining extent to update progress */
1095 map->m_llen = offset + cur - map->m_la;
1096 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1104 z_erofs_page_mark_eio(page);
1105 z_erofs_onlinepage_endio(page);
1109 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
1110 unsigned int readahead_pages)
1112 /* auto: enable for read_folio, disable for readahead */
1113 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1117 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1118 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1124 static bool z_erofs_page_is_invalidated(struct page *page)
1126 return !page->mapping && !z_erofs_is_shortlived_page(page);
1129 struct z_erofs_decompress_backend {
1130 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1131 struct super_block *sb;
1132 struct z_erofs_pcluster *pcl;
1134 /* pages with the longest decompressed length for deduplication */
1135 struct page **decompressed_pages;
1136 /* pages to keep the compressed data */
1137 struct page **compressed_pages;
1139 struct list_head decompressed_secondary_bvecs;
1140 struct page **pagepool;
1141 unsigned int onstack_used, nr_pages;
1144 struct z_erofs_bvec_item {
1145 struct z_erofs_bvec bvec;
1146 struct list_head list;
1149 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1150 struct z_erofs_bvec *bvec)
1152 struct z_erofs_bvec_item *item;
1154 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1157 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1158 DBG_BUGON(pgnr >= be->nr_pages);
1159 if (!be->decompressed_pages[pgnr]) {
1160 be->decompressed_pages[pgnr] = bvec->page;
1165 /* (cold path) one pcluster is requested multiple times */
1166 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1168 list_add(&item->list, &be->decompressed_secondary_bvecs);
1171 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1174 unsigned int off0 = be->pcl->pageofs_out;
1175 struct list_head *p, *n;
1177 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1178 struct z_erofs_bvec_item *bvi;
1179 unsigned int end, cur;
1182 bvi = container_of(p, struct z_erofs_bvec_item, list);
1183 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1184 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1186 dst = kmap_local_page(bvi->bvec.page);
1188 unsigned int pgnr, scur, len;
1190 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1191 DBG_BUGON(pgnr >= be->nr_pages);
1193 scur = bvi->bvec.offset + cur -
1194 ((pgnr << PAGE_SHIFT) - off0);
1195 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1196 if (!be->decompressed_pages[pgnr]) {
1197 err = -EFSCORRUPTED;
1201 src = kmap_local_page(be->decompressed_pages[pgnr]);
1202 memcpy(dst + cur, src + scur, len);
1208 z_erofs_page_mark_eio(bvi->bvec.page);
1209 z_erofs_onlinepage_endio(bvi->bvec.page);
1215 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1217 struct z_erofs_pcluster *pcl = be->pcl;
1218 struct z_erofs_bvec_iter biter;
1219 struct page *old_bvpage;
1222 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1223 for (i = 0; i < pcl->vcnt; ++i) {
1224 struct z_erofs_bvec bvec;
1226 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1229 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1231 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1232 z_erofs_do_decompressed_bvec(be, &bvec);
1235 old_bvpage = z_erofs_bvec_iter_end(&biter);
1237 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1240 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1243 struct z_erofs_pcluster *pcl = be->pcl;
1244 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1247 *overlapped = false;
1248 for (i = 0; i < pclusterpages; ++i) {
1249 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1250 struct page *page = bvec->page;
1252 /* compressed pages ought to be present before decompressing */
1257 be->compressed_pages[i] = page;
1259 if (z_erofs_is_inline_pcluster(pcl)) {
1260 if (!PageUptodate(page))
1265 DBG_BUGON(z_erofs_page_is_invalidated(page));
1266 if (!z_erofs_is_shortlived_page(page)) {
1267 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1268 if (!PageUptodate(page))
1272 z_erofs_do_decompressed_bvec(be, bvec);
1282 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1285 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1286 struct z_erofs_pcluster *pcl = be->pcl;
1287 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1288 unsigned int i, inputsize;
1293 mutex_lock(&pcl->lock);
1294 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1296 /* allocate (de)compressed page arrays if cannot be kept on stack */
1297 be->decompressed_pages = NULL;
1298 be->compressed_pages = NULL;
1299 be->onstack_used = 0;
1300 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1301 be->decompressed_pages = be->onstack_pages;
1302 be->onstack_used = be->nr_pages;
1303 memset(be->decompressed_pages, 0,
1304 sizeof(struct page *) * be->nr_pages);
1307 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1308 be->compressed_pages = be->onstack_pages + be->onstack_used;
1310 if (!be->decompressed_pages)
1311 be->decompressed_pages =
1312 kvcalloc(be->nr_pages, sizeof(struct page *),
1313 GFP_KERNEL | __GFP_NOFAIL);
1314 if (!be->compressed_pages)
1315 be->compressed_pages =
1316 kvcalloc(pclusterpages, sizeof(struct page *),
1317 GFP_KERNEL | __GFP_NOFAIL);
1319 z_erofs_parse_out_bvecs(be);
1320 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1326 if (z_erofs_is_inline_pcluster(pcl))
1327 inputsize = pcl->tailpacking_size;
1329 inputsize = pclusterpages * PAGE_SIZE;
1331 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
1333 .in = be->compressed_pages,
1334 .out = be->decompressed_pages,
1335 .pageofs_in = pcl->pageofs_in,
1336 .pageofs_out = pcl->pageofs_out,
1337 .inputsize = inputsize,
1338 .outputsize = pcl->length,
1339 .alg = pcl->algorithmformat,
1340 .inplace_io = overlapped,
1341 .partial_decoding = pcl->partial,
1342 .fillgaps = pcl->multibases,
1346 /* must handle all compressed pages before actual file pages */
1347 if (z_erofs_is_inline_pcluster(pcl)) {
1348 page = pcl->compressed_bvecs[0].page;
1349 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1352 for (i = 0; i < pclusterpages; ++i) {
1353 page = pcl->compressed_bvecs[i].page;
1355 if (erofs_page_is_managed(sbi, page))
1358 /* recycle all individual short-lived pages */
1359 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1360 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1363 if (be->compressed_pages < be->onstack_pages ||
1364 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1365 kvfree(be->compressed_pages);
1366 z_erofs_fill_other_copies(be, err);
1368 for (i = 0; i < be->nr_pages; ++i) {
1369 page = be->decompressed_pages[i];
1373 DBG_BUGON(z_erofs_page_is_invalidated(page));
1375 /* recycle all individual short-lived pages */
1376 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1379 z_erofs_page_mark_eio(page);
1380 z_erofs_onlinepage_endio(page);
1383 if (be->decompressed_pages != be->onstack_pages)
1384 kvfree(be->decompressed_pages);
1387 pcl->partial = true;
1388 pcl->multibases = false;
1389 pcl->bvset.nextpage = NULL;
1392 /* pcluster lock MUST be taken before the following line */
1393 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1394 mutex_unlock(&pcl->lock);
1398 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1399 struct page **pagepool)
1401 struct z_erofs_decompress_backend be = {
1403 .pagepool = pagepool,
1404 .decompressed_secondary_bvecs =
1405 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1407 z_erofs_next_pcluster_t owned = io->head;
1409 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1410 /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1411 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1412 /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
1413 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1415 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1416 owned = READ_ONCE(be.pcl->next);
1418 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1419 erofs_workgroup_put(&be.pcl->obj);
1423 static void z_erofs_decompressqueue_work(struct work_struct *work)
1425 struct z_erofs_decompressqueue *bgq =
1426 container_of(work, struct z_erofs_decompressqueue, u.work);
1427 struct page *pagepool = NULL;
1429 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1430 z_erofs_decompress_queue(bgq, &pagepool);
1431 erofs_release_pages(&pagepool);
1435 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1436 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1438 z_erofs_decompressqueue_work((struct work_struct *)work);
1442 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1445 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1447 /* wake up the caller thread for sync decompression */
1449 if (!atomic_add_return(bios, &io->pending_bios))
1450 complete(&io->u.done);
1454 if (atomic_add_return(bios, &io->pending_bios))
1456 /* Use (kthread_)work and sync decompression for atomic contexts only */
1457 if (in_atomic() || irqs_disabled()) {
1458 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1459 struct kthread_worker *worker;
1462 worker = rcu_dereference(
1463 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1465 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1466 queue_work(z_erofs_workqueue, &io->u.work);
1468 kthread_queue_work(worker, &io->u.kthread_work);
1472 queue_work(z_erofs_workqueue, &io->u.work);
1474 /* enable sync decompression for readahead */
1475 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1476 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1479 z_erofs_decompressqueue_work(&io->u.work);
1482 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1484 struct page **pagepool,
1485 struct address_space *mc)
1487 const pgoff_t index = pcl->obj.index;
1488 gfp_t gfp = mapping_gfp_mask(mc);
1489 bool tocache = false;
1491 struct address_space *mapping;
1492 struct page *oldpage, *page;
1496 page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1502 justfound = (unsigned long)page & 1UL;
1503 page = (struct page *)((unsigned long)page & ~1UL);
1506 * preallocated cached pages, which is used to avoid direct reclaim
1507 * otherwise, it will go inplace I/O path instead.
1509 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1510 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1511 set_page_private(page, 0);
1515 mapping = READ_ONCE(page->mapping);
1518 * file-backed online pages in plcuster are all locked steady,
1519 * therefore it is impossible for `mapping' to be NULL.
1521 if (mapping && mapping != mc)
1522 /* ought to be unmanaged pages */
1525 /* directly return for shortlived page as well */
1526 if (z_erofs_is_shortlived_page(page))
1531 /* only true if page reclaim goes wrong, should never happen */
1532 DBG_BUGON(justfound && PagePrivate(page));
1534 /* the page is still in manage cache */
1535 if (page->mapping == mc) {
1536 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1538 if (!PagePrivate(page)) {
1540 * impossible to be !PagePrivate(page) for
1541 * the current restriction as well if
1542 * the page is already in compressed_bvecs[].
1544 DBG_BUGON(!justfound);
1547 set_page_private(page, (unsigned long)pcl);
1548 SetPagePrivate(page);
1551 /* no need to submit io if it is already up-to-date */
1552 if (PageUptodate(page)) {
1560 * the managed page has been truncated, it's unsafe to
1561 * reuse this one, let's allocate a new cache-managed page.
1563 DBG_BUGON(page->mapping);
1564 DBG_BUGON(!justfound);
1570 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1571 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1573 erofs_pagepool_add(pagepool, page);
1578 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1579 /* turn into temporary page if fails (1 ref) */
1580 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1583 attach_page_private(page, pcl);
1584 /* drop a refcount added by allocpage (then we have 2 refs here) */
1587 out: /* the only exit (for tracing and debugging) */
1591 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1592 struct z_erofs_decompressqueue *fgq, bool *fg)
1594 struct z_erofs_decompressqueue *q;
1597 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1602 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1603 kthread_init_work(&q->u.kthread_work,
1604 z_erofs_decompressqueue_kthread_work);
1606 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1611 init_completion(&fgq->u.done);
1612 atomic_set(&fgq->pending_bios, 0);
1617 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1621 /* define decompression jobqueue types */
1628 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1629 z_erofs_next_pcluster_t qtail[],
1630 z_erofs_next_pcluster_t owned_head)
1632 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1633 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1635 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1636 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1637 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1639 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1641 WRITE_ONCE(*submit_qtail, owned_head);
1642 WRITE_ONCE(*bypass_qtail, &pcl->next);
1644 qtail[JQ_BYPASS] = &pcl->next;
1647 static void z_erofs_decompressqueue_endio(struct bio *bio)
1649 struct z_erofs_decompressqueue *q = bio->bi_private;
1650 blk_status_t err = bio->bi_status;
1651 struct bio_vec *bvec;
1652 struct bvec_iter_all iter_all;
1654 bio_for_each_segment_all(bvec, bio, iter_all) {
1655 struct page *page = bvec->bv_page;
1657 DBG_BUGON(PageUptodate(page));
1658 DBG_BUGON(z_erofs_page_is_invalidated(page));
1660 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1662 SetPageUptodate(page);
1668 z_erofs_decompress_kickoff(q, -1);
1672 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1673 struct page **pagepool,
1674 struct z_erofs_decompressqueue *fgq,
1677 struct super_block *sb = f->inode->i_sb;
1678 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1679 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1680 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1681 z_erofs_next_pcluster_t owned_head = f->owned_head;
1682 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1684 struct block_device *last_bdev;
1685 unsigned int nr_bios = 0;
1686 struct bio *bio = NULL;
1687 unsigned long pflags;
1691 * if managed cache is enabled, bypass jobqueue is needed,
1692 * no need to read from device for all pclusters in this queue.
1694 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1695 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1697 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1698 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1700 /* by default, all need io submission */
1701 q[JQ_SUBMIT]->head = owned_head;
1704 struct erofs_map_dev mdev;
1705 struct z_erofs_pcluster *pcl;
1710 /* no possible 'owned_head' equals the following */
1711 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1712 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1714 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1716 /* close the main owned chain at first */
1717 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1718 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1719 if (z_erofs_is_inline_pcluster(pcl)) {
1720 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1724 /* no device id here, thus it will always succeed */
1725 mdev = (struct erofs_map_dev) {
1726 .m_pa = erofs_pos(sb, pcl->obj.index),
1728 (void)erofs_map_dev(sb, &mdev);
1730 cur = erofs_blknr(sb, mdev.m_pa);
1731 end = cur + pcl->pclusterpages;
1736 page = pickup_page_for_submission(pcl, i++, pagepool,
1741 if (bio && (cur != last_index + 1 ||
1742 last_bdev != mdev.m_bdev)) {
1746 psi_memstall_leave(&pflags);
1752 if (unlikely(PageWorkingset(page)) && !memstall) {
1753 psi_memstall_enter(&pflags);
1758 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1759 REQ_OP_READ, GFP_NOIO);
1760 bio->bi_end_io = z_erofs_decompressqueue_endio;
1762 last_bdev = mdev.m_bdev;
1763 bio->bi_iter.bi_sector = (sector_t)cur <<
1764 (sb->s_blocksize_bits - 9);
1765 bio->bi_private = q[JQ_SUBMIT];
1767 bio->bi_opf |= REQ_RAHEAD;
1771 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1772 goto submit_bio_retry;
1776 } while (++cur < end);
1779 qtail[JQ_SUBMIT] = &pcl->next;
1781 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1782 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1787 psi_memstall_leave(&pflags);
1791 * although background is preferred, no one is pending for submission.
1792 * don't issue decompression but drop it directly instead.
1794 if (!*force_fg && !nr_bios) {
1795 kvfree(q[JQ_SUBMIT]);
1798 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1801 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1802 struct page **pagepool, bool force_fg)
1804 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1806 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1808 z_erofs_submit_queue(f, pagepool, io, &force_fg);
1810 /* handle bypass queue (no i/o pclusters) immediately */
1811 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1816 /* wait until all bios are completed */
1817 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1819 /* handle synchronous decompress queue in the caller context */
1820 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1824 * Since partial uptodate is still unimplemented for now, we have to use
1825 * approximate readmore strategies as a start.
1827 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1828 struct readahead_control *rac,
1830 struct page **pagepool,
1833 struct inode *inode = f->inode;
1834 struct erofs_map_blocks *map = &f->map;
1840 err = z_erofs_map_blocks_iter(inode, map,
1841 EROFS_GET_BLOCKS_READMORE);
1845 /* expend ra for the trailing edge if readahead */
1847 loff_t newstart = readahead_pos(rac);
1849 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1850 readahead_expand(rac, newstart, cur - newstart);
1853 end = round_up(end, PAGE_SIZE);
1855 end = round_up(map->m_la, PAGE_SIZE);
1861 cur = map->m_la + map->m_llen - 1;
1862 while (cur >= end) {
1863 pgoff_t index = cur >> PAGE_SHIFT;
1866 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1868 if (PageUptodate(page)) {
1871 err = z_erofs_do_read_page(f, page, pagepool);
1873 erofs_err(inode->i_sb,
1874 "readmore error at page %lu @ nid %llu",
1875 index, EROFS_I(inode)->nid);
1880 if (cur < PAGE_SIZE)
1882 cur = (index << PAGE_SHIFT) - 1;
1886 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1888 struct page *page = &folio->page;
1889 struct inode *const inode = page->mapping->host;
1890 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1891 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1892 struct page *pagepool = NULL;
1895 trace_erofs_readpage(page, false);
1896 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1898 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1900 err = z_erofs_do_read_page(&f, page, &pagepool);
1901 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1903 (void)z_erofs_collector_end(&f);
1905 /* if some compressed cluster ready, need submit them anyway */
1906 z_erofs_runqueue(&f, &pagepool,
1907 z_erofs_get_sync_decompress_policy(sbi, 0));
1910 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1912 erofs_put_metabuf(&f.map.buf);
1913 erofs_release_pages(&pagepool);
1917 static void z_erofs_readahead(struct readahead_control *rac)
1919 struct inode *const inode = rac->mapping->host;
1920 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1921 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1922 struct page *pagepool = NULL, *head = NULL, *page;
1923 unsigned int nr_pages;
1926 f.headoffset = readahead_pos(rac);
1928 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1929 readahead_length(rac) - 1, &pagepool, true);
1930 nr_pages = readahead_count(rac);
1931 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1933 while ((page = readahead_page(rac))) {
1934 set_page_private(page, (unsigned long)head);
1939 struct page *page = head;
1942 /* traversal in reverse order */
1943 head = (void *)page_private(page);
1945 err = z_erofs_do_read_page(&f, page, &pagepool);
1947 erofs_err(inode->i_sb,
1948 "readahead error at page %lu @ nid %llu",
1949 page->index, EROFS_I(inode)->nid);
1952 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1953 (void)z_erofs_collector_end(&f);
1955 z_erofs_runqueue(&f, &pagepool,
1956 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1957 erofs_put_metabuf(&f.map.buf);
1958 erofs_release_pages(&pagepool);
1961 const struct address_space_operations z_erofs_aops = {
1962 .read_folio = z_erofs_read_folio,
1963 .readahead = z_erofs_readahead,