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 if (!atomic_add_return(bios, &io->pending_bios))
1070 complete(&io->u.done);
1075 if (atomic_add_return(bios, &io->pending_bios))
1077 /* Use workqueue and sync decompression for atomic contexts only */
1078 if (in_atomic() || irqs_disabled()) {
1079 queue_work(z_erofs_workqueue, &io->u.work);
1080 /* enable sync decompression for readahead */
1081 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1082 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1085 z_erofs_decompressqueue_work(&io->u.work);
1088 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1090 struct page **pagepool,
1091 struct address_space *mc)
1093 const pgoff_t index = pcl->obj.index;
1094 gfp_t gfp = mapping_gfp_mask(mc);
1095 bool tocache = false;
1097 struct address_space *mapping;
1098 struct page *oldpage, *page;
1100 compressed_page_t t;
1104 page = READ_ONCE(pcl->compressed_pages[nr]);
1110 /* process the target tagged pointer */
1111 t = tagptr_init(compressed_page_t, page);
1112 justfound = tagptr_unfold_tags(t);
1113 page = tagptr_unfold_ptr(t);
1116 * preallocated cached pages, which is used to avoid direct reclaim
1117 * otherwise, it will go inplace I/O path instead.
1119 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1120 WRITE_ONCE(pcl->compressed_pages[nr], page);
1121 set_page_private(page, 0);
1125 mapping = READ_ONCE(page->mapping);
1128 * file-backed online pages in plcuster are all locked steady,
1129 * therefore it is impossible for `mapping' to be NULL.
1131 if (mapping && mapping != mc)
1132 /* ought to be unmanaged pages */
1135 /* directly return for shortlived page as well */
1136 if (z_erofs_is_shortlived_page(page))
1141 /* only true if page reclaim goes wrong, should never happen */
1142 DBG_BUGON(justfound && PagePrivate(page));
1144 /* the page is still in manage cache */
1145 if (page->mapping == mc) {
1146 WRITE_ONCE(pcl->compressed_pages[nr], page);
1148 ClearPageError(page);
1149 if (!PagePrivate(page)) {
1151 * impossible to be !PagePrivate(page) for
1152 * the current restriction as well if
1153 * the page is already in compressed_pages[].
1155 DBG_BUGON(!justfound);
1158 set_page_private(page, (unsigned long)pcl);
1159 SetPagePrivate(page);
1162 /* no need to submit io if it is already up-to-date */
1163 if (PageUptodate(page)) {
1171 * the managed page has been truncated, it's unsafe to
1172 * reuse this one, let's allocate a new cache-managed page.
1174 DBG_BUGON(page->mapping);
1175 DBG_BUGON(!justfound);
1181 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1182 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1183 erofs_pagepool_add(pagepool, page);
1188 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1189 /* turn into temporary page if fails (1 ref) */
1190 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1193 attach_page_private(page, pcl);
1194 /* drop a refcount added by allocpage (then we have 2 refs here) */
1197 out: /* the only exit (for tracing and debugging) */
1201 static struct z_erofs_decompressqueue *
1202 jobqueue_init(struct super_block *sb,
1203 struct z_erofs_decompressqueue *fgq, bool *fg)
1205 struct z_erofs_decompressqueue *q;
1208 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1213 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1217 init_completion(&fgq->u.done);
1218 atomic_set(&fgq->pending_bios, 0);
1221 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1225 /* define decompression jobqueue types */
1232 static void *jobqueueset_init(struct super_block *sb,
1233 struct z_erofs_decompressqueue *q[],
1234 struct z_erofs_decompressqueue *fgq, bool *fg)
1237 * if managed cache is enabled, bypass jobqueue is needed,
1238 * no need to read from device for all pclusters in this queue.
1240 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1241 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1243 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1246 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1247 z_erofs_next_pcluster_t qtail[],
1248 z_erofs_next_pcluster_t owned_head)
1250 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1251 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1253 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1254 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1255 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1257 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1259 WRITE_ONCE(*submit_qtail, owned_head);
1260 WRITE_ONCE(*bypass_qtail, &pcl->next);
1262 qtail[JQ_BYPASS] = &pcl->next;
1265 static void z_erofs_decompressqueue_endio(struct bio *bio)
1267 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
1268 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
1269 blk_status_t err = bio->bi_status;
1270 struct bio_vec *bvec;
1271 struct bvec_iter_all iter_all;
1273 bio_for_each_segment_all(bvec, bio, iter_all) {
1274 struct page *page = bvec->bv_page;
1276 DBG_BUGON(PageUptodate(page));
1277 DBG_BUGON(z_erofs_page_is_invalidated(page));
1282 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1284 SetPageUptodate(page);
1288 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
1292 static void z_erofs_submit_queue(struct super_block *sb,
1293 struct z_erofs_decompress_frontend *f,
1294 struct page **pagepool,
1295 struct z_erofs_decompressqueue *fgq,
1298 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1299 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1300 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1302 z_erofs_next_pcluster_t owned_head = f->owned_head;
1303 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1305 struct block_device *last_bdev;
1306 unsigned int nr_bios = 0;
1307 struct bio *bio = NULL;
1309 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1310 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1311 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1313 /* by default, all need io submission */
1314 q[JQ_SUBMIT]->head = owned_head;
1317 struct erofs_map_dev mdev;
1318 struct z_erofs_pcluster *pcl;
1323 /* no possible 'owned_head' equals the following */
1324 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1325 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1327 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1329 /* close the main owned chain at first */
1330 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1331 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1332 if (z_erofs_is_inline_pcluster(pcl)) {
1333 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1337 /* no device id here, thus it will always succeed */
1338 mdev = (struct erofs_map_dev) {
1339 .m_pa = blknr_to_addr(pcl->obj.index),
1341 (void)erofs_map_dev(sb, &mdev);
1343 cur = erofs_blknr(mdev.m_pa);
1344 end = cur + pcl->pclusterpages;
1349 page = pickup_page_for_submission(pcl, i++, pagepool,
1354 if (bio && (cur != last_index + 1 ||
1355 last_bdev != mdev.m_bdev)) {
1362 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1363 REQ_OP_READ, GFP_NOIO);
1364 bio->bi_end_io = z_erofs_decompressqueue_endio;
1366 last_bdev = mdev.m_bdev;
1367 bio->bi_iter.bi_sector = (sector_t)cur <<
1368 LOG_SECTORS_PER_BLOCK;
1369 bio->bi_private = bi_private;
1371 bio->bi_opf |= REQ_RAHEAD;
1375 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1376 goto submit_bio_retry;
1380 } while (++cur < end);
1383 qtail[JQ_SUBMIT] = &pcl->next;
1385 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1386 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1392 * although background is preferred, no one is pending for submission.
1393 * don't issue workqueue for decompression but drop it directly instead.
1395 if (!*force_fg && !nr_bios) {
1396 kvfree(q[JQ_SUBMIT]);
1399 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1402 static void z_erofs_runqueue(struct super_block *sb,
1403 struct z_erofs_decompress_frontend *f,
1404 struct page **pagepool, bool force_fg)
1406 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1408 if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
1410 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1412 /* handle bypass queue (no i/o pclusters) immediately */
1413 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1418 /* wait until all bios are completed */
1419 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1421 /* handle synchronous decompress queue in the caller context */
1422 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1426 * Since partial uptodate is still unimplemented for now, we have to use
1427 * approximate readmore strategies as a start.
1429 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1430 struct readahead_control *rac,
1432 struct page **pagepool,
1435 struct inode *inode = f->inode;
1436 struct erofs_map_blocks *map = &f->map;
1442 err = z_erofs_map_blocks_iter(inode, map,
1443 EROFS_GET_BLOCKS_READMORE);
1447 /* expend ra for the trailing edge if readahead */
1449 loff_t newstart = readahead_pos(rac);
1451 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1452 readahead_expand(rac, newstart, cur - newstart);
1455 end = round_up(end, PAGE_SIZE);
1457 end = round_up(map->m_la, PAGE_SIZE);
1463 cur = map->m_la + map->m_llen - 1;
1464 while (cur >= end) {
1465 pgoff_t index = cur >> PAGE_SHIFT;
1468 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1472 if (PageUptodate(page)) {
1478 err = z_erofs_do_read_page(f, page, pagepool);
1480 erofs_err(inode->i_sb,
1481 "readmore error at page %lu @ nid %llu",
1482 index, EROFS_I(inode)->nid);
1485 if (cur < PAGE_SIZE)
1487 cur = (index << PAGE_SHIFT) - 1;
1491 static int z_erofs_readpage(struct file *file, struct page *page)
1493 struct inode *const inode = page->mapping->host;
1494 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1495 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1496 struct page *pagepool = NULL;
1499 trace_erofs_readpage(page, false);
1500 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1502 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1504 err = z_erofs_do_read_page(&f, page, &pagepool);
1505 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1507 (void)z_erofs_collector_end(&f);
1509 /* if some compressed cluster ready, need submit them anyway */
1510 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1511 z_erofs_get_sync_decompress_policy(sbi, 0));
1514 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1516 erofs_put_metabuf(&f.map.buf);
1517 erofs_release_pages(&pagepool);
1521 static void z_erofs_readahead(struct readahead_control *rac)
1523 struct inode *const inode = rac->mapping->host;
1524 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1525 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1526 struct page *pagepool = NULL, *head = NULL, *page;
1527 unsigned int nr_pages;
1530 f.headoffset = readahead_pos(rac);
1532 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1533 readahead_length(rac) - 1, &pagepool, true);
1534 nr_pages = readahead_count(rac);
1535 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1537 while ((page = readahead_page(rac))) {
1538 set_page_private(page, (unsigned long)head);
1543 struct page *page = head;
1546 /* traversal in reverse order */
1547 head = (void *)page_private(page);
1549 err = z_erofs_do_read_page(&f, page, &pagepool);
1551 erofs_err(inode->i_sb,
1552 "readahead error at page %lu @ nid %llu",
1553 page->index, EROFS_I(inode)->nid);
1556 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1557 (void)z_erofs_collector_end(&f);
1559 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1560 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1561 erofs_put_metabuf(&f.map.buf);
1562 erofs_release_pages(&pagepool);
1565 const struct address_space_operations z_erofs_aops = {
1566 .readpage = z_erofs_readpage,
1567 .readahead = z_erofs_readahead,