1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
12 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS 2
16 * let's leave a type here in case of introducing
17 * another tagged pointer later.
19 typedef void *z_erofs_next_pcluster_t;
27 #define __Z_EROFS_BVSET(name, total) \
29 /* point to the next page which contains the following bvecs */ \
30 struct page *nextpage; \
31 struct z_erofs_bvec bvec[total]; \
33 __Z_EROFS_BVSET(z_erofs_bvset,);
34 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
37 * Structure fields follow one of the following exclusion rules.
39 * I: Modifiable by initialization/destruction paths and read-only
42 * L: Field should be protected by the pcluster lock;
44 * A: Field should be accessed / updated in atomic for parallelized code.
46 struct z_erofs_pcluster {
47 struct erofs_workgroup obj;
50 /* A: point to next chained pcluster or TAILs */
51 z_erofs_next_pcluster_t next;
53 /* L: the maximum decompression size of this round */
56 /* L: total number of bvecs */
59 /* I: page offset of start position of decompression */
60 unsigned short pageofs_out;
62 /* I: page offset of inline compressed data */
63 unsigned short pageofs_in;
66 /* L: inline a certain number of bvec for bootstrap */
67 struct z_erofs_bvset_inline bvset;
69 /* I: can be used to free the pcluster by RCU. */
74 /* I: physical cluster size in pages */
75 unsigned short pclusterpages;
77 /* I: tailpacking inline compressed size */
78 unsigned short tailpacking_size;
81 /* I: compression algorithm format */
82 unsigned char algorithmformat;
84 /* L: whether partial decompression or not */
87 /* L: indicate several pageofs_outs or not */
90 /* A: compressed bvecs (can be cached or inplaced pages) */
91 struct z_erofs_bvec compressed_bvecs[];
94 /* let's avoid the valid 32-bit kernel addresses */
96 /* the chained workgroup has't submitted io (still open) */
97 #define Z_EROFS_PCLUSTER_TAIL ((void *)0x5F0ECAFE)
98 /* the chained workgroup has already submitted io */
99 #define Z_EROFS_PCLUSTER_TAIL_CLOSED ((void *)0x5F0EDEAD)
101 #define Z_EROFS_PCLUSTER_NIL (NULL)
103 struct z_erofs_decompressqueue {
104 struct super_block *sb;
105 atomic_t pending_bios;
106 z_erofs_next_pcluster_t head;
109 struct completion done;
110 struct work_struct work;
111 struct kthread_work kthread_work;
116 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
118 return !pcl->obj.index;
121 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
123 if (z_erofs_is_inline_pcluster(pcl))
125 return pcl->pclusterpages;
129 * bit 30: I/O error occurred on this page
130 * bit 0 - 29: remaining parts to complete this page
132 #define Z_EROFS_PAGE_EIO (1 << 30)
134 static inline void z_erofs_onlinepage_init(struct page *page)
139 } u = { .o = ATOMIC_INIT(1) };
141 set_page_private(page, u.v);
143 SetPagePrivate(page);
146 static inline void z_erofs_onlinepage_split(struct page *page)
148 atomic_inc((atomic_t *)&page->private);
151 static inline void z_erofs_page_mark_eio(struct page *page)
156 orig = atomic_read((atomic_t *)&page->private);
157 } while (atomic_cmpxchg((atomic_t *)&page->private, orig,
158 orig | Z_EROFS_PAGE_EIO) != orig);
161 static inline void z_erofs_onlinepage_endio(struct page *page)
165 DBG_BUGON(!PagePrivate(page));
166 v = atomic_dec_return((atomic_t *)&page->private);
167 if (!(v & ~Z_EROFS_PAGE_EIO)) {
168 set_page_private(page, 0);
169 ClearPagePrivate(page);
170 if (!(v & Z_EROFS_PAGE_EIO))
171 SetPageUptodate(page);
176 #define Z_EROFS_ONSTACK_PAGES 32
179 * since pclustersize is variable for big pcluster feature, introduce slab
180 * pools implementation for different pcluster sizes.
182 struct z_erofs_pcluster_slab {
183 struct kmem_cache *slab;
184 unsigned int maxpages;
188 #define _PCLP(n) { .maxpages = n }
190 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
191 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
192 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
195 struct z_erofs_bvec_iter {
197 struct z_erofs_bvset *bvset;
198 unsigned int nr, cur;
201 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
204 kunmap_local(iter->bvset);
208 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
210 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
211 /* have to access nextpage in advance, otherwise it will be unmapped */
212 struct page *nextpage = iter->bvset->nextpage;
213 struct page *oldpage;
215 DBG_BUGON(!nextpage);
216 oldpage = z_erofs_bvec_iter_end(iter);
217 iter->bvpage = nextpage;
218 iter->bvset = kmap_local_page(nextpage);
219 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
224 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
225 struct z_erofs_bvset_inline *bvset,
226 unsigned int bootstrap_nr,
229 *iter = (struct z_erofs_bvec_iter) {
231 .bvset = (struct z_erofs_bvset *)bvset,
234 while (cur > iter->nr) {
236 z_erofs_bvset_flip(iter);
241 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
242 struct z_erofs_bvec *bvec,
243 struct page **candidate_bvpage)
245 if (iter->cur == iter->nr) {
246 if (!*candidate_bvpage)
249 DBG_BUGON(iter->bvset->nextpage);
250 iter->bvset->nextpage = *candidate_bvpage;
251 z_erofs_bvset_flip(iter);
253 iter->bvset->nextpage = NULL;
254 *candidate_bvpage = NULL;
256 iter->bvset->bvec[iter->cur++] = *bvec;
260 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
261 struct z_erofs_bvec *bvec,
262 struct page **old_bvpage)
264 if (iter->cur == iter->nr)
265 *old_bvpage = z_erofs_bvset_flip(iter);
268 *bvec = iter->bvset->bvec[iter->cur++];
271 static void z_erofs_destroy_pcluster_pool(void)
275 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
276 if (!pcluster_pool[i].slab)
278 kmem_cache_destroy(pcluster_pool[i].slab);
279 pcluster_pool[i].slab = NULL;
283 static int z_erofs_create_pcluster_pool(void)
285 struct z_erofs_pcluster_slab *pcs;
286 struct z_erofs_pcluster *a;
289 for (pcs = pcluster_pool;
290 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
291 size = struct_size(a, compressed_bvecs, pcs->maxpages);
293 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
294 pcs->slab = kmem_cache_create(pcs->name, size, 0,
295 SLAB_RECLAIM_ACCOUNT, NULL);
299 z_erofs_destroy_pcluster_pool();
305 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
309 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
310 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
311 struct z_erofs_pcluster *pcl;
313 if (nrpages > pcs->maxpages)
316 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
318 return ERR_PTR(-ENOMEM);
319 pcl->pclusterpages = nrpages;
322 return ERR_PTR(-EINVAL);
325 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
327 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
330 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
331 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
333 if (pclusterpages > pcs->maxpages)
336 kmem_cache_free(pcs->slab, pcl);
342 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
344 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
345 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
347 static void erofs_destroy_percpu_workers(void)
349 struct kthread_worker *worker;
352 for_each_possible_cpu(cpu) {
353 worker = rcu_dereference_protected(
354 z_erofs_pcpu_workers[cpu], 1);
355 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
357 kthread_destroy_worker(worker);
359 kfree(z_erofs_pcpu_workers);
362 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
364 struct kthread_worker *worker =
365 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
369 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
370 sched_set_fifo_low(worker->task);
374 static int erofs_init_percpu_workers(void)
376 struct kthread_worker *worker;
379 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
380 sizeof(struct kthread_worker *), GFP_ATOMIC);
381 if (!z_erofs_pcpu_workers)
384 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
385 worker = erofs_init_percpu_worker(cpu);
387 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
392 static inline void erofs_destroy_percpu_workers(void) {}
393 static inline int erofs_init_percpu_workers(void) { return 0; }
396 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
397 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
398 static enum cpuhp_state erofs_cpuhp_state;
400 static int erofs_cpu_online(unsigned int cpu)
402 struct kthread_worker *worker, *old;
404 worker = erofs_init_percpu_worker(cpu);
406 return PTR_ERR(worker);
408 spin_lock(&z_erofs_pcpu_worker_lock);
409 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
410 lockdep_is_held(&z_erofs_pcpu_worker_lock));
412 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
413 spin_unlock(&z_erofs_pcpu_worker_lock);
415 kthread_destroy_worker(worker);
419 static int erofs_cpu_offline(unsigned int cpu)
421 struct kthread_worker *worker;
423 spin_lock(&z_erofs_pcpu_worker_lock);
424 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
425 lockdep_is_held(&z_erofs_pcpu_worker_lock));
426 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
427 spin_unlock(&z_erofs_pcpu_worker_lock);
431 kthread_destroy_worker(worker);
435 static int erofs_cpu_hotplug_init(void)
439 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
440 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
444 erofs_cpuhp_state = state;
448 static void erofs_cpu_hotplug_destroy(void)
450 if (erofs_cpuhp_state)
451 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
453 #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
454 static inline int erofs_cpu_hotplug_init(void) { return 0; }
455 static inline void erofs_cpu_hotplug_destroy(void) {}
458 void z_erofs_exit_zip_subsystem(void)
460 erofs_cpu_hotplug_destroy();
461 erofs_destroy_percpu_workers();
462 destroy_workqueue(z_erofs_workqueue);
463 z_erofs_destroy_pcluster_pool();
466 int __init z_erofs_init_zip_subsystem(void)
468 int err = z_erofs_create_pcluster_pool();
471 goto out_error_pcluster_pool;
473 z_erofs_workqueue = alloc_workqueue("erofs_worker",
474 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
475 if (!z_erofs_workqueue) {
477 goto out_error_workqueue_init;
480 err = erofs_init_percpu_workers();
482 goto out_error_pcpu_worker;
484 err = erofs_cpu_hotplug_init();
486 goto out_error_cpuhp_init;
489 out_error_cpuhp_init:
490 erofs_destroy_percpu_workers();
491 out_error_pcpu_worker:
492 destroy_workqueue(z_erofs_workqueue);
493 out_error_workqueue_init:
494 z_erofs_destroy_pcluster_pool();
495 out_error_pcluster_pool:
499 enum z_erofs_pclustermode {
500 Z_EROFS_PCLUSTER_INFLIGHT,
502 * The current pclusters was the tail of an exist chain, in addition
503 * that the previous processed chained pclusters are all decided to
504 * be hooked up to it.
505 * A new chain will be created for the remaining pclusters which are
506 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
507 * the next pcluster cannot reuse the whole page safely for inplace I/O
508 * in the following scenario:
509 * ________________________________________________________________
510 * | tail (partial) page | head (partial) page |
511 * | (belongs to the next pcl) | (belongs to the current pcl) |
512 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
514 Z_EROFS_PCLUSTER_HOOKED,
516 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
517 * could be dispatched into bypass queue later due to uptodated managed
518 * pages. All related online pages cannot be reused for inplace I/O (or
519 * bvpage) since it can be directly decoded without I/O submission.
521 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
523 * The current collection has been linked with the owned chain, and
524 * could also be linked with the remaining collections, which means
525 * if the processing page is the tail page of the collection, thus
526 * the current collection can safely use the whole page (since
527 * the previous collection is under control) for in-place I/O, as
529 * ________________________________________________________________
530 * | tail (partial) page | head (partial) page |
531 * | (of the current cl) | (of the previous collection) |
532 * | PCLUSTER_FOLLOWED or | |
533 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
535 * [ (*) the above page can be used as inplace I/O. ]
537 Z_EROFS_PCLUSTER_FOLLOWED,
540 struct z_erofs_decompress_frontend {
541 struct inode *const inode;
542 struct erofs_map_blocks map;
543 struct z_erofs_bvec_iter biter;
545 struct page *candidate_bvpage;
546 struct z_erofs_pcluster *pcl, *tailpcl;
547 z_erofs_next_pcluster_t owned_head;
548 enum z_erofs_pclustermode mode;
550 /* used for applying cache strategy on the fly */
552 erofs_off_t headoffset;
554 /* a pointer used to pick up inplace I/O pages */
558 #define DECOMPRESS_FRONTEND_INIT(__i) { \
559 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
560 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
562 static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
564 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
566 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
572 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
573 fe->map.m_la < fe->headoffset)
579 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
580 struct page **pagepool)
582 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
583 struct z_erofs_pcluster *pcl = fe->pcl;
584 bool shouldalloc = z_erofs_should_alloc_cache(fe);
585 bool standalone = true;
587 * optimistic allocation without direct reclaim since inplace I/O
588 * can be used if low memory otherwise.
590 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
591 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
594 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
597 for (i = 0; i < pcl->pclusterpages; ++i) {
599 void *t; /* mark pages just found for debugging */
600 struct page *newpage = NULL;
602 /* the compressed page was loaded before */
603 if (READ_ONCE(pcl->compressed_bvecs[i].page))
606 page = find_get_page(mc, pcl->obj.index + i);
609 t = (void *)((unsigned long)page | 1);
611 /* I/O is needed, no possible to decompress directly */
617 * try to use cached I/O if page allocation
618 * succeeds or fallback to in-place I/O instead
619 * to avoid any direct reclaim.
621 newpage = erofs_allocpage(pagepool, gfp);
624 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
625 t = (void *)((unsigned long)newpage | 1);
628 if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
634 erofs_pagepool_add(pagepool, newpage);
638 * don't do inplace I/O if all compressed pages are available in
639 * managed cache since it can be moved to the bypass queue instead.
642 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
645 /* called by erofs_shrinker to get rid of all compressed_pages */
646 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
647 struct erofs_workgroup *grp)
649 struct z_erofs_pcluster *const pcl =
650 container_of(grp, struct z_erofs_pcluster, obj);
653 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
655 * refcount of workgroup is now freezed as 1,
656 * therefore no need to worry about available decompression users.
658 for (i = 0; i < pcl->pclusterpages; ++i) {
659 struct page *page = pcl->compressed_bvecs[i].page;
664 /* block other users from reclaiming or migrating the page */
665 if (!trylock_page(page))
668 if (!erofs_page_is_managed(sbi, page))
671 /* barrier is implied in the following 'unlock_page' */
672 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
673 detach_page_private(page);
679 int erofs_try_to_free_cached_page(struct page *page)
681 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
684 if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
688 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
689 for (i = 0; i < pcl->pclusterpages; ++i) {
690 if (pcl->compressed_bvecs[i].page == page) {
691 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
696 erofs_workgroup_unfreeze(&pcl->obj, 1);
698 detach_page_private(page);
702 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
703 struct z_erofs_bvec *bvec)
705 struct z_erofs_pcluster *const pcl = fe->pcl;
707 while (fe->icur > 0) {
708 if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
710 pcl->compressed_bvecs[fe->icur] = *bvec;
717 /* callers must be with pcluster lock held */
718 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
719 struct z_erofs_bvec *bvec, bool exclusive)
724 /* give priority for inplaceio to use file pages first */
725 if (z_erofs_try_inplace_io(fe, bvec))
727 /* otherwise, check if it can be used as a bvpage */
728 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
729 !fe->candidate_bvpage)
730 fe->candidate_bvpage = bvec->page;
732 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
733 fe->pcl->vcnt += (ret >= 0);
737 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
739 struct z_erofs_pcluster *pcl = f->pcl;
740 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
742 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
743 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
744 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
745 *owned_head = &pcl->next;
746 /* so we can attach this pcluster to our submission chain. */
747 f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
752 * type 2, link to the end of an existing open chain, be careful
753 * that its submission is controlled by the original attached chain.
755 if (*owned_head != &pcl->next && pcl != f->tailpcl &&
756 cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
757 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
758 *owned_head = Z_EROFS_PCLUSTER_TAIL;
759 f->mode = Z_EROFS_PCLUSTER_HOOKED;
763 /* type 3, it belongs to a chain, but it isn't the end of the chain */
764 f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
767 static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
769 struct erofs_map_blocks *map = &fe->map;
770 bool ztailpacking = map->m_flags & EROFS_MAP_META;
771 struct z_erofs_pcluster *pcl;
772 struct erofs_workgroup *grp;
775 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
776 (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
778 return -EFSCORRUPTED;
781 /* no available pcluster, let's allocate one */
782 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
783 map->m_plen >> PAGE_SHIFT);
787 atomic_set(&pcl->obj.refcount, 1);
788 pcl->algorithmformat = map->m_algorithmformat;
792 /* new pclusters should be claimed as type 1, primary and followed */
793 pcl->next = fe->owned_head;
794 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
795 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
798 * lock all primary followed works before visible to others
799 * and mutex_trylock *never* fails for a new pcluster.
801 mutex_init(&pcl->lock);
802 DBG_BUGON(!mutex_trylock(&pcl->lock));
805 pcl->obj.index = 0; /* which indicates ztailpacking */
806 pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
807 pcl->tailpacking_size = map->m_plen;
809 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
811 grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
817 if (grp != &pcl->obj) {
818 fe->pcl = container_of(grp,
819 struct z_erofs_pcluster, obj);
824 /* used to check tail merging loop due to corrupted images */
825 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
827 fe->owned_head = &pcl->next;
832 mutex_unlock(&pcl->lock);
833 z_erofs_free_pcluster(pcl);
837 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
839 struct erofs_map_blocks *map = &fe->map;
840 struct erofs_workgroup *grp = NULL;
845 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
846 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
847 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
849 if (!(map->m_flags & EROFS_MAP_META)) {
850 grp = erofs_find_workgroup(fe->inode->i_sb,
851 map->m_pa >> PAGE_SHIFT);
852 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
854 return -EFSCORRUPTED;
858 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
861 ret = z_erofs_register_pcluster(fe);
864 if (ret == -EEXIST) {
865 mutex_lock(&fe->pcl->lock);
866 /* used to check tail merging loop due to corrupted images */
867 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
868 fe->tailpcl = fe->pcl;
870 z_erofs_try_to_claim_pcluster(fe);
874 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
875 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
876 /* since file-backed online pages are traversed in reverse order */
877 fe->icur = z_erofs_pclusterpages(fe->pcl);
882 * keep in mind that no referenced pclusters will be freed
883 * only after a RCU grace period.
885 static void z_erofs_rcu_callback(struct rcu_head *head)
887 z_erofs_free_pcluster(container_of(head,
888 struct z_erofs_pcluster, rcu));
891 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
893 struct z_erofs_pcluster *const pcl =
894 container_of(grp, struct z_erofs_pcluster, obj);
896 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
899 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
901 struct z_erofs_pcluster *pcl = fe->pcl;
906 z_erofs_bvec_iter_end(&fe->biter);
907 mutex_unlock(&pcl->lock);
909 if (fe->candidate_bvpage) {
910 DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
911 fe->candidate_bvpage = NULL;
915 * if all pending pages are added, don't hold its reference
916 * any longer if the pcluster isn't hosted by ourselves.
918 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
919 erofs_workgroup_put(&pcl->obj);
925 static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
926 struct page *page, unsigned int pageofs,
929 struct super_block *sb = inode->i_sb;
930 struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
931 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
936 return -EFSCORRUPTED;
938 buf.inode = packed_inode;
939 pos += EROFS_I(inode)->z_fragmentoff;
940 for (i = 0; i < len; i += cnt) {
941 cnt = min_t(unsigned int, len - i,
942 sb->s_blocksize - erofs_blkoff(sb, pos));
943 src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
945 erofs_put_metabuf(&buf);
949 dst = kmap_local_page(page);
950 memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
954 erofs_put_metabuf(&buf);
958 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
959 struct page *page, struct page **pagepool)
961 struct inode *const inode = fe->inode;
962 struct erofs_map_blocks *const map = &fe->map;
963 const loff_t offset = page_offset(page);
964 bool tight = true, exclusive;
965 unsigned int cur, end, spiltted;
968 /* register locked file pages as online pages in pack */
969 z_erofs_onlinepage_init(page);
976 if (offset + cur < map->m_la ||
977 offset + cur >= map->m_la + map->m_llen) {
978 if (z_erofs_collector_end(fe))
979 fe->backmost = false;
980 map->m_la = offset + cur;
982 err = z_erofs_map_blocks_iter(inode, map, 0);
988 /* didn't get a valid pcluster previously (very rare) */
991 if (!(map->m_flags & EROFS_MAP_MAPPED) ||
992 map->m_flags & EROFS_MAP_FRAGMENT)
995 err = z_erofs_collector_begin(fe);
999 if (z_erofs_is_inline_pcluster(fe->pcl)) {
1002 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
1003 erofs_blknr(inode->i_sb, map->m_pa),
1007 erofs_err(inode->i_sb,
1008 "failed to get inline page, err %d", err);
1011 get_page(fe->map.buf.page);
1012 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1014 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1016 /* bind cache first when cached decompression is preferred */
1017 z_erofs_bind_cache(fe, pagepool);
1021 * Ensure the current partial page belongs to this submit chain rather
1022 * than other concurrent submit chains or the noio(bypass) chain since
1023 * those chains are handled asynchronously thus the page cannot be used
1024 * for inplace I/O or bvpage (should be processed in a strict order.)
1026 tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1027 fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1029 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
1030 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1031 zero_user_segment(page, cur, end);
1034 if (map->m_flags & EROFS_MAP_FRAGMENT) {
1035 unsigned int pageofs, skip, len;
1037 if (offset > map->m_la) {
1039 skip = offset - map->m_la;
1041 pageofs = map->m_la & ~PAGE_MASK;
1044 len = min_t(unsigned int, map->m_llen - skip, end - cur);
1045 err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1053 exclusive = (!cur && (!spiltted || tight));
1055 tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1058 err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
1060 .offset = offset - map->m_la,
1063 /* should allocate an additional short-lived page for bvset */
1064 if (err == -EAGAIN && !fe->candidate_bvpage) {
1065 fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
1066 set_page_private(fe->candidate_bvpage,
1067 Z_EROFS_SHORTLIVED_PAGE);
1072 DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
1076 z_erofs_onlinepage_split(page);
1077 /* bump up the number of spiltted parts of a page */
1079 if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1080 fe->pcl->multibases = true;
1081 if (fe->pcl->length < offset + end - map->m_la) {
1082 fe->pcl->length = offset + end - map->m_la;
1083 fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1085 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1086 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1087 fe->pcl->length == map->m_llen)
1088 fe->pcl->partial = false;
1090 /* shorten the remaining extent to update progress */
1091 map->m_llen = offset + cur - map->m_la;
1092 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1100 z_erofs_page_mark_eio(page);
1101 z_erofs_onlinepage_endio(page);
1105 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1106 unsigned int readahead_pages)
1108 /* auto: enable for read_folio, disable for readahead */
1109 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1113 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1114 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1120 static bool z_erofs_page_is_invalidated(struct page *page)
1122 return !page->mapping && !z_erofs_is_shortlived_page(page);
1125 struct z_erofs_decompress_backend {
1126 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1127 struct super_block *sb;
1128 struct z_erofs_pcluster *pcl;
1130 /* pages with the longest decompressed length for deduplication */
1131 struct page **decompressed_pages;
1132 /* pages to keep the compressed data */
1133 struct page **compressed_pages;
1135 struct list_head decompressed_secondary_bvecs;
1136 struct page **pagepool;
1137 unsigned int onstack_used, nr_pages;
1140 struct z_erofs_bvec_item {
1141 struct z_erofs_bvec bvec;
1142 struct list_head list;
1145 static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
1146 struct z_erofs_bvec *bvec)
1148 struct z_erofs_bvec_item *item;
1150 if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1153 pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
1154 DBG_BUGON(pgnr >= be->nr_pages);
1155 if (!be->decompressed_pages[pgnr]) {
1156 be->decompressed_pages[pgnr] = bvec->page;
1161 /* (cold path) one pcluster is requested multiple times */
1162 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1164 list_add(&item->list, &be->decompressed_secondary_bvecs);
1167 static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1170 unsigned int off0 = be->pcl->pageofs_out;
1171 struct list_head *p, *n;
1173 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1174 struct z_erofs_bvec_item *bvi;
1175 unsigned int end, cur;
1178 bvi = container_of(p, struct z_erofs_bvec_item, list);
1179 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1180 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1182 dst = kmap_local_page(bvi->bvec.page);
1184 unsigned int pgnr, scur, len;
1186 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1187 DBG_BUGON(pgnr >= be->nr_pages);
1189 scur = bvi->bvec.offset + cur -
1190 ((pgnr << PAGE_SHIFT) - off0);
1191 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1192 if (!be->decompressed_pages[pgnr]) {
1193 err = -EFSCORRUPTED;
1197 src = kmap_local_page(be->decompressed_pages[pgnr]);
1198 memcpy(dst + cur, src + scur, len);
1204 z_erofs_page_mark_eio(bvi->bvec.page);
1205 z_erofs_onlinepage_endio(bvi->bvec.page);
1211 static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
1213 struct z_erofs_pcluster *pcl = be->pcl;
1214 struct z_erofs_bvec_iter biter;
1215 struct page *old_bvpage;
1218 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1219 for (i = 0; i < pcl->vcnt; ++i) {
1220 struct z_erofs_bvec bvec;
1222 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1225 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1227 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1228 z_erofs_do_decompressed_bvec(be, &bvec);
1231 old_bvpage = z_erofs_bvec_iter_end(&biter);
1233 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1236 static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
1239 struct z_erofs_pcluster *pcl = be->pcl;
1240 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1243 *overlapped = false;
1244 for (i = 0; i < pclusterpages; ++i) {
1245 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1246 struct page *page = bvec->page;
1248 /* compressed pages ought to be present before decompressing */
1253 be->compressed_pages[i] = page;
1255 if (z_erofs_is_inline_pcluster(pcl)) {
1256 if (!PageUptodate(page))
1261 DBG_BUGON(z_erofs_page_is_invalidated(page));
1262 if (!z_erofs_is_shortlived_page(page)) {
1263 if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
1264 if (!PageUptodate(page))
1268 z_erofs_do_decompressed_bvec(be, bvec);
1278 static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
1281 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1282 struct z_erofs_pcluster *pcl = be->pcl;
1283 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1284 const struct z_erofs_decompressor *decompressor =
1285 &erofs_decompressors[pcl->algorithmformat];
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 = decompressor->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,
1673 bool *force_fg, bool readahead)
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, bool ra)
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, ra);
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,
1827 struct page **pagepool, bool backmost)
1829 struct inode *inode = f->inode;
1830 struct erofs_map_blocks *map = &f->map;
1831 erofs_off_t cur, end, headoffset = f->headoffset;
1836 end = headoffset + readahead_length(rac) - 1;
1838 end = headoffset + PAGE_SIZE - 1;
1840 err = z_erofs_map_blocks_iter(inode, map,
1841 EROFS_GET_BLOCKS_READMORE);
1845 /* expand ra for the trailing edge if readahead */
1847 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1848 readahead_expand(rac, headoffset, cur - headoffset);
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, &pagepool, true);
1897 err = z_erofs_do_read_page(&f, page, &pagepool);
1898 z_erofs_pcluster_readmore(&f, NULL, &pagepool, false);
1900 (void)z_erofs_collector_end(&f);
1902 /* if some compressed cluster ready, need submit them anyway */
1903 z_erofs_runqueue(&f, &pagepool, z_erofs_is_sync_decompress(sbi, 0),
1907 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1909 erofs_put_metabuf(&f.map.buf);
1910 erofs_release_pages(&pagepool);
1914 static void z_erofs_readahead(struct readahead_control *rac)
1916 struct inode *const inode = rac->mapping->host;
1917 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1918 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1919 struct page *pagepool = NULL, *head = NULL, *page;
1920 unsigned int nr_pages;
1922 f.headoffset = readahead_pos(rac);
1924 z_erofs_pcluster_readmore(&f, rac, &pagepool, true);
1925 nr_pages = readahead_count(rac);
1926 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1928 while ((page = readahead_page(rac))) {
1929 set_page_private(page, (unsigned long)head);
1934 struct page *page = head;
1937 /* traversal in reverse order */
1938 head = (void *)page_private(page);
1940 err = z_erofs_do_read_page(&f, page, &pagepool);
1942 erofs_err(inode->i_sb,
1943 "readahead error at page %lu @ nid %llu",
1944 page->index, EROFS_I(inode)->nid);
1947 z_erofs_pcluster_readmore(&f, rac, &pagepool, false);
1948 (void)z_erofs_collector_end(&f);
1950 z_erofs_runqueue(&f, &pagepool,
1951 z_erofs_is_sync_decompress(sbi, nr_pages), true);
1952 erofs_put_metabuf(&f.map.buf);
1953 erofs_release_pages(&pagepool);
1956 const struct address_space_operations z_erofs_aops = {
1957 .read_folio = z_erofs_read_folio,
1958 .readahead = z_erofs_readahead,