1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * http://www.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();
46 const unsigned int flags = WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE;
49 * no need to spawn too many threads, limiting threads could minimum
50 * scheduling overhead, perhaps per-CPU threads should be better?
52 z_erofs_workqueue = alloc_workqueue("erofs_unzipd", flags,
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 static void z_erofs_pcluster_init_always(struct z_erofs_pcluster *pcl)
72 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
74 atomic_set(&pcl->obj.refcount, 1);
76 DBG_BUGON(cl->nr_pages);
80 int __init z_erofs_init_zip_subsystem(void)
82 pcluster_cachep = kmem_cache_create("erofs_compress",
83 Z_EROFS_WORKGROUP_SIZE, 0,
85 z_erofs_pcluster_init_once);
86 if (pcluster_cachep) {
87 if (!z_erofs_init_workqueue())
90 kmem_cache_destroy(pcluster_cachep);
95 enum z_erofs_collectmode {
99 * The current collection was the tail of an exist chain, in addition
100 * that the previous processed chained collections are all decided to
101 * be hooked up to it.
102 * A new chain will be created for the remaining collections which are
103 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
104 * the next collection cannot reuse the whole page safely in
105 * the following scenario:
106 * ________________________________________________________________
107 * | tail (partial) page | head (partial) page |
108 * | (belongs to the next cl) | (belongs to the current cl) |
109 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
111 COLLECT_PRIMARY_HOOKED,
112 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
114 * The current collection has been linked with the owned chain, and
115 * could also be linked with the remaining collections, which means
116 * if the processing page is the tail page of the collection, thus
117 * the current collection can safely use the whole page (since
118 * the previous collection is under control) for in-place I/O, as
120 * ________________________________________________________________
121 * | tail (partial) page | head (partial) page |
122 * | (of the current cl) | (of the previous collection) |
123 * | PRIMARY_FOLLOWED or | |
124 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
126 * [ (*) the above page can be used as inplace I/O. ]
128 COLLECT_PRIMARY_FOLLOWED,
131 struct z_erofs_collector {
132 struct z_erofs_pagevec_ctor vector;
134 struct z_erofs_pcluster *pcl, *tailpcl;
135 struct z_erofs_collection *cl;
136 struct page **compressedpages;
137 z_erofs_next_pcluster_t owned_head;
139 enum z_erofs_collectmode mode;
142 struct z_erofs_decompress_frontend {
143 struct inode *const inode;
145 struct z_erofs_collector clt;
146 struct erofs_map_blocks map;
148 /* used for applying cache strategy on the fly */
150 erofs_off_t headoffset;
153 #define COLLECTOR_INIT() { \
154 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
155 .mode = COLLECT_PRIMARY_FOLLOWED }
157 #define DECOMPRESS_FRONTEND_INIT(__i) { \
158 .inode = __i, .clt = COLLECTOR_INIT(), \
161 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
162 static DEFINE_MUTEX(z_pagemap_global_lock);
164 static void preload_compressed_pages(struct z_erofs_collector *clt,
165 struct address_space *mc,
166 enum z_erofs_cache_alloctype type,
167 struct list_head *pagepool)
169 const struct z_erofs_pcluster *pcl = clt->pcl;
170 const unsigned int clusterpages = BIT(pcl->clusterbits);
171 struct page **pages = clt->compressedpages;
172 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
173 bool standalone = true;
175 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
178 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
182 /* the compressed page was loaded before */
183 if (READ_ONCE(*pages))
186 page = find_get_page(mc, index);
189 t = tag_compressed_page_justfound(page);
190 } else if (type == DELAYEDALLOC) {
191 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
192 } else { /* DONTALLOC */
194 clt->compressedpages = pages;
199 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
206 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
207 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
210 /* called by erofs_shrinker to get rid of all compressed_pages */
211 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
212 struct erofs_workgroup *grp)
214 struct z_erofs_pcluster *const pcl =
215 container_of(grp, struct z_erofs_pcluster, obj);
216 struct address_space *const mapping = MNGD_MAPPING(sbi);
217 const unsigned int clusterpages = BIT(pcl->clusterbits);
221 * refcount of workgroup is now freezed as 1,
222 * therefore no need to worry about available decompression users.
224 for (i = 0; i < clusterpages; ++i) {
225 struct page *page = pcl->compressed_pages[i];
230 /* block other users from reclaiming or migrating the page */
231 if (!trylock_page(page))
234 if (page->mapping != mapping)
237 /* barrier is implied in the following 'unlock_page' */
238 WRITE_ONCE(pcl->compressed_pages[i], NULL);
239 set_page_private(page, 0);
240 ClearPagePrivate(page);
248 int erofs_try_to_free_cached_page(struct address_space *mapping,
251 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
252 const unsigned int clusterpages = BIT(pcl->clusterbits);
253 int ret = 0; /* 0 - busy */
255 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
258 for (i = 0; i < clusterpages; ++i) {
259 if (pcl->compressed_pages[i] == page) {
260 WRITE_ONCE(pcl->compressed_pages[i], NULL);
265 erofs_workgroup_unfreeze(&pcl->obj, 1);
268 ClearPagePrivate(page);
275 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
276 static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
279 struct z_erofs_pcluster *const pcl = clt->pcl;
280 const unsigned int clusterpages = BIT(pcl->clusterbits);
282 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
283 if (!cmpxchg(clt->compressedpages++, NULL, page))
289 /* callers must be with collection lock held */
290 static int z_erofs_attach_page(struct z_erofs_collector *clt,
292 enum z_erofs_page_type type)
297 /* give priority for inplaceio */
298 if (clt->mode >= COLLECT_PRIMARY &&
299 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
300 z_erofs_try_inplace_io(clt, page))
303 ret = z_erofs_pagevec_enqueue(&clt->vector,
304 page, type, &occupied);
305 clt->cl->vcnt += (unsigned int)ret;
307 return ret ? 0 : -EAGAIN;
310 static enum z_erofs_collectmode
311 try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
312 z_erofs_next_pcluster_t *owned_head)
314 /* let's claim these following types of pclusters */
316 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
317 /* type 1, nil pcluster */
318 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
319 *owned_head) != Z_EROFS_PCLUSTER_NIL)
322 *owned_head = &pcl->next;
323 /* lucky, I am the followee :) */
324 return COLLECT_PRIMARY_FOLLOWED;
325 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
327 * type 2, link to the end of a existing open chain,
328 * be careful that its submission itself is governed
329 * by the original owned chain.
331 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
332 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
334 *owned_head = Z_EROFS_PCLUSTER_TAIL;
335 return COLLECT_PRIMARY_HOOKED;
337 return COLLECT_PRIMARY; /* :( better luck next time */
340 static struct z_erofs_collection *cllookup(struct z_erofs_collector *clt,
342 struct erofs_map_blocks *map)
344 struct erofs_workgroup *grp;
345 struct z_erofs_pcluster *pcl;
346 struct z_erofs_collection *cl;
350 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT, &tag);
354 pcl = container_of(grp, struct z_erofs_pcluster, obj);
355 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
357 erofs_workgroup_put(grp);
358 return ERR_PTR(-EFSCORRUPTED);
361 cl = z_erofs_primarycollection(pcl);
362 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
364 erofs_workgroup_put(grp);
365 return ERR_PTR(-EFSCORRUPTED);
368 length = READ_ONCE(pcl->length);
369 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
370 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
372 erofs_workgroup_put(grp);
373 return ERR_PTR(-EFSCORRUPTED);
376 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
378 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
379 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
381 while (llen > length &&
382 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
384 length = READ_ONCE(pcl->length);
387 mutex_lock(&cl->lock);
388 /* used to check tail merging loop due to corrupted images */
389 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
391 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
392 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
393 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
400 static struct z_erofs_collection *clregister(struct z_erofs_collector *clt,
402 struct erofs_map_blocks *map)
404 struct z_erofs_pcluster *pcl;
405 struct z_erofs_collection *cl;
408 /* no available workgroup, let's allocate one */
409 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
411 return ERR_PTR(-ENOMEM);
413 z_erofs_pcluster_init_always(pcl);
414 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
416 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
417 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
418 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
420 if (map->m_flags & EROFS_MAP_ZIPPED)
421 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
423 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
425 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
426 pcl->clusterbits -= PAGE_SHIFT;
428 /* new pclusters should be claimed as type 1, primary and followed */
429 pcl->next = clt->owned_head;
430 clt->mode = COLLECT_PRIMARY_FOLLOWED;
432 cl = z_erofs_primarycollection(pcl);
433 cl->pageofs = map->m_la & ~PAGE_MASK;
436 * lock all primary followed works before visible to others
437 * and mutex_trylock *never* fails for a new pcluster.
439 mutex_trylock(&cl->lock);
441 err = erofs_register_workgroup(inode->i_sb, &pcl->obj, 0);
443 mutex_unlock(&cl->lock);
444 kmem_cache_free(pcluster_cachep, pcl);
445 return ERR_PTR(-EAGAIN);
447 /* used to check tail merging loop due to corrupted images */
448 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
450 clt->owned_head = &pcl->next;
456 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
458 struct erofs_map_blocks *map)
460 struct z_erofs_collection *cl;
464 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
465 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
466 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
468 if (!PAGE_ALIGNED(map->m_pa)) {
474 cl = cllookup(clt, inode, map);
476 cl = clregister(clt, inode, map);
478 if (cl == ERR_PTR(-EAGAIN))
485 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
486 cl->pagevec, cl->vcnt);
488 clt->compressedpages = clt->pcl->compressed_pages;
489 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
490 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
495 * keep in mind that no referenced pclusters will be freed
496 * only after a RCU grace period.
498 static void z_erofs_rcu_callback(struct rcu_head *head)
500 struct z_erofs_collection *const cl =
501 container_of(head, struct z_erofs_collection, rcu);
503 kmem_cache_free(pcluster_cachep,
504 container_of(cl, struct z_erofs_pcluster,
505 primary_collection));
508 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
510 struct z_erofs_pcluster *const pcl =
511 container_of(grp, struct z_erofs_pcluster, obj);
512 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
514 call_rcu(&cl->rcu, z_erofs_rcu_callback);
517 static void z_erofs_collection_put(struct z_erofs_collection *cl)
519 struct z_erofs_pcluster *const pcl =
520 container_of(cl, struct z_erofs_pcluster, primary_collection);
522 erofs_workgroup_put(&pcl->obj);
525 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
527 struct z_erofs_collection *cl = clt->cl;
532 z_erofs_pagevec_ctor_exit(&clt->vector, false);
533 mutex_unlock(&cl->lock);
536 * if all pending pages are added, don't hold its reference
537 * any longer if the pcluster isn't hosted by ourselves.
539 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
540 z_erofs_collection_put(cl);
546 static inline struct page *__stagingpage_alloc(struct list_head *pagepool,
549 struct page *page = erofs_allocpage(pagepool, gfp, true);
551 page->mapping = Z_EROFS_MAPPING_STAGING;
555 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
556 unsigned int cachestrategy,
559 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
565 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
569 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
571 struct list_head *pagepool)
573 struct inode *const inode = fe->inode;
574 struct erofs_sb_info *const sbi __maybe_unused = EROFS_I_SB(inode);
575 struct erofs_map_blocks *const map = &fe->map;
576 struct z_erofs_collector *const clt = &fe->clt;
577 const loff_t offset = page_offset(page);
580 enum z_erofs_cache_alloctype cache_strategy;
581 enum z_erofs_page_type page_type;
582 unsigned int cur, end, spiltted, index;
585 /* register locked file pages as online pages in pack */
586 z_erofs_onlinepage_init(page);
593 /* lucky, within the range of the current map_blocks */
594 if (offset + cur >= map->m_la &&
595 offset + cur < map->m_la + map->m_llen) {
596 /* didn't get a valid collection previously (very rare) */
602 /* go ahead the next map_blocks */
603 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
605 if (z_erofs_collector_end(clt))
606 fe->backmost = false;
608 map->m_la = offset + cur;
610 err = z_erofs_map_blocks_iter(inode, map, 0);
615 if (!(map->m_flags & EROFS_MAP_MAPPED))
618 err = z_erofs_collector_begin(clt, inode, map);
622 /* preload all compressed pages (maybe downgrade role if necessary) */
623 if (should_alloc_managed_pages(fe, sbi->cache_strategy, map->m_la))
624 cache_strategy = DELAYEDALLOC;
626 cache_strategy = DONTALLOC;
628 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
629 cache_strategy, pagepool);
633 * Ensure the current partial page belongs to this submit chain rather
634 * than other concurrent submit chains or the noio(bypass) chain since
635 * those chains are handled asynchronously thus the page cannot be used
636 * for inplace I/O or pagevec (should be processed in strict order.)
638 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
639 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
641 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
642 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
643 zero_user_segment(page, cur, end);
647 /* let's derive page type */
648 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
649 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
650 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
651 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
654 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
657 err = z_erofs_attach_page(clt, page, page_type);
658 /* should allocate an additional staging page for pagevec */
659 if (err == -EAGAIN) {
660 struct page *const newpage =
661 __stagingpage_alloc(pagepool, GFP_NOFS);
663 err = z_erofs_attach_page(clt, newpage,
664 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
672 index = page->index - (map->m_la >> PAGE_SHIFT);
674 z_erofs_onlinepage_fixup(page, index, true);
676 /* bump up the number of spiltted parts of a page */
678 /* also update nr_pages */
679 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
681 /* can be used for verification */
682 map->m_llen = offset + cur - map->m_la;
689 z_erofs_onlinepage_endio(page);
691 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
692 __func__, page, spiltted, map->m_llen);
695 /* if some error occurred while processing this page */
701 static void z_erofs_vle_unzip_kickoff(void *ptr, int bios)
703 tagptr1_t t = tagptr_init(tagptr1_t, ptr);
704 struct z_erofs_unzip_io *io = tagptr_unfold_ptr(t);
705 bool background = tagptr_unfold_tags(t);
710 spin_lock_irqsave(&io->u.wait.lock, flags);
711 if (!atomic_add_return(bios, &io->pending_bios))
712 wake_up_locked(&io->u.wait);
713 spin_unlock_irqrestore(&io->u.wait.lock, flags);
717 if (!atomic_add_return(bios, &io->pending_bios))
718 queue_work(z_erofs_workqueue, &io->u.work);
721 static inline void z_erofs_vle_read_endio(struct bio *bio)
723 struct erofs_sb_info *sbi = NULL;
724 blk_status_t err = bio->bi_status;
725 struct bio_vec *bvec;
726 struct bvec_iter_all iter_all;
728 bio_for_each_segment_all(bvec, bio, iter_all) {
729 struct page *page = bvec->bv_page;
730 bool cachemngd = false;
732 DBG_BUGON(PageUptodate(page));
733 DBG_BUGON(!page->mapping);
735 if (!sbi && !z_erofs_page_is_staging(page))
736 sbi = EROFS_SB(page->mapping->host->i_sb);
738 /* sbi should already be gotten if the page is managed */
740 cachemngd = erofs_page_is_managed(sbi, page);
745 SetPageUptodate(page);
751 z_erofs_vle_unzip_kickoff(bio->bi_private, -1);
755 static int z_erofs_decompress_pcluster(struct super_block *sb,
756 struct z_erofs_pcluster *pcl,
757 struct list_head *pagepool)
759 struct erofs_sb_info *const sbi = EROFS_SB(sb);
760 const unsigned int clusterpages = BIT(pcl->clusterbits);
761 struct z_erofs_pagevec_ctor ctor;
762 unsigned int i, outputsize, llen, nr_pages;
763 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
764 struct page **pages, **compressed_pages, *page;
766 enum z_erofs_page_type page_type;
767 bool overlapped, partial;
768 struct z_erofs_collection *cl;
772 cl = z_erofs_primarycollection(pcl);
773 DBG_BUGON(!READ_ONCE(cl->nr_pages));
775 mutex_lock(&cl->lock);
776 nr_pages = cl->nr_pages;
778 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
779 pages = pages_onstack;
780 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
781 mutex_trylock(&z_pagemap_global_lock)) {
782 pages = z_pagemap_global;
784 gfp_t gfp_flags = GFP_KERNEL;
786 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
787 gfp_flags |= __GFP_NOFAIL;
789 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
792 /* fallback to global pagemap for the lowmem scenario */
794 mutex_lock(&z_pagemap_global_lock);
795 pages = z_pagemap_global;
799 for (i = 0; i < nr_pages; ++i)
803 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
806 for (i = 0; i < cl->vcnt; ++i) {
809 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
811 /* all pages in pagevec ought to be valid */
813 DBG_BUGON(!page->mapping);
815 if (z_erofs_put_stagingpage(pagepool, page))
818 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
821 pagenr = z_erofs_onlinepage_index(page);
823 DBG_BUGON(pagenr >= nr_pages);
826 * currently EROFS doesn't support multiref(dedup),
827 * so here erroring out one multiref page.
831 SetPageError(pages[pagenr]);
832 z_erofs_onlinepage_endio(pages[pagenr]);
835 pages[pagenr] = page;
837 z_erofs_pagevec_ctor_exit(&ctor, true);
840 compressed_pages = pcl->compressed_pages;
842 for (i = 0; i < clusterpages; ++i) {
845 page = compressed_pages[i];
847 /* all compressed pages ought to be valid */
849 DBG_BUGON(!page->mapping);
851 if (!z_erofs_page_is_staging(page)) {
852 if (erofs_page_is_managed(sbi, page)) {
853 if (!PageUptodate(page))
859 * only if non-head page can be selected
860 * for inplace decompression
862 pagenr = z_erofs_onlinepage_index(page);
864 DBG_BUGON(pagenr >= nr_pages);
867 SetPageError(pages[pagenr]);
868 z_erofs_onlinepage_endio(pages[pagenr]);
871 pages[pagenr] = page;
876 /* PG_error needs checking for inplaced and staging pages */
877 if (PageError(page)) {
878 DBG_BUGON(PageUptodate(page));
886 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
887 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
889 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
891 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
895 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
897 .in = compressed_pages,
899 .pageofs_out = cl->pageofs,
900 .inputsize = PAGE_SIZE,
901 .outputsize = outputsize,
902 .alg = pcl->algorithmformat,
903 .inplace_io = overlapped,
904 .partial_decoding = partial
908 /* must handle all compressed pages before endding pages */
909 for (i = 0; i < clusterpages; ++i) {
910 page = compressed_pages[i];
912 if (erofs_page_is_managed(sbi, page))
915 /* recycle all individual staging pages */
916 (void)z_erofs_put_stagingpage(pagepool, page);
918 WRITE_ONCE(compressed_pages[i], NULL);
921 for (i = 0; i < nr_pages; ++i) {
926 DBG_BUGON(!page->mapping);
928 /* recycle all individual staging pages */
929 if (z_erofs_put_stagingpage(pagepool, page))
935 z_erofs_onlinepage_endio(page);
938 if (pages == z_pagemap_global)
939 mutex_unlock(&z_pagemap_global_lock);
940 else if (pages != pages_onstack)
946 /* all cl locks MUST be taken before the following line */
947 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
949 /* all cl locks SHOULD be released right now */
950 mutex_unlock(&cl->lock);
952 z_erofs_collection_put(cl);
956 static void z_erofs_vle_unzip_all(struct super_block *sb,
957 struct z_erofs_unzip_io *io,
958 struct list_head *pagepool)
960 z_erofs_next_pcluster_t owned = io->head;
962 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
963 struct z_erofs_pcluster *pcl;
965 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
966 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
968 /* no possible that 'owned' equals NULL */
969 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
971 pcl = container_of(owned, struct z_erofs_pcluster, next);
972 owned = READ_ONCE(pcl->next);
974 z_erofs_decompress_pcluster(sb, pcl, pagepool);
978 static void z_erofs_vle_unzip_wq(struct work_struct *work)
980 struct z_erofs_unzip_io_sb *iosb =
981 container_of(work, struct z_erofs_unzip_io_sb, io.u.work);
984 DBG_BUGON(iosb->io.head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
985 z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &pagepool);
987 put_pages_list(&pagepool);
991 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
993 struct list_head *pagepool,
994 struct address_space *mc,
997 /* determined at compile time to avoid too many #ifdefs */
998 const bool nocache = __builtin_constant_p(mc) ? !mc : false;
999 const pgoff_t index = pcl->obj.index;
1000 bool tocache = false;
1002 struct address_space *mapping;
1003 struct page *oldpage, *page;
1005 compressed_page_t t;
1009 page = READ_ONCE(pcl->compressed_pages[nr]);
1016 * the cached page has not been allocated and
1017 * an placeholder is out there, prepare it now.
1019 if (!nocache && page == PAGE_UNALLOCATED) {
1024 /* process the target tagged pointer */
1025 t = tagptr_init(compressed_page_t, page);
1026 justfound = tagptr_unfold_tags(t);
1027 page = tagptr_unfold_ptr(t);
1029 mapping = READ_ONCE(page->mapping);
1032 * if managed cache is disabled, it's no way to
1033 * get such a cached-like page.
1036 /* if managed cache is disabled, it is impossible `justfound' */
1037 DBG_BUGON(justfound);
1039 /* and it should be locked, not uptodate, and not truncated */
1040 DBG_BUGON(!PageLocked(page));
1041 DBG_BUGON(PageUptodate(page));
1042 DBG_BUGON(!mapping);
1047 * unmanaged (file) pages are all locked solidly,
1048 * therefore it is impossible for `mapping' to be NULL.
1050 if (mapping && mapping != mc)
1051 /* ought to be unmanaged pages */
1056 /* only true if page reclaim goes wrong, should never happen */
1057 DBG_BUGON(justfound && PagePrivate(page));
1059 /* the page is still in manage cache */
1060 if (page->mapping == mc) {
1061 WRITE_ONCE(pcl->compressed_pages[nr], page);
1063 ClearPageError(page);
1064 if (!PagePrivate(page)) {
1066 * impossible to be !PagePrivate(page) for
1067 * the current restriction as well if
1068 * the page is already in compressed_pages[].
1070 DBG_BUGON(!justfound);
1073 set_page_private(page, (unsigned long)pcl);
1074 SetPagePrivate(page);
1077 /* no need to submit io if it is already up-to-date */
1078 if (PageUptodate(page)) {
1086 * the managed page has been truncated, it's unsafe to
1087 * reuse this one, let's allocate a new cache-managed page.
1089 DBG_BUGON(page->mapping);
1090 DBG_BUGON(!justfound);
1096 page = __stagingpage_alloc(pagepool, gfp);
1097 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1098 list_add(&page->lru, pagepool);
1102 if (nocache || !tocache)
1104 if (add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1105 page->mapping = Z_EROFS_MAPPING_STAGING;
1109 set_page_private(page, (unsigned long)pcl);
1110 SetPagePrivate(page);
1111 out: /* the only exit (for tracing and debugging) */
1115 static struct z_erofs_unzip_io *jobqueue_init(struct super_block *sb,
1116 struct z_erofs_unzip_io *io,
1119 struct z_erofs_unzip_io_sb *iosb;
1122 /* waitqueue available for foreground io */
1125 init_waitqueue_head(&io->u.wait);
1126 atomic_set(&io->pending_bios, 0);
1130 iosb = kvzalloc(sizeof(*iosb), GFP_KERNEL | __GFP_NOFAIL);
1133 /* initialize fields in the allocated descriptor */
1136 INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq);
1138 io->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1142 /* define decompression jobqueue types */
1149 static void *jobqueueset_init(struct super_block *sb,
1150 z_erofs_next_pcluster_t qtail[],
1151 struct z_erofs_unzip_io *q[],
1152 struct z_erofs_unzip_io *fgq,
1156 * if managed cache is enabled, bypass jobqueue is needed,
1157 * no need to read from device for all pclusters in this queue.
1159 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true);
1160 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1162 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg);
1163 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1165 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg));
1168 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1169 z_erofs_next_pcluster_t qtail[],
1170 z_erofs_next_pcluster_t owned_head)
1172 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1173 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1175 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1176 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1177 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1179 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1181 WRITE_ONCE(*submit_qtail, owned_head);
1182 WRITE_ONCE(*bypass_qtail, &pcl->next);
1184 qtail[JQ_BYPASS] = &pcl->next;
1187 static bool postsubmit_is_all_bypassed(struct z_erofs_unzip_io *q[],
1188 unsigned int nr_bios,
1192 * although background is preferred, no one is pending for submission.
1193 * don't issue workqueue for decompression but drop it directly instead.
1195 if (force_fg || nr_bios)
1198 kvfree(container_of(q[JQ_SUBMIT], struct z_erofs_unzip_io_sb, io));
1202 static bool z_erofs_vle_submit_all(struct super_block *sb,
1203 z_erofs_next_pcluster_t owned_head,
1204 struct list_head *pagepool,
1205 struct z_erofs_unzip_io *fgq,
1208 struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb);
1209 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1210 struct z_erofs_unzip_io *q[NR_JOBQUEUES];
1213 /* since bio will be NULL, no need to initialize last_index */
1214 pgoff_t uninitialized_var(last_index);
1215 bool force_submit = false;
1216 unsigned int nr_bios;
1218 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1221 force_submit = false;
1224 bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg);
1226 /* by default, all need io submission */
1227 q[JQ_SUBMIT]->head = owned_head;
1230 struct z_erofs_pcluster *pcl;
1231 unsigned int clusterpages;
1232 pgoff_t first_index;
1234 unsigned int i = 0, bypass = 0;
1237 /* no possible 'owned_head' equals the following */
1238 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1239 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1241 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1243 clusterpages = BIT(pcl->clusterbits);
1245 /* close the main owned chain at first */
1246 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1247 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1249 first_index = pcl->obj.index;
1250 force_submit |= (first_index != last_index + 1);
1253 page = pickup_page_for_submission(pcl, i, pagepool,
1257 force_submit = true;
1262 if (bio && force_submit) {
1269 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1271 bio->bi_end_io = z_erofs_vle_read_endio;
1272 bio_set_dev(bio, sb->s_bdev);
1273 bio->bi_iter.bi_sector = (sector_t)(first_index + i) <<
1274 LOG_SECTORS_PER_BLOCK;
1275 bio->bi_private = bi_private;
1276 bio->bi_opf = REQ_OP_READ;
1281 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1282 if (err < PAGE_SIZE)
1283 goto submit_bio_retry;
1285 force_submit = false;
1286 last_index = first_index + i;
1288 if (++i < clusterpages)
1291 if (bypass < clusterpages)
1292 qtail[JQ_SUBMIT] = &pcl->next;
1294 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1295 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1300 if (postsubmit_is_all_bypassed(q, nr_bios, force_fg))
1303 z_erofs_vle_unzip_kickoff(bi_private, nr_bios);
1307 static void z_erofs_submit_and_unzip(struct super_block *sb,
1308 struct z_erofs_collector *clt,
1309 struct list_head *pagepool,
1312 struct z_erofs_unzip_io io[NR_JOBQUEUES];
1314 if (!z_erofs_vle_submit_all(sb, clt->owned_head,
1315 pagepool, io, force_fg))
1318 /* decompress no I/O pclusters immediately */
1319 z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool);
1324 /* wait until all bios are completed */
1325 wait_event(io[JQ_SUBMIT].u.wait,
1326 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1328 /* let's synchronous decompression */
1329 z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool);
1332 static int z_erofs_vle_normalaccess_readpage(struct file *file,
1335 struct inode *const inode = page->mapping->host;
1336 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1338 LIST_HEAD(pagepool);
1340 trace_erofs_readpage(page, false);
1342 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1344 err = z_erofs_do_read_page(&f, page, &pagepool);
1345 (void)z_erofs_collector_end(&f.clt);
1347 /* if some compressed cluster ready, need submit them anyway */
1348 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, true);
1351 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1354 put_page(f.map.mpage);
1356 /* clean up the remaining free pages */
1357 put_pages_list(&pagepool);
1361 static bool should_decompress_synchronously(struct erofs_sb_info *sbi,
1364 return nr <= sbi->max_sync_decompress_pages;
1367 static int z_erofs_vle_normalaccess_readpages(struct file *filp,
1368 struct address_space *mapping,
1369 struct list_head *pages,
1370 unsigned int nr_pages)
1372 struct inode *const inode = mapping->host;
1373 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1375 bool sync = should_decompress_synchronously(sbi, nr_pages);
1376 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1377 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1378 struct page *head = NULL;
1379 LIST_HEAD(pagepool);
1381 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1384 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1386 for (; nr_pages; --nr_pages) {
1387 struct page *page = lru_to_page(pages);
1389 prefetchw(&page->flags);
1390 list_del(&page->lru);
1393 * A pure asynchronous readahead is indicated if
1394 * a PG_readahead marked page is hitted at first.
1395 * Let's also do asynchronous decompression for this case.
1397 sync &= !(PageReadahead(page) && !head);
1399 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1400 list_add(&page->lru, &pagepool);
1404 set_page_private(page, (unsigned long)head);
1409 struct page *page = head;
1412 /* traversal in reverse order */
1413 head = (void *)page_private(page);
1415 err = z_erofs_do_read_page(&f, page, &pagepool);
1417 erofs_err(inode->i_sb,
1418 "readahead error at page %lu @ nid %llu",
1419 page->index, EROFS_I(inode)->nid);
1423 (void)z_erofs_collector_end(&f.clt);
1425 z_erofs_submit_and_unzip(inode->i_sb, &f.clt, &pagepool, sync);
1428 put_page(f.map.mpage);
1430 /* clean up the remaining free pages */
1431 put_pages_list(&pagepool);
1435 const struct address_space_operations z_erofs_vle_normalaccess_aops = {
1436 .readpage = z_erofs_vle_normalaccess_readpage,
1437 .readpages = z_erofs_vle_normalaccess_readpages,