1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://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();
48 * no need to spawn too many threads, limiting threads could minimum
49 * scheduling overhead, perhaps per-CPU threads should be better?
51 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
52 WQ_UNBOUND | WQ_HIGHPRI,
53 onlinecpus + onlinecpus / 4);
54 return z_erofs_workqueue ? 0 : -ENOMEM;
57 static void z_erofs_pcluster_init_once(void *ptr)
59 struct z_erofs_pcluster *pcl = ptr;
60 struct z_erofs_collection *cl = z_erofs_primarycollection(pcl);
63 mutex_init(&cl->lock);
66 for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
67 pcl->compressed_pages[i] = NULL;
70 int __init z_erofs_init_zip_subsystem(void)
72 pcluster_cachep = kmem_cache_create("erofs_compress",
73 Z_EROFS_WORKGROUP_SIZE, 0,
75 z_erofs_pcluster_init_once);
76 if (pcluster_cachep) {
77 if (!z_erofs_init_workqueue())
80 kmem_cache_destroy(pcluster_cachep);
85 enum z_erofs_collectmode {
89 * The current collection was the tail of an exist chain, in addition
90 * that the previous processed chained collections are all decided to
92 * A new chain will be created for the remaining collections which are
93 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
94 * the next collection cannot reuse the whole page safely in
95 * the following scenario:
96 * ________________________________________________________________
97 * | tail (partial) page | head (partial) page |
98 * | (belongs to the next cl) | (belongs to the current cl) |
99 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
101 COLLECT_PRIMARY_HOOKED,
102 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
104 * The current collection has been linked with the owned chain, and
105 * could also be linked with the remaining collections, which means
106 * if the processing page is the tail page of the collection, thus
107 * the current collection can safely use the whole page (since
108 * the previous collection is under control) for in-place I/O, as
110 * ________________________________________________________________
111 * | tail (partial) page | head (partial) page |
112 * | (of the current cl) | (of the previous collection) |
113 * | PRIMARY_FOLLOWED or | |
114 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
116 * [ (*) the above page can be used as inplace I/O. ]
118 COLLECT_PRIMARY_FOLLOWED,
121 struct z_erofs_collector {
122 struct z_erofs_pagevec_ctor vector;
124 struct z_erofs_pcluster *pcl, *tailpcl;
125 struct z_erofs_collection *cl;
126 struct page **compressedpages;
127 z_erofs_next_pcluster_t owned_head;
129 enum z_erofs_collectmode mode;
132 struct z_erofs_decompress_frontend {
133 struct inode *const inode;
135 struct z_erofs_collector clt;
136 struct erofs_map_blocks map;
139 /* used for applying cache strategy on the fly */
141 erofs_off_t headoffset;
144 #define COLLECTOR_INIT() { \
145 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
146 .mode = COLLECT_PRIMARY_FOLLOWED }
148 #define DECOMPRESS_FRONTEND_INIT(__i) { \
149 .inode = __i, .clt = COLLECTOR_INIT(), \
152 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
153 static DEFINE_MUTEX(z_pagemap_global_lock);
155 static void preload_compressed_pages(struct z_erofs_collector *clt,
156 struct address_space *mc,
157 enum z_erofs_cache_alloctype type)
159 const struct z_erofs_pcluster *pcl = clt->pcl;
160 const unsigned int clusterpages = BIT(pcl->clusterbits);
161 struct page **pages = clt->compressedpages;
162 pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages);
163 bool standalone = true;
165 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
168 for (; pages < pcl->compressed_pages + clusterpages; ++pages) {
172 /* the compressed page was loaded before */
173 if (READ_ONCE(*pages))
176 page = find_get_page(mc, index);
179 t = tag_compressed_page_justfound(page);
180 } else if (type == DELAYEDALLOC) {
181 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
182 } else { /* DONTALLOC */
184 clt->compressedpages = pages;
189 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
196 if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */
197 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
200 /* called by erofs_shrinker to get rid of all compressed_pages */
201 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
202 struct erofs_workgroup *grp)
204 struct z_erofs_pcluster *const pcl =
205 container_of(grp, struct z_erofs_pcluster, obj);
206 struct address_space *const mapping = MNGD_MAPPING(sbi);
207 const unsigned int clusterpages = BIT(pcl->clusterbits);
211 * refcount of workgroup is now freezed as 1,
212 * therefore no need to worry about available decompression users.
214 for (i = 0; i < clusterpages; ++i) {
215 struct page *page = pcl->compressed_pages[i];
220 /* block other users from reclaiming or migrating the page */
221 if (!trylock_page(page))
224 if (page->mapping != mapping)
227 /* barrier is implied in the following 'unlock_page' */
228 WRITE_ONCE(pcl->compressed_pages[i], NULL);
229 set_page_private(page, 0);
230 ClearPagePrivate(page);
238 int erofs_try_to_free_cached_page(struct address_space *mapping,
241 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
242 const unsigned int clusterpages = BIT(pcl->clusterbits);
243 int ret = 0; /* 0 - busy */
245 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
248 for (i = 0; i < clusterpages; ++i) {
249 if (pcl->compressed_pages[i] == page) {
250 WRITE_ONCE(pcl->compressed_pages[i], NULL);
255 erofs_workgroup_unfreeze(&pcl->obj, 1);
258 ClearPagePrivate(page);
265 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
266 static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
269 struct z_erofs_pcluster *const pcl = clt->pcl;
270 const unsigned int clusterpages = BIT(pcl->clusterbits);
272 while (clt->compressedpages < pcl->compressed_pages + clusterpages) {
273 if (!cmpxchg(clt->compressedpages++, NULL, page))
279 /* callers must be with collection lock held */
280 static int z_erofs_attach_page(struct z_erofs_collector *clt,
282 enum z_erofs_page_type type)
287 /* give priority for inplaceio */
288 if (clt->mode >= COLLECT_PRIMARY &&
289 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
290 z_erofs_try_inplace_io(clt, page))
293 ret = z_erofs_pagevec_enqueue(&clt->vector,
294 page, type, &occupied);
295 clt->cl->vcnt += (unsigned int)ret;
297 return ret ? 0 : -EAGAIN;
300 static enum z_erofs_collectmode
301 try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
302 z_erofs_next_pcluster_t *owned_head)
304 /* let's claim these following types of pclusters */
306 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
307 /* type 1, nil pcluster */
308 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
309 *owned_head) != Z_EROFS_PCLUSTER_NIL)
312 *owned_head = &pcl->next;
313 /* lucky, I am the followee :) */
314 return COLLECT_PRIMARY_FOLLOWED;
315 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
317 * type 2, link to the end of a existing open chain,
318 * be careful that its submission itself is governed
319 * by the original owned chain.
321 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
322 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
324 *owned_head = Z_EROFS_PCLUSTER_TAIL;
325 return COLLECT_PRIMARY_HOOKED;
327 return COLLECT_PRIMARY; /* :( better luck next time */
330 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
332 struct erofs_map_blocks *map)
334 struct z_erofs_pcluster *pcl = clt->pcl;
335 struct z_erofs_collection *cl;
338 /* to avoid unexpected loop formed by corrupted images */
339 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
341 return -EFSCORRUPTED;
344 cl = z_erofs_primarycollection(pcl);
345 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
347 return -EFSCORRUPTED;
350 length = READ_ONCE(pcl->length);
351 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
352 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
354 return -EFSCORRUPTED;
357 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
359 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
360 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
362 while (llen > length &&
363 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
365 length = READ_ONCE(pcl->length);
368 mutex_lock(&cl->lock);
369 /* used to check tail merging loop due to corrupted images */
370 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
372 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
373 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
374 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
380 static int z_erofs_register_collection(struct z_erofs_collector *clt,
382 struct erofs_map_blocks *map)
384 struct z_erofs_pcluster *pcl;
385 struct z_erofs_collection *cl;
386 struct erofs_workgroup *grp;
389 /* no available workgroup, let's allocate one */
390 pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS);
394 atomic_set(&pcl->obj.refcount, 1);
395 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
397 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
398 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
399 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
401 if (map->m_flags & EROFS_MAP_ZIPPED)
402 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
404 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
406 pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0];
407 pcl->clusterbits -= PAGE_SHIFT;
409 /* new pclusters should be claimed as type 1, primary and followed */
410 pcl->next = clt->owned_head;
411 clt->mode = COLLECT_PRIMARY_FOLLOWED;
413 cl = z_erofs_primarycollection(pcl);
415 /* must be cleaned before freeing to slab */
416 DBG_BUGON(cl->nr_pages);
419 cl->pageofs = map->m_la & ~PAGE_MASK;
422 * lock all primary followed works before visible to others
423 * and mutex_trylock *never* fails for a new pcluster.
425 DBG_BUGON(!mutex_trylock(&cl->lock));
427 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
433 if (grp != &pcl->obj) {
434 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
438 /* used to check tail merging loop due to corrupted images */
439 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
441 clt->owned_head = &pcl->next;
447 mutex_unlock(&cl->lock);
448 kmem_cache_free(pcluster_cachep, pcl);
452 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
454 struct erofs_map_blocks *map)
456 struct erofs_workgroup *grp;
461 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
462 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
463 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
465 if (!PAGE_ALIGNED(map->m_pa)) {
470 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
472 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
474 ret = z_erofs_register_collection(clt, inode, map);
482 ret = z_erofs_lookup_collection(clt, inode, map);
484 erofs_workgroup_put(&clt->pcl->obj);
489 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
490 clt->cl->pagevec, clt->cl->vcnt);
492 clt->compressedpages = clt->pcl->compressed_pages;
493 if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */
494 clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES;
499 * keep in mind that no referenced pclusters will be freed
500 * only after a RCU grace period.
502 static void z_erofs_rcu_callback(struct rcu_head *head)
504 struct z_erofs_collection *const cl =
505 container_of(head, struct z_erofs_collection, rcu);
507 kmem_cache_free(pcluster_cachep,
508 container_of(cl, struct z_erofs_pcluster,
509 primary_collection));
512 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
514 struct z_erofs_pcluster *const pcl =
515 container_of(grp, struct z_erofs_pcluster, obj);
516 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
518 call_rcu(&cl->rcu, z_erofs_rcu_callback);
521 static void z_erofs_collection_put(struct z_erofs_collection *cl)
523 struct z_erofs_pcluster *const pcl =
524 container_of(cl, struct z_erofs_pcluster, primary_collection);
526 erofs_workgroup_put(&pcl->obj);
529 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
531 struct z_erofs_collection *cl = clt->cl;
536 z_erofs_pagevec_ctor_exit(&clt->vector, false);
537 mutex_unlock(&cl->lock);
540 * if all pending pages are added, don't hold its reference
541 * any longer if the pcluster isn't hosted by ourselves.
543 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
544 z_erofs_collection_put(cl);
550 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
551 unsigned int cachestrategy,
554 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
560 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
564 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
567 struct inode *const inode = fe->inode;
568 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
569 struct erofs_map_blocks *const map = &fe->map;
570 struct z_erofs_collector *const clt = &fe->clt;
571 const loff_t offset = page_offset(page);
574 enum z_erofs_cache_alloctype cache_strategy;
575 enum z_erofs_page_type page_type;
576 unsigned int cur, end, spiltted, index;
579 /* register locked file pages as online pages in pack */
580 z_erofs_onlinepage_init(page);
587 /* lucky, within the range of the current map_blocks */
588 if (offset + cur >= map->m_la &&
589 offset + cur < map->m_la + map->m_llen) {
590 /* didn't get a valid collection previously (very rare) */
596 /* go ahead the next map_blocks */
597 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
599 if (z_erofs_collector_end(clt))
600 fe->backmost = false;
602 map->m_la = offset + cur;
604 err = z_erofs_map_blocks_iter(inode, map, 0);
609 if (!(map->m_flags & EROFS_MAP_MAPPED))
612 err = z_erofs_collector_begin(clt, inode, map);
616 /* preload all compressed pages (maybe downgrade role if necessary) */
617 if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
618 cache_strategy = DELAYEDALLOC;
620 cache_strategy = DONTALLOC;
622 preload_compressed_pages(clt, MNGD_MAPPING(sbi), cache_strategy);
626 * Ensure the current partial page belongs to this submit chain rather
627 * than other concurrent submit chains or the noio(bypass) chain since
628 * those chains are handled asynchronously thus the page cannot be used
629 * for inplace I/O or pagevec (should be processed in strict order.)
631 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
632 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
634 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
635 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
636 zero_user_segment(page, cur, end);
640 /* let's derive page type */
641 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
642 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
643 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
644 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
647 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
650 err = z_erofs_attach_page(clt, page, page_type);
651 /* should allocate an additional staging page for pagevec */
652 if (err == -EAGAIN) {
653 struct page *const newpage =
654 alloc_page(GFP_NOFS | __GFP_NOFAIL);
656 newpage->mapping = Z_EROFS_MAPPING_STAGING;
657 err = z_erofs_attach_page(clt, newpage,
658 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
666 index = page->index - (map->m_la >> PAGE_SHIFT);
668 z_erofs_onlinepage_fixup(page, index, true);
670 /* bump up the number of spiltted parts of a page */
672 /* also update nr_pages */
673 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
675 /* can be used for verification */
676 map->m_llen = offset + cur - map->m_la;
683 z_erofs_onlinepage_endio(page);
685 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
686 __func__, page, spiltted, map->m_llen);
689 /* if some error occurred while processing this page */
695 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
698 /* wake up the caller thread for sync decompression */
702 spin_lock_irqsave(&io->u.wait.lock, flags);
703 if (!atomic_add_return(bios, &io->pending_bios))
704 wake_up_locked(&io->u.wait);
705 spin_unlock_irqrestore(&io->u.wait.lock, flags);
709 if (!atomic_add_return(bios, &io->pending_bios))
710 queue_work(z_erofs_workqueue, &io->u.work);
713 static void z_erofs_decompressqueue_endio(struct bio *bio)
715 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
716 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
717 blk_status_t err = bio->bi_status;
718 struct bio_vec *bvec;
719 struct bvec_iter_all iter_all;
721 bio_for_each_segment_all(bvec, bio, iter_all) {
722 struct page *page = bvec->bv_page;
724 DBG_BUGON(PageUptodate(page));
725 DBG_BUGON(!page->mapping);
730 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
732 SetPageUptodate(page);
736 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
740 static int z_erofs_decompress_pcluster(struct super_block *sb,
741 struct z_erofs_pcluster *pcl,
742 struct list_head *pagepool)
744 struct erofs_sb_info *const sbi = EROFS_SB(sb);
745 const unsigned int clusterpages = BIT(pcl->clusterbits);
746 struct z_erofs_pagevec_ctor ctor;
747 unsigned int i, outputsize, llen, nr_pages;
748 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
749 struct page **pages, **compressed_pages, *page;
751 enum z_erofs_page_type page_type;
752 bool overlapped, partial;
753 struct z_erofs_collection *cl;
757 cl = z_erofs_primarycollection(pcl);
758 DBG_BUGON(!READ_ONCE(cl->nr_pages));
760 mutex_lock(&cl->lock);
761 nr_pages = cl->nr_pages;
763 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
764 pages = pages_onstack;
765 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
766 mutex_trylock(&z_pagemap_global_lock)) {
767 pages = z_pagemap_global;
769 gfp_t gfp_flags = GFP_KERNEL;
771 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
772 gfp_flags |= __GFP_NOFAIL;
774 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
777 /* fallback to global pagemap for the lowmem scenario */
779 mutex_lock(&z_pagemap_global_lock);
780 pages = z_pagemap_global;
784 for (i = 0; i < nr_pages; ++i)
788 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
791 for (i = 0; i < cl->vcnt; ++i) {
794 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
796 /* all pages in pagevec ought to be valid */
798 DBG_BUGON(!page->mapping);
800 if (z_erofs_put_stagingpage(pagepool, page))
803 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
806 pagenr = z_erofs_onlinepage_index(page);
808 DBG_BUGON(pagenr >= nr_pages);
811 * currently EROFS doesn't support multiref(dedup),
812 * so here erroring out one multiref page.
816 SetPageError(pages[pagenr]);
817 z_erofs_onlinepage_endio(pages[pagenr]);
820 pages[pagenr] = page;
822 z_erofs_pagevec_ctor_exit(&ctor, true);
825 compressed_pages = pcl->compressed_pages;
827 for (i = 0; i < clusterpages; ++i) {
830 page = compressed_pages[i];
832 /* all compressed pages ought to be valid */
834 DBG_BUGON(!page->mapping);
836 if (!z_erofs_page_is_staging(page)) {
837 if (erofs_page_is_managed(sbi, page)) {
838 if (!PageUptodate(page))
844 * only if non-head page can be selected
845 * for inplace decompression
847 pagenr = z_erofs_onlinepage_index(page);
849 DBG_BUGON(pagenr >= nr_pages);
852 SetPageError(pages[pagenr]);
853 z_erofs_onlinepage_endio(pages[pagenr]);
856 pages[pagenr] = page;
861 /* PG_error needs checking for inplaced and staging pages */
862 if (PageError(page)) {
863 DBG_BUGON(PageUptodate(page));
871 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
872 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
874 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
876 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
880 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
882 .in = compressed_pages,
884 .pageofs_out = cl->pageofs,
885 .inputsize = PAGE_SIZE,
886 .outputsize = outputsize,
887 .alg = pcl->algorithmformat,
888 .inplace_io = overlapped,
889 .partial_decoding = partial
893 /* must handle all compressed pages before endding pages */
894 for (i = 0; i < clusterpages; ++i) {
895 page = compressed_pages[i];
897 if (erofs_page_is_managed(sbi, page))
900 /* recycle all individual staging pages */
901 (void)z_erofs_put_stagingpage(pagepool, page);
903 WRITE_ONCE(compressed_pages[i], NULL);
906 for (i = 0; i < nr_pages; ++i) {
911 DBG_BUGON(!page->mapping);
913 /* recycle all individual staging pages */
914 if (z_erofs_put_stagingpage(pagepool, page))
920 z_erofs_onlinepage_endio(page);
923 if (pages == z_pagemap_global)
924 mutex_unlock(&z_pagemap_global_lock);
925 else if (pages != pages_onstack)
931 /* all cl locks MUST be taken before the following line */
932 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
934 /* all cl locks SHOULD be released right now */
935 mutex_unlock(&cl->lock);
937 z_erofs_collection_put(cl);
941 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
942 struct list_head *pagepool)
944 z_erofs_next_pcluster_t owned = io->head;
946 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
947 struct z_erofs_pcluster *pcl;
949 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
950 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
952 /* no possible that 'owned' equals NULL */
953 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
955 pcl = container_of(owned, struct z_erofs_pcluster, next);
956 owned = READ_ONCE(pcl->next);
958 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
962 static void z_erofs_decompressqueue_work(struct work_struct *work)
964 struct z_erofs_decompressqueue *bgq =
965 container_of(work, struct z_erofs_decompressqueue, u.work);
968 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
969 z_erofs_decompress_queue(bgq, &pagepool);
971 put_pages_list(&pagepool);
975 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
977 struct list_head *pagepool,
978 struct address_space *mc,
981 const pgoff_t index = pcl->obj.index;
982 bool tocache = false;
984 struct address_space *mapping;
985 struct page *oldpage, *page;
991 page = READ_ONCE(pcl->compressed_pages[nr]);
998 * the cached page has not been allocated and
999 * an placeholder is out there, prepare it now.
1001 if (page == PAGE_UNALLOCATED) {
1006 /* process the target tagged pointer */
1007 t = tagptr_init(compressed_page_t, page);
1008 justfound = tagptr_unfold_tags(t);
1009 page = tagptr_unfold_ptr(t);
1011 mapping = READ_ONCE(page->mapping);
1014 * unmanaged (file) pages are all locked solidly,
1015 * therefore it is impossible for `mapping' to be NULL.
1017 if (mapping && mapping != mc)
1018 /* ought to be unmanaged pages */
1023 /* only true if page reclaim goes wrong, should never happen */
1024 DBG_BUGON(justfound && PagePrivate(page));
1026 /* the page is still in manage cache */
1027 if (page->mapping == mc) {
1028 WRITE_ONCE(pcl->compressed_pages[nr], page);
1030 ClearPageError(page);
1031 if (!PagePrivate(page)) {
1033 * impossible to be !PagePrivate(page) for
1034 * the current restriction as well if
1035 * the page is already in compressed_pages[].
1037 DBG_BUGON(!justfound);
1040 set_page_private(page, (unsigned long)pcl);
1041 SetPagePrivate(page);
1044 /* no need to submit io if it is already up-to-date */
1045 if (PageUptodate(page)) {
1053 * the managed page has been truncated, it's unsafe to
1054 * reuse this one, let's allocate a new cache-managed page.
1056 DBG_BUGON(page->mapping);
1057 DBG_BUGON(!justfound);
1063 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1064 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1065 /* non-LRU / non-movable temporary page is needed */
1066 page->mapping = Z_EROFS_MAPPING_STAGING;
1070 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1072 /* since it added to managed cache successfully */
1076 list_add(&page->lru, pagepool);
1081 set_page_private(page, (unsigned long)pcl);
1082 SetPagePrivate(page);
1083 out: /* the only exit (for tracing and debugging) */
1087 static struct z_erofs_decompressqueue *
1088 jobqueue_init(struct super_block *sb,
1089 struct z_erofs_decompressqueue *fgq, bool *fg)
1091 struct z_erofs_decompressqueue *q;
1094 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1099 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1103 init_waitqueue_head(&fgq->u.wait);
1104 atomic_set(&fgq->pending_bios, 0);
1107 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1111 /* define decompression jobqueue types */
1118 static void *jobqueueset_init(struct super_block *sb,
1119 struct z_erofs_decompressqueue *q[],
1120 struct z_erofs_decompressqueue *fgq, bool *fg)
1123 * if managed cache is enabled, bypass jobqueue is needed,
1124 * no need to read from device for all pclusters in this queue.
1126 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1127 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1129 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1132 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1133 z_erofs_next_pcluster_t qtail[],
1134 z_erofs_next_pcluster_t owned_head)
1136 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1137 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1139 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1140 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1141 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1143 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1145 WRITE_ONCE(*submit_qtail, owned_head);
1146 WRITE_ONCE(*bypass_qtail, &pcl->next);
1148 qtail[JQ_BYPASS] = &pcl->next;
1151 static void z_erofs_submit_queue(struct super_block *sb,
1152 struct z_erofs_decompress_frontend *f,
1153 struct list_head *pagepool,
1154 struct z_erofs_decompressqueue *fgq,
1157 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1158 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1159 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1161 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1162 /* since bio will be NULL, no need to initialize last_index */
1164 unsigned int nr_bios = 0;
1165 struct bio *bio = NULL;
1167 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1168 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1169 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1171 /* by default, all need io submission */
1172 q[JQ_SUBMIT]->head = owned_head;
1175 struct z_erofs_pcluster *pcl;
1180 /* no possible 'owned_head' equals the following */
1181 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1182 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1184 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1186 cur = pcl->obj.index;
1187 end = cur + BIT(pcl->clusterbits);
1189 /* close the main owned chain at first */
1190 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1191 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1196 page = pickup_page_for_submission(pcl, i++, pagepool,
1202 if (bio && cur != last_index + 1) {
1209 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1211 bio->bi_end_io = z_erofs_decompressqueue_endio;
1212 bio_set_dev(bio, sb->s_bdev);
1213 bio->bi_iter.bi_sector = (sector_t)cur <<
1214 LOG_SECTORS_PER_BLOCK;
1215 bio->bi_private = bi_private;
1216 bio->bi_opf = REQ_OP_READ;
1218 bio->bi_opf |= REQ_RAHEAD;
1222 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1223 goto submit_bio_retry;
1227 } while (++cur < end);
1230 qtail[JQ_SUBMIT] = &pcl->next;
1232 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1233 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1239 * although background is preferred, no one is pending for submission.
1240 * don't issue workqueue for decompression but drop it directly instead.
1242 if (!*force_fg && !nr_bios) {
1243 kvfree(q[JQ_SUBMIT]);
1246 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1249 static void z_erofs_runqueue(struct super_block *sb,
1250 struct z_erofs_decompress_frontend *f,
1251 struct list_head *pagepool, bool force_fg)
1253 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1255 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1257 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1259 /* handle bypass queue (no i/o pclusters) immediately */
1260 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1265 /* wait until all bios are completed */
1266 io_wait_event(io[JQ_SUBMIT].u.wait,
1267 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1269 /* handle synchronous decompress queue in the caller context */
1270 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1273 static int z_erofs_readpage(struct file *file, struct page *page)
1275 struct inode *const inode = page->mapping->host;
1276 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1278 LIST_HEAD(pagepool);
1280 trace_erofs_readpage(page, false);
1282 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1284 err = z_erofs_do_read_page(&f, page);
1285 (void)z_erofs_collector_end(&f.clt);
1287 /* if some compressed cluster ready, need submit them anyway */
1288 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1291 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1294 put_page(f.map.mpage);
1296 /* clean up the remaining free pages */
1297 put_pages_list(&pagepool);
1301 static void z_erofs_readahead(struct readahead_control *rac)
1303 struct inode *const inode = rac->mapping->host;
1304 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1306 unsigned int nr_pages = readahead_count(rac);
1307 bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages);
1308 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1309 struct page *page, *head = NULL;
1310 LIST_HEAD(pagepool);
1312 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1315 f.headoffset = readahead_pos(rac);
1317 while ((page = readahead_page(rac))) {
1318 prefetchw(&page->flags);
1321 * A pure asynchronous readahead is indicated if
1322 * a PG_readahead marked page is hitted at first.
1323 * Let's also do asynchronous decompression for this case.
1325 sync &= !(PageReadahead(page) && !head);
1327 set_page_private(page, (unsigned long)head);
1332 struct page *page = head;
1335 /* traversal in reverse order */
1336 head = (void *)page_private(page);
1338 err = z_erofs_do_read_page(&f, page);
1340 erofs_err(inode->i_sb,
1341 "readahead error at page %lu @ nid %llu",
1342 page->index, EROFS_I(inode)->nid);
1346 (void)z_erofs_collector_end(&f.clt);
1348 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
1351 put_page(f.map.mpage);
1353 /* clean up the remaining free pages */
1354 put_pages_list(&pagepool);
1357 const struct address_space_operations z_erofs_aops = {
1358 .readpage = z_erofs_readpage,
1359 .readahead = z_erofs_readahead,