1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 #include <linux/prefetch.h>
11 #include <trace/events/erofs.h>
14 * a compressed_pages[] placeholder in order to avoid
15 * being filled with file pages for in-place decompression.
17 #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
19 /* how to allocate cached pages for a pcluster */
20 enum z_erofs_cache_alloctype {
21 DONTALLOC, /* don't allocate any cached pages */
22 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
26 * tagged pointer with 1-bit tag for all compressed pages
27 * tag 0 - the page is just found with an extra page reference
29 typedef tagptr1_t compressed_page_t;
31 #define tag_compressed_page_justfound(page) \
32 tagptr_fold(compressed_page_t, page, 1)
34 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
35 static struct kmem_cache *pcluster_cachep __read_mostly;
37 void z_erofs_exit_zip_subsystem(void)
39 destroy_workqueue(z_erofs_workqueue);
40 kmem_cache_destroy(pcluster_cachep);
43 static inline int z_erofs_init_workqueue(void)
45 const unsigned int onlinecpus = num_possible_cpus();
48 * no need to spawn too many threads, limiting threads could minimum
49 * scheduling overhead, perhaps per-CPU threads should be better?
51 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
52 WQ_UNBOUND | WQ_HIGHPRI,
53 onlinecpus + onlinecpus / 4);
54 return z_erofs_workqueue ? 0 : -ENOMEM;
57 static void z_erofs_pcluster_init_once(void *ptr)
59 struct z_erofs_pcluster *pcl = ptr;
60 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
63 mutex_init(&cl->lock);
66 for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
67 pcl->compressed_pages[i] = NULL;
70 int __init z_erofs_init_zip_subsystem(void)
72 pcluster_cachep = kmem_cache_create("erofs_compress",
73 Z_EROFS_WORKGROUP_SIZE, 0,
75 z_erofs_pcluster_init_once);
76 if (pcluster_cachep) {
77 if (!z_erofs_init_workqueue())
80 kmem_cache_destroy(pcluster_cachep);
85 enum z_erofs_collectmode {
89 * The current collection was the tail of an exist chain, in addition
90 * that the previous processed chained collections are all decided to
92 * A new chain will be created for the remaining collections which are
93 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
94 * the next collection cannot reuse the whole page safely in
95 * the following scenario:
96 * ________________________________________________________________
97 * | tail (partial) page | head (partial) page |
98 * | (belongs to the next cl) | (belongs to the current cl) |
99 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
101 COLLECT_PRIMARY_HOOKED,
102 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
104 * The current collection has been linked with the owned chain, and
105 * could also be linked with the remaining collections, which means
106 * if the processing page is the tail page of the collection, thus
107 * the current collection can safely use the whole page (since
108 * the previous collection is under control) for in-place I/O, as
110 * ________________________________________________________________
111 * | tail (partial) page | head (partial) page |
112 * | (of the current cl) | (of the previous collection) |
113 * | PRIMARY_FOLLOWED or | |
114 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
116 * [ (*) the above page can be used as inplace I/O. ]
118 COLLECT_PRIMARY_FOLLOWED,
121 struct z_erofs_collector {
122 struct z_erofs_pagevec_ctor vector;
124 struct z_erofs_pcluster *pcl, *tailpcl;
125 struct z_erofs_collection *cl;
126 struct page **compressedpages;
127 z_erofs_next_pcluster_t owned_head;
129 enum z_erofs_collectmode mode;
132 struct z_erofs_decompress_frontend {
133 struct inode *const inode;
135 struct z_erofs_collector clt;
136 struct erofs_map_blocks map;
139 /* used for applying cache strategy on the fly */
141 erofs_off_t headoffset;
144 #define COLLECTOR_INIT() { \
145 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
146 .mode = COLLECT_PRIMARY_FOLLOWED }
148 #define DECOMPRESS_FRONTEND_INIT(__i) { \
149 .inode = __i, .clt = COLLECTOR_INIT(), \
152 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
153 static DEFINE_MUTEX(z_pagemap_global_lock);
155 static void preload_compressed_pages(struct z_erofs_collector *clt,
156 struct address_space *mc,
157 enum z_erofs_cache_alloctype type)
159 const struct z_erofs_pcluster *pcl = clt->pcl;
160 const unsigned int clusterpages = BIT(pcl->clusterbits);
161 struct page **pages = clt->compressedpages;
162 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
163 bool standalone = true;
165 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
168 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
172 /* the compressed page was loaded before */
173 if (READ_ONCE(*pages))
176 page = find_get_page(mc, index);
179 t = tag_compressed_page_justfound(page);
180 } else if (type == DELAYEDALLOC) {
181 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
182 } else { /* DONTALLOC */
184 clt->compressedpages = pages;
189 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
196 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
197 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
200 /* called by erofs_shrinker to get rid of all compressed_pages */
201 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
202 struct erofs_workgroup *grp)
204 struct z_erofs_pcluster *const pcl =
205 container_of(grp, struct z_erofs_pcluster, obj);
206 struct address_space *const mapping = MNGD_MAPPING(sbi);
207 const unsigned int clusterpages = BIT(pcl->clusterbits);
211 * refcount of workgroup is now freezed as 1,
212 * therefore no need to worry about available decompression users.
214 for (i = 0; i < clusterpages; ++i) {
215 struct page *page = pcl->compressed_pages[i];
220 /* block other users from reclaiming or migrating the page */
221 if (!trylock_page(page))
224 if (page->mapping != mapping)
227 /* barrier is implied in the following 'unlock_page' */
228 WRITE_ONCE(pcl->compressed_pages[i], NULL);
229 set_page_private(page, 0);
230 ClearPagePrivate(page);
238 int erofs_try_to_free_cached_page(struct address_space *mapping,
241 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
242 const unsigned int clusterpages = BIT(pcl->clusterbits);
243 int ret = 0; /* 0 - busy */
245 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
248 for (i = 0; i < clusterpages; ++i) {
249 if (pcl->compressed_pages[i] == page) {
250 WRITE_ONCE(pcl->compressed_pages[i], NULL);
255 erofs_workgroup_unfreeze(&pcl->obj, 1);
258 ClearPagePrivate(page);
265 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
266 static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
269 struct z_erofs_pcluster *const pcl = clt->pcl;
270 const unsigned int clusterpages = BIT(pcl->clusterbits);
272 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
273 if (!cmpxchg(clt->compressedpages++, NULL, page))
279 /* callers must be with collection lock held */
280 static int z_erofs_attach_page(struct z_erofs_collector *clt,
281 struct page *page, enum z_erofs_page_type type,
286 /* give priority for inplaceio */
287 if (clt->mode >= COLLECT_PRIMARY &&
288 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
289 z_erofs_try_inplace_io(clt, page))
292 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
294 clt->cl->vcnt += (unsigned int)ret;
295 return ret ? 0 : -EAGAIN;
298 static enum z_erofs_collectmode
299 try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
300 z_erofs_next_pcluster_t *owned_head)
302 /* let's claim these following types of pclusters */
304 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
305 /* type 1, nil pcluster */
306 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
307 *owned_head) != Z_EROFS_PCLUSTER_NIL)
310 *owned_head = &pcl->next;
311 /* lucky, I am the followee :) */
312 return COLLECT_PRIMARY_FOLLOWED;
313 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
315 * type 2, link to the end of a existing open chain,
316 * be careful that its submission itself is governed
317 * by the original owned chain.
319 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
320 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
322 *owned_head = Z_EROFS_PCLUSTER_TAIL;
323 return COLLECT_PRIMARY_HOOKED;
325 return COLLECT_PRIMARY; /* :( better luck next time */
328 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
330 struct erofs_map_blocks *map)
332 struct z_erofs_pcluster *pcl = clt->pcl;
333 struct z_erofs_collection *cl;
336 /* to avoid unexpected loop formed by corrupted images */
337 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
339 return -EFSCORRUPTED;
342 cl = z_erofs_primarycollection(pcl);
343 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
345 return -EFSCORRUPTED;
348 length = READ_ONCE(pcl->length);
349 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
350 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
352 return -EFSCORRUPTED;
355 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
357 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
358 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
360 while (llen > length &&
361 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
363 length = READ_ONCE(pcl->length);
366 mutex_lock(&cl->lock);
367 /* used to check tail merging loop due to corrupted images */
368 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
370 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
371 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
372 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
378 static int z_erofs_register_collection(struct z_erofs_collector *clt,
380 struct erofs_map_blocks *map)
382 struct z_erofs_pcluster *pcl;
383 struct z_erofs_collection *cl;
384 struct erofs_workgroup *grp;
387 /* no available workgroup, let's allocate one */
388 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
392 atomic_set(&pcl->obj.refcount, 1);
393 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
395 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
396 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
397 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
399 if (map->m_flags & EROFS_MAP_ZIPPED)
400 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
402 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
404 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
405 pcl->clusterbits -= PAGE_SHIFT;
407 /* new pclusters should be claimed as type 1, primary and followed */
408 pcl->next = clt->owned_head;
409 clt->mode = COLLECT_PRIMARY_FOLLOWED;
411 cl = z_erofs_primarycollection(pcl);
413 /* must be cleaned before freeing to slab */
414 DBG_BUGON(cl->nr_pages);
417 cl->pageofs = map->m_la & ~PAGE_MASK;
420 * lock all primary followed works before visible to others
421 * and mutex_trylock *never* fails for a new pcluster.
423 DBG_BUGON(!mutex_trylock(&cl->lock));
425 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
431 if (grp != &pcl->obj) {
432 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
436 /* used to check tail merging loop due to corrupted images */
437 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
439 clt->owned_head = &pcl->next;
445 mutex_unlock(&cl->lock);
446 kmem_cache_free(pcluster_cachep, pcl);
450 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
452 struct erofs_map_blocks *map)
454 struct erofs_workgroup *grp;
459 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
460 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
461 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
463 if (!PAGE_ALIGNED(map->m_pa)) {
468 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
470 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
472 ret = z_erofs_register_collection(clt, inode, map);
480 ret = z_erofs_lookup_collection(clt, inode, map);
482 erofs_workgroup_put(&clt->pcl->obj);
487 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
488 clt->cl->pagevec, clt->cl->vcnt);
490 clt->compressedpages = clt->pcl->compressed_pages;
491 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
492 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
497 * keep in mind that no referenced pclusters will be freed
498 * only after a RCU grace period.
500 static void z_erofs_rcu_callback(struct rcu_head *head)
502 struct z_erofs_collection *const cl =
503 container_of(head, struct z_erofs_collection, rcu);
505 kmem_cache_free(pcluster_cachep,
506 container_of(cl, struct z_erofs_pcluster,
507 primary_collection));
510 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
512 struct z_erofs_pcluster *const pcl =
513 container_of(grp, struct z_erofs_pcluster, obj);
514 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
516 call_rcu(&cl->rcu, z_erofs_rcu_callback);
519 static void z_erofs_collection_put(struct z_erofs_collection *cl)
521 struct z_erofs_pcluster *const pcl =
522 container_of(cl, struct z_erofs_pcluster, primary_collection);
524 erofs_workgroup_put(&pcl->obj);
527 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
529 struct z_erofs_collection *cl = clt->cl;
534 z_erofs_pagevec_ctor_exit(&clt->vector, false);
535 mutex_unlock(&cl->lock);
538 * if all pending pages are added, don't hold its reference
539 * any longer if the pcluster isn't hosted by ourselves.
541 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
542 z_erofs_collection_put(cl);
548 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
549 unsigned int cachestrategy,
552 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
558 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
562 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
565 struct inode *const inode = fe->inode;
566 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
567 struct erofs_map_blocks *const map = &fe->map;
568 struct z_erofs_collector *const clt = &fe->clt;
569 const loff_t offset = page_offset(page);
572 enum z_erofs_cache_alloctype cache_strategy;
573 enum z_erofs_page_type page_type;
574 unsigned int cur, end, spiltted, index;
577 /* register locked file pages as online pages in pack */
578 z_erofs_onlinepage_init(page);
585 /* lucky, within the range of the current map_blocks */
586 if (offset + cur >= map->m_la &&
587 offset + cur < map->m_la + map->m_llen) {
588 /* didn't get a valid collection previously (very rare) */
594 /* go ahead the next map_blocks */
595 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
597 if (z_erofs_collector_end(clt))
598 fe->backmost = false;
600 map->m_la = offset + cur;
602 err = z_erofs_map_blocks_iter(inode, map, 0);
607 if (!(map->m_flags & EROFS_MAP_MAPPED))
610 err = z_erofs_collector_begin(clt, inode, map);
614 /* preload all compressed pages (maybe downgrade role if necessary) */
615 if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
616 cache_strategy = DELAYEDALLOC;
618 cache_strategy = DONTALLOC;
620 preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy);
624 * Ensure the current partial page belongs to this submit chain rather
625 * than other concurrent submit chains or the noio(bypass) chain since
626 * those chains are handled asynchronously thus the page cannot be used
627 * for inplace I/O or pagevec (should be processed in strict order.)
629 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
630 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
632 cur = end - min_t(erofs_off_t, offset + end - map->m_la, end);
633 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
634 zero_user_segment(page, cur, end);
640 /* let's derive page type */
641 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
642 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
643 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
644 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
647 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
650 err = z_erofs_attach_page(clt, page, page_type,
651 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
652 /* should allocate an additional staging page for pagevec */
653 if (err == -EAGAIN) {
654 struct page *const newpage =
655 alloc_page(GFP_NOFS | __GFP_NOFAIL);
657 newpage->mapping = Z_EROFS_MAPPING_STAGING;
658 err = z_erofs_attach_page(clt, newpage,
659 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
667 index = page->index - (map->m_la >> PAGE_SHIFT);
669 z_erofs_onlinepage_fixup(page, index, true);
671 /* bump up the number of spiltted parts of a page */
673 /* also update nr_pages */
674 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
676 /* can be used for verification */
677 map->m_llen = offset + cur - map->m_la;
684 z_erofs_onlinepage_endio(page);
686 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
687 __func__, page, spiltted, map->m_llen);
690 /* if some error occurred while processing this page */
696 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
699 /* wake up the caller thread for sync decompression */
703 spin_lock_irqsave(&io->u.wait.lock, flags);
704 if (!atomic_add_return(bios, &io->pending_bios))
705 wake_up_locked(&io->u.wait);
706 spin_unlock_irqrestore(&io->u.wait.lock, flags);
710 if (!atomic_add_return(bios, &io->pending_bios))
711 queue_work(z_erofs_workqueue, &io->u.work);
714 static void z_erofs_decompressqueue_endio(struct bio *bio)
716 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
717 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
718 blk_status_t err = bio->bi_status;
719 struct bio_vec *bvec;
720 struct bvec_iter_all iter_all;
722 bio_for_each_segment_all(bvec, bio, iter_all) {
723 struct page *page = bvec->bv_page;
725 DBG_BUGON(PageUptodate(page));
726 DBG_BUGON(!page->mapping);
731 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
733 SetPageUptodate(page);
737 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
741 static int z_erofs_decompress_pcluster(struct super_block *sb,
742 struct z_erofs_pcluster *pcl,
743 struct list_head *pagepool)
745 struct erofs_sb_info *const sbi = EROFS_SB(sb);
746 const unsigned int clusterpages = BIT(pcl->clusterbits);
747 struct z_erofs_pagevec_ctor ctor;
748 unsigned int i, outputsize, llen, nr_pages;
749 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
750 struct page **pages, **compressed_pages, *page;
752 enum z_erofs_page_type page_type;
753 bool overlapped, partial;
754 struct z_erofs_collection *cl;
758 cl = z_erofs_primarycollection(pcl);
759 DBG_BUGON(!READ_ONCE(cl->nr_pages));
761 mutex_lock(&cl->lock);
762 nr_pages = cl->nr_pages;
764 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
765 pages = pages_onstack;
766 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
767 mutex_trylock(&z_pagemap_global_lock)) {
768 pages = z_pagemap_global;
770 gfp_t gfp_flags = GFP_KERNEL;
772 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
773 gfp_flags |= __GFP_NOFAIL;
775 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
778 /* fallback to global pagemap for the lowmem scenario */
780 mutex_lock(&z_pagemap_global_lock);
781 pages = z_pagemap_global;
785 for (i = 0; i < nr_pages; ++i)
789 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
792 for (i = 0; i < cl->vcnt; ++i) {
795 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
797 /* all pages in pagevec ought to be valid */
799 DBG_BUGON(!page->mapping);
801 if (z_erofs_put_stagingpage(pagepool, page))
804 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
807 pagenr = z_erofs_onlinepage_index(page);
809 DBG_BUGON(pagenr >= nr_pages);
812 * currently EROFS doesn't support multiref(dedup),
813 * so here erroring out one multiref page.
817 SetPageError(pages[pagenr]);
818 z_erofs_onlinepage_endio(pages[pagenr]);
821 pages[pagenr] = page;
823 z_erofs_pagevec_ctor_exit(&ctor, true);
826 compressed_pages = pcl->compressed_pages;
828 for (i = 0; i < clusterpages; ++i) {
831 page = compressed_pages[i];
833 /* all compressed pages ought to be valid */
835 DBG_BUGON(!page->mapping);
837 if (!z_erofs_page_is_staging(page)) {
838 if (erofs_page_is_managed(sbi, page)) {
839 if (!PageUptodate(page))
845 * only if non-head page can be selected
846 * for inplace decompression
848 pagenr = z_erofs_onlinepage_index(page);
850 DBG_BUGON(pagenr >= nr_pages);
853 SetPageError(pages[pagenr]);
854 z_erofs_onlinepage_endio(pages[pagenr]);
857 pages[pagenr] = page;
862 /* PG_error needs checking for inplaced and staging pages */
863 if (PageError(page)) {
864 DBG_BUGON(PageUptodate(page));
872 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
873 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
875 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
877 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
881 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
883 .in = compressed_pages,
885 .pageofs_out = cl->pageofs,
886 .inputsize = PAGE_SIZE,
887 .outputsize = outputsize,
888 .alg = pcl->algorithmformat,
889 .inplace_io = overlapped,
890 .partial_decoding = partial
894 /* must handle all compressed pages before endding pages */
895 for (i = 0; i < clusterpages; ++i) {
896 page = compressed_pages[i];
898 if (erofs_page_is_managed(sbi, page))
901 /* recycle all individual staging pages */
902 (void)z_erofs_put_stagingpage(pagepool, page);
904 WRITE_ONCE(compressed_pages[i], NULL);
907 for (i = 0; i < nr_pages; ++i) {
912 DBG_BUGON(!page->mapping);
914 /* recycle all individual staging pages */
915 if (z_erofs_put_stagingpage(pagepool, page))
921 z_erofs_onlinepage_endio(page);
924 if (pages == z_pagemap_global)
925 mutex_unlock(&z_pagemap_global_lock);
926 else if (pages != pages_onstack)
932 /* all cl locks MUST be taken before the following line */
933 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
935 /* all cl locks SHOULD be released right now */
936 mutex_unlock(&cl->lock);
938 z_erofs_collection_put(cl);
942 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
943 struct list_head *pagepool)
945 z_erofs_next_pcluster_t owned = io->head;
947 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
948 struct z_erofs_pcluster *pcl;
950 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
951 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
953 /* no possible that 'owned' equals NULL */
954 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
956 pcl = container_of(owned, struct z_erofs_pcluster, next);
957 owned = READ_ONCE(pcl->next);
959 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
963 static void z_erofs_decompressqueue_work(struct work_struct *work)
965 struct z_erofs_decompressqueue *bgq =
966 container_of(work, struct z_erofs_decompressqueue, u.work);
969 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
970 z_erofs_decompress_queue(bgq, &pagepool);
972 put_pages_list(&pagepool);
976 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
978 struct list_head *pagepool,
979 struct address_space *mc,
982 const pgoff_t index = pcl->obj.index;
983 bool tocache = false;
985 struct address_space *mapping;
986 struct page *oldpage, *page;
992 page = READ_ONCE(pcl->compressed_pages[nr]);
999 * the cached page has not been allocated and
1000 * an placeholder is out there, prepare it now.
1002 if (page == PAGE_UNALLOCATED) {
1007 /* process the target tagged pointer */
1008 t = tagptr_init(compressed_page_t, page);
1009 justfound = tagptr_unfold_tags(t);
1010 page = tagptr_unfold_ptr(t);
1012 mapping = READ_ONCE(page->mapping);
1015 * unmanaged (file) pages are all locked solidly,
1016 * therefore it is impossible for `mapping' to be NULL.
1018 if (mapping && mapping != mc)
1019 /* ought to be unmanaged pages */
1024 /* only true if page reclaim goes wrong, should never happen */
1025 DBG_BUGON(justfound && PagePrivate(page));
1027 /* the page is still in manage cache */
1028 if (page->mapping == mc) {
1029 WRITE_ONCE(pcl->compressed_pages[nr], page);
1031 ClearPageError(page);
1032 if (!PagePrivate(page)) {
1034 * impossible to be !PagePrivate(page) for
1035 * the current restriction as well if
1036 * the page is already in compressed_pages[].
1038 DBG_BUGON(!justfound);
1041 set_page_private(page, (unsigned long)pcl);
1042 SetPagePrivate(page);
1045 /* no need to submit io if it is already up-to-date */
1046 if (PageUptodate(page)) {
1054 * the managed page has been truncated, it's unsafe to
1055 * reuse this one, let's allocate a new cache-managed page.
1057 DBG_BUGON(page->mapping);
1058 DBG_BUGON(!justfound);
1064 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1065 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1066 /* non-LRU / non-movable temporary page is needed */
1067 page->mapping = Z_EROFS_MAPPING_STAGING;
1071 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1073 /* since it added to managed cache successfully */
1077 list_add(&page->lru, pagepool);
1084 set_page_private(page, (unsigned long)pcl);
1085 SetPagePrivate(page);
1087 out: /* the only exit (for tracing and debugging) */
1091 static struct z_erofs_decompressqueue *
1092 jobqueue_init(struct super_block *sb,
1093 struct z_erofs_decompressqueue *fgq, bool *fg)
1095 struct z_erofs_decompressqueue *q;
1098 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1103 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1107 init_waitqueue_head(&fgq->u.wait);
1108 atomic_set(&fgq->pending_bios, 0);
1111 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1115 /* define decompression jobqueue types */
1122 static void *jobqueueset_init(struct super_block *sb,
1123 struct z_erofs_decompressqueue *q[],
1124 struct z_erofs_decompressqueue *fgq, bool *fg)
1127 * if managed cache is enabled, bypass jobqueue is needed,
1128 * no need to read from device for all pclusters in this queue.
1130 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1131 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1133 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1136 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1137 z_erofs_next_pcluster_t qtail[],
1138 z_erofs_next_pcluster_t owned_head)
1140 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1141 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1143 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1144 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1145 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1147 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1149 WRITE_ONCE(*submit_qtail, owned_head);
1150 WRITE_ONCE(*bypass_qtail, &pcl->next);
1152 qtail[JQ_BYPASS] = &pcl->next;
1155 static void z_erofs_submit_queue(struct super_block *sb,
1156 struct z_erofs_decompress_frontend *f,
1157 struct list_head *pagepool,
1158 struct z_erofs_decompressqueue *fgq,
1161 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1162 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1163 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1165 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1166 /* since bio will be NULL, no need to initialize last_index */
1168 unsigned int nr_bios = 0;
1169 struct bio *bio = NULL;
1171 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1172 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1173 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1175 /* by default, all need io submission */
1176 q[JQ_SUBMIT]->head = owned_head;
1179 struct z_erofs_pcluster *pcl;
1184 /* no possible 'owned_head' equals the following */
1185 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1186 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1188 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1190 cur = pcl->obj.index;
1191 end = cur + BIT(pcl->clusterbits);
1193 /* close the main owned chain at first */
1194 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1195 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1200 page = pickup_page_for_submission(pcl, i++, pagepool,
1206 if (bio && cur != last_index + 1) {
1213 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1215 bio->bi_end_io = z_erofs_decompressqueue_endio;
1216 bio_set_dev(bio, sb->s_bdev);
1217 bio->bi_iter.bi_sector = (sector_t)cur <<
1218 LOG_SECTORS_PER_BLOCK;
1219 bio->bi_private = bi_private;
1220 bio->bi_opf = REQ_OP_READ;
1222 bio->bi_opf |= REQ_RAHEAD;
1226 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1227 goto submit_bio_retry;
1231 } while (++cur < end);
1234 qtail[JQ_SUBMIT] = &pcl->next;
1236 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1237 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1243 * although background is preferred, no one is pending for submission.
1244 * don't issue workqueue for decompression but drop it directly instead.
1246 if (!*force_fg && !nr_bios) {
1247 kvfree(q[JQ_SUBMIT]);
1250 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1253 static void z_erofs_runqueue(struct super_block *sb,
1254 struct z_erofs_decompress_frontend *f,
1255 struct list_head *pagepool, bool force_fg)
1257 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1259 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1261 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1263 /* handle bypass queue (no i/o pclusters) immediately */
1264 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1269 /* wait until all bios are completed */
1270 io_wait_event(io[JQ_SUBMIT].u.wait,
1271 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1273 /* handle synchronous decompress queue in the caller context */
1274 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1277 static int z_erofs_readpage(struct file *file, struct page *page)
1279 struct inode *const inode = page->mapping->host;
1280 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1282 LIST_HEAD(pagepool);
1284 trace_erofs_readpage(page, false);
1286 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1288 err = z_erofs_do_read_page(&f, page);
1289 (void)z_erofs_collector_end(&f.clt);
1291 /* if some compressed cluster ready, need submit them anyway */
1292 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1295 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1298 put_page(f.map.mpage);
1300 /* clean up the remaining free pages */
1301 put_pages_list(&pagepool);
1305 static void z_erofs_readahead(struct readahead_control *rac)
1307 struct inode *const inode = rac->mapping->host;
1308 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1310 unsigned int nr_pages = readahead_count(rac);
1311 bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
1312 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1313 struct page *page, *head = NULL;
1314 LIST_HEAD(pagepool);
1316 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1319 f.headoffset = readahead_pos(rac);
1321 while ((page = readahead_page(rac))) {
1322 prefetchw(&page->flags);
1325 * A pure asynchronous readahead is indicated if
1326 * a PG_readahead marked page is hitted at first.
1327 * Let's also do asynchronous decompression for this case.
1329 sync &= !(PageReadahead(page) && !head);
1331 set_page_private(page, (unsigned long)head);
1336 struct page *page = head;
1339 /* traversal in reverse order */
1340 head = (void *)page_private(page);
1342 err = z_erofs_do_read_page(&f, page);
1344 erofs_err(inode->i_sb,
1345 "readahead error at page %lu @ nid %llu",
1346 page->index, EROFS_I(inode)->nid);
1350 (void)z_erofs_collector_end(&f.clt);
1352 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
1355 put_page(f.map.mpage);
1357 /* clean up the remaining free pages */
1358 put_pages_list(&pagepool);
1361 const struct address_space_operations z_erofs_aops = {
1362 .readpage = z_erofs_readpage,
1363 .readahead = z_erofs_readahead,