1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/unzip_vle.c
5 * Copyright (C) 2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of the Linux
11 * distribution for more details.
13 #include "unzip_vle.h"
15 #include <linux/prefetch.h>
17 #include <trace/events/erofs.h>
20 * a compressed_pages[] placeholder in order to avoid
21 * being filled with file pages for in-place decompression.
23 #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
25 /* how to allocate cached pages for a workgroup */
26 enum z_erofs_cache_alloctype {
27 DONTALLOC, /* don't allocate any cached pages */
28 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
32 * tagged pointer with 1-bit tag for all compressed pages
33 * tag 0 - the page is just found with an extra page reference
35 typedef tagptr1_t compressed_page_t;
37 #define tag_compressed_page_justfound(page) \
38 tagptr_fold(compressed_page_t, page, 1)
40 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
41 static struct kmem_cache *z_erofs_workgroup_cachep __read_mostly;
43 void z_erofs_exit_zip_subsystem(void)
45 destroy_workqueue(z_erofs_workqueue);
46 kmem_cache_destroy(z_erofs_workgroup_cachep);
49 static inline int init_unzip_workqueue(void)
51 const unsigned int onlinecpus = num_possible_cpus();
54 * we don't need too many threads, limiting threads
55 * could improve scheduling performance.
58 alloc_workqueue("erofs_unzipd",
59 WQ_UNBOUND | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
60 onlinecpus + onlinecpus / 4);
62 return z_erofs_workqueue ? 0 : -ENOMEM;
65 static void init_once(void *ptr)
67 struct z_erofs_vle_workgroup *grp = ptr;
68 struct z_erofs_vle_work *const work =
69 z_erofs_vle_grab_primary_work(grp);
72 mutex_init(&work->lock);
75 for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i)
76 grp->compressed_pages[i] = NULL;
79 static void init_always(struct z_erofs_vle_workgroup *grp)
81 struct z_erofs_vle_work *const work =
82 z_erofs_vle_grab_primary_work(grp);
84 atomic_set(&grp->obj.refcount, 1);
87 DBG_BUGON(work->nr_pages);
88 DBG_BUGON(work->vcnt);
91 int __init z_erofs_init_zip_subsystem(void)
93 z_erofs_workgroup_cachep =
94 kmem_cache_create("erofs_compress",
95 Z_EROFS_WORKGROUP_SIZE, 0,
96 SLAB_RECLAIM_ACCOUNT, init_once);
98 if (z_erofs_workgroup_cachep) {
99 if (!init_unzip_workqueue())
102 kmem_cache_destroy(z_erofs_workgroup_cachep);
107 enum z_erofs_vle_work_role {
108 Z_EROFS_VLE_WORK_SECONDARY,
109 Z_EROFS_VLE_WORK_PRIMARY,
111 * The current work was the tail of an exist chain, and the previous
112 * processed chained works are all decided to be hooked up to it.
113 * A new chain should be created for the remaining unprocessed works,
114 * therefore different from Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED,
115 * the next work cannot reuse the whole page in the following scenario:
116 * ________________________________________________________________
117 * | tail (partial) page | head (partial) page |
118 * | (belongs to the next work) | (belongs to the current work) |
119 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
121 Z_EROFS_VLE_WORK_PRIMARY_HOOKED,
123 * The current work has been linked with the processed chained works,
124 * and could be also linked with the potential remaining works, which
125 * means if the processing page is the tail partial page of the work,
126 * the current work can safely use the whole page (since the next work
127 * is under control) for in-place decompression, as illustrated below:
128 * ________________________________________________________________
129 * | tail (partial) page | head (partial) page |
130 * | (of the current work) | (of the previous work) |
131 * | PRIMARY_FOLLOWED or | |
132 * |_____PRIMARY_HOOKED____|____________PRIMARY_FOLLOWED____________|
134 * [ (*) the above page can be used for the current work itself. ]
136 Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED,
140 struct z_erofs_vle_work_builder {
141 enum z_erofs_vle_work_role role;
143 * 'hosted = false' means that the current workgroup doesn't belong to
144 * the owned chained workgroups. In the other words, it is none of our
145 * business to submit this workgroup.
149 struct z_erofs_vle_workgroup *grp;
150 struct z_erofs_vle_work *work;
151 struct z_erofs_pagevec_ctor vector;
153 /* pages used for reading the compressed data */
154 struct page **compressed_pages;
155 unsigned int compressed_deficit;
158 #define VLE_WORK_BUILDER_INIT() \
159 { .work = NULL, .role = Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED }
161 #ifdef EROFS_FS_HAS_MANAGED_CACHE
162 static void preload_compressed_pages(struct z_erofs_vle_work_builder *bl,
163 struct address_space *mc,
165 unsigned int clusterpages,
166 enum z_erofs_cache_alloctype type,
167 struct list_head *pagepool,
170 struct page **const pages = bl->compressed_pages;
171 const unsigned int remaining = bl->compressed_deficit;
172 bool standalone = true;
173 unsigned int i, j = 0;
175 if (bl->role < Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED)
178 gfp = mapping_gfp_constraint(mc, gfp) & ~__GFP_RECLAIM;
180 index += clusterpages - remaining;
182 for (i = 0; i < remaining; ++i) {
186 /* the compressed page was loaded before */
187 if (READ_ONCE(pages[i]))
190 page = find_get_page(mc, index + i);
193 t = tag_compressed_page_justfound(page);
194 } else if (type == DELAYEDALLOC) {
195 t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED);
196 } else { /* DONTALLOC */
203 if (!cmpxchg_relaxed(&pages[i], NULL, tagptr_cast_ptr(t)))
209 bl->compressed_pages += j;
210 bl->compressed_deficit = remaining - j;
213 bl->role = Z_EROFS_VLE_WORK_PRIMARY;
216 /* called by erofs_shrinker to get rid of all compressed_pages */
217 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
218 struct erofs_workgroup *egrp)
220 struct z_erofs_vle_workgroup *const grp =
221 container_of(egrp, struct z_erofs_vle_workgroup, obj);
222 struct address_space *const mapping = MNGD_MAPPING(sbi);
223 const int clusterpages = erofs_clusterpages(sbi);
227 * refcount of workgroup is now freezed as 1,
228 * therefore no need to worry about available decompression users.
230 for (i = 0; i < clusterpages; ++i) {
231 struct page *page = grp->compressed_pages[i];
233 if (!page || page->mapping != mapping)
236 /* block other users from reclaiming or migrating the page */
237 if (!trylock_page(page))
240 /* barrier is implied in the following 'unlock_page' */
241 WRITE_ONCE(grp->compressed_pages[i], NULL);
243 set_page_private(page, 0);
244 ClearPagePrivate(page);
252 int erofs_try_to_free_cached_page(struct address_space *mapping,
255 struct erofs_sb_info *const sbi = EROFS_SB(mapping->host->i_sb);
256 const unsigned int clusterpages = erofs_clusterpages(sbi);
257 struct z_erofs_vle_workgroup *const grp = (void *)page_private(page);
258 int ret = 0; /* 0 - busy */
260 if (erofs_workgroup_try_to_freeze(&grp->obj, 1)) {
263 for (i = 0; i < clusterpages; ++i) {
264 if (grp->compressed_pages[i] == page) {
265 WRITE_ONCE(grp->compressed_pages[i], NULL);
270 erofs_workgroup_unfreeze(&grp->obj, 1);
273 ClearPagePrivate(page);
280 static void preload_compressed_pages(struct z_erofs_vle_work_builder *bl,
281 struct address_space *mc,
283 unsigned int clusterpages,
284 enum z_erofs_cache_alloctype type,
285 struct list_head *pagepool,
288 /* nowhere to load compressed pages from */
292 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
293 static inline bool try_to_reuse_as_compressed_page(
294 struct z_erofs_vle_work_builder *b,
297 while (b->compressed_deficit) {
298 --b->compressed_deficit;
299 if (!cmpxchg(b->compressed_pages++, NULL, page))
306 /* callers must be with work->lock held */
307 static int z_erofs_vle_work_add_page(
308 struct z_erofs_vle_work_builder *builder,
310 enum z_erofs_page_type type)
315 /* give priority for the compressed data storage */
316 if (builder->role >= Z_EROFS_VLE_WORK_PRIMARY &&
317 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
318 try_to_reuse_as_compressed_page(builder, page))
321 ret = z_erofs_pagevec_ctor_enqueue(&builder->vector,
322 page, type, &occupied);
323 builder->work->vcnt += (unsigned int)ret;
325 return ret ? 0 : -EAGAIN;
328 static enum z_erofs_vle_work_role
329 try_to_claim_workgroup(struct z_erofs_vle_workgroup *grp,
330 z_erofs_vle_owned_workgrp_t *owned_head,
335 /* let's claim these following types of workgroup */
337 if (grp->next == Z_EROFS_VLE_WORKGRP_NIL) {
338 /* type 1, nil workgroup */
339 if (cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_NIL,
340 *owned_head) != Z_EROFS_VLE_WORKGRP_NIL)
343 *owned_head = &grp->next;
345 /* lucky, I am the followee :) */
346 return Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED;
348 } else if (grp->next == Z_EROFS_VLE_WORKGRP_TAIL) {
350 * type 2, link to the end of a existing open chain,
351 * be careful that its submission itself is governed
352 * by the original owned chain.
354 if (cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_TAIL,
355 *owned_head) != Z_EROFS_VLE_WORKGRP_TAIL)
357 *owned_head = Z_EROFS_VLE_WORKGRP_TAIL;
358 return Z_EROFS_VLE_WORK_PRIMARY_HOOKED;
361 return Z_EROFS_VLE_WORK_PRIMARY; /* :( better luck next time */
364 struct z_erofs_vle_work_finder {
365 struct super_block *sb;
367 unsigned int pageofs;
369 struct z_erofs_vle_workgroup **grp_ret;
370 enum z_erofs_vle_work_role *role;
371 z_erofs_vle_owned_workgrp_t *owned_head;
375 static struct z_erofs_vle_work *
376 z_erofs_vle_work_lookup(const struct z_erofs_vle_work_finder *f)
379 struct erofs_workgroup *egrp;
380 struct z_erofs_vle_workgroup *grp;
381 struct z_erofs_vle_work *work;
383 egrp = erofs_find_workgroup(f->sb, f->idx, &tag);
389 grp = container_of(egrp, struct z_erofs_vle_workgroup, obj);
392 work = z_erofs_vle_grab_work(grp, f->pageofs);
393 /* if multiref is disabled, `primary' is always true */
396 if (work->pageofs != f->pageofs) {
398 erofs_workgroup_put(egrp);
399 return ERR_PTR(-EIO);
403 * lock must be taken first to avoid grp->next == NIL between
404 * claiming workgroup and adding pages:
408 * mutex_lock(&work->lock)
409 * add all pages to pagevec
411 * [correct locking case 1]:
412 * mutex_lock(grp->work[a])
414 * mutex_lock(grp->work[b]) mutex_lock(grp->work[c])
415 * ... *role = SECONDARY
416 * add all pages to pagevec
418 * mutex_unlock(grp->work[c])
419 * mutex_lock(grp->work[c])
424 * [correct locking case 2]:
425 * mutex_lock(grp->work[b])
427 * mutex_lock(grp->work[a])
429 * mutex_lock(grp->work[c])
433 * mutex_lock(grp->work[a])
434 * *role = PRIMARY_OWNER
435 * add all pages to pagevec
438 mutex_lock(&work->lock);
442 *f->role = Z_EROFS_VLE_WORK_SECONDARY;
443 else /* claim the workgroup if possible */
444 *f->role = try_to_claim_workgroup(grp, f->owned_head,
449 static struct z_erofs_vle_work *
450 z_erofs_vle_work_register(const struct z_erofs_vle_work_finder *f,
451 struct erofs_map_blocks *map)
454 struct z_erofs_vle_workgroup *grp = *f->grp_ret;
455 struct z_erofs_vle_work *work;
457 /* if multiref is disabled, grp should never be nullptr */
460 return ERR_PTR(-EINVAL);
463 /* no available workgroup, let's allocate one */
464 grp = kmem_cache_alloc(z_erofs_workgroup_cachep, GFP_NOFS);
466 return ERR_PTR(-ENOMEM);
469 grp->obj.index = f->idx;
470 grp->llen = map->m_llen;
472 z_erofs_vle_set_workgrp_fmt(grp, (map->m_flags & EROFS_MAP_ZIPPED) ?
473 Z_EROFS_VLE_WORKGRP_FMT_LZ4 :
474 Z_EROFS_VLE_WORKGRP_FMT_PLAIN);
476 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
477 grp->flags |= Z_EROFS_VLE_WORKGRP_FULL_LENGTH;
479 /* new workgrps have been claimed as type 1 */
480 WRITE_ONCE(grp->next, *f->owned_head);
481 /* primary and followed work for all new workgrps */
482 *f->role = Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED;
483 /* it should be submitted by ourselves */
487 work = z_erofs_vle_grab_primary_work(grp);
488 work->pageofs = f->pageofs;
491 * lock all primary followed works before visible to others
492 * and mutex_trylock *never* fails for a new workgroup.
494 mutex_trylock(&work->lock);
497 int err = erofs_register_workgroup(f->sb, &grp->obj, 0);
500 mutex_unlock(&work->lock);
501 kmem_cache_free(z_erofs_workgroup_cachep, grp);
502 return ERR_PTR(-EAGAIN);
506 *f->owned_head = &grp->next;
511 #define builder_is_hooked(builder) \
512 ((builder)->role >= Z_EROFS_VLE_WORK_PRIMARY_HOOKED)
514 #define builder_is_followed(builder) \
515 ((builder)->role >= Z_EROFS_VLE_WORK_PRIMARY_FOLLOWED)
517 static int z_erofs_vle_work_iter_begin(struct z_erofs_vle_work_builder *builder,
518 struct super_block *sb,
519 struct erofs_map_blocks *map,
520 z_erofs_vle_owned_workgrp_t *owned_head)
522 const unsigned int clusterpages = erofs_clusterpages(EROFS_SB(sb));
523 struct z_erofs_vle_workgroup *grp;
524 const struct z_erofs_vle_work_finder finder = {
526 .idx = erofs_blknr(map->m_pa),
527 .pageofs = map->m_la & ~PAGE_MASK,
529 .role = &builder->role,
530 .owned_head = owned_head,
531 .hosted = &builder->hosted
533 struct z_erofs_vle_work *work;
535 DBG_BUGON(builder->work);
537 /* must be Z_EROFS_WORK_TAIL or the next chained work */
538 DBG_BUGON(*owned_head == Z_EROFS_VLE_WORKGRP_NIL);
539 DBG_BUGON(*owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
541 DBG_BUGON(erofs_blkoff(map->m_pa));
544 work = z_erofs_vle_work_lookup(&finder);
546 unsigned int orig_llen;
548 /* increase workgroup `llen' if needed */
549 while ((orig_llen = READ_ONCE(grp->llen)) < map->m_llen &&
550 orig_llen != cmpxchg_relaxed(&grp->llen,
551 orig_llen, map->m_llen))
556 work = z_erofs_vle_work_register(&finder, map);
557 if (unlikely(work == ERR_PTR(-EAGAIN)))
561 return PTR_ERR(work);
563 z_erofs_pagevec_ctor_init(&builder->vector, Z_EROFS_NR_INLINE_PAGEVECS,
564 work->pagevec, work->vcnt);
566 if (builder->role >= Z_EROFS_VLE_WORK_PRIMARY) {
567 /* enable possibly in-place decompression */
568 builder->compressed_pages = grp->compressed_pages;
569 builder->compressed_deficit = clusterpages;
571 builder->compressed_pages = NULL;
572 builder->compressed_deficit = 0;
576 builder->work = work;
581 * keep in mind that no referenced workgroups will be freed
582 * only after a RCU grace period, so rcu_read_lock() could
583 * prevent a workgroup from being freed.
585 static void z_erofs_rcu_callback(struct rcu_head *head)
587 struct z_erofs_vle_work *work = container_of(head,
588 struct z_erofs_vle_work, rcu);
589 struct z_erofs_vle_workgroup *grp =
590 z_erofs_vle_work_workgroup(work, true);
592 kmem_cache_free(z_erofs_workgroup_cachep, grp);
595 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
597 struct z_erofs_vle_workgroup *const vgrp = container_of(grp,
598 struct z_erofs_vle_workgroup, obj);
599 struct z_erofs_vle_work *const work = &vgrp->work;
601 call_rcu(&work->rcu, z_erofs_rcu_callback);
605 __z_erofs_vle_work_release(struct z_erofs_vle_workgroup *grp,
606 struct z_erofs_vle_work *work __maybe_unused)
608 erofs_workgroup_put(&grp->obj);
611 static void z_erofs_vle_work_release(struct z_erofs_vle_work *work)
613 struct z_erofs_vle_workgroup *grp =
614 z_erofs_vle_work_workgroup(work, true);
616 __z_erofs_vle_work_release(grp, work);
620 z_erofs_vle_work_iter_end(struct z_erofs_vle_work_builder *builder)
622 struct z_erofs_vle_work *work = builder->work;
627 z_erofs_pagevec_ctor_exit(&builder->vector, false);
628 mutex_unlock(&work->lock);
631 * if all pending pages are added, don't hold work reference
632 * any longer if the current work isn't hosted by ourselves.
634 if (!builder->hosted)
635 __z_erofs_vle_work_release(builder->grp, work);
637 builder->work = NULL;
642 static inline struct page *__stagingpage_alloc(struct list_head *pagepool,
645 struct page *page = erofs_allocpage(pagepool, gfp);
650 page->mapping = Z_EROFS_MAPPING_STAGING;
654 struct z_erofs_vle_frontend {
655 struct inode *const inode;
657 struct z_erofs_vle_work_builder builder;
658 struct erofs_map_blocks map;
660 z_erofs_vle_owned_workgrp_t owned_head;
662 /* used for applying cache strategy on the fly */
664 erofs_off_t headoffset;
667 #define VLE_FRONTEND_INIT(__i) { \
674 .builder = VLE_WORK_BUILDER_INIT(), \
675 .owned_head = Z_EROFS_VLE_WORKGRP_TAIL, \
678 #ifdef EROFS_FS_HAS_MANAGED_CACHE
680 should_alloc_managed_pages(struct z_erofs_vle_frontend *fe, erofs_off_t la)
685 if (EROFS_FS_ZIP_CACHE_LVL >= 2)
686 return la < fe->headoffset;
692 should_alloc_managed_pages(struct z_erofs_vle_frontend *fe, erofs_off_t la)
698 static int z_erofs_do_read_page(struct z_erofs_vle_frontend *fe,
700 struct list_head *page_pool)
702 struct super_block *const sb = fe->inode->i_sb;
703 struct erofs_sb_info *const sbi __maybe_unused = EROFS_SB(sb);
704 struct erofs_map_blocks *const map = &fe->map;
705 struct z_erofs_vle_work_builder *const builder = &fe->builder;
706 const loff_t offset = page_offset(page);
708 bool tight = builder_is_hooked(builder);
709 struct z_erofs_vle_work *work = builder->work;
711 enum z_erofs_cache_alloctype cache_strategy;
712 enum z_erofs_page_type page_type;
713 unsigned int cur, end, spiltted, index;
716 /* register locked file pages as online pages in pack */
717 z_erofs_onlinepage_init(page);
724 /* lucky, within the range of the current map_blocks */
725 if (offset + cur >= map->m_la &&
726 offset + cur < map->m_la + map->m_llen) {
727 /* didn't get a valid unzip work previously (very rare) */
733 /* go ahead the next map_blocks */
734 debugln("%s: [out-of-range] pos %llu", __func__, offset + cur);
736 if (z_erofs_vle_work_iter_end(builder))
737 fe->backmost = false;
739 map->m_la = offset + cur;
741 err = z_erofs_map_blocks_iter(fe->inode, map, 0);
746 if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED)))
749 DBG_BUGON(map->m_plen != 1 << sbi->clusterbits);
750 DBG_BUGON(erofs_blkoff(map->m_pa));
752 err = z_erofs_vle_work_iter_begin(builder, sb, map, &fe->owned_head);
756 /* preload all compressed pages (maybe downgrade role if necessary) */
757 if (should_alloc_managed_pages(fe, map->m_la))
758 cache_strategy = DELAYEDALLOC;
760 cache_strategy = DONTALLOC;
762 preload_compressed_pages(builder, MNGD_MAPPING(sbi),
763 map->m_pa / PAGE_SIZE,
764 map->m_plen / PAGE_SIZE,
765 cache_strategy, page_pool, GFP_KERNEL);
767 tight &= builder_is_hooked(builder);
768 work = builder->work;
770 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
771 if (unlikely(!(map->m_flags & EROFS_MAP_MAPPED))) {
772 zero_user_segment(page, cur, end);
776 /* let's derive page type */
777 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
778 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
779 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
780 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
783 tight &= builder_is_followed(builder);
786 err = z_erofs_vle_work_add_page(builder, page, page_type);
787 /* should allocate an additional staging page for pagevec */
788 if (err == -EAGAIN) {
789 struct page *const newpage =
790 __stagingpage_alloc(page_pool, GFP_NOFS);
792 err = z_erofs_vle_work_add_page(builder, newpage,
793 Z_EROFS_PAGE_TYPE_EXCLUSIVE);
801 index = page->index - map->m_la / PAGE_SIZE;
803 /* FIXME! avoid the last relundant fixup & endio */
804 z_erofs_onlinepage_fixup(page, index, true);
806 /* bump up the number of spiltted parts of a page */
808 /* also update nr_pages */
809 work->nr_pages = max_t(pgoff_t, work->nr_pages, index + 1);
811 /* can be used for verification */
812 map->m_llen = offset + cur - map->m_la;
819 /* FIXME! avoid the last relundant fixup & endio */
820 z_erofs_onlinepage_endio(page);
822 debugln("%s, finish page: %pK spiltted: %u map->m_llen %llu",
823 __func__, page, spiltted, map->m_llen);
826 /* if some error occurred while processing this page */
832 static void z_erofs_vle_unzip_kickoff(void *ptr, int bios)
834 tagptr1_t t = tagptr_init(tagptr1_t, ptr);
835 struct z_erofs_vle_unzip_io *io = tagptr_unfold_ptr(t);
836 bool background = tagptr_unfold_tags(t);
841 spin_lock_irqsave(&io->u.wait.lock, flags);
842 if (!atomic_add_return(bios, &io->pending_bios))
843 wake_up_locked(&io->u.wait);
844 spin_unlock_irqrestore(&io->u.wait.lock, flags);
848 if (!atomic_add_return(bios, &io->pending_bios))
849 queue_work(z_erofs_workqueue, &io->u.work);
852 static inline void z_erofs_vle_read_endio(struct bio *bio)
854 struct erofs_sb_info *sbi = NULL;
855 blk_status_t err = bio->bi_status;
856 struct bio_vec *bvec;
857 struct bvec_iter_all iter_all;
859 bio_for_each_segment_all(bvec, bio, iter_all) {
860 struct page *page = bvec->bv_page;
861 bool cachemngd = false;
863 DBG_BUGON(PageUptodate(page));
864 DBG_BUGON(!page->mapping);
866 if (unlikely(!sbi && !z_erofs_page_is_staging(page))) {
867 sbi = EROFS_SB(page->mapping->host->i_sb);
869 if (time_to_inject(sbi, FAULT_READ_IO)) {
870 erofs_show_injection_info(FAULT_READ_IO);
875 /* sbi should already be gotten if the page is managed */
877 cachemngd = erofs_page_is_managed(sbi, page);
882 SetPageUptodate(page);
888 z_erofs_vle_unzip_kickoff(bio->bi_private, -1);
892 static struct page *z_pagemap_global[Z_EROFS_VLE_VMAP_GLOBAL_PAGES];
893 static DEFINE_MUTEX(z_pagemap_global_lock);
895 static int z_erofs_vle_unzip(struct super_block *sb,
896 struct z_erofs_vle_workgroup *grp,
897 struct list_head *page_pool)
899 struct erofs_sb_info *const sbi = EROFS_SB(sb);
900 const unsigned int clusterpages = erofs_clusterpages(sbi);
902 struct z_erofs_pagevec_ctor ctor;
903 unsigned int nr_pages;
904 unsigned int sparsemem_pages = 0;
905 struct page *pages_onstack[Z_EROFS_VLE_VMAP_ONSTACK_PAGES];
906 struct page **pages, **compressed_pages, *page;
907 unsigned int algorithm;
908 unsigned int i, outputsize;
910 enum z_erofs_page_type page_type;
911 bool overlapped, partial;
912 struct z_erofs_vle_work *work;
916 work = z_erofs_vle_grab_primary_work(grp);
917 DBG_BUGON(!READ_ONCE(work->nr_pages));
919 mutex_lock(&work->lock);
920 nr_pages = work->nr_pages;
922 if (likely(nr_pages <= Z_EROFS_VLE_VMAP_ONSTACK_PAGES))
923 pages = pages_onstack;
924 else if (nr_pages <= Z_EROFS_VLE_VMAP_GLOBAL_PAGES &&
925 mutex_trylock(&z_pagemap_global_lock))
926 pages = z_pagemap_global;
929 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
932 /* fallback to global pagemap for the lowmem scenario */
933 if (unlikely(!pages)) {
934 if (nr_pages > Z_EROFS_VLE_VMAP_GLOBAL_PAGES)
937 mutex_lock(&z_pagemap_global_lock);
938 pages = z_pagemap_global;
943 for (i = 0; i < nr_pages; ++i)
947 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
950 for (i = 0; i < work->vcnt; ++i) {
953 page = z_erofs_pagevec_ctor_dequeue(&ctor, &page_type);
955 /* all pages in pagevec ought to be valid */
957 DBG_BUGON(!page->mapping);
959 if (z_erofs_put_stagingpage(page_pool, page))
962 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
965 pagenr = z_erofs_onlinepage_index(page);
967 DBG_BUGON(pagenr >= nr_pages);
970 * currently EROFS doesn't support multiref(dedup),
971 * so here erroring out one multiref page.
975 SetPageError(pages[pagenr]);
976 z_erofs_onlinepage_endio(pages[pagenr]);
979 pages[pagenr] = page;
983 z_erofs_pagevec_ctor_exit(&ctor, true);
986 compressed_pages = grp->compressed_pages;
988 for (i = 0; i < clusterpages; ++i) {
991 page = compressed_pages[i];
993 /* all compressed pages ought to be valid */
995 DBG_BUGON(!page->mapping);
997 if (!z_erofs_page_is_staging(page)) {
998 if (erofs_page_is_managed(sbi, page)) {
999 if (unlikely(!PageUptodate(page)))
1005 * only if non-head page can be selected
1006 * for inplace decompression
1008 pagenr = z_erofs_onlinepage_index(page);
1010 DBG_BUGON(pagenr >= nr_pages);
1011 if (pages[pagenr]) {
1013 SetPageError(pages[pagenr]);
1014 z_erofs_onlinepage_endio(pages[pagenr]);
1018 pages[pagenr] = page;
1023 /* PG_error needs checking for inplaced and staging pages */
1024 if (unlikely(PageError(page))) {
1025 DBG_BUGON(PageUptodate(page));
1033 if (nr_pages << PAGE_SHIFT >= work->pageofs + grp->llen) {
1034 outputsize = grp->llen;
1035 partial = !(grp->flags & Z_EROFS_VLE_WORKGRP_FULL_LENGTH);
1037 outputsize = (nr_pages << PAGE_SHIFT) - work->pageofs;
1041 if (z_erofs_vle_workgrp_fmt(grp) == Z_EROFS_VLE_WORKGRP_FMT_PLAIN)
1042 algorithm = Z_EROFS_COMPRESSION_SHIFTED;
1044 algorithm = Z_EROFS_COMPRESSION_LZ4;
1046 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
1048 .in = compressed_pages,
1050 .pageofs_out = work->pageofs,
1051 .inputsize = PAGE_SIZE,
1052 .outputsize = outputsize,
1054 .inplace_io = overlapped,
1055 .partial_decoding = partial
1059 /* must handle all compressed pages before endding pages */
1060 for (i = 0; i < clusterpages; ++i) {
1061 page = compressed_pages[i];
1063 if (erofs_page_is_managed(sbi, page))
1066 /* recycle all individual staging pages */
1067 (void)z_erofs_put_stagingpage(page_pool, page);
1069 WRITE_ONCE(compressed_pages[i], NULL);
1072 for (i = 0; i < nr_pages; ++i) {
1077 DBG_BUGON(!page->mapping);
1079 /* recycle all individual staging pages */
1080 if (z_erofs_put_stagingpage(page_pool, page))
1083 if (unlikely(err < 0))
1086 z_erofs_onlinepage_endio(page);
1089 if (pages == z_pagemap_global)
1090 mutex_unlock(&z_pagemap_global_lock);
1091 else if (unlikely(pages != pages_onstack))
1097 /* all work locks MUST be taken before the following line */
1099 WRITE_ONCE(grp->next, Z_EROFS_VLE_WORKGRP_NIL);
1101 /* all work locks SHOULD be released right now */
1102 mutex_unlock(&work->lock);
1104 z_erofs_vle_work_release(work);
1108 static void z_erofs_vle_unzip_all(struct super_block *sb,
1109 struct z_erofs_vle_unzip_io *io,
1110 struct list_head *page_pool)
1112 z_erofs_vle_owned_workgrp_t owned = io->head;
1114 while (owned != Z_EROFS_VLE_WORKGRP_TAIL_CLOSED) {
1115 struct z_erofs_vle_workgroup *grp;
1117 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1118 DBG_BUGON(owned == Z_EROFS_VLE_WORKGRP_TAIL);
1120 /* no possible that 'owned' equals NULL */
1121 DBG_BUGON(owned == Z_EROFS_VLE_WORKGRP_NIL);
1123 grp = container_of(owned, struct z_erofs_vle_workgroup, next);
1124 owned = READ_ONCE(grp->next);
1126 z_erofs_vle_unzip(sb, grp, page_pool);
1130 static void z_erofs_vle_unzip_wq(struct work_struct *work)
1132 struct z_erofs_vle_unzip_io_sb *iosb = container_of(work,
1133 struct z_erofs_vle_unzip_io_sb, io.u.work);
1134 LIST_HEAD(page_pool);
1136 DBG_BUGON(iosb->io.head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1137 z_erofs_vle_unzip_all(iosb->sb, &iosb->io, &page_pool);
1139 put_pages_list(&page_pool);
1143 static struct page *
1144 pickup_page_for_submission(struct z_erofs_vle_workgroup *grp,
1146 struct list_head *pagepool,
1147 struct address_space *mc,
1150 /* determined at compile time to avoid too many #ifdefs */
1151 const bool nocache = __builtin_constant_p(mc) ? !mc : false;
1152 const pgoff_t index = grp->obj.index;
1153 bool tocache = false;
1155 struct address_space *mapping;
1156 struct page *oldpage, *page;
1158 compressed_page_t t;
1162 page = READ_ONCE(grp->compressed_pages[nr]);
1169 * the cached page has not been allocated and
1170 * an placeholder is out there, prepare it now.
1172 if (!nocache && page == PAGE_UNALLOCATED) {
1177 /* process the target tagged pointer */
1178 t = tagptr_init(compressed_page_t, page);
1179 justfound = tagptr_unfold_tags(t);
1180 page = tagptr_unfold_ptr(t);
1182 mapping = READ_ONCE(page->mapping);
1185 * if managed cache is disabled, it's no way to
1186 * get such a cached-like page.
1189 /* if managed cache is disabled, it is impossible `justfound' */
1190 DBG_BUGON(justfound);
1192 /* and it should be locked, not uptodate, and not truncated */
1193 DBG_BUGON(!PageLocked(page));
1194 DBG_BUGON(PageUptodate(page));
1195 DBG_BUGON(!mapping);
1200 * unmanaged (file) pages are all locked solidly,
1201 * therefore it is impossible for `mapping' to be NULL.
1203 if (mapping && mapping != mc)
1204 /* ought to be unmanaged pages */
1209 /* only true if page reclaim goes wrong, should never happen */
1210 DBG_BUGON(justfound && PagePrivate(page));
1212 /* the page is still in manage cache */
1213 if (page->mapping == mc) {
1214 WRITE_ONCE(grp->compressed_pages[nr], page);
1216 ClearPageError(page);
1217 if (!PagePrivate(page)) {
1219 * impossible to be !PagePrivate(page) for
1220 * the current restriction as well if
1221 * the page is already in compressed_pages[].
1223 DBG_BUGON(!justfound);
1226 set_page_private(page, (unsigned long)grp);
1227 SetPagePrivate(page);
1230 /* no need to submit io if it is already up-to-date */
1231 if (PageUptodate(page)) {
1239 * the managed page has been truncated, it's unsafe to
1240 * reuse this one, let's allocate a new cache-managed page.
1242 DBG_BUGON(page->mapping);
1243 DBG_BUGON(!justfound);
1249 page = __stagingpage_alloc(pagepool, gfp);
1250 if (oldpage != cmpxchg(&grp->compressed_pages[nr], oldpage, page)) {
1251 list_add(&page->lru, pagepool);
1255 if (nocache || !tocache)
1257 if (add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1258 page->mapping = Z_EROFS_MAPPING_STAGING;
1262 set_page_private(page, (unsigned long)grp);
1263 SetPagePrivate(page);
1264 out: /* the only exit (for tracing and debugging) */
1268 static struct z_erofs_vle_unzip_io *
1269 jobqueue_init(struct super_block *sb,
1270 struct z_erofs_vle_unzip_io *io,
1273 struct z_erofs_vle_unzip_io_sb *iosb;
1276 /* waitqueue available for foreground io */
1279 init_waitqueue_head(&io->u.wait);
1280 atomic_set(&io->pending_bios, 0);
1284 iosb = kvzalloc(sizeof(*iosb), GFP_KERNEL | __GFP_NOFAIL);
1287 /* initialize fields in the allocated descriptor */
1290 INIT_WORK(&io->u.work, z_erofs_vle_unzip_wq);
1292 io->head = Z_EROFS_VLE_WORKGRP_TAIL_CLOSED;
1296 /* define workgroup jobqueue types */
1298 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1305 static void *jobqueueset_init(struct super_block *sb,
1306 z_erofs_vle_owned_workgrp_t qtail[],
1307 struct z_erofs_vle_unzip_io *q[],
1308 struct z_erofs_vle_unzip_io *fgq,
1311 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1313 * if managed cache is enabled, bypass jobqueue is needed,
1314 * no need to read from device for all workgroups in this queue.
1316 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, true);
1317 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1320 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, forcefg);
1321 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1323 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], !forcefg));
1326 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1327 static void move_to_bypass_jobqueue(struct z_erofs_vle_workgroup *grp,
1328 z_erofs_vle_owned_workgrp_t qtail[],
1329 z_erofs_vle_owned_workgrp_t owned_head)
1331 z_erofs_vle_owned_workgrp_t *const submit_qtail = qtail[JQ_SUBMIT];
1332 z_erofs_vle_owned_workgrp_t *const bypass_qtail = qtail[JQ_BYPASS];
1334 DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1335 if (owned_head == Z_EROFS_VLE_WORKGRP_TAIL)
1336 owned_head = Z_EROFS_VLE_WORKGRP_TAIL_CLOSED;
1338 WRITE_ONCE(grp->next, Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1340 WRITE_ONCE(*submit_qtail, owned_head);
1341 WRITE_ONCE(*bypass_qtail, &grp->next);
1343 qtail[JQ_BYPASS] = &grp->next;
1346 static bool postsubmit_is_all_bypassed(struct z_erofs_vle_unzip_io *q[],
1347 unsigned int nr_bios,
1351 * although background is preferred, no one is pending for submission.
1352 * don't issue workqueue for decompression but drop it directly instead.
1354 if (force_fg || nr_bios)
1357 kvfree(container_of(q[JQ_SUBMIT],
1358 struct z_erofs_vle_unzip_io_sb,
1363 static void move_to_bypass_jobqueue(struct z_erofs_vle_workgroup *grp,
1364 z_erofs_vle_owned_workgrp_t qtail[],
1365 z_erofs_vle_owned_workgrp_t owned_head)
1367 /* impossible to bypass submission for managed cache disabled */
1371 static bool postsubmit_is_all_bypassed(struct z_erofs_vle_unzip_io *q[],
1372 unsigned int nr_bios,
1375 /* bios should be >0 if managed cache is disabled */
1376 DBG_BUGON(!nr_bios);
1381 static bool z_erofs_vle_submit_all(struct super_block *sb,
1382 z_erofs_vle_owned_workgrp_t owned_head,
1383 struct list_head *pagepool,
1384 struct z_erofs_vle_unzip_io *fgq,
1387 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1388 const unsigned int clusterpages = erofs_clusterpages(sbi);
1389 const gfp_t gfp = GFP_NOFS;
1391 z_erofs_vle_owned_workgrp_t qtail[NR_JOBQUEUES];
1392 struct z_erofs_vle_unzip_io *q[NR_JOBQUEUES];
1395 /* since bio will be NULL, no need to initialize last_index */
1396 pgoff_t uninitialized_var(last_index);
1397 bool force_submit = false;
1398 unsigned int nr_bios;
1400 if (unlikely(owned_head == Z_EROFS_VLE_WORKGRP_TAIL))
1403 force_submit = false;
1406 bi_private = jobqueueset_init(sb, qtail, q, fgq, force_fg);
1408 /* by default, all need io submission */
1409 q[JQ_SUBMIT]->head = owned_head;
1412 struct z_erofs_vle_workgroup *grp;
1413 pgoff_t first_index;
1415 unsigned int i = 0, bypass = 0;
1418 /* no possible 'owned_head' equals the following */
1419 DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1420 DBG_BUGON(owned_head == Z_EROFS_VLE_WORKGRP_NIL);
1422 grp = container_of(owned_head,
1423 struct z_erofs_vle_workgroup, next);
1425 /* close the main owned chain at first */
1426 owned_head = cmpxchg(&grp->next, Z_EROFS_VLE_WORKGRP_TAIL,
1427 Z_EROFS_VLE_WORKGRP_TAIL_CLOSED);
1429 first_index = grp->obj.index;
1430 force_submit |= (first_index != last_index + 1);
1433 page = pickup_page_for_submission(grp, i, pagepool,
1434 MNGD_MAPPING(sbi), gfp);
1436 force_submit = true;
1441 if (bio && force_submit) {
1443 __submit_bio(bio, REQ_OP_READ, 0);
1448 bio = erofs_grab_bio(sb, first_index + i,
1449 BIO_MAX_PAGES, bi_private,
1450 z_erofs_vle_read_endio, true);
1454 err = bio_add_page(bio, page, PAGE_SIZE, 0);
1455 if (err < PAGE_SIZE)
1456 goto submit_bio_retry;
1458 force_submit = false;
1459 last_index = first_index + i;
1461 if (++i < clusterpages)
1464 if (bypass < clusterpages)
1465 qtail[JQ_SUBMIT] = &grp->next;
1467 move_to_bypass_jobqueue(grp, qtail, owned_head);
1468 } while (owned_head != Z_EROFS_VLE_WORKGRP_TAIL);
1471 __submit_bio(bio, REQ_OP_READ, 0);
1473 if (postsubmit_is_all_bypassed(q, nr_bios, force_fg))
1476 z_erofs_vle_unzip_kickoff(bi_private, nr_bios);
1480 static void z_erofs_submit_and_unzip(struct z_erofs_vle_frontend *f,
1481 struct list_head *pagepool,
1484 struct super_block *sb = f->inode->i_sb;
1485 struct z_erofs_vle_unzip_io io[NR_JOBQUEUES];
1487 if (!z_erofs_vle_submit_all(sb, f->owned_head, pagepool, io, force_fg))
1490 #ifdef EROFS_FS_HAS_MANAGED_CACHE
1491 z_erofs_vle_unzip_all(sb, &io[JQ_BYPASS], pagepool);
1496 /* wait until all bios are completed */
1497 wait_event(io[JQ_SUBMIT].u.wait,
1498 !atomic_read(&io[JQ_SUBMIT].pending_bios));
1500 /* let's synchronous decompression */
1501 z_erofs_vle_unzip_all(sb, &io[JQ_SUBMIT], pagepool);
1504 static int z_erofs_vle_normalaccess_readpage(struct file *file,
1507 struct inode *const inode = page->mapping->host;
1508 struct z_erofs_vle_frontend f = VLE_FRONTEND_INIT(inode);
1510 LIST_HEAD(pagepool);
1512 trace_erofs_readpage(page, false);
1514 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1516 err = z_erofs_do_read_page(&f, page, &pagepool);
1517 (void)z_erofs_vle_work_iter_end(&f.builder);
1519 /* if some compressed cluster ready, need submit them anyway */
1520 z_erofs_submit_and_unzip(&f, &pagepool, true);
1523 errln("%s, failed to read, err [%d]", __func__, err);
1526 put_page(f.map.mpage);
1528 /* clean up the remaining free pages */
1529 put_pages_list(&pagepool);
1533 static int z_erofs_vle_normalaccess_readpages(struct file *filp,
1534 struct address_space *mapping,
1535 struct list_head *pages,
1536 unsigned int nr_pages)
1538 struct inode *const inode = mapping->host;
1539 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1541 bool sync = __should_decompress_synchronously(sbi, nr_pages);
1542 struct z_erofs_vle_frontend f = VLE_FRONTEND_INIT(inode);
1543 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
1544 struct page *head = NULL;
1545 LIST_HEAD(pagepool);
1547 trace_erofs_readpages(mapping->host, lru_to_page(pages),
1550 f.headoffset = (erofs_off_t)lru_to_page(pages)->index << PAGE_SHIFT;
1552 for (; nr_pages; --nr_pages) {
1553 struct page *page = lru_to_page(pages);
1555 prefetchw(&page->flags);
1556 list_del(&page->lru);
1559 * A pure asynchronous readahead is indicated if
1560 * a PG_readahead marked page is hitted at first.
1561 * Let's also do asynchronous decompression for this case.
1563 sync &= !(PageReadahead(page) && !head);
1565 if (add_to_page_cache_lru(page, mapping, page->index, gfp)) {
1566 list_add(&page->lru, &pagepool);
1570 set_page_private(page, (unsigned long)head);
1575 struct page *page = head;
1578 /* traversal in reverse order */
1579 head = (void *)page_private(page);
1581 err = z_erofs_do_read_page(&f, page, &pagepool);
1583 struct erofs_vnode *vi = EROFS_V(inode);
1585 errln("%s, readahead error at page %lu of nid %llu",
1586 __func__, page->index, vi->nid);
1592 (void)z_erofs_vle_work_iter_end(&f.builder);
1594 z_erofs_submit_and_unzip(&f, &pagepool, sync);
1597 put_page(f.map.mpage);
1599 /* clean up the remaining free pages */
1600 put_pages_list(&pagepool);
1604 const struct address_space_operations z_erofs_vle_normalaccess_aops = {
1605 .readpage = z_erofs_vle_normalaccess_readpage,
1606 .readpages = z_erofs_vle_normalaccess_readpages,