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);
375 static int erofs_init_percpu_workers(void)
377 struct kthread_worker *worker;
380 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
381 sizeof(struct kthread_worker *), GFP_ATOMIC);
382 if (!z_erofs_pcpu_workers)
385 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
386 worker = erofs_init_percpu_worker(cpu);
388 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
393 static inline void erofs_destroy_percpu_workers(void) {}
394 static inline int erofs_init_percpu_workers(void) { return 0; }
397 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
398 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
399 static enum cpuhp_state erofs_cpuhp_state;
401 static int erofs_cpu_online(unsigned int cpu)
403 struct kthread_worker *worker, *old;
405 worker = erofs_init_percpu_worker(cpu);
407 return PTR_ERR(worker);
409 spin_lock(&z_erofs_pcpu_worker_lock);
410 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
411 lockdep_is_held(&z_erofs_pcpu_worker_lock));
413 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
414 spin_unlock(&z_erofs_pcpu_worker_lock);
416 kthread_destroy_worker(worker);
420 static int erofs_cpu_offline(unsigned int cpu)
422 struct kthread_worker *worker;
424 spin_lock(&z_erofs_pcpu_worker_lock);
425 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
426 lockdep_is_held(&z_erofs_pcpu_worker_lock));
427 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
428 spin_unlock(&z_erofs_pcpu_worker_lock);
432 kthread_destroy_worker(worker);
436 static int erofs_cpu_hotplug_init(void)
440 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
441 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
445 erofs_cpuhp_state = state;
449 static void erofs_cpu_hotplug_destroy(void)
451 if (erofs_cpuhp_state)
452 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
454 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
455 static inline int erofs_cpu_hotplug_init(void) { return 0; }
456 static inline void erofs_cpu_hotplug_destroy(void) {}
459 void z_erofs_exit_zip_subsystem(void)
461 erofs_cpu_hotplug_destroy();
462 erofs_destroy_percpu_workers();
463 destroy_workqueue(z_erofs_workqueue);
464 z_erofs_destroy_pcluster_pool();
467 int __init z_erofs_init_zip_subsystem(void)
469 int err = z_erofs_create_pcluster_pool();
472 goto out_error_pcluster_pool;
474 z_erofs_workqueue = alloc_workqueue("erofs_worker",
475 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
476 if (!z_erofs_workqueue) {
478 goto out_error_workqueue_init;
481 err = erofs_init_percpu_workers();
483 goto out_error_pcpu_worker;
485 err = erofs_cpu_hotplug_init();
487 goto out_error_cpuhp_init;
490 out_error_cpuhp_init:
491 erofs_destroy_percpu_workers();
492 out_error_pcpu_worker:
493 destroy_workqueue(z_erofs_workqueue);
494 out_error_workqueue_init:
495 z_erofs_destroy_pcluster_pool();
496 out_error_pcluster_pool:
500 enum z_erofs_pclustermode {
501 Z_EROFS_PCLUSTER_INFLIGHT,
503 * The current pclusters was the tail of an exist chain, in addition
504 * that the previous processed chained pclusters are all decided to
505 * be hooked up to it.
506 * A new chain will be created for the remaining pclusters which are
507 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
508 * the next pcluster cannot reuse the whole page safely for inplace I/O
509 * in the following scenario:
510 * ________________________________________________________________
511 * | tail (partial) page | head (partial) page |
512 * | (belongs to the next pcl) | (belongs to the current pcl) |
513 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
515 Z_EROFS_PCLUSTER_HOOKED,
517 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
518 * could be dispatched into bypass queue later due to uptodated managed
519 * pages. All related online pages cannot be reused for inplace I/O (or
520 * bvpage) since it can be directly decoded without I/O submission.
522 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
524 * The current collection has been linked with the owned chain, and
525 * could also be linked with the remaining collections, which means
526 * if the processing page is the tail page of the collection, thus
527 * the current collection can safely use the whole page (since
528 * the previous collection is under control) for in-place I/O, as
530 * ________________________________________________________________
531 * | tail (partial) page | head (partial) page |
532 * | (of the current cl) | (of the previous collection) |
533 * | PCLUSTER_FOLLOWED or | |
534 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
536 * [ (*) the above page can be used as inplace I/O. ]
538 Z_EROFS_PCLUSTER_FOLLOWED,
541 struct z_erofs_decompress_frontend {
542 struct inode *const inode;
543 struct erofs_map_blocks map;
544 struct z_erofs_bvec_iter biter;
546 struct page *candidate_bvpage;
547 struct z_erofs_pcluster *pcl, *tailpcl;
548 z_erofs_next_pcluster_t owned_head;
549 enum z_erofs_pclustermode mode;
552 /* used for applying cache strategy on the fly */
554 erofs_off_t headoffset;
556 /* a pointer used to pick up inplace I/O pages */
560 #define DECOMPRESS_FRONTEND_INIT(__i) { \
561 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
562 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
564 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
566 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
568 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
574 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
575 fe->map.m_la < fe->headoffset)
581 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
582 struct page **pagepool)
584 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
585 struct z_erofs_pcluster *pcl = fe->pcl;
586 bool shouldalloc = z_erofs_should_alloc_cache(fe);
587 bool standalone = true;
589 * optimistic allocation without direct reclaim since inplace I/O
590 * can be used if low memory otherwise.
592 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
593 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
596 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
599 for (i = 0; i < pcl->pclusterpages; ++i) {
601 void *t; /* mark pages just found for debugging */
602 struct page *newpage = NULL;
604 /* the compressed page was loaded before */
605 if (READ_ONCE(pcl->compressed_bvecs[i].page))
608 page = find_get_page(mc, pcl->obj.index + i);
611 t = (void *)((unsigned long)page | 1);
613 /* I/O is needed, no possible to decompress directly */
619 * try to use cached I/O if page allocation
620 * succeeds or fallback to in-place I/O instead
621 * to avoid any direct reclaim.
623 newpage = erofs_allocpage(pagepool, gfp);
626 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
627 t = (void *)((unsigned long)newpage | 1);
630 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
636 erofs_pagepool_add(pagepool, newpage);
640 * don't do inplace I/O if all compressed pages are available in
641 * managed cache since it can be moved to the bypass queue instead.
644 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
647 /* called by erofs_shrinker to get rid of all compressed_pages */
648 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
649 struct erofs_workgroup *grp)
651 struct z_erofs_pcluster *const pcl =
652 container_of(grp, struct z_erofs_pcluster, obj);
655 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
657 * refcount of workgroup is now freezed as 1,
658 * therefore no need to worry about available decompression users.
660 for (i = 0; i < pcl->pclusterpages; ++i) {
661 struct page *page = pcl->compressed_bvecs[i].page;
666 /* block other users from reclaiming or migrating the page */
667 if (!trylock_page(page))
670 if (!erofs_page_is_managed(sbi, page))
673 /* barrier is implied in the following 'unlock_page' */
674 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
675 detach_page_private(page);
681 int erofs_try_to_free_cached_page(struct page *page)
683 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
686 if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
690 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
691 for (i = 0; i < pcl->pclusterpages; ++i) {
692 if (pcl->compressed_bvecs[i].page == page) {
693 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
698 erofs_workgroup_unfreeze(&pcl->obj, 1);
700 detach_page_private(page);
704 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
705 struct z_erofs_bvec *bvec)
707 struct z_erofs_pcluster *const pcl = fe->pcl;
709 while (fe->icur > 0) {
710 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
712 pcl->compressed_bvecs[fe->icur] = *bvec;
719 /* callers must be with pcluster lock held */
720 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
721 struct z_erofs_bvec *bvec, bool exclusive)
726 /* give priority for inplaceio to use file pages first */
727 if (z_erofs_try_inplace_io(fe, bvec))
729 /* otherwise, check if it can be used as a bvpage */
730 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
731 !fe->candidate_bvpage)
732 fe->candidate_bvpage = bvec->page;
734 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
735 fe->pcl->vcnt += (ret >= 0);
739 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
741 struct z_erofs_pcluster *pcl = f->pcl;
742 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
744 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
745 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
746 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
747 *owned_head = &pcl->next;
748 /* so we can attach this pcluster to our submission chain. */
749 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
754 * type 2, link to the end of an existing open chain, be careful
755 * that its submission is controlled by the original attached chain.
757 if (*owned_head != &pcl->next && pcl != f->tailpcl &&
758 cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
759 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
760 *owned_head = Z_EROFS_PCLUSTER_TAIL;
761 f->mode = Z_EROFS_PCLUSTER_HOOKED;
765 /* type 3, it belongs to a chain, but it isn't the end of the chain */
766 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
769 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
771 struct erofs_map_blocks *map = &fe->map;
772 bool ztailpacking = map->m_flags & EROFS_MAP_META;
773 struct z_erofs_pcluster *pcl;
774 struct erofs_workgroup *grp;
777 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
778 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
780 return -EFSCORRUPTED;
783 /* no available pcluster, let's allocate one */
784 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
785 map->m_plen >> PAGE_SHIFT);
789 atomic_set(&pcl->obj.refcount, 1);
790 pcl->algorithmformat = map->m_algorithmformat;
794 /* new pclusters should be claimed as type 1, primary and followed */
795 pcl->next = fe->owned_head;
796 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
797 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
800 * lock all primary followed works before visible to others
801 * and mutex_trylock *never* fails for a new pcluster.
803 mutex_init(&pcl->lock);
804 DBG_BUGON(!mutex_trylock(&pcl->lock));
807 pcl->obj.index = 0; /* which indicates ztailpacking */
808 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
809 pcl->tailpacking_size = map->m_plen;
811 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
813 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
819 if (grp != &pcl->obj) {
820 fe->pcl = container_of(grp,
821 struct z_erofs_pcluster, obj);
826 /* used to check tail merging loop due to corrupted images */
827 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
829 fe->owned_head = &pcl->next;
834 mutex_unlock(&pcl->lock);
835 z_erofs_free_pcluster(pcl);
839 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
841 struct erofs_map_blocks *map = &fe->map;
842 struct erofs_workgroup *grp = NULL;
847 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
848 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
849 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
851 if (!(map->m_flags & EROFS_MAP_META)) {
852 grp = erofs_find_workgroup(fe->inode->i_sb,
853 map->m_pa >> PAGE_SHIFT);
854 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
856 return -EFSCORRUPTED;
860 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
863 ret = z_erofs_register_pcluster(fe);
866 if (ret == -EEXIST) {
867 mutex_lock(&fe->pcl->lock);
868 /* used to check tail merging loop due to corrupted images */
869 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
870 fe->tailpcl = fe->pcl;
872 z_erofs_try_to_claim_pcluster(fe);
876 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
877 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
878 /* since file-backed online pages are traversed in reverse order */
879 fe->icur = z_erofs_pclusterpages(fe->pcl);
884 * keep in mind that no referenced pclusters will be freed
885 * only after a RCU grace period.
887 static void z_erofs_rcu_callback(struct rcu_head *head)
889 z_erofs_free_pcluster(container_of(head,
890 struct z_erofs_pcluster, rcu));
893 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
895 struct z_erofs_pcluster *const pcl =
896 container_of(grp, struct z_erofs_pcluster, obj);
898 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
901 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
903 struct z_erofs_pcluster *pcl = fe->pcl;
908 z_erofs_bvec_iter_end(&fe->biter);
909 mutex_unlock(&pcl->lock);
911 if (fe->candidate_bvpage) {
912 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
913 fe->candidate_bvpage = NULL;
917 * if all pending pages are added, don't hold its reference
918 * any longer if the pcluster isn't hosted by ourselves.
920 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
921 erofs_workgroup_put(&pcl->obj);
927 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
928 struct page *page, unsigned int pageofs,
931 struct super_block *sb = inode->i_sb;
932 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
933 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
938 return -EFSCORRUPTED;
940 buf.inode = packed_inode;
941 pos += EROFS_I(inode)->z_fragmentoff;
942 for (i = 0; i < len; i += cnt) {
943 cnt = min_t(unsigned int, len - i,
944 sb->s_blocksize - erofs_blkoff(sb, pos));
945 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
947 erofs_put_metabuf(&buf);
951 dst = kmap_local_page(page);
952 memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
956 erofs_put_metabuf(&buf);
960 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
961 struct page *page, struct page **pagepool)
963 struct inode *const inode = fe->inode;
964 struct erofs_map_blocks *const map = &fe->map;
965 const loff_t offset = page_offset(page);
966 bool tight = true, exclusive;
967 unsigned int cur, end, spiltted;
970 /* register locked file pages as online pages in pack */
971 z_erofs_onlinepage_init(page);
978 if (offset + cur < map->m_la ||
979 offset + cur >= map->m_la + map->m_llen) {
980 if (z_erofs_collector_end(fe))
981 fe->backmost = false;
982 map->m_la = offset + cur;
984 err = z_erofs_map_blocks_iter(inode, map, 0);
990 /* didn't get a valid pcluster previously (very rare) */
993 if (!(map->m_flags & EROFS_MAP_MAPPED) ||
994 map->m_flags & EROFS_MAP_FRAGMENT)
997 err = z_erofs_collector_begin(fe);
1001 if (z_erofs_is_inline_pcluster(fe->pcl)) {
1004 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
1005 erofs_blknr(inode->i_sb, map->m_pa),
1009 erofs_err(inode->i_sb,
1010 "failed to get inline page, err %d", err);
1013 get_page(fe->map.buf.page);
1014 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1016 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1018 /* bind cache first when cached decompression is preferred */
1019 z_erofs_bind_cache(fe, pagepool);
1023 * Ensure the current partial page belongs to this submit chain rather
1024 * than other concurrent submit chains or the noio(bypass) chain since
1025 * those chains are handled asynchronously thus the page cannot be used
1026 * for inplace I/O or bvpage (should be processed in a strict order.)
1028 tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1029 fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1031 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
1032 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1033 zero_user_segment(page, cur, end);
1036 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1037 unsigned int pageofs, skip, len;
1039 if (offset > map->m_la) {
1041 skip = offset - map->m_la;
1043 pageofs = map->m_la & ~PAGE_MASK;
1046 len = min_t(unsigned int, map->m_llen - skip, end - cur);
1047 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1055 exclusive = (!cur && (!spiltted || tight));
1057 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1060 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1062 .offset = offset - map->m_la,
1065 /* should allocate an additional short-lived page for bvset */
1066 if (err == -EAGAIN && !fe->candidate_bvpage) {
1067 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
1068 set_page_private(fe->candidate_bvpage,
1069 Z_EROFS_SHORTLIVED_PAGE);
1074 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
1078 z_erofs_onlinepage_split(page);
1079 /* bump up the number of spiltted parts of a page */
1081 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1082 fe->pcl->multibases = true;
1083 if (fe->pcl->length < offset + end - map->m_la) {
1084 fe->pcl->length = offset + end - map->m_la;
1085 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1087 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1088 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1089 fe->pcl->length == map->m_llen)
1090 fe->pcl->partial = false;
1092 /* shorten the remaining extent to update progress */
1093 map->m_llen = offset + cur - map->m_la;
1094 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1102 z_erofs_page_mark_eio(page);
1103 z_erofs_onlinepage_endio(page);
1107 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
1108 unsigned int readahead_pages)
1110 /* auto: enable for read_folio, disable for readahead */
1111 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1115 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1116 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1122 static bool z_erofs_page_is_invalidated(struct page *page)
1124 return !page->mapping && !z_erofs_is_shortlived_page(page);
1127 struct z_erofs_decompress_backend {
1128 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1129 struct super_block *sb;
1130 struct z_erofs_pcluster *pcl;
1132 /* pages with the longest decompressed length for deduplication */
1133 struct page **decompressed_pages;
1134 /* pages to keep the compressed data */
1135 struct page **compressed_pages;
1137 struct list_head decompressed_secondary_bvecs;
1138 struct page **pagepool;
1139 unsigned int onstack_used, nr_pages;
1142 struct z_erofs_bvec_item {
1143 struct z_erofs_bvec bvec;
1144 struct list_head list;
1147 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1148 struct z_erofs_bvec *bvec)
1150 struct z_erofs_bvec_item *item;
1152 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1155 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1156 DBG_BUGON(pgnr >= be->nr_pages);
1157 if (!be->decompressed_pages[pgnr]) {
1158 be->decompressed_pages[pgnr] = bvec->page;
1163 /* (cold path) one pcluster is requested multiple times */
1164 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1166 list_add(&item->list, &be->decompressed_secondary_bvecs);
1169 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1172 unsigned int off0 = be->pcl->pageofs_out;
1173 struct list_head *p, *n;
1175 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1176 struct z_erofs_bvec_item *bvi;
1177 unsigned int end, cur;
1180 bvi = container_of(p, struct z_erofs_bvec_item, list);
1181 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1182 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1184 dst = kmap_local_page(bvi->bvec.page);
1186 unsigned int pgnr, scur, len;
1188 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1189 DBG_BUGON(pgnr >= be->nr_pages);
1191 scur = bvi->bvec.offset + cur -
1192 ((pgnr << PAGE_SHIFT) - off0);
1193 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1194 if (!be->decompressed_pages[pgnr]) {
1195 err = -EFSCORRUPTED;
1199 src = kmap_local_page(be->decompressed_pages[pgnr]);
1200 memcpy(dst + cur, src + scur, len);
1206 z_erofs_page_mark_eio(bvi->bvec.page);
1207 z_erofs_onlinepage_endio(bvi->bvec.page);
1213 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1215 struct z_erofs_pcluster *pcl = be->pcl;
1216 struct z_erofs_bvec_iter biter;
1217 struct page *old_bvpage;
1220 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1221 for (i = 0; i < pcl->vcnt; ++i) {
1222 struct z_erofs_bvec bvec;
1224 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1227 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1229 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1230 z_erofs_do_decompressed_bvec(be, &bvec);
1233 old_bvpage = z_erofs_bvec_iter_end(&biter);
1235 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1238 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1241 struct z_erofs_pcluster *pcl = be->pcl;
1242 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1245 *overlapped = false;
1246 for (i = 0; i < pclusterpages; ++i) {
1247 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1248 struct page *page = bvec->page;
1250 /* compressed pages ought to be present before decompressing */
1255 be->compressed_pages[i] = page;
1257 if (z_erofs_is_inline_pcluster(pcl)) {
1258 if (!PageUptodate(page))
1263 DBG_BUGON(z_erofs_page_is_invalidated(page));
1264 if (!z_erofs_is_shortlived_page(page)) {
1265 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1266 if (!PageUptodate(page))
1270 z_erofs_do_decompressed_bvec(be, bvec);
1280 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1283 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1284 struct z_erofs_pcluster *pcl = be->pcl;
1285 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1286 unsigned int i, inputsize;
1291 mutex_lock(&pcl->lock);
1292 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1294 /* allocate (de)compressed page arrays if cannot be kept on stack */
1295 be->decompressed_pages = NULL;
1296 be->compressed_pages = NULL;
1297 be->onstack_used = 0;
1298 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1299 be->decompressed_pages = be->onstack_pages;
1300 be->onstack_used = be->nr_pages;
1301 memset(be->decompressed_pages, 0,
1302 sizeof(struct page *) * be->nr_pages);
1305 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1306 be->compressed_pages = be->onstack_pages + be->onstack_used;
1308 if (!be->decompressed_pages)
1309 be->decompressed_pages =
1310 kvcalloc(be->nr_pages, sizeof(struct page *),
1311 GFP_KERNEL | __GFP_NOFAIL);
1312 if (!be->compressed_pages)
1313 be->compressed_pages =
1314 kvcalloc(pclusterpages, sizeof(struct page *),
1315 GFP_KERNEL | __GFP_NOFAIL);
1317 z_erofs_parse_out_bvecs(be);
1318 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1324 if (z_erofs_is_inline_pcluster(pcl))
1325 inputsize = pcl->tailpacking_size;
1327 inputsize = pclusterpages * PAGE_SIZE;
1329 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
1331 .in = be->compressed_pages,
1332 .out = be->decompressed_pages,
1333 .pageofs_in = pcl->pageofs_in,
1334 .pageofs_out = pcl->pageofs_out,
1335 .inputsize = inputsize,
1336 .outputsize = pcl->length,
1337 .alg = pcl->algorithmformat,
1338 .inplace_io = overlapped,
1339 .partial_decoding = pcl->partial,
1340 .fillgaps = pcl->multibases,
1344 /* must handle all compressed pages before actual file pages */
1345 if (z_erofs_is_inline_pcluster(pcl)) {
1346 page = pcl->compressed_bvecs[0].page;
1347 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1350 for (i = 0; i < pclusterpages; ++i) {
1351 page = pcl->compressed_bvecs[i].page;
1353 if (erofs_page_is_managed(sbi, page))
1356 /* recycle all individual short-lived pages */
1357 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1358 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1361 if (be->compressed_pages < be->onstack_pages ||
1362 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1363 kvfree(be->compressed_pages);
1364 z_erofs_fill_other_copies(be, err);
1366 for (i = 0; i < be->nr_pages; ++i) {
1367 page = be->decompressed_pages[i];
1371 DBG_BUGON(z_erofs_page_is_invalidated(page));
1373 /* recycle all individual short-lived pages */
1374 if (z_erofs_put_shortlivedpage(be->pagepool, page))
1377 z_erofs_page_mark_eio(page);
1378 z_erofs_onlinepage_endio(page);
1381 if (be->decompressed_pages != be->onstack_pages)
1382 kvfree(be->decompressed_pages);
1385 pcl->partial = true;
1386 pcl->multibases = false;
1387 pcl->bvset.nextpage = NULL;
1390 /* pcluster lock MUST be taken before the following line */
1391 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1392 mutex_unlock(&pcl->lock);
1396 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1397 struct page **pagepool)
1399 struct z_erofs_decompress_backend be = {
1401 .pagepool = pagepool,
1402 .decompressed_secondary_bvecs =
1403 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1405 z_erofs_next_pcluster_t owned = io->head;
1407 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1408 /* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1409 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1410 /* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
1411 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1413 be.pcl = container_of(owned, struct z_erofs_pcluster, next);
1414 owned = READ_ONCE(be.pcl->next);
1416 z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
1417 erofs_workgroup_put(&be.pcl->obj);
1421 static void z_erofs_decompressqueue_work(struct work_struct *work)
1423 struct z_erofs_decompressqueue *bgq =
1424 container_of(work, struct z_erofs_decompressqueue, u.work);
1425 struct page *pagepool = NULL;
1427 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1428 z_erofs_decompress_queue(bgq, &pagepool);
1429 erofs_release_pages(&pagepool);
1433 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1434 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1436 z_erofs_decompressqueue_work((struct work_struct *)work);
1440 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1443 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1445 /* wake up the caller thread for sync decompression */
1447 if (!atomic_add_return(bios, &io->pending_bios))
1448 complete(&io->u.done);
1452 if (atomic_add_return(bios, &io->pending_bios))
1454 /* Use (kthread_)work and sync decompression for atomic contexts only */
1455 if (in_atomic() || irqs_disabled()) {
1456 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1457 struct kthread_worker *worker;
1460 worker = rcu_dereference(
1461 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1463 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1464 queue_work(z_erofs_workqueue, &io->u.work);
1466 kthread_queue_work(worker, &io->u.kthread_work);
1470 queue_work(z_erofs_workqueue, &io->u.work);
1472 /* enable sync decompression for readahead */
1473 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1474 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1477 z_erofs_decompressqueue_work(&io->u.work);
1480 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1482 struct page **pagepool,
1483 struct address_space *mc)
1485 const pgoff_t index = pcl->obj.index;
1486 gfp_t gfp = mapping_gfp_mask(mc);
1487 bool tocache = false;
1489 struct address_space *mapping;
1490 struct page *oldpage, *page;
1494 page = READ_ONCE(pcl->compressed_bvecs[nr].page);
1500 justfound = (unsigned long)page & 1UL;
1501 page = (struct page *)((unsigned long)page & ~1UL);
1504 * preallocated cached pages, which is used to avoid direct reclaim
1505 * otherwise, it will go inplace I/O path instead.
1507 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1508 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1509 set_page_private(page, 0);
1513 mapping = READ_ONCE(page->mapping);
1516 * file-backed online pages in plcuster are all locked steady,
1517 * therefore it is impossible for `mapping' to be NULL.
1519 if (mapping && mapping != mc)
1520 /* ought to be unmanaged pages */
1523 /* directly return for shortlived page as well */
1524 if (z_erofs_is_shortlived_page(page))
1529 /* only true if page reclaim goes wrong, should never happen */
1530 DBG_BUGON(justfound && PagePrivate(page));
1532 /* the page is still in manage cache */
1533 if (page->mapping == mc) {
1534 WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
1536 if (!PagePrivate(page)) {
1538 * impossible to be !PagePrivate(page) for
1539 * the current restriction as well if
1540 * the page is already in compressed_bvecs[].
1542 DBG_BUGON(!justfound);
1545 set_page_private(page, (unsigned long)pcl);
1546 SetPagePrivate(page);
1549 /* no need to submit io if it is already up-to-date */
1550 if (PageUptodate(page)) {
1558 * the managed page has been truncated, it's unsafe to
1559 * reuse this one, let's allocate a new cache-managed page.
1561 DBG_BUGON(page->mapping);
1562 DBG_BUGON(!justfound);
1568 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1569 if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1571 erofs_pagepool_add(pagepool, page);
1576 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1577 /* turn into temporary page if fails (1 ref) */
1578 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1581 attach_page_private(page, pcl);
1582 /* drop a refcount added by allocpage (then we have 2 refs here) */
1585 out: /* the only exit (for tracing and debugging) */
1589 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1590 struct z_erofs_decompressqueue *fgq, bool *fg)
1592 struct z_erofs_decompressqueue *q;
1595 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1600 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1601 kthread_init_work(&q->u.kthread_work,
1602 z_erofs_decompressqueue_kthread_work);
1604 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1609 init_completion(&fgq->u.done);
1610 atomic_set(&fgq->pending_bios, 0);
1615 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1619 /* define decompression jobqueue types */
1626 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1627 z_erofs_next_pcluster_t qtail[],
1628 z_erofs_next_pcluster_t owned_head)
1630 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1631 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1633 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1634 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1635 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1637 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1639 WRITE_ONCE(*submit_qtail, owned_head);
1640 WRITE_ONCE(*bypass_qtail, &pcl->next);
1642 qtail[JQ_BYPASS] = &pcl->next;
1645 static void z_erofs_decompressqueue_endio(struct bio *bio)
1647 struct z_erofs_decompressqueue *q = bio->bi_private;
1648 blk_status_t err = bio->bi_status;
1649 struct bio_vec *bvec;
1650 struct bvec_iter_all iter_all;
1652 bio_for_each_segment_all(bvec, bio, iter_all) {
1653 struct page *page = bvec->bv_page;
1655 DBG_BUGON(PageUptodate(page));
1656 DBG_BUGON(z_erofs_page_is_invalidated(page));
1658 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1660 SetPageUptodate(page);
1666 z_erofs_decompress_kickoff(q, -1);
1670 static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1671 struct page **pagepool,
1672 struct z_erofs_decompressqueue *fgq,
1675 struct super_block *sb = f->inode->i_sb;
1676 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1677 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1678 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1679 z_erofs_next_pcluster_t owned_head = f->owned_head;
1680 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1682 struct block_device *last_bdev;
1683 unsigned int nr_bios = 0;
1684 struct bio *bio = NULL;
1685 unsigned long pflags;
1689 * if managed cache is enabled, bypass jobqueue is needed,
1690 * no need to read from device for all pclusters in this queue.
1692 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1693 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1695 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1696 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1698 /* by default, all need io submission */
1699 q[JQ_SUBMIT]->head = owned_head;
1702 struct erofs_map_dev mdev;
1703 struct z_erofs_pcluster *pcl;
1708 /* no possible 'owned_head' equals the following */
1709 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1710 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1712 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1714 /* close the main owned chain at first */
1715 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1716 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1717 if (z_erofs_is_inline_pcluster(pcl)) {
1718 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1722 /* no device id here, thus it will always succeed */
1723 mdev = (struct erofs_map_dev) {
1724 .m_pa = erofs_pos(sb, pcl->obj.index),
1726 (void)erofs_map_dev(sb, &mdev);
1728 cur = erofs_blknr(sb, mdev.m_pa);
1729 end = cur + pcl->pclusterpages;
1734 page = pickup_page_for_submission(pcl, i++, pagepool,
1739 if (bio && (cur != last_index + 1 ||
1740 last_bdev != mdev.m_bdev)) {
1744 psi_memstall_leave(&pflags);
1750 if (unlikely(PageWorkingset(page)) && !memstall) {
1751 psi_memstall_enter(&pflags);
1756 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1757 REQ_OP_READ, GFP_NOIO);
1758 bio->bi_end_io = z_erofs_decompressqueue_endio;
1760 last_bdev = mdev.m_bdev;
1761 bio->bi_iter.bi_sector = (sector_t)cur <<
1762 (sb->s_blocksize_bits - 9);
1763 bio->bi_private = q[JQ_SUBMIT];
1765 bio->bi_opf |= REQ_RAHEAD;
1769 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1770 goto submit_bio_retry;
1774 } while (++cur < end);
1777 qtail[JQ_SUBMIT] = &pcl->next;
1779 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1780 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1785 psi_memstall_leave(&pflags);
1789 * although background is preferred, no one is pending for submission.
1790 * don't issue decompression but drop it directly instead.
1792 if (!*force_fg && !nr_bios) {
1793 kvfree(q[JQ_SUBMIT]);
1796 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1799 static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1800 struct page **pagepool, bool force_fg)
1802 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1804 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1806 z_erofs_submit_queue(f, pagepool, io, &force_fg);
1808 /* handle bypass queue (no i/o pclusters) immediately */
1809 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1814 /* wait until all bios are completed */
1815 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1817 /* handle synchronous decompress queue in the caller context */
1818 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1822 * Since partial uptodate is still unimplemented for now, we have to use
1823 * approximate readmore strategies as a start.
1825 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1826 struct readahead_control *rac,
1828 struct page **pagepool,
1831 struct inode *inode = f->inode;
1832 struct erofs_map_blocks *map = &f->map;
1838 err = z_erofs_map_blocks_iter(inode, map,
1839 EROFS_GET_BLOCKS_READMORE);
1843 /* expend ra for the trailing edge if readahead */
1845 loff_t newstart = readahead_pos(rac);
1847 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1848 readahead_expand(rac, newstart, cur - newstart);
1851 end = round_up(end, PAGE_SIZE);
1853 end = round_up(map->m_la, PAGE_SIZE);
1859 cur = map->m_la + map->m_llen - 1;
1860 while (cur >= end) {
1861 pgoff_t index = cur >> PAGE_SHIFT;
1864 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1866 if (PageUptodate(page)) {
1869 err = z_erofs_do_read_page(f, page, pagepool);
1871 erofs_err(inode->i_sb,
1872 "readmore error at page %lu @ nid %llu",
1873 index, EROFS_I(inode)->nid);
1878 if (cur < PAGE_SIZE)
1880 cur = (index << PAGE_SHIFT) - 1;
1884 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1886 struct page *page = &folio->page;
1887 struct inode *const inode = page->mapping->host;
1888 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1889 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1890 struct page *pagepool = NULL;
1893 trace_erofs_readpage(page, false);
1894 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1896 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1898 err = z_erofs_do_read_page(&f, page, &pagepool);
1899 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1901 (void)z_erofs_collector_end(&f);
1903 /* if some compressed cluster ready, need submit them anyway */
1904 z_erofs_runqueue(&f, &pagepool,
1905 z_erofs_get_sync_decompress_policy(sbi, 0));
1908 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1910 erofs_put_metabuf(&f.map.buf);
1911 erofs_release_pages(&pagepool);
1915 static void z_erofs_readahead(struct readahead_control *rac)
1917 struct inode *const inode = rac->mapping->host;
1918 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1919 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1920 struct page *pagepool = NULL, *head = NULL, *page;
1921 unsigned int nr_pages;
1924 f.headoffset = readahead_pos(rac);
1926 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1927 readahead_length(rac) - 1, &pagepool, true);
1928 nr_pages = readahead_count(rac);
1929 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1931 while ((page = readahead_page(rac))) {
1932 set_page_private(page, (unsigned long)head);
1937 struct page *page = head;
1940 /* traversal in reverse order */
1941 head = (void *)page_private(page);
1943 err = z_erofs_do_read_page(&f, page, &pagepool);
1945 erofs_err(inode->i_sb,
1946 "readahead error at page %lu @ nid %llu",
1947 page->index, EROFS_I(inode)->nid);
1950 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1951 (void)z_erofs_collector_end(&f);
1953 z_erofs_runqueue(&f, &pagepool,
1954 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1955 erofs_put_metabuf(&f.map.buf);
1956 erofs_release_pages(&pagepool);
1959 const struct address_space_operations z_erofs_aops = {
1960 .read_folio = z_erofs_read_folio,
1961 .readahead = z_erofs_readahead,