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)
87 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
88 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
90 if (pcl->pclusterpages > pcs->maxpages)
93 kmem_cache_free(pcs->slab, pcl);
100 * a compressed_pages[] placeholder in order to avoid
101 * being filled with file pages for in-place decompression.
103 #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
105 /* how to allocate cached pages for a pcluster */
106 enum z_erofs_cache_alloctype {
107 DONTALLOC, /* don't allocate any cached pages */
108 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
110 * try to use cached I/O if page allocation succeeds or fallback
111 * to in-place I/O instead to avoid any direct reclaim.
117 * tagged pointer with 1-bit tag for all compressed pages
118 * tag 0 - the page is just found with an extra page reference
120 typedef tagptr1_t compressed_page_t;
122 #define tag_compressed_page_justfound(page) \
123 tagptr_fold(compressed_page_t, page, 1)
125 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
127 void z_erofs_exit_zip_subsystem(void)
129 destroy_workqueue(z_erofs_workqueue);
130 z_erofs_destroy_pcluster_pool();
133 static inline int z_erofs_init_workqueue(void)
135 const unsigned int onlinecpus = num_possible_cpus();
138 * no need to spawn too many threads, limiting threads could minimum
139 * scheduling overhead, perhaps per-CPU threads should be better?
141 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
142 WQ_UNBOUND | WQ_HIGHPRI,
143 onlinecpus + onlinecpus / 4);
144 return z_erofs_workqueue ? 0 : -ENOMEM;
147 int __init z_erofs_init_zip_subsystem(void)
149 int err = z_erofs_create_pcluster_pool();
153 err = z_erofs_init_workqueue();
155 z_erofs_destroy_pcluster_pool();
159 enum z_erofs_collectmode {
163 * The current collection was the tail of an exist chain, in addition
164 * that the previous processed chained collections are all decided to
165 * be hooked up to it.
166 * A new chain will be created for the remaining collections which are
167 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
168 * the next collection cannot reuse the whole page safely in
169 * the following scenario:
170 * ________________________________________________________________
171 * | tail (partial) page | head (partial) page |
172 * | (belongs to the next cl) | (belongs to the current cl) |
173 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
175 COLLECT_PRIMARY_HOOKED,
177 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
178 * could be dispatched into bypass queue later due to uptodated managed
179 * pages. All related online pages cannot be reused for inplace I/O (or
180 * pagevec) since it can be directly decoded without I/O submission.
182 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
184 * The current collection has been linked with the owned chain, and
185 * could also be linked with the remaining collections, which means
186 * if the processing page is the tail page of the collection, thus
187 * the current collection can safely use the whole page (since
188 * the previous collection is under control) for in-place I/O, as
190 * ________________________________________________________________
191 * | tail (partial) page | head (partial) page |
192 * | (of the current cl) | (of the previous collection) |
193 * | PRIMARY_FOLLOWED or | |
194 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
196 * [ (*) the above page can be used as inplace I/O. ]
198 COLLECT_PRIMARY_FOLLOWED,
201 struct z_erofs_collector {
202 struct z_erofs_pagevec_ctor vector;
204 struct z_erofs_pcluster *pcl, *tailpcl;
205 struct z_erofs_collection *cl;
206 /* a pointer used to pick up inplace I/O pages */
207 struct page **icpage_ptr;
208 z_erofs_next_pcluster_t owned_head;
210 enum z_erofs_collectmode mode;
213 struct z_erofs_decompress_frontend {
214 struct inode *const inode;
216 struct z_erofs_collector clt;
217 struct erofs_map_blocks map;
220 /* used for applying cache strategy on the fly */
222 erofs_off_t headoffset;
225 #define COLLECTOR_INIT() { \
226 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
227 .mode = COLLECT_PRIMARY_FOLLOWED }
229 #define DECOMPRESS_FRONTEND_INIT(__i) { \
230 .inode = __i, .clt = COLLECTOR_INIT(), \
233 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
234 static DEFINE_MUTEX(z_pagemap_global_lock);
236 static void preload_compressed_pages(struct z_erofs_collector *clt,
237 struct address_space *mc,
238 enum z_erofs_cache_alloctype type,
239 struct page **pagepool)
241 struct z_erofs_pcluster *pcl = clt->pcl;
242 bool standalone = true;
243 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
244 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
248 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
251 pages = pcl->compressed_pages;
252 index = pcl->obj.index;
253 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
256 struct page *newpage = NULL;
258 /* the compressed page was loaded before */
259 if (READ_ONCE(*pages))
262 page = find_get_page(mc, index);
265 t = tag_compressed_page_justfound(page);
267 /* I/O is needed, no possible to decompress directly */
271 t = tagptr_init(compressed_page_t,
275 newpage = erofs_allocpage(pagepool, gfp);
278 set_page_private(newpage,
279 Z_EROFS_PREALLOCATED_PAGE);
280 t = tag_compressed_page_justfound(newpage);
282 default: /* DONTALLOC */
287 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
293 erofs_pagepool_add(pagepool, newpage);
297 * don't do inplace I/O if all compressed pages are available in
298 * managed cache since it can be moved to the bypass queue instead.
301 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
304 /* called by erofs_shrinker to get rid of all compressed_pages */
305 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
306 struct erofs_workgroup *grp)
308 struct z_erofs_pcluster *const pcl =
309 container_of(grp, struct z_erofs_pcluster, obj);
313 * refcount of workgroup is now freezed as 1,
314 * therefore no need to worry about available decompression users.
316 for (i = 0; i < pcl->pclusterpages; ++i) {
317 struct page *page = pcl->compressed_pages[i];
322 /* block other users from reclaiming or migrating the page */
323 if (!trylock_page(page))
326 if (!erofs_page_is_managed(sbi, page))
329 /* barrier is implied in the following 'unlock_page' */
330 WRITE_ONCE(pcl->compressed_pages[i], NULL);
331 detach_page_private(page);
337 int erofs_try_to_free_cached_page(struct page *page)
339 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
340 int ret = 0; /* 0 - busy */
342 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
345 for (i = 0; i < pcl->pclusterpages; ++i) {
346 if (pcl->compressed_pages[i] == page) {
347 WRITE_ONCE(pcl->compressed_pages[i], NULL);
352 erofs_workgroup_unfreeze(&pcl->obj, 1);
355 detach_page_private(page);
360 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
361 static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
364 struct z_erofs_pcluster *const pcl = clt->pcl;
366 while (clt->icpage_ptr > pcl->compressed_pages)
367 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
372 /* callers must be with collection lock held */
373 static int z_erofs_attach_page(struct z_erofs_collector *clt,
375 enum z_erofs_page_type type)
379 /* give priority for inplaceio */
380 if (clt->mode >= COLLECT_PRIMARY &&
381 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
382 z_erofs_try_inplace_io(clt, page))
385 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type);
386 clt->cl->vcnt += (unsigned int)ret;
388 return ret ? 0 : -EAGAIN;
391 static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
393 struct z_erofs_pcluster *pcl = clt->pcl;
394 z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
396 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
397 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
398 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
399 *owned_head = &pcl->next;
400 /* so we can attach this pcluster to our submission chain. */
401 clt->mode = COLLECT_PRIMARY_FOLLOWED;
406 * type 2, link to the end of an existing open chain, be careful
407 * that its submission is controlled by the original attached chain.
409 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
410 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
411 *owned_head = Z_EROFS_PCLUSTER_TAIL;
412 clt->mode = COLLECT_PRIMARY_HOOKED;
416 /* type 3, it belongs to a chain, but it isn't the end of the chain */
417 clt->mode = COLLECT_PRIMARY;
420 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
422 struct erofs_map_blocks *map)
424 struct z_erofs_pcluster *pcl = clt->pcl;
425 struct z_erofs_collection *cl;
428 /* to avoid unexpected loop formed by corrupted images */
429 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
431 return -EFSCORRUPTED;
434 cl = z_erofs_primarycollection(pcl);
435 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
437 return -EFSCORRUPTED;
440 length = READ_ONCE(pcl->length);
441 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
442 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
444 return -EFSCORRUPTED;
447 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
449 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
450 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
452 while (llen > length &&
453 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
455 length = READ_ONCE(pcl->length);
458 mutex_lock(&cl->lock);
459 /* used to check tail merging loop due to corrupted images */
460 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
463 z_erofs_try_to_claim_pcluster(clt);
468 static int z_erofs_register_collection(struct z_erofs_collector *clt,
470 struct erofs_map_blocks *map)
472 struct z_erofs_pcluster *pcl;
473 struct z_erofs_collection *cl;
474 struct erofs_workgroup *grp;
477 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
479 return -EFSCORRUPTED;
482 /* no available pcluster, let's allocate one */
483 pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
487 atomic_set(&pcl->obj.refcount, 1);
488 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
489 pcl->algorithmformat = map->m_algorithmformat;
490 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
491 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
492 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
494 /* new pclusters should be claimed as type 1, primary and followed */
495 pcl->next = clt->owned_head;
496 clt->mode = COLLECT_PRIMARY_FOLLOWED;
498 cl = z_erofs_primarycollection(pcl);
499 cl->pageofs = map->m_la & ~PAGE_MASK;
502 * lock all primary followed works before visible to others
503 * and mutex_trylock *never* fails for a new pcluster.
505 mutex_init(&cl->lock);
506 DBG_BUGON(!mutex_trylock(&cl->lock));
508 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
514 if (grp != &pcl->obj) {
515 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
519 /* used to check tail merging loop due to corrupted images */
520 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
522 clt->owned_head = &pcl->next;
528 mutex_unlock(&cl->lock);
529 z_erofs_free_pcluster(pcl);
533 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
535 struct erofs_map_blocks *map)
537 struct erofs_workgroup *grp;
542 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
543 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
544 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
546 if (!PAGE_ALIGNED(map->m_pa)) {
551 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
553 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
555 ret = z_erofs_register_collection(clt, inode, map);
563 ret = z_erofs_lookup_collection(clt, inode, map);
565 erofs_workgroup_put(&clt->pcl->obj);
570 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
571 clt->cl->pagevec, clt->cl->vcnt);
573 /* since file-backed online pages are traversed in reverse order */
574 clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
579 * keep in mind that no referenced pclusters will be freed
580 * only after a RCU grace period.
582 static void z_erofs_rcu_callback(struct rcu_head *head)
584 struct z_erofs_collection *const cl =
585 container_of(head, struct z_erofs_collection, rcu);
587 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
588 primary_collection));
591 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
593 struct z_erofs_pcluster *const pcl =
594 container_of(grp, struct z_erofs_pcluster, obj);
595 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
597 call_rcu(&cl->rcu, z_erofs_rcu_callback);
600 static void z_erofs_collection_put(struct z_erofs_collection *cl)
602 struct z_erofs_pcluster *const pcl =
603 container_of(cl, struct z_erofs_pcluster, primary_collection);
605 erofs_workgroup_put(&pcl->obj);
608 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
610 struct z_erofs_collection *cl = clt->cl;
615 z_erofs_pagevec_ctor_exit(&clt->vector, false);
616 mutex_unlock(&cl->lock);
619 * if all pending pages are added, don't hold its reference
620 * any longer if the pcluster isn't hosted by ourselves.
622 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
623 z_erofs_collection_put(cl);
629 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
630 unsigned int cachestrategy,
633 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
639 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
643 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
644 struct page *page, struct page **pagepool)
646 struct inode *const inode = fe->inode;
647 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
648 struct erofs_map_blocks *const map = &fe->map;
649 struct z_erofs_collector *const clt = &fe->clt;
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(clt))
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(clt, inode, map);
695 /* preload all compressed pages (maybe downgrade role if necessary) */
696 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy, map->m_la))
697 cache_strategy = TRYALLOC;
699 cache_strategy = DONTALLOC;
701 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
702 cache_strategy, pagepool);
706 * Ensure the current partial page belongs to this submit chain rather
707 * than other concurrent submit chains or the noio(bypass) chain since
708 * those chains are handled asynchronously thus the page cannot be used
709 * for inplace I/O or pagevec (should be processed in strict order.)
711 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
712 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
714 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
715 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
716 zero_user_segment(page, cur, end);
720 /* let's derive page type */
721 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
722 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
723 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
724 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
727 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
730 err = z_erofs_attach_page(clt, page, page_type);
731 /* should allocate an additional short-lived page for pagevec */
732 if (err == -EAGAIN) {
733 struct page *const newpage =
734 alloc_page(GFP_NOFS | __GFP_NOFAIL);
736 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
737 err = z_erofs_attach_page(clt, newpage,
738 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
746 index = page->index - (map->m_la >> PAGE_SHIFT);
748 z_erofs_onlinepage_fixup(page, index, true);
750 /* bump up the number of spiltted parts of a page */
752 /* also update nr_pages */
753 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
755 /* can be used for verification */
756 map->m_llen = offset + cur - map->m_la;
763 z_erofs_onlinepage_endio(page);
765 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
766 __func__, page, spiltted, map->m_llen);
769 /* if some error occurred while processing this page */
775 static void z_erofs_decompressqueue_work(struct work_struct *work);
776 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
779 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
781 /* wake up the caller thread for sync decompression */
785 spin_lock_irqsave(&io->u.wait.lock, flags);
786 if (!atomic_add_return(bios, &io->pending_bios))
787 wake_up_locked(&io->u.wait);
788 spin_unlock_irqrestore(&io->u.wait.lock, flags);
792 if (atomic_add_return(bios, &io->pending_bios))
794 /* Use workqueue and sync decompression for atomic contexts only */
795 if (in_atomic() || irqs_disabled()) {
796 queue_work(z_erofs_workqueue, &io->u.work);
797 sbi->opt.readahead_sync_decompress = true;
800 z_erofs_decompressqueue_work(&io->u.work);
803 static bool z_erofs_page_is_invalidated(struct page *page)
805 return !page->mapping && !z_erofs_is_shortlived_page(page);
808 static void z_erofs_decompressqueue_endio(struct bio *bio)
810 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
811 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
812 blk_status_t err = bio->bi_status;
813 struct bio_vec *bvec;
814 struct bvec_iter_all iter_all;
816 bio_for_each_segment_all(bvec, bio, iter_all) {
817 struct page *page = bvec->bv_page;
819 DBG_BUGON(PageUptodate(page));
820 DBG_BUGON(z_erofs_page_is_invalidated(page));
825 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
827 SetPageUptodate(page);
831 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
835 static int z_erofs_decompress_pcluster(struct super_block *sb,
836 struct z_erofs_pcluster *pcl,
837 struct page **pagepool)
839 struct erofs_sb_info *const sbi = EROFS_SB(sb);
840 struct z_erofs_pagevec_ctor ctor;
841 unsigned int i, inputsize, outputsize, llen, nr_pages;
842 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
843 struct page **pages, **compressed_pages, *page;
845 enum z_erofs_page_type page_type;
846 bool overlapped, partial;
847 struct z_erofs_collection *cl;
851 cl = z_erofs_primarycollection(pcl);
852 DBG_BUGON(!READ_ONCE(cl->nr_pages));
854 mutex_lock(&cl->lock);
855 nr_pages = cl->nr_pages;
857 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
858 pages = pages_onstack;
859 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
860 mutex_trylock(&z_pagemap_global_lock)) {
861 pages = z_pagemap_global;
863 gfp_t gfp_flags = GFP_KERNEL;
865 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
866 gfp_flags |= __GFP_NOFAIL;
868 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
871 /* fallback to global pagemap for the lowmem scenario */
873 mutex_lock(&z_pagemap_global_lock);
874 pages = z_pagemap_global;
878 for (i = 0; i < nr_pages; ++i)
882 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
885 for (i = 0; i < cl->vcnt; ++i) {
888 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
890 /* all pages in pagevec ought to be valid */
892 DBG_BUGON(z_erofs_page_is_invalidated(page));
894 if (z_erofs_put_shortlivedpage(pagepool, page))
897 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
900 pagenr = z_erofs_onlinepage_index(page);
902 DBG_BUGON(pagenr >= nr_pages);
905 * currently EROFS doesn't support multiref(dedup),
906 * so here erroring out one multiref page.
910 SetPageError(pages[pagenr]);
911 z_erofs_onlinepage_endio(pages[pagenr]);
914 pages[pagenr] = page;
916 z_erofs_pagevec_ctor_exit(&ctor, true);
919 compressed_pages = pcl->compressed_pages;
921 for (i = 0; i < pcl->pclusterpages; ++i) {
924 page = compressed_pages[i];
926 /* all compressed pages ought to be valid */
928 DBG_BUGON(z_erofs_page_is_invalidated(page));
930 if (!z_erofs_is_shortlived_page(page)) {
931 if (erofs_page_is_managed(sbi, page)) {
932 if (!PageUptodate(page))
938 * only if non-head page can be selected
939 * for inplace decompression
941 pagenr = z_erofs_onlinepage_index(page);
943 DBG_BUGON(pagenr >= nr_pages);
946 SetPageError(pages[pagenr]);
947 z_erofs_onlinepage_endio(pages[pagenr]);
950 pages[pagenr] = page;
955 /* PG_error needs checking for all non-managed pages */
956 if (PageError(page)) {
957 DBG_BUGON(PageUptodate(page));
965 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
966 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
968 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
970 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
974 inputsize = pcl->pclusterpages * PAGE_SIZE;
975 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
977 .in = compressed_pages,
979 .pageofs_out = cl->pageofs,
980 .inputsize = inputsize,
981 .outputsize = outputsize,
982 .alg = pcl->algorithmformat,
983 .inplace_io = overlapped,
984 .partial_decoding = partial
988 /* must handle all compressed pages before ending pages */
989 for (i = 0; i < pcl->pclusterpages; ++i) {
990 page = compressed_pages[i];
992 if (erofs_page_is_managed(sbi, page))
995 /* recycle all individual short-lived pages */
996 (void)z_erofs_put_shortlivedpage(pagepool, page);
998 WRITE_ONCE(compressed_pages[i], NULL);
1001 for (i = 0; i < nr_pages; ++i) {
1006 DBG_BUGON(z_erofs_page_is_invalidated(page));
1008 /* recycle all individual short-lived pages */
1009 if (z_erofs_put_shortlivedpage(pagepool, page))
1015 z_erofs_onlinepage_endio(page);
1018 if (pages == z_pagemap_global)
1019 mutex_unlock(&z_pagemap_global_lock);
1020 else if (pages != pages_onstack)
1026 /* all cl locks MUST be taken before the following line */
1027 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1029 /* all cl locks SHOULD be released right now */
1030 mutex_unlock(&cl->lock);
1032 z_erofs_collection_put(cl);
1036 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1037 struct page **pagepool)
1039 z_erofs_next_pcluster_t owned = io->head;
1041 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1042 struct z_erofs_pcluster *pcl;
1044 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1045 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1047 /* no possible that 'owned' equals NULL */
1048 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1050 pcl = container_of(owned, struct z_erofs_pcluster, next);
1051 owned = READ_ONCE(pcl->next);
1053 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
1057 static void z_erofs_decompressqueue_work(struct work_struct *work)
1059 struct z_erofs_decompressqueue *bgq =
1060 container_of(work, struct z_erofs_decompressqueue, u.work);
1061 struct page *pagepool = NULL;
1063 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1064 z_erofs_decompress_queue(bgq, &pagepool);
1066 erofs_release_pages(&pagepool);
1070 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1072 struct page **pagepool,
1073 struct address_space *mc,
1076 const pgoff_t index = pcl->obj.index;
1077 bool tocache = false;
1079 struct address_space *mapping;
1080 struct page *oldpage, *page;
1082 compressed_page_t t;
1086 page = READ_ONCE(pcl->compressed_pages[nr]);
1093 * the cached page has not been allocated and
1094 * an placeholder is out there, prepare it now.
1096 if (page == PAGE_UNALLOCATED) {
1101 /* process the target tagged pointer */
1102 t = tagptr_init(compressed_page_t, page);
1103 justfound = tagptr_unfold_tags(t);
1104 page = tagptr_unfold_ptr(t);
1107 * preallocated cached pages, which is used to avoid direct reclaim
1108 * otherwise, it will go inplace I/O path instead.
1110 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1111 WRITE_ONCE(pcl->compressed_pages[nr], page);
1112 set_page_private(page, 0);
1116 mapping = READ_ONCE(page->mapping);
1119 * file-backed online pages in plcuster are all locked steady,
1120 * therefore it is impossible for `mapping' to be NULL.
1122 if (mapping && mapping != mc)
1123 /* ought to be unmanaged pages */
1126 /* directly return for shortlived page as well */
1127 if (z_erofs_is_shortlived_page(page))
1132 /* only true if page reclaim goes wrong, should never happen */
1133 DBG_BUGON(justfound && PagePrivate(page));
1135 /* the page is still in manage cache */
1136 if (page->mapping == mc) {
1137 WRITE_ONCE(pcl->compressed_pages[nr], page);
1139 ClearPageError(page);
1140 if (!PagePrivate(page)) {
1142 * impossible to be !PagePrivate(page) for
1143 * the current restriction as well if
1144 * the page is already in compressed_pages[].
1146 DBG_BUGON(!justfound);
1149 set_page_private(page, (unsigned long)pcl);
1150 SetPagePrivate(page);
1153 /* no need to submit io if it is already up-to-date */
1154 if (PageUptodate(page)) {
1162 * the managed page has been truncated, it's unsafe to
1163 * reuse this one, let's allocate a new cache-managed page.
1165 DBG_BUGON(page->mapping);
1166 DBG_BUGON(!justfound);
1172 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1173 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1174 erofs_pagepool_add(pagepool, page);
1179 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1180 /* turn into temporary page if fails (1 ref) */
1181 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1184 attach_page_private(page, pcl);
1185 /* drop a refcount added by allocpage (then we have 2 refs here) */
1188 out: /* the only exit (for tracing and debugging) */
1192 static struct z_erofs_decompressqueue *
1193 jobqueue_init(struct super_block *sb,
1194 struct z_erofs_decompressqueue *fgq, bool *fg)
1196 struct z_erofs_decompressqueue *q;
1199 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1204 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1208 init_waitqueue_head(&fgq->u.wait);
1209 atomic_set(&fgq->pending_bios, 0);
1212 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1216 /* define decompression jobqueue types */
1223 static void *jobqueueset_init(struct super_block *sb,
1224 struct z_erofs_decompressqueue *q[],
1225 struct z_erofs_decompressqueue *fgq, bool *fg)
1228 * if managed cache is enabled, bypass jobqueue is needed,
1229 * no need to read from device for all pclusters in this queue.
1231 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1232 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1234 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1237 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1238 z_erofs_next_pcluster_t qtail[],
1239 z_erofs_next_pcluster_t owned_head)
1241 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1242 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1244 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1245 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1246 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1248 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1250 WRITE_ONCE(*submit_qtail, owned_head);
1251 WRITE_ONCE(*bypass_qtail, &pcl->next);
1253 qtail[JQ_BYPASS] = &pcl->next;
1256 static void z_erofs_submit_queue(struct super_block *sb,
1257 struct z_erofs_decompress_frontend *f,
1258 struct page **pagepool,
1259 struct z_erofs_decompressqueue *fgq,
1262 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1263 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1264 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1266 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1267 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1269 struct block_device *last_bdev;
1270 unsigned int nr_bios = 0;
1271 struct bio *bio = NULL;
1273 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1274 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1275 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1277 /* by default, all need io submission */
1278 q[JQ_SUBMIT]->head = owned_head;
1281 struct erofs_map_dev mdev;
1282 struct z_erofs_pcluster *pcl;
1287 /* no possible 'owned_head' equals the following */
1288 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1289 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1291 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1293 /* no device id here, thus it will always succeed */
1294 mdev = (struct erofs_map_dev) {
1295 .m_pa = blknr_to_addr(pcl->obj.index),
1297 (void)erofs_map_dev(sb, &mdev);
1299 cur = erofs_blknr(mdev.m_pa);
1300 end = cur + pcl->pclusterpages;
1302 /* close the main owned chain at first */
1303 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1304 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1309 page = pickup_page_for_submission(pcl, i++, pagepool,
1315 if (bio && (cur != last_index + 1 ||
1316 last_bdev != mdev.m_bdev)) {
1323 bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
1324 bio->bi_end_io = z_erofs_decompressqueue_endio;
1326 bio_set_dev(bio, mdev.m_bdev);
1327 last_bdev = mdev.m_bdev;
1328 bio->bi_iter.bi_sector = (sector_t)cur <<
1329 LOG_SECTORS_PER_BLOCK;
1330 bio->bi_private = bi_private;
1331 bio->bi_opf = REQ_OP_READ;
1333 bio->bi_opf |= REQ_RAHEAD;
1337 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1338 goto submit_bio_retry;
1342 } while (++cur < end);
1345 qtail[JQ_SUBMIT] = &pcl->next;
1347 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1348 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1354 * although background is preferred, no one is pending for submission.
1355 * don't issue workqueue for decompression but drop it directly instead.
1357 if (!*force_fg && !nr_bios) {
1358 kvfree(q[JQ_SUBMIT]);
1361 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1364 static void z_erofs_runqueue(struct super_block *sb,
1365 struct z_erofs_decompress_frontend *f,
1366 struct page **pagepool, bool force_fg)
1368 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1370 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1372 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1374 /* handle bypass queue (no i/o pclusters) immediately */
1375 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1380 /* wait until all bios are completed */
1381 io_wait_event(io[JQ_SUBMIT].u.wait,
1382 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1384 /* handle synchronous decompress queue in the caller context */
1385 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1389 * Since partial uptodate is still unimplemented for now, we have to use
1390 * approximate readmore strategies as a start.
1392 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1393 struct readahead_control *rac,
1395 struct page **pagepool,
1398 struct inode *inode = f->inode;
1399 struct erofs_map_blocks *map = &f->map;
1405 err = z_erofs_map_blocks_iter(inode, map,
1406 EROFS_GET_BLOCKS_READMORE);
1410 /* expend ra for the trailing edge if readahead */
1412 loff_t newstart = readahead_pos(rac);
1414 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1415 readahead_expand(rac, newstart, cur - newstart);
1418 end = round_up(end, PAGE_SIZE);
1420 end = round_up(map->m_la, PAGE_SIZE);
1426 cur = map->m_la + map->m_llen - 1;
1427 while (cur >= end) {
1428 pgoff_t index = cur >> PAGE_SHIFT;
1431 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1435 if (PageUptodate(page)) {
1441 err = z_erofs_do_read_page(f, page, pagepool);
1443 erofs_err(inode->i_sb,
1444 "readmore error at page %lu @ nid %llu",
1445 index, EROFS_I(inode)->nid);
1448 if (cur < PAGE_SIZE)
1450 cur = (index << PAGE_SHIFT) - 1;
1454 static int z_erofs_readpage(struct file *file, struct page *page)
1456 struct inode *const inode = page->mapping->host;
1457 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1458 struct page *pagepool = NULL;
1461 trace_erofs_readpage(page, false);
1462 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1464 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1466 err = z_erofs_do_read_page(&f, page, &pagepool);
1467 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1469 (void)z_erofs_collector_end(&f.clt);
1471 /* if some compressed cluster ready, need submit them anyway */
1472 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1475 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1478 put_page(f.map.mpage);
1480 erofs_release_pages(&pagepool);
1484 static void z_erofs_readahead(struct readahead_control *rac)
1486 struct inode *const inode = rac->mapping->host;
1487 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1488 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1489 struct page *pagepool = NULL, *head = NULL, *page;
1490 unsigned int nr_pages;
1493 f.headoffset = readahead_pos(rac);
1495 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1496 readahead_length(rac) - 1, &pagepool, true);
1497 nr_pages = readahead_count(rac);
1498 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1500 while ((page = readahead_page(rac))) {
1501 set_page_private(page, (unsigned long)head);
1506 struct page *page = head;
1509 /* traversal in reverse order */
1510 head = (void *)page_private(page);
1512 err = z_erofs_do_read_page(&f, page, &pagepool);
1514 erofs_err(inode->i_sb,
1515 "readahead error at page %lu @ nid %llu",
1516 page->index, EROFS_I(inode)->nid);
1519 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1520 (void)z_erofs_collector_end(&f.clt);
1522 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1523 sbi->opt.readahead_sync_decompress &&
1524 nr_pages <= sbi->opt.max_sync_decompress_pages);
1526 put_page(f.map.mpage);
1527 erofs_release_pages(&pagepool);
1530 const struct address_space_operations z_erofs_aops = {
1531 .readpage = z_erofs_readpage,
1532 .readahead = z_erofs_readahead,