1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
8 #include <linux/prefetch.h>
10 #include <trace/events/erofs.h>
13 * since pclustersize is variable for big pcluster feature, introduce slab
14 * pools implementation for different pcluster sizes.
16 struct z_erofs_pcluster_slab {
17 struct kmem_cache *slab;
18 unsigned int maxpages;
22 #define _PCLP(n) { .maxpages = n }
24 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
25 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
26 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
29 static void z_erofs_destroy_pcluster_pool(void)
33 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
34 if (!pcluster_pool[i].slab)
36 kmem_cache_destroy(pcluster_pool[i].slab);
37 pcluster_pool[i].slab = NULL;
41 static int z_erofs_create_pcluster_pool(void)
43 struct z_erofs_pcluster_slab *pcs;
44 struct z_erofs_pcluster *a;
47 for (pcs = pcluster_pool;
48 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
49 size = struct_size(a, compressed_pages, pcs->maxpages);
51 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
52 pcs->slab = kmem_cache_create(pcs->name, size, 0,
53 SLAB_RECLAIM_ACCOUNT, NULL);
57 z_erofs_destroy_pcluster_pool();
63 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
67 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
68 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
69 struct z_erofs_pcluster *pcl;
71 if (nrpages > pcs->maxpages)
74 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
76 return ERR_PTR(-ENOMEM);
77 pcl->pclusterpages = nrpages;
80 return ERR_PTR(-EINVAL);
83 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
85 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
88 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
91 if (pclusterpages > pcs->maxpages)
94 kmem_cache_free(pcs->slab, pcl);
100 /* how to allocate cached pages for a pcluster */
101 enum z_erofs_cache_alloctype {
102 DONTALLOC, /* don't allocate any cached pages */
104 * try to use cached I/O if page allocation succeeds or fallback
105 * to in-place I/O instead to avoid any direct reclaim.
111 * tagged pointer with 1-bit tag for all compressed pages
112 * tag 0 - the page is just found with an extra page reference
114 typedef tagptr1_t compressed_page_t;
116 #define tag_compressed_page_justfound(page) \
117 tagptr_fold(compressed_page_t, page, 1)
119 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
121 void z_erofs_exit_zip_subsystem(void)
123 destroy_workqueue(z_erofs_workqueue);
124 z_erofs_destroy_pcluster_pool();
127 static inline int z_erofs_init_workqueue(void)
129 const unsigned int onlinecpus = num_possible_cpus();
132 * no need to spawn too many threads, limiting threads could minimum
133 * scheduling overhead, perhaps per-CPU threads should be better?
135 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
136 WQ_UNBOUND | WQ_HIGHPRI,
137 onlinecpus + onlinecpus / 4);
138 return z_erofs_workqueue ? 0 : -ENOMEM;
141 int __init z_erofs_init_zip_subsystem(void)
143 int err = z_erofs_create_pcluster_pool();
147 err = z_erofs_init_workqueue();
149 z_erofs_destroy_pcluster_pool();
153 enum z_erofs_collectmode {
157 * The current collection was the tail of an exist chain, in addition
158 * that the previous processed chained collections are all decided to
159 * be hooked up to it.
160 * A new chain will be created for the remaining collections which are
161 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
162 * the next collection cannot reuse the whole page safely in
163 * the following scenario:
164 * ________________________________________________________________
165 * | tail (partial) page | head (partial) page |
166 * | (belongs to the next cl) | (belongs to the current cl) |
167 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
169 COLLECT_PRIMARY_HOOKED,
171 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
172 * could be dispatched into bypass queue later due to uptodated managed
173 * pages. All related online pages cannot be reused for inplace I/O (or
174 * pagevec) since it can be directly decoded without I/O submission.
176 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
178 * The current collection has been linked with the owned chain, and
179 * could also be linked with the remaining collections, which means
180 * if the processing page is the tail page of the collection, thus
181 * the current collection can safely use the whole page (since
182 * the previous collection is under control) for in-place I/O, as
184 * ________________________________________________________________
185 * | tail (partial) page | head (partial) page |
186 * | (of the current cl) | (of the previous collection) |
187 * | PRIMARY_FOLLOWED or | |
188 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
190 * [ (*) the above page can be used as inplace I/O. ]
192 COLLECT_PRIMARY_FOLLOWED,
195 struct z_erofs_decompress_frontend {
196 struct inode *const inode;
197 struct erofs_map_blocks map;
199 struct z_erofs_pagevec_ctor vector;
201 struct z_erofs_pcluster *pcl, *tailpcl;
202 struct z_erofs_collection *cl;
203 /* a pointer used to pick up inplace I/O pages */
204 struct page **icpage_ptr;
205 z_erofs_next_pcluster_t owned_head;
207 enum z_erofs_collectmode mode;
210 /* used for applying cache strategy on the fly */
212 erofs_off_t headoffset;
215 #define DECOMPRESS_FRONTEND_INIT(__i) { \
216 .inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
217 .mode = COLLECT_PRIMARY_FOLLOWED }
219 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
220 static DEFINE_MUTEX(z_pagemap_global_lock);
222 static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
223 enum z_erofs_cache_alloctype type,
224 struct page **pagepool)
226 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
227 struct z_erofs_pcluster *pcl = fe->pcl;
228 bool standalone = true;
230 * optimistic allocation without direct reclaim since inplace I/O
231 * can be used if low memory otherwise.
233 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
234 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
238 if (fe->mode < COLLECT_PRIMARY_FOLLOWED)
241 pages = pcl->compressed_pages;
242 index = pcl->obj.index;
243 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
246 struct page *newpage = NULL;
248 /* the compressed page was loaded before */
249 if (READ_ONCE(*pages))
252 page = find_get_page(mc, index);
255 t = tag_compressed_page_justfound(page);
257 /* I/O is needed, no possible to decompress directly */
261 newpage = erofs_allocpage(pagepool, gfp);
264 set_page_private(newpage,
265 Z_EROFS_PREALLOCATED_PAGE);
266 t = tag_compressed_page_justfound(newpage);
268 default: /* DONTALLOC */
273 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
279 erofs_pagepool_add(pagepool, newpage);
283 * don't do inplace I/O if all compressed pages are available in
284 * managed cache since it can be moved to the bypass queue instead.
287 fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
290 /* called by erofs_shrinker to get rid of all compressed_pages */
291 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
292 struct erofs_workgroup *grp)
294 struct z_erofs_pcluster *const pcl =
295 container_of(grp, struct z_erofs_pcluster, obj);
298 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
300 * refcount of workgroup is now freezed as 1,
301 * therefore no need to worry about available decompression users.
303 for (i = 0; i < pcl->pclusterpages; ++i) {
304 struct page *page = pcl->compressed_pages[i];
309 /* block other users from reclaiming or migrating the page */
310 if (!trylock_page(page))
313 if (!erofs_page_is_managed(sbi, page))
316 /* barrier is implied in the following 'unlock_page' */
317 WRITE_ONCE(pcl->compressed_pages[i], NULL);
318 detach_page_private(page);
324 int erofs_try_to_free_cached_page(struct page *page)
326 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
327 int ret = 0; /* 0 - busy */
329 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
332 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
333 for (i = 0; i < pcl->pclusterpages; ++i) {
334 if (pcl->compressed_pages[i] == page) {
335 WRITE_ONCE(pcl->compressed_pages[i], NULL);
340 erofs_workgroup_unfreeze(&pcl->obj, 1);
343 detach_page_private(page);
348 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
349 static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
352 struct z_erofs_pcluster *const pcl = fe->pcl;
354 while (fe->icpage_ptr > pcl->compressed_pages)
355 if (!cmpxchg(--fe->icpage_ptr, NULL, page))
360 /* callers must be with collection lock held */
361 static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
362 struct page *page, enum z_erofs_page_type type,
367 /* give priority for inplaceio */
368 if (fe->mode >= COLLECT_PRIMARY &&
369 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
370 z_erofs_try_inplace_io(fe, page))
373 ret = z_erofs_pagevec_enqueue(&fe->vector, page, type,
375 fe->cl->vcnt += (unsigned int)ret;
376 return ret ? 0 : -EAGAIN;
379 static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
381 struct z_erofs_pcluster *pcl = f->pcl;
382 z_erofs_next_pcluster_t *owned_head = &f->owned_head;
384 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
385 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
386 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
387 *owned_head = &pcl->next;
388 /* so we can attach this pcluster to our submission chain. */
389 f->mode = COLLECT_PRIMARY_FOLLOWED;
394 * type 2, link to the end of an existing open chain, be careful
395 * that its submission is controlled by the original attached chain.
397 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
398 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
399 *owned_head = Z_EROFS_PCLUSTER_TAIL;
400 f->mode = COLLECT_PRIMARY_HOOKED;
404 /* type 3, it belongs to a chain, but it isn't the end of the chain */
405 f->mode = COLLECT_PRIMARY;
408 static int z_erofs_lookup_collection(struct z_erofs_decompress_frontend *fe,
410 struct erofs_map_blocks *map)
412 struct z_erofs_pcluster *pcl = fe->pcl;
413 struct z_erofs_collection *cl;
416 /* to avoid unexpected loop formed by corrupted images */
417 if (fe->owned_head == &pcl->next || pcl == fe->tailpcl) {
419 return -EFSCORRUPTED;
422 cl = z_erofs_primarycollection(pcl);
423 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
425 return -EFSCORRUPTED;
428 length = READ_ONCE(pcl->length);
429 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
430 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
432 return -EFSCORRUPTED;
435 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
437 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
438 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
440 while (llen > length &&
441 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
443 length = READ_ONCE(pcl->length);
446 mutex_lock(&cl->lock);
447 /* used to check tail merging loop due to corrupted images */
448 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
451 z_erofs_try_to_claim_pcluster(fe);
456 static int z_erofs_register_collection(struct z_erofs_decompress_frontend *fe,
458 struct erofs_map_blocks *map)
460 bool ztailpacking = map->m_flags & EROFS_MAP_META;
461 struct z_erofs_pcluster *pcl;
462 struct z_erofs_collection *cl;
463 struct erofs_workgroup *grp;
466 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
468 return -EFSCORRUPTED;
471 /* no available pcluster, let's allocate one */
472 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
473 map->m_plen >> PAGE_SHIFT);
477 atomic_set(&pcl->obj.refcount, 1);
478 pcl->algorithmformat = map->m_algorithmformat;
479 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
480 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
481 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
483 /* new pclusters should be claimed as type 1, primary and followed */
484 pcl->next = fe->owned_head;
485 fe->mode = COLLECT_PRIMARY_FOLLOWED;
487 cl = z_erofs_primarycollection(pcl);
488 cl->pageofs = map->m_la & ~PAGE_MASK;
491 * lock all primary followed works before visible to others
492 * and mutex_trylock *never* fails for a new pcluster.
494 mutex_init(&cl->lock);
495 DBG_BUGON(!mutex_trylock(&cl->lock));
498 pcl->obj.index = 0; /* which indicates ztailpacking */
499 pcl->pageofs_in = erofs_blkoff(map->m_pa);
500 pcl->tailpacking_size = map->m_plen;
502 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
504 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
510 if (grp != &pcl->obj) {
511 fe->pcl = container_of(grp,
512 struct z_erofs_pcluster, obj);
517 /* used to check tail merging loop due to corrupted images */
518 if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
520 fe->owned_head = &pcl->next;
526 mutex_unlock(&cl->lock);
527 z_erofs_free_pcluster(pcl);
531 static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe,
533 struct erofs_map_blocks *map)
535 struct erofs_workgroup *grp;
540 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
541 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
542 DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
544 if (map->m_flags & EROFS_MAP_META) {
545 if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
547 return -EFSCORRUPTED;
552 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
554 fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
557 ret = z_erofs_register_collection(fe, inode, map);
564 ret = z_erofs_lookup_collection(fe, inode, map);
566 erofs_workgroup_put(&fe->pcl->obj);
571 z_erofs_pagevec_ctor_init(&fe->vector, Z_EROFS_NR_INLINE_PAGEVECS,
572 fe->cl->pagevec, fe->cl->vcnt);
573 /* since file-backed online pages are traversed in reverse order */
574 fe->icpage_ptr = fe->pcl->compressed_pages +
575 z_erofs_pclusterpages(fe->pcl);
580 * keep in mind that no referenced pclusters will be freed
581 * only after a RCU grace period.
583 static void z_erofs_rcu_callback(struct rcu_head *head)
585 struct z_erofs_collection *const cl =
586 container_of(head, struct z_erofs_collection, rcu);
588 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
589 primary_collection));
592 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
594 struct z_erofs_pcluster *const pcl =
595 container_of(grp, struct z_erofs_pcluster, obj);
596 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
598 call_rcu(&cl->rcu, z_erofs_rcu_callback);
601 static void z_erofs_collection_put(struct z_erofs_collection *cl)
603 struct z_erofs_pcluster *const pcl =
604 container_of(cl, struct z_erofs_pcluster, primary_collection);
606 erofs_workgroup_put(&pcl->obj);
609 static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
611 struct z_erofs_collection *cl = fe->cl;
616 z_erofs_pagevec_ctor_exit(&fe->vector, false);
617 mutex_unlock(&cl->lock);
620 * if all pending pages are added, don't hold its reference
621 * any longer if the pcluster isn't hosted by ourselves.
623 if (fe->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
624 z_erofs_collection_put(cl);
630 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
631 unsigned int cachestrategy,
634 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
640 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
644 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
645 struct page *page, struct page **pagepool)
647 struct inode *const inode = fe->inode;
648 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
649 struct erofs_map_blocks *const map = &fe->map;
650 const loff_t offset = page_offset(page);
653 enum z_erofs_cache_alloctype cache_strategy;
654 enum z_erofs_page_type page_type;
655 unsigned int cur, end, spiltted, index;
658 /* register locked file pages as online pages in pack */
659 z_erofs_onlinepage_init(page);
666 /* lucky, within the range of the current map_blocks */
667 if (offset + cur >= map->m_la &&
668 offset + cur < map->m_la + map->m_llen) {
669 /* didn't get a valid collection previously (very rare) */
675 /* go ahead the next map_blocks */
676 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
678 if (z_erofs_collector_end(fe))
679 fe->backmost = false;
681 map->m_la = offset + cur;
683 err = z_erofs_map_blocks_iter(inode, map, 0);
688 if (!(map->m_flags & EROFS_MAP_MAPPED))
691 err = z_erofs_collector_begin(fe, inode, map);
695 if (z_erofs_is_inline_pcluster(fe->pcl)) {
698 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
699 erofs_blknr(map->m_pa), EROFS_NO_KMAP);
702 erofs_err(inode->i_sb,
703 "failed to get inline page, err %d", err);
706 get_page(fe->map.buf.page);
707 WRITE_ONCE(fe->pcl->compressed_pages[0], fe->map.buf.page);
708 fe->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
710 /* bind cache first when cached decompression is preferred */
711 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
713 cache_strategy = TRYALLOC;
715 cache_strategy = DONTALLOC;
717 z_erofs_bind_cache(fe, cache_strategy, pagepool);
721 * Ensure the current partial page belongs to this submit chain rather
722 * than other concurrent submit chains or the noio(bypass) chain since
723 * those chains are handled asynchronously thus the page cannot be used
724 * for inplace I/O or pagevec (should be processed in strict order.)
726 tight &= (fe->mode >= COLLECT_PRIMARY_HOOKED &&
727 fe->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
729 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
730 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
731 zero_user_segment(page, cur, end);
735 /* let's derive page type */
736 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
737 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
738 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
739 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
742 tight &= (fe->mode >= COLLECT_PRIMARY_FOLLOWED);
745 err = z_erofs_attach_page(fe, page, page_type,
746 fe->mode >= COLLECT_PRIMARY_FOLLOWED);
747 /* should allocate an additional short-lived page for pagevec */
748 if (err == -EAGAIN) {
749 struct page *const newpage =
750 alloc_page(GFP_NOFS | __GFP_NOFAIL);
752 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
753 err = z_erofs_attach_page(fe, newpage,
754 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
762 index = page->index - (map->m_la >> PAGE_SHIFT);
764 z_erofs_onlinepage_fixup(page, index, true);
766 /* bump up the number of spiltted parts of a page */
768 /* also update nr_pages */
769 fe->cl->nr_pages = max_t(pgoff_t, fe->cl->nr_pages, index + 1);
771 /* can be used for verification */
772 map->m_llen = offset + cur - map->m_la;
779 z_erofs_onlinepage_endio(page);
781 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
782 __func__, page, spiltted, map->m_llen);
785 /* if some error occurred while processing this page */
791 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
792 unsigned int readahead_pages)
794 /* auto: enable for readpage, disable for readahead */
795 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
799 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
800 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
806 static bool z_erofs_page_is_invalidated(struct page *page)
808 return !page->mapping && !z_erofs_is_shortlived_page(page);
811 static int z_erofs_decompress_pcluster(struct super_block *sb,
812 struct z_erofs_pcluster *pcl,
813 struct page **pagepool)
815 struct erofs_sb_info *const sbi = EROFS_SB(sb);
816 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
817 struct z_erofs_pagevec_ctor ctor;
818 unsigned int i, inputsize, outputsize, llen, nr_pages;
819 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
820 struct page **pages, **compressed_pages, *page;
822 enum z_erofs_page_type page_type;
823 bool overlapped, partial;
824 struct z_erofs_collection *cl;
828 cl = z_erofs_primarycollection(pcl);
829 DBG_BUGON(!READ_ONCE(cl->nr_pages));
831 mutex_lock(&cl->lock);
832 nr_pages = cl->nr_pages;
834 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
835 pages = pages_onstack;
836 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
837 mutex_trylock(&z_pagemap_global_lock)) {
838 pages = z_pagemap_global;
840 gfp_t gfp_flags = GFP_KERNEL;
842 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
843 gfp_flags |= __GFP_NOFAIL;
845 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
848 /* fallback to global pagemap for the lowmem scenario */
850 mutex_lock(&z_pagemap_global_lock);
851 pages = z_pagemap_global;
855 for (i = 0; i < nr_pages; ++i)
859 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
862 for (i = 0; i < cl->vcnt; ++i) {
865 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
867 /* all pages in pagevec ought to be valid */
869 DBG_BUGON(z_erofs_page_is_invalidated(page));
871 if (z_erofs_put_shortlivedpage(pagepool, page))
874 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
877 pagenr = z_erofs_onlinepage_index(page);
879 DBG_BUGON(pagenr >= nr_pages);
882 * currently EROFS doesn't support multiref(dedup),
883 * so here erroring out one multiref page.
887 SetPageError(pages[pagenr]);
888 z_erofs_onlinepage_endio(pages[pagenr]);
891 pages[pagenr] = page;
893 z_erofs_pagevec_ctor_exit(&ctor, true);
896 compressed_pages = pcl->compressed_pages;
898 for (i = 0; i < pclusterpages; ++i) {
901 page = compressed_pages[i];
902 /* all compressed pages ought to be valid */
905 if (z_erofs_is_inline_pcluster(pcl)) {
906 if (!PageUptodate(page))
911 DBG_BUGON(z_erofs_page_is_invalidated(page));
912 if (!z_erofs_is_shortlived_page(page)) {
913 if (erofs_page_is_managed(sbi, page)) {
914 if (!PageUptodate(page))
920 * only if non-head page can be selected
921 * for inplace decompression
923 pagenr = z_erofs_onlinepage_index(page);
925 DBG_BUGON(pagenr >= nr_pages);
928 SetPageError(pages[pagenr]);
929 z_erofs_onlinepage_endio(pages[pagenr]);
932 pages[pagenr] = page;
937 /* PG_error needs checking for all non-managed pages */
938 if (PageError(page)) {
939 DBG_BUGON(PageUptodate(page));
947 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
948 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
950 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
952 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
956 if (z_erofs_is_inline_pcluster(pcl))
957 inputsize = pcl->tailpacking_size;
959 inputsize = pclusterpages * PAGE_SIZE;
961 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
963 .in = compressed_pages,
965 .pageofs_in = pcl->pageofs_in,
966 .pageofs_out = cl->pageofs,
967 .inputsize = inputsize,
968 .outputsize = outputsize,
969 .alg = pcl->algorithmformat,
970 .inplace_io = overlapped,
971 .partial_decoding = partial
975 /* must handle all compressed pages before actual file pages */
976 if (z_erofs_is_inline_pcluster(pcl)) {
977 page = compressed_pages[0];
978 WRITE_ONCE(compressed_pages[0], NULL);
981 for (i = 0; i < pclusterpages; ++i) {
982 page = compressed_pages[i];
984 if (erofs_page_is_managed(sbi, page))
987 /* recycle all individual short-lived pages */
988 (void)z_erofs_put_shortlivedpage(pagepool, page);
989 WRITE_ONCE(compressed_pages[i], NULL);
993 for (i = 0; i < nr_pages; ++i) {
998 DBG_BUGON(z_erofs_page_is_invalidated(page));
1000 /* recycle all individual short-lived pages */
1001 if (z_erofs_put_shortlivedpage(pagepool, page))
1007 z_erofs_onlinepage_endio(page);
1010 if (pages == z_pagemap_global)
1011 mutex_unlock(&z_pagemap_global_lock);
1012 else if (pages != pages_onstack)
1018 /* all cl locks MUST be taken before the following line */
1019 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1021 /* all cl locks SHOULD be released right now */
1022 mutex_unlock(&cl->lock);
1024 z_erofs_collection_put(cl);
1028 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1029 struct page **pagepool)
1031 z_erofs_next_pcluster_t owned = io->head;
1033 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1034 struct z_erofs_pcluster *pcl;
1036 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1037 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1039 /* no possible that 'owned' equals NULL */
1040 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1042 pcl = container_of(owned, struct z_erofs_pcluster, next);
1043 owned = READ_ONCE(pcl->next);
1045 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
1049 static void z_erofs_decompressqueue_work(struct work_struct *work)
1051 struct z_erofs_decompressqueue *bgq =
1052 container_of(work, struct z_erofs_decompressqueue, u.work);
1053 struct page *pagepool = NULL;
1055 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1056 z_erofs_decompress_queue(bgq, &pagepool);
1058 erofs_release_pages(&pagepool);
1062 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1063 bool sync, int bios)
1065 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1067 /* wake up the caller thread for sync decompression */
1069 unsigned long flags;
1071 spin_lock_irqsave(&io->u.wait.lock, flags);
1072 if (!atomic_add_return(bios, &io->pending_bios))
1073 wake_up_locked(&io->u.wait);
1074 spin_unlock_irqrestore(&io->u.wait.lock, flags);
1078 if (atomic_add_return(bios, &io->pending_bios))
1080 /* Use workqueue and sync decompression for atomic contexts only */
1081 if (in_atomic() || irqs_disabled()) {
1082 queue_work(z_erofs_workqueue, &io->u.work);
1083 /* enable sync decompression for readahead */
1084 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1085 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1088 z_erofs_decompressqueue_work(&io->u.work);
1091 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1093 struct page **pagepool,
1094 struct address_space *mc)
1096 const pgoff_t index = pcl->obj.index;
1097 gfp_t gfp = mapping_gfp_mask(mc);
1098 bool tocache = false;
1100 struct address_space *mapping;
1101 struct page *oldpage, *page;
1103 compressed_page_t t;
1107 page = READ_ONCE(pcl->compressed_pages[nr]);
1113 /* process the target tagged pointer */
1114 t = tagptr_init(compressed_page_t, page);
1115 justfound = tagptr_unfold_tags(t);
1116 page = tagptr_unfold_ptr(t);
1119 * preallocated cached pages, which is used to avoid direct reclaim
1120 * otherwise, it will go inplace I/O path instead.
1122 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1123 WRITE_ONCE(pcl->compressed_pages[nr], page);
1124 set_page_private(page, 0);
1128 mapping = READ_ONCE(page->mapping);
1131 * file-backed online pages in plcuster are all locked steady,
1132 * therefore it is impossible for `mapping' to be NULL.
1134 if (mapping && mapping != mc)
1135 /* ought to be unmanaged pages */
1138 /* directly return for shortlived page as well */
1139 if (z_erofs_is_shortlived_page(page))
1144 /* only true if page reclaim goes wrong, should never happen */
1145 DBG_BUGON(justfound && PagePrivate(page));
1147 /* the page is still in manage cache */
1148 if (page->mapping == mc) {
1149 WRITE_ONCE(pcl->compressed_pages[nr], page);
1151 ClearPageError(page);
1152 if (!PagePrivate(page)) {
1154 * impossible to be !PagePrivate(page) for
1155 * the current restriction as well if
1156 * the page is already in compressed_pages[].
1158 DBG_BUGON(!justfound);
1161 set_page_private(page, (unsigned long)pcl);
1162 SetPagePrivate(page);
1165 /* no need to submit io if it is already up-to-date */
1166 if (PageUptodate(page)) {
1174 * the managed page has been truncated, it's unsafe to
1175 * reuse this one, let's allocate a new cache-managed page.
1177 DBG_BUGON(page->mapping);
1178 DBG_BUGON(!justfound);
1184 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1185 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1186 erofs_pagepool_add(pagepool, page);
1191 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1192 /* turn into temporary page if fails (1 ref) */
1193 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1196 attach_page_private(page, pcl);
1197 /* drop a refcount added by allocpage (then we have 2 refs here) */
1200 out: /* the only exit (for tracing and debugging) */
1204 static struct z_erofs_decompressqueue *
1205 jobqueue_init(struct super_block *sb,
1206 struct z_erofs_decompressqueue *fgq, bool *fg)
1208 struct z_erofs_decompressqueue *q;
1211 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1216 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1220 init_waitqueue_head(&fgq->u.wait);
1221 atomic_set(&fgq->pending_bios, 0);
1224 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1228 /* define decompression jobqueue types */
1235 static void *jobqueueset_init(struct super_block *sb,
1236 struct z_erofs_decompressqueue *q[],
1237 struct z_erofs_decompressqueue *fgq, bool *fg)
1240 * if managed cache is enabled, bypass jobqueue is needed,
1241 * no need to read from device for all pclusters in this queue.
1243 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1244 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1246 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1249 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1250 z_erofs_next_pcluster_t qtail[],
1251 z_erofs_next_pcluster_t owned_head)
1253 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1254 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1256 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1257 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1258 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1260 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1262 WRITE_ONCE(*submit_qtail, owned_head);
1263 WRITE_ONCE(*bypass_qtail, &pcl->next);
1265 qtail[JQ_BYPASS] = &pcl->next;
1268 static void z_erofs_decompressqueue_endio(struct bio *bio)
1270 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
1271 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
1272 blk_status_t err = bio->bi_status;
1273 struct bio_vec *bvec;
1274 struct bvec_iter_all iter_all;
1276 bio_for_each_segment_all(bvec, bio, iter_all) {
1277 struct page *page = bvec->bv_page;
1279 DBG_BUGON(PageUptodate(page));
1280 DBG_BUGON(z_erofs_page_is_invalidated(page));
1285 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1287 SetPageUptodate(page);
1291 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
1295 static void z_erofs_submit_queue(struct super_block *sb,
1296 struct z_erofs_decompress_frontend *f,
1297 struct page **pagepool,
1298 struct z_erofs_decompressqueue *fgq,
1301 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1302 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1303 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1305 z_erofs_next_pcluster_t owned_head = f->owned_head;
1306 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1308 struct block_device *last_bdev;
1309 unsigned int nr_bios = 0;
1310 struct bio *bio = NULL;
1312 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1313 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1314 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1316 /* by default, all need io submission */
1317 q[JQ_SUBMIT]->head = owned_head;
1320 struct erofs_map_dev mdev;
1321 struct z_erofs_pcluster *pcl;
1326 /* no possible 'owned_head' equals the following */
1327 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1328 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1330 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1332 /* close the main owned chain at first */
1333 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1334 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1335 if (z_erofs_is_inline_pcluster(pcl)) {
1336 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1340 /* no device id here, thus it will always succeed */
1341 mdev = (struct erofs_map_dev) {
1342 .m_pa = blknr_to_addr(pcl->obj.index),
1344 (void)erofs_map_dev(sb, &mdev);
1346 cur = erofs_blknr(mdev.m_pa);
1347 end = cur + pcl->pclusterpages;
1352 page = pickup_page_for_submission(pcl, i++, pagepool,
1357 if (bio && (cur != last_index + 1 ||
1358 last_bdev != mdev.m_bdev)) {
1365 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1366 REQ_OP_READ, GFP_NOIO);
1367 bio->bi_end_io = z_erofs_decompressqueue_endio;
1369 last_bdev = mdev.m_bdev;
1370 bio->bi_iter.bi_sector = (sector_t)cur <<
1371 LOG_SECTORS_PER_BLOCK;
1372 bio->bi_private = bi_private;
1374 bio->bi_opf |= REQ_RAHEAD;
1378 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1379 goto submit_bio_retry;
1383 } while (++cur < end);
1386 qtail[JQ_SUBMIT] = &pcl->next;
1388 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1389 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1395 * although background is preferred, no one is pending for submission.
1396 * don't issue workqueue for decompression but drop it directly instead.
1398 if (!*force_fg && !nr_bios) {
1399 kvfree(q[JQ_SUBMIT]);
1402 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1405 static void z_erofs_runqueue(struct super_block *sb,
1406 struct z_erofs_decompress_frontend *f,
1407 struct page **pagepool, bool force_fg)
1409 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1411 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1413 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1415 /* handle bypass queue (no i/o pclusters) immediately */
1416 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1421 /* wait until all bios are completed */
1422 io_wait_event(io[JQ_SUBMIT].u.wait,
1423 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1425 /* handle synchronous decompress queue in the caller context */
1426 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1430 * Since partial uptodate is still unimplemented for now, we have to use
1431 * approximate readmore strategies as a start.
1433 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1434 struct readahead_control *rac,
1436 struct page **pagepool,
1439 struct inode *inode = f->inode;
1440 struct erofs_map_blocks *map = &f->map;
1446 err = z_erofs_map_blocks_iter(inode, map,
1447 EROFS_GET_BLOCKS_READMORE);
1451 /* expend ra for the trailing edge if readahead */
1453 loff_t newstart = readahead_pos(rac);
1455 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1456 readahead_expand(rac, newstart, cur - newstart);
1459 end = round_up(end, PAGE_SIZE);
1461 end = round_up(map->m_la, PAGE_SIZE);
1467 cur = map->m_la + map->m_llen - 1;
1468 while (cur >= end) {
1469 pgoff_t index = cur >> PAGE_SHIFT;
1472 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1476 if (PageUptodate(page)) {
1482 err = z_erofs_do_read_page(f, page, pagepool);
1484 erofs_err(inode->i_sb,
1485 "readmore error at page %lu @ nid %llu",
1486 index, EROFS_I(inode)->nid);
1489 if (cur < PAGE_SIZE)
1491 cur = (index << PAGE_SHIFT) - 1;
1495 static int z_erofs_readpage(struct file *file, struct page *page)
1497 struct inode *const inode = page->mapping->host;
1498 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1499 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1500 struct page *pagepool = NULL;
1503 trace_erofs_readpage(page, false);
1504 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1506 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1508 err = z_erofs_do_read_page(&f, page, &pagepool);
1509 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1511 (void)z_erofs_collector_end(&f);
1513 /* if some compressed cluster ready, need submit them anyway */
1514 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1515 z_erofs_get_sync_decompress_policy(sbi, 0));
1518 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1520 erofs_put_metabuf(&f.map.buf);
1521 erofs_release_pages(&pagepool);
1525 static void z_erofs_readahead(struct readahead_control *rac)
1527 struct inode *const inode = rac->mapping->host;
1528 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1529 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1530 struct page *pagepool = NULL, *head = NULL, *page;
1531 unsigned int nr_pages;
1534 f.headoffset = readahead_pos(rac);
1536 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1537 readahead_length(rac) - 1, &pagepool, true);
1538 nr_pages = readahead_count(rac);
1539 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1541 while ((page = readahead_page(rac))) {
1542 set_page_private(page, (unsigned long)head);
1547 struct page *page = head;
1550 /* traversal in reverse order */
1551 head = (void *)page_private(page);
1553 err = z_erofs_do_read_page(&f, page, &pagepool);
1555 erofs_err(inode->i_sb,
1556 "readahead error at page %lu @ nid %llu",
1557 page->index, EROFS_I(inode)->nid);
1560 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1561 (void)z_erofs_collector_end(&f);
1563 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1564 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1565 erofs_put_metabuf(&f.map.buf);
1566 erofs_release_pages(&pagepool);
1569 const struct address_space_operations z_erofs_aops = {
1570 .readpage = z_erofs_readpage,
1571 .readahead = z_erofs_readahead,