1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
8 #include <linux/prefetch.h>
10 #include <trace/events/erofs.h>
13 * since pclustersize is variable for big pcluster feature, introduce slab
14 * pools implementation for different pcluster sizes.
16 struct z_erofs_pcluster_slab {
17 struct kmem_cache *slab;
18 unsigned int maxpages;
22 #define _PCLP(n) { .maxpages = n }
24 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
25 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
26 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
29 static void z_erofs_destroy_pcluster_pool(void)
33 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
34 if (!pcluster_pool[i].slab)
36 kmem_cache_destroy(pcluster_pool[i].slab);
37 pcluster_pool[i].slab = NULL;
41 static int z_erofs_create_pcluster_pool(void)
43 struct z_erofs_pcluster_slab *pcs;
44 struct z_erofs_pcluster *a;
47 for (pcs = pcluster_pool;
48 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
49 size = struct_size(a, compressed_pages, pcs->maxpages);
51 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
52 pcs->slab = kmem_cache_create(pcs->name, size, 0,
53 SLAB_RECLAIM_ACCOUNT, NULL);
57 z_erofs_destroy_pcluster_pool();
63 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
67 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
68 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
69 struct z_erofs_pcluster *pcl;
71 if (nrpages > pcs->maxpages)
74 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
76 return ERR_PTR(-ENOMEM);
77 pcl->pclusterpages = nrpages;
80 return ERR_PTR(-EINVAL);
83 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
85 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
88 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
91 if (pclusterpages > pcs->maxpages)
94 kmem_cache_free(pcs->slab, pcl);
100 /* how to allocate cached pages for a pcluster */
101 enum z_erofs_cache_alloctype {
102 DONTALLOC, /* don't allocate any cached pages */
104 * try to use cached I/O if page allocation succeeds or fallback
105 * to in-place I/O instead to avoid any direct reclaim.
111 * tagged pointer with 1-bit tag for all compressed pages
112 * tag 0 - the page is just found with an extra page reference
114 typedef tagptr1_t compressed_page_t;
116 #define tag_compressed_page_justfound(page) \
117 tagptr_fold(compressed_page_t, page, 1)
119 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
121 void z_erofs_exit_zip_subsystem(void)
123 destroy_workqueue(z_erofs_workqueue);
124 z_erofs_destroy_pcluster_pool();
127 static inline int z_erofs_init_workqueue(void)
129 const unsigned int onlinecpus = num_possible_cpus();
132 * no need to spawn too many threads, limiting threads could minimum
133 * scheduling overhead, perhaps per-CPU threads should be better?
135 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
136 WQ_UNBOUND | WQ_HIGHPRI,
137 onlinecpus + onlinecpus / 4);
138 return z_erofs_workqueue ? 0 : -ENOMEM;
141 int __init z_erofs_init_zip_subsystem(void)
143 int err = z_erofs_create_pcluster_pool();
147 err = z_erofs_init_workqueue();
149 z_erofs_destroy_pcluster_pool();
153 enum z_erofs_collectmode {
157 * The current collection was the tail of an exist chain, in addition
158 * that the previous processed chained collections are all decided to
159 * be hooked up to it.
160 * A new chain will be created for the remaining collections which are
161 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
162 * the next collection cannot reuse the whole page safely in
163 * the following scenario:
164 * ________________________________________________________________
165 * | tail (partial) page | head (partial) page |
166 * | (belongs to the next cl) | (belongs to the current cl) |
167 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
169 COLLECT_PRIMARY_HOOKED,
171 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
172 * could be dispatched into bypass queue later due to uptodated managed
173 * pages. All related online pages cannot be reused for inplace I/O (or
174 * pagevec) since it can be directly decoded without I/O submission.
176 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
178 * The current collection has been linked with the owned chain, and
179 * could also be linked with the remaining collections, which means
180 * if the processing page is the tail page of the collection, thus
181 * the current collection can safely use the whole page (since
182 * the previous collection is under control) for in-place I/O, as
184 * ________________________________________________________________
185 * | tail (partial) page | head (partial) page |
186 * | (of the current cl) | (of the previous collection) |
187 * | PRIMARY_FOLLOWED or | |
188 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
190 * [ (*) the above page can be used as inplace I/O. ]
192 COLLECT_PRIMARY_FOLLOWED,
195 struct z_erofs_collector {
196 struct z_erofs_pagevec_ctor vector;
198 struct z_erofs_pcluster *pcl, *tailpcl;
199 struct z_erofs_collection *cl;
200 /* a pointer used to pick up inplace I/O pages */
201 struct page **icpage_ptr;
202 z_erofs_next_pcluster_t owned_head;
204 enum z_erofs_collectmode mode;
207 struct z_erofs_decompress_frontend {
208 struct inode *const inode;
210 struct z_erofs_collector clt;
211 struct erofs_map_blocks map;
214 /* used for applying cache strategy on the fly */
216 erofs_off_t headoffset;
219 #define COLLECTOR_INIT() { \
220 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
221 .mode = COLLECT_PRIMARY_FOLLOWED }
223 #define DECOMPRESS_FRONTEND_INIT(__i) { \
224 .inode = __i, .clt = COLLECTOR_INIT(), \
227 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
228 static DEFINE_MUTEX(z_pagemap_global_lock);
230 static void preload_compressed_pages(struct z_erofs_collector *clt,
231 struct address_space *mc,
232 enum z_erofs_cache_alloctype type,
233 struct page **pagepool)
235 struct z_erofs_pcluster *pcl = clt->pcl;
236 bool standalone = true;
237 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
238 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
242 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
245 pages = pcl->compressed_pages;
246 index = pcl->obj.index;
247 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
250 struct page *newpage = NULL;
252 /* the compressed page was loaded before */
253 if (READ_ONCE(*pages))
256 page = find_get_page(mc, index);
259 t = tag_compressed_page_justfound(page);
261 /* I/O is needed, no possible to decompress directly */
265 newpage = erofs_allocpage(pagepool, gfp);
268 set_page_private(newpage,
269 Z_EROFS_PREALLOCATED_PAGE);
270 t = tag_compressed_page_justfound(newpage);
272 default: /* DONTALLOC */
277 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
283 erofs_pagepool_add(pagepool, newpage);
287 * don't do inplace I/O if all compressed pages are available in
288 * managed cache since it can be moved to the bypass queue instead.
291 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
294 /* called by erofs_shrinker to get rid of all compressed_pages */
295 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
296 struct erofs_workgroup *grp)
298 struct z_erofs_pcluster *const pcl =
299 container_of(grp, struct z_erofs_pcluster, obj);
302 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
304 * refcount of workgroup is now freezed as 1,
305 * therefore no need to worry about available decompression users.
307 for (i = 0; i < pcl->pclusterpages; ++i) {
308 struct page *page = pcl->compressed_pages[i];
313 /* block other users from reclaiming or migrating the page */
314 if (!trylock_page(page))
317 if (!erofs_page_is_managed(sbi, page))
320 /* barrier is implied in the following 'unlock_page' */
321 WRITE_ONCE(pcl->compressed_pages[i], NULL);
322 detach_page_private(page);
328 int erofs_try_to_free_cached_page(struct page *page)
330 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
331 int ret = 0; /* 0 - busy */
333 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
336 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
337 for (i = 0; i < pcl->pclusterpages; ++i) {
338 if (pcl->compressed_pages[i] == page) {
339 WRITE_ONCE(pcl->compressed_pages[i], NULL);
344 erofs_workgroup_unfreeze(&pcl->obj, 1);
347 detach_page_private(page);
352 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
353 static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
356 struct z_erofs_pcluster *const pcl = clt->pcl;
358 while (clt->icpage_ptr > pcl->compressed_pages)
359 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
364 /* callers must be with collection lock held */
365 static int z_erofs_attach_page(struct z_erofs_collector *clt,
366 struct page *page, enum z_erofs_page_type type,
371 /* give priority for inplaceio */
372 if (clt->mode >= COLLECT_PRIMARY &&
373 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
374 z_erofs_try_inplace_io(clt, page))
377 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
379 clt->cl->vcnt += (unsigned int)ret;
380 return ret ? 0 : -EAGAIN;
383 static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt)
385 struct z_erofs_pcluster *pcl = clt->pcl;
386 z_erofs_next_pcluster_t *owned_head = &clt->owned_head;
388 /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
389 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
390 *owned_head) == Z_EROFS_PCLUSTER_NIL) {
391 *owned_head = &pcl->next;
392 /* so we can attach this pcluster to our submission chain. */
393 clt->mode = COLLECT_PRIMARY_FOLLOWED;
398 * type 2, link to the end of an existing open chain, be careful
399 * that its submission is controlled by the original attached chain.
401 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
402 *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
403 *owned_head = Z_EROFS_PCLUSTER_TAIL;
404 clt->mode = COLLECT_PRIMARY_HOOKED;
408 /* type 3, it belongs to a chain, but it isn't the end of the chain */
409 clt->mode = COLLECT_PRIMARY;
412 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
414 struct erofs_map_blocks *map)
416 struct z_erofs_pcluster *pcl = clt->pcl;
417 struct z_erofs_collection *cl;
420 /* to avoid unexpected loop formed by corrupted images */
421 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
423 return -EFSCORRUPTED;
426 cl = z_erofs_primarycollection(pcl);
427 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
429 return -EFSCORRUPTED;
432 length = READ_ONCE(pcl->length);
433 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
434 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
436 return -EFSCORRUPTED;
439 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
441 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
442 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
444 while (llen > length &&
445 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
447 length = READ_ONCE(pcl->length);
450 mutex_lock(&cl->lock);
451 /* used to check tail merging loop due to corrupted images */
452 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
455 z_erofs_try_to_claim_pcluster(clt);
460 static int z_erofs_register_collection(struct z_erofs_collector *clt,
462 struct erofs_map_blocks *map)
464 bool ztailpacking = map->m_flags & EROFS_MAP_META;
465 struct z_erofs_pcluster *pcl;
466 struct z_erofs_collection *cl;
467 struct erofs_workgroup *grp;
470 if (!(map->m_flags & EROFS_MAP_ENCODED)) {
472 return -EFSCORRUPTED;
475 /* no available pcluster, let's allocate one */
476 pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
477 map->m_plen >> PAGE_SHIFT);
481 atomic_set(&pcl->obj.refcount, 1);
482 pcl->algorithmformat = map->m_algorithmformat;
483 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
484 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
485 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
487 /* new pclusters should be claimed as type 1, primary and followed */
488 pcl->next = clt->owned_head;
489 clt->mode = COLLECT_PRIMARY_FOLLOWED;
491 cl = z_erofs_primarycollection(pcl);
492 cl->pageofs = map->m_la & ~PAGE_MASK;
495 * lock all primary followed works before visible to others
496 * and mutex_trylock *never* fails for a new pcluster.
498 mutex_init(&cl->lock);
499 DBG_BUGON(!mutex_trylock(&cl->lock));
502 pcl->obj.index = 0; /* which indicates ztailpacking */
503 pcl->pageofs_in = erofs_blkoff(map->m_pa);
504 pcl->tailpacking_size = map->m_plen;
506 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
508 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
514 if (grp != &pcl->obj) {
515 clt->pcl = container_of(grp,
516 struct z_erofs_pcluster, obj);
521 /* used to check tail merging loop due to corrupted images */
522 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
524 clt->owned_head = &pcl->next;
530 mutex_unlock(&cl->lock);
531 z_erofs_free_pcluster(pcl);
535 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
537 struct erofs_map_blocks *map)
539 struct erofs_workgroup *grp;
544 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
545 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
546 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
548 if (map->m_flags & EROFS_MAP_META) {
549 if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
551 return -EFSCORRUPTED;
556 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
558 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
561 ret = z_erofs_register_collection(clt, inode, map);
568 ret = z_erofs_lookup_collection(clt, inode, map);
570 erofs_workgroup_put(&clt->pcl->obj);
575 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
576 clt->cl->pagevec, clt->cl->vcnt);
577 /* since file-backed online pages are traversed in reverse order */
578 clt->icpage_ptr = clt->pcl->compressed_pages +
579 z_erofs_pclusterpages(clt->pcl);
584 * keep in mind that no referenced pclusters will be freed
585 * only after a RCU grace period.
587 static void z_erofs_rcu_callback(struct rcu_head *head)
589 struct z_erofs_collection *const cl =
590 container_of(head, struct z_erofs_collection, rcu);
592 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
593 primary_collection));
596 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
598 struct z_erofs_pcluster *const pcl =
599 container_of(grp, struct z_erofs_pcluster, obj);
600 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
602 call_rcu(&cl->rcu, z_erofs_rcu_callback);
605 static void z_erofs_collection_put(struct z_erofs_collection *cl)
607 struct z_erofs_pcluster *const pcl =
608 container_of(cl, struct z_erofs_pcluster, primary_collection);
610 erofs_workgroup_put(&pcl->obj);
613 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
615 struct z_erofs_collection *cl = clt->cl;
620 z_erofs_pagevec_ctor_exit(&clt->vector, false);
621 mutex_unlock(&cl->lock);
624 * if all pending pages are added, don't hold its reference
625 * any longer if the pcluster isn't hosted by ourselves.
627 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
628 z_erofs_collection_put(cl);
634 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
635 unsigned int cachestrategy,
638 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
644 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
648 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
649 struct page *page, struct page **pagepool)
651 struct inode *const inode = fe->inode;
652 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
653 struct erofs_map_blocks *const map = &fe->map;
654 struct z_erofs_collector *const clt = &fe->clt;
655 const loff_t offset = page_offset(page);
658 enum z_erofs_cache_alloctype cache_strategy;
659 enum z_erofs_page_type page_type;
660 unsigned int cur, end, spiltted, index;
663 /* register locked file pages as online pages in pack */
664 z_erofs_onlinepage_init(page);
671 /* lucky, within the range of the current map_blocks */
672 if (offset + cur >= map->m_la &&
673 offset + cur < map->m_la + map->m_llen) {
674 /* didn't get a valid collection previously (very rare) */
680 /* go ahead the next map_blocks */
681 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
683 if (z_erofs_collector_end(clt))
684 fe->backmost = false;
686 map->m_la = offset + cur;
688 err = z_erofs_map_blocks_iter(inode, map, 0);
693 if (!(map->m_flags & EROFS_MAP_MAPPED))
696 err = z_erofs_collector_begin(clt, inode, map);
700 if (z_erofs_is_inline_pcluster(clt->pcl)) {
703 mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
704 erofs_blknr(map->m_pa), EROFS_NO_KMAP);
707 erofs_err(inode->i_sb,
708 "failed to get inline page, err %d", err);
711 get_page(fe->map.buf.page);
712 WRITE_ONCE(clt->pcl->compressed_pages[0], fe->map.buf.page);
713 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
715 /* preload all compressed pages (can change mode if needed) */
716 if (should_alloc_managed_pages(fe, sbi->opt.cache_strategy,
718 cache_strategy = TRYALLOC;
720 cache_strategy = DONTALLOC;
722 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
723 cache_strategy, pagepool);
728 * Ensure the current partial page belongs to this submit chain rather
729 * than other concurrent submit chains or the noio(bypass) chain since
730 * those chains are handled asynchronously thus the page cannot be used
731 * for inplace I/O or pagevec (should be processed in strict order.)
733 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
734 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
736 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
737 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
738 zero_user_segment(page, cur, end);
742 /* let's derive page type */
743 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
744 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
745 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
746 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
749 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
752 err = z_erofs_attach_page(clt, page, page_type,
753 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
754 /* should allocate an additional short-lived page for pagevec */
755 if (err == -EAGAIN) {
756 struct page *const newpage =
757 alloc_page(GFP_NOFS | __GFP_NOFAIL);
759 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
760 err = z_erofs_attach_page(clt, newpage,
761 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
769 index = page->index - (map->m_la >> PAGE_SHIFT);
771 z_erofs_onlinepage_fixup(page, index, true);
773 /* bump up the number of spiltted parts of a page */
775 /* also update nr_pages */
776 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
778 /* can be used for verification */
779 map->m_llen = offset + cur - map->m_la;
786 z_erofs_onlinepage_endio(page);
788 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
789 __func__, page, spiltted, map->m_llen);
792 /* if some error occurred while processing this page */
798 static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
799 unsigned int readahead_pages)
801 /* auto: enable for readpage, disable for readahead */
802 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
806 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
807 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
813 static bool z_erofs_page_is_invalidated(struct page *page)
815 return !page->mapping && !z_erofs_is_shortlived_page(page);
818 static int z_erofs_decompress_pcluster(struct super_block *sb,
819 struct z_erofs_pcluster *pcl,
820 struct page **pagepool)
822 struct erofs_sb_info *const sbi = EROFS_SB(sb);
823 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
824 struct z_erofs_pagevec_ctor ctor;
825 unsigned int i, inputsize, outputsize, llen, nr_pages;
826 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
827 struct page **pages, **compressed_pages, *page;
829 enum z_erofs_page_type page_type;
830 bool overlapped, partial;
831 struct z_erofs_collection *cl;
835 cl = z_erofs_primarycollection(pcl);
836 DBG_BUGON(!READ_ONCE(cl->nr_pages));
838 mutex_lock(&cl->lock);
839 nr_pages = cl->nr_pages;
841 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
842 pages = pages_onstack;
843 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
844 mutex_trylock(&z_pagemap_global_lock)) {
845 pages = z_pagemap_global;
847 gfp_t gfp_flags = GFP_KERNEL;
849 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
850 gfp_flags |= __GFP_NOFAIL;
852 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
855 /* fallback to global pagemap for the lowmem scenario */
857 mutex_lock(&z_pagemap_global_lock);
858 pages = z_pagemap_global;
862 for (i = 0; i < nr_pages; ++i)
866 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
869 for (i = 0; i < cl->vcnt; ++i) {
872 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
874 /* all pages in pagevec ought to be valid */
876 DBG_BUGON(z_erofs_page_is_invalidated(page));
878 if (z_erofs_put_shortlivedpage(pagepool, page))
881 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
884 pagenr = z_erofs_onlinepage_index(page);
886 DBG_BUGON(pagenr >= nr_pages);
889 * currently EROFS doesn't support multiref(dedup),
890 * so here erroring out one multiref page.
894 SetPageError(pages[pagenr]);
895 z_erofs_onlinepage_endio(pages[pagenr]);
898 pages[pagenr] = page;
900 z_erofs_pagevec_ctor_exit(&ctor, true);
903 compressed_pages = pcl->compressed_pages;
905 for (i = 0; i < pclusterpages; ++i) {
908 page = compressed_pages[i];
909 /* all compressed pages ought to be valid */
912 if (z_erofs_is_inline_pcluster(pcl)) {
913 if (!PageUptodate(page))
918 DBG_BUGON(z_erofs_page_is_invalidated(page));
919 if (!z_erofs_is_shortlived_page(page)) {
920 if (erofs_page_is_managed(sbi, page)) {
921 if (!PageUptodate(page))
927 * only if non-head page can be selected
928 * for inplace decompression
930 pagenr = z_erofs_onlinepage_index(page);
932 DBG_BUGON(pagenr >= nr_pages);
935 SetPageError(pages[pagenr]);
936 z_erofs_onlinepage_endio(pages[pagenr]);
939 pages[pagenr] = page;
944 /* PG_error needs checking for all non-managed pages */
945 if (PageError(page)) {
946 DBG_BUGON(PageUptodate(page));
954 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
955 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
957 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
959 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
963 if (z_erofs_is_inline_pcluster(pcl))
964 inputsize = pcl->tailpacking_size;
966 inputsize = pclusterpages * PAGE_SIZE;
968 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
970 .in = compressed_pages,
972 .pageofs_in = pcl->pageofs_in,
973 .pageofs_out = cl->pageofs,
974 .inputsize = inputsize,
975 .outputsize = outputsize,
976 .alg = pcl->algorithmformat,
977 .inplace_io = overlapped,
978 .partial_decoding = partial
982 /* must handle all compressed pages before actual file pages */
983 if (z_erofs_is_inline_pcluster(pcl)) {
984 page = compressed_pages[0];
985 WRITE_ONCE(compressed_pages[0], NULL);
988 for (i = 0; i < pclusterpages; ++i) {
989 page = compressed_pages[i];
991 if (erofs_page_is_managed(sbi, page))
994 /* recycle all individual short-lived pages */
995 (void)z_erofs_put_shortlivedpage(pagepool, page);
996 WRITE_ONCE(compressed_pages[i], NULL);
1000 for (i = 0; i < nr_pages; ++i) {
1005 DBG_BUGON(z_erofs_page_is_invalidated(page));
1007 /* recycle all individual short-lived pages */
1008 if (z_erofs_put_shortlivedpage(pagepool, page))
1014 z_erofs_onlinepage_endio(page);
1017 if (pages == z_pagemap_global)
1018 mutex_unlock(&z_pagemap_global_lock);
1019 else if (pages != pages_onstack)
1025 /* all cl locks MUST be taken before the following line */
1026 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1028 /* all cl locks SHOULD be released right now */
1029 mutex_unlock(&cl->lock);
1031 z_erofs_collection_put(cl);
1035 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1036 struct page **pagepool)
1038 z_erofs_next_pcluster_t owned = io->head;
1040 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1041 struct z_erofs_pcluster *pcl;
1043 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1044 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1046 /* no possible that 'owned' equals NULL */
1047 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1049 pcl = container_of(owned, struct z_erofs_pcluster, next);
1050 owned = READ_ONCE(pcl->next);
1052 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
1056 static void z_erofs_decompressqueue_work(struct work_struct *work)
1058 struct z_erofs_decompressqueue *bgq =
1059 container_of(work, struct z_erofs_decompressqueue, u.work);
1060 struct page *pagepool = NULL;
1062 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1063 z_erofs_decompress_queue(bgq, &pagepool);
1065 erofs_release_pages(&pagepool);
1069 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1070 bool sync, int bios)
1072 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1074 /* wake up the caller thread for sync decompression */
1076 unsigned long flags;
1078 spin_lock_irqsave(&io->u.wait.lock, flags);
1079 if (!atomic_add_return(bios, &io->pending_bios))
1080 wake_up_locked(&io->u.wait);
1081 spin_unlock_irqrestore(&io->u.wait.lock, flags);
1085 if (atomic_add_return(bios, &io->pending_bios))
1087 /* Use workqueue and sync decompression for atomic contexts only */
1088 if (in_atomic() || irqs_disabled()) {
1089 queue_work(z_erofs_workqueue, &io->u.work);
1090 /* enable sync decompression for readahead */
1091 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1092 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1095 z_erofs_decompressqueue_work(&io->u.work);
1098 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1100 struct page **pagepool,
1101 struct address_space *mc,
1104 const pgoff_t index = pcl->obj.index;
1105 bool tocache = false;
1107 struct address_space *mapping;
1108 struct page *oldpage, *page;
1110 compressed_page_t t;
1114 page = READ_ONCE(pcl->compressed_pages[nr]);
1120 /* process the target tagged pointer */
1121 t = tagptr_init(compressed_page_t, page);
1122 justfound = tagptr_unfold_tags(t);
1123 page = tagptr_unfold_ptr(t);
1126 * preallocated cached pages, which is used to avoid direct reclaim
1127 * otherwise, it will go inplace I/O path instead.
1129 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1130 WRITE_ONCE(pcl->compressed_pages[nr], page);
1131 set_page_private(page, 0);
1135 mapping = READ_ONCE(page->mapping);
1138 * file-backed online pages in plcuster are all locked steady,
1139 * therefore it is impossible for `mapping' to be NULL.
1141 if (mapping && mapping != mc)
1142 /* ought to be unmanaged pages */
1145 /* directly return for shortlived page as well */
1146 if (z_erofs_is_shortlived_page(page))
1151 /* only true if page reclaim goes wrong, should never happen */
1152 DBG_BUGON(justfound && PagePrivate(page));
1154 /* the page is still in manage cache */
1155 if (page->mapping == mc) {
1156 WRITE_ONCE(pcl->compressed_pages[nr], page);
1158 ClearPageError(page);
1159 if (!PagePrivate(page)) {
1161 * impossible to be !PagePrivate(page) for
1162 * the current restriction as well if
1163 * the page is already in compressed_pages[].
1165 DBG_BUGON(!justfound);
1168 set_page_private(page, (unsigned long)pcl);
1169 SetPagePrivate(page);
1172 /* no need to submit io if it is already up-to-date */
1173 if (PageUptodate(page)) {
1181 * the managed page has been truncated, it's unsafe to
1182 * reuse this one, let's allocate a new cache-managed page.
1184 DBG_BUGON(page->mapping);
1185 DBG_BUGON(!justfound);
1191 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1192 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1193 erofs_pagepool_add(pagepool, page);
1198 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1199 /* turn into temporary page if fails (1 ref) */
1200 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1203 attach_page_private(page, pcl);
1204 /* drop a refcount added by allocpage (then we have 2 refs here) */
1207 out: /* the only exit (for tracing and debugging) */
1211 static struct z_erofs_decompressqueue *
1212 jobqueue_init(struct super_block *sb,
1213 struct z_erofs_decompressqueue *fgq, bool *fg)
1215 struct z_erofs_decompressqueue *q;
1218 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1223 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1227 init_waitqueue_head(&fgq->u.wait);
1228 atomic_set(&fgq->pending_bios, 0);
1231 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1235 /* define decompression jobqueue types */
1242 static void *jobqueueset_init(struct super_block *sb,
1243 struct z_erofs_decompressqueue *q[],
1244 struct z_erofs_decompressqueue *fgq, bool *fg)
1247 * if managed cache is enabled, bypass jobqueue is needed,
1248 * no need to read from device for all pclusters in this queue.
1250 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1251 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1253 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1256 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1257 z_erofs_next_pcluster_t qtail[],
1258 z_erofs_next_pcluster_t owned_head)
1260 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1261 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1263 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1264 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1265 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1267 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1269 WRITE_ONCE(*submit_qtail, owned_head);
1270 WRITE_ONCE(*bypass_qtail, &pcl->next);
1272 qtail[JQ_BYPASS] = &pcl->next;
1275 static void z_erofs_decompressqueue_endio(struct bio *bio)
1277 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
1278 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
1279 blk_status_t err = bio->bi_status;
1280 struct bio_vec *bvec;
1281 struct bvec_iter_all iter_all;
1283 bio_for_each_segment_all(bvec, bio, iter_all) {
1284 struct page *page = bvec->bv_page;
1286 DBG_BUGON(PageUptodate(page));
1287 DBG_BUGON(z_erofs_page_is_invalidated(page));
1292 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
1294 SetPageUptodate(page);
1298 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
1302 static void z_erofs_submit_queue(struct super_block *sb,
1303 struct z_erofs_decompress_frontend *f,
1304 struct page **pagepool,
1305 struct z_erofs_decompressqueue *fgq,
1308 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1309 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1310 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1312 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1313 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1315 struct block_device *last_bdev;
1316 unsigned int nr_bios = 0;
1317 struct bio *bio = NULL;
1319 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1320 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1321 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1323 /* by default, all need io submission */
1324 q[JQ_SUBMIT]->head = owned_head;
1327 struct erofs_map_dev mdev;
1328 struct z_erofs_pcluster *pcl;
1333 /* no possible 'owned_head' equals the following */
1334 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1335 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1337 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1339 /* close the main owned chain at first */
1340 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1341 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1342 if (z_erofs_is_inline_pcluster(pcl)) {
1343 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1347 /* no device id here, thus it will always succeed */
1348 mdev = (struct erofs_map_dev) {
1349 .m_pa = blknr_to_addr(pcl->obj.index),
1351 (void)erofs_map_dev(sb, &mdev);
1353 cur = erofs_blknr(mdev.m_pa);
1354 end = cur + pcl->pclusterpages;
1359 page = pickup_page_for_submission(pcl, i++, pagepool,
1365 if (bio && (cur != last_index + 1 ||
1366 last_bdev != mdev.m_bdev)) {
1373 bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS);
1374 bio->bi_end_io = z_erofs_decompressqueue_endio;
1376 bio_set_dev(bio, mdev.m_bdev);
1377 last_bdev = mdev.m_bdev;
1378 bio->bi_iter.bi_sector = (sector_t)cur <<
1379 LOG_SECTORS_PER_BLOCK;
1380 bio->bi_private = bi_private;
1381 bio->bi_opf = REQ_OP_READ;
1383 bio->bi_opf |= REQ_RAHEAD;
1387 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1388 goto submit_bio_retry;
1392 } while (++cur < end);
1395 qtail[JQ_SUBMIT] = &pcl->next;
1397 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1398 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1404 * although background is preferred, no one is pending for submission.
1405 * don't issue workqueue for decompression but drop it directly instead.
1407 if (!*force_fg && !nr_bios) {
1408 kvfree(q[JQ_SUBMIT]);
1411 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1414 static void z_erofs_runqueue(struct super_block *sb,
1415 struct z_erofs_decompress_frontend *f,
1416 struct page **pagepool, bool force_fg)
1418 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1420 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1422 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1424 /* handle bypass queue (no i/o pclusters) immediately */
1425 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1430 /* wait until all bios are completed */
1431 io_wait_event(io[JQ_SUBMIT].u.wait,
1432 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1434 /* handle synchronous decompress queue in the caller context */
1435 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1439 * Since partial uptodate is still unimplemented for now, we have to use
1440 * approximate readmore strategies as a start.
1442 static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1443 struct readahead_control *rac,
1445 struct page **pagepool,
1448 struct inode *inode = f->inode;
1449 struct erofs_map_blocks *map = &f->map;
1455 err = z_erofs_map_blocks_iter(inode, map,
1456 EROFS_GET_BLOCKS_READMORE);
1460 /* expend ra for the trailing edge if readahead */
1462 loff_t newstart = readahead_pos(rac);
1464 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1465 readahead_expand(rac, newstart, cur - newstart);
1468 end = round_up(end, PAGE_SIZE);
1470 end = round_up(map->m_la, PAGE_SIZE);
1476 cur = map->m_la + map->m_llen - 1;
1477 while (cur >= end) {
1478 pgoff_t index = cur >> PAGE_SHIFT;
1481 page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1485 if (PageUptodate(page)) {
1491 err = z_erofs_do_read_page(f, page, pagepool);
1493 erofs_err(inode->i_sb,
1494 "readmore error at page %lu @ nid %llu",
1495 index, EROFS_I(inode)->nid);
1498 if (cur < PAGE_SIZE)
1500 cur = (index << PAGE_SHIFT) - 1;
1504 static int z_erofs_readpage(struct file *file, struct page *page)
1506 struct inode *const inode = page->mapping->host;
1507 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1508 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1509 struct page *pagepool = NULL;
1512 trace_erofs_readpage(page, false);
1513 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1515 z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
1517 err = z_erofs_do_read_page(&f, page, &pagepool);
1518 z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
1520 (void)z_erofs_collector_end(&f.clt);
1522 /* if some compressed cluster ready, need submit them anyway */
1523 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1524 z_erofs_get_sync_decompress_policy(sbi, 0));
1527 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1529 erofs_put_metabuf(&f.map.buf);
1530 erofs_release_pages(&pagepool);
1534 static void z_erofs_readahead(struct readahead_control *rac)
1536 struct inode *const inode = rac->mapping->host;
1537 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1538 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1539 struct page *pagepool = NULL, *head = NULL, *page;
1540 unsigned int nr_pages;
1543 f.headoffset = readahead_pos(rac);
1545 z_erofs_pcluster_readmore(&f, rac, f.headoffset +
1546 readahead_length(rac) - 1, &pagepool, true);
1547 nr_pages = readahead_count(rac);
1548 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1550 while ((page = readahead_page(rac))) {
1551 set_page_private(page, (unsigned long)head);
1556 struct page *page = head;
1559 /* traversal in reverse order */
1560 head = (void *)page_private(page);
1562 err = z_erofs_do_read_page(&f, page, &pagepool);
1564 erofs_err(inode->i_sb,
1565 "readahead error at page %lu @ nid %llu",
1566 page->index, EROFS_I(inode)->nid);
1569 z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
1570 (void)z_erofs_collector_end(&f.clt);
1572 z_erofs_runqueue(inode->i_sb, &f, &pagepool,
1573 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
1574 erofs_put_metabuf(&f.map.buf);
1575 erofs_release_pages(&pagepool);
1578 const struct address_space_operations z_erofs_aops = {
1579 .readpage = z_erofs_readpage,
1580 .readahead = z_erofs_readahead,