xref: /openbmc/linux/fs/erofs/zdata.c (revision 3fffb589)
147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
506a304cdSGao Xiang  * Copyright (C) 2022 Alibaba Cloud
647e4937aSGao Xiang  */
747e4937aSGao Xiang #include "compress.h"
847e4937aSGao Xiang #include <linux/prefetch.h>
999486c51SChristoph Hellwig #include <linux/psi.h>
10*3fffb589SSandeep Dhavale #include <linux/cpuhotplug.h>
1147e4937aSGao Xiang #include <trace/events/erofs.h>
1247e4937aSGao Xiang 
13a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_MAX_PAGES	(Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
14a9a94d93SGao Xiang #define Z_EROFS_INLINE_BVECS		2
15a9a94d93SGao Xiang 
16a9a94d93SGao Xiang /*
17a9a94d93SGao Xiang  * let's leave a type here in case of introducing
18a9a94d93SGao Xiang  * another tagged pointer later.
19a9a94d93SGao Xiang  */
20a9a94d93SGao Xiang typedef void *z_erofs_next_pcluster_t;
21a9a94d93SGao Xiang 
22a9a94d93SGao Xiang struct z_erofs_bvec {
23a9a94d93SGao Xiang 	struct page *page;
24a9a94d93SGao Xiang 	int offset;
25a9a94d93SGao Xiang 	unsigned int end;
26a9a94d93SGao Xiang };
27a9a94d93SGao Xiang 
28a9a94d93SGao Xiang #define __Z_EROFS_BVSET(name, total) \
29a9a94d93SGao Xiang struct name { \
30a9a94d93SGao Xiang 	/* point to the next page which contains the following bvecs */ \
31a9a94d93SGao Xiang 	struct page *nextpage; \
32a9a94d93SGao Xiang 	struct z_erofs_bvec bvec[total]; \
33a9a94d93SGao Xiang }
34a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset,);
35a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
36a9a94d93SGao Xiang 
37a9a94d93SGao Xiang /*
38a9a94d93SGao Xiang  * Structure fields follow one of the following exclusion rules.
39a9a94d93SGao Xiang  *
40a9a94d93SGao Xiang  * I: Modifiable by initialization/destruction paths and read-only
41a9a94d93SGao Xiang  *    for everyone else;
42a9a94d93SGao Xiang  *
43a9a94d93SGao Xiang  * L: Field should be protected by the pcluster lock;
44a9a94d93SGao Xiang  *
45a9a94d93SGao Xiang  * A: Field should be accessed / updated in atomic for parallelized code.
46a9a94d93SGao Xiang  */
47a9a94d93SGao Xiang struct z_erofs_pcluster {
48a9a94d93SGao Xiang 	struct erofs_workgroup obj;
49a9a94d93SGao Xiang 	struct mutex lock;
50a9a94d93SGao Xiang 
51a9a94d93SGao Xiang 	/* A: point to next chained pcluster or TAILs */
52a9a94d93SGao Xiang 	z_erofs_next_pcluster_t next;
53a9a94d93SGao Xiang 
54a9a94d93SGao Xiang 	/* L: the maximum decompression size of this round */
55a9a94d93SGao Xiang 	unsigned int length;
56a9a94d93SGao Xiang 
57a9a94d93SGao Xiang 	/* L: total number of bvecs */
58a9a94d93SGao Xiang 	unsigned int vcnt;
59a9a94d93SGao Xiang 
60a9a94d93SGao Xiang 	/* I: page offset of start position of decompression */
61a9a94d93SGao Xiang 	unsigned short pageofs_out;
62a9a94d93SGao Xiang 
63a9a94d93SGao Xiang 	/* I: page offset of inline compressed data */
64a9a94d93SGao Xiang 	unsigned short pageofs_in;
65a9a94d93SGao Xiang 
66a9a94d93SGao Xiang 	union {
67a9a94d93SGao Xiang 		/* L: inline a certain number of bvec for bootstrap */
68a9a94d93SGao Xiang 		struct z_erofs_bvset_inline bvset;
69a9a94d93SGao Xiang 
70a9a94d93SGao Xiang 		/* I: can be used to free the pcluster by RCU. */
71a9a94d93SGao Xiang 		struct rcu_head rcu;
72a9a94d93SGao Xiang 	};
73a9a94d93SGao Xiang 
74a9a94d93SGao Xiang 	union {
75a9a94d93SGao Xiang 		/* I: physical cluster size in pages */
76a9a94d93SGao Xiang 		unsigned short pclusterpages;
77a9a94d93SGao Xiang 
78a9a94d93SGao Xiang 		/* I: tailpacking inline compressed size */
79a9a94d93SGao Xiang 		unsigned short tailpacking_size;
80a9a94d93SGao Xiang 	};
81a9a94d93SGao Xiang 
82a9a94d93SGao Xiang 	/* I: compression algorithm format */
83a9a94d93SGao Xiang 	unsigned char algorithmformat;
84a9a94d93SGao Xiang 
85a9a94d93SGao Xiang 	/* L: whether partial decompression or not */
86a9a94d93SGao Xiang 	bool partial;
87a9a94d93SGao Xiang 
88a9a94d93SGao Xiang 	/* L: indicate several pageofs_outs or not */
89a9a94d93SGao Xiang 	bool multibases;
90a9a94d93SGao Xiang 
91a9a94d93SGao Xiang 	/* A: compressed bvecs (can be cached or inplaced pages) */
92a9a94d93SGao Xiang 	struct z_erofs_bvec compressed_bvecs[];
93a9a94d93SGao Xiang };
94a9a94d93SGao Xiang 
95a9a94d93SGao Xiang /* let's avoid the valid 32-bit kernel addresses */
96a9a94d93SGao Xiang 
97a9a94d93SGao Xiang /* the chained workgroup has't submitted io (still open) */
98a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL           ((void *)0x5F0ECAFE)
99a9a94d93SGao Xiang /* the chained workgroup has already submitted io */
100a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL_CLOSED    ((void *)0x5F0EDEAD)
101a9a94d93SGao Xiang 
102a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL            (NULL)
103a9a94d93SGao Xiang 
104a9a94d93SGao Xiang struct z_erofs_decompressqueue {
105a9a94d93SGao Xiang 	struct super_block *sb;
106a9a94d93SGao Xiang 	atomic_t pending_bios;
107a9a94d93SGao Xiang 	z_erofs_next_pcluster_t head;
108a9a94d93SGao Xiang 
109a9a94d93SGao Xiang 	union {
110a9a94d93SGao Xiang 		struct completion done;
111a9a94d93SGao Xiang 		struct work_struct work;
112*3fffb589SSandeep Dhavale 		struct kthread_work kthread_work;
113a9a94d93SGao Xiang 	} u;
114a9a94d93SGao Xiang 	bool eio, sync;
115a9a94d93SGao Xiang };
116a9a94d93SGao Xiang 
117a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
118a9a94d93SGao Xiang {
119a9a94d93SGao Xiang 	return !pcl->obj.index;
120a9a94d93SGao Xiang }
121a9a94d93SGao Xiang 
122a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
123a9a94d93SGao Xiang {
124a9a94d93SGao Xiang 	if (z_erofs_is_inline_pcluster(pcl))
125a9a94d93SGao Xiang 		return 1;
126a9a94d93SGao Xiang 	return pcl->pclusterpages;
127a9a94d93SGao Xiang }
128a9a94d93SGao Xiang 
129a9a94d93SGao Xiang /*
130a9a94d93SGao Xiang  * bit 30: I/O error occurred on this page
131a9a94d93SGao Xiang  * bit 0 - 29: remaining parts to complete this page
132a9a94d93SGao Xiang  */
133a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO			(1 << 30)
134a9a94d93SGao Xiang 
135a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page)
136a9a94d93SGao Xiang {
137a9a94d93SGao Xiang 	union {
138a9a94d93SGao Xiang 		atomic_t o;
139a9a94d93SGao Xiang 		unsigned long v;
140a9a94d93SGao Xiang 	} u = { .o = ATOMIC_INIT(1) };
141a9a94d93SGao Xiang 
142a9a94d93SGao Xiang 	set_page_private(page, u.v);
143a9a94d93SGao Xiang 	smp_wmb();
144a9a94d93SGao Xiang 	SetPagePrivate(page);
145a9a94d93SGao Xiang }
146a9a94d93SGao Xiang 
147a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page)
148a9a94d93SGao Xiang {
149a9a94d93SGao Xiang 	atomic_inc((atomic_t *)&page->private);
150a9a94d93SGao Xiang }
151a9a94d93SGao Xiang 
152a9a94d93SGao Xiang static inline void z_erofs_page_mark_eio(struct page *page)
153a9a94d93SGao Xiang {
154a9a94d93SGao Xiang 	int orig;
155a9a94d93SGao Xiang 
156a9a94d93SGao Xiang 	do {
157a9a94d93SGao Xiang 		orig = atomic_read((atomic_t *)&page->private);
158a9a94d93SGao Xiang 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig,
159a9a94d93SGao Xiang 				orig | Z_EROFS_PAGE_EIO) != orig);
160a9a94d93SGao Xiang }
161a9a94d93SGao Xiang 
162a9a94d93SGao Xiang static inline void z_erofs_onlinepage_endio(struct page *page)
163a9a94d93SGao Xiang {
164a9a94d93SGao Xiang 	unsigned int v;
165a9a94d93SGao Xiang 
166a9a94d93SGao Xiang 	DBG_BUGON(!PagePrivate(page));
167a9a94d93SGao Xiang 	v = atomic_dec_return((atomic_t *)&page->private);
168a9a94d93SGao Xiang 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
169a9a94d93SGao Xiang 		set_page_private(page, 0);
170a9a94d93SGao Xiang 		ClearPagePrivate(page);
171a9a94d93SGao Xiang 		if (!(v & Z_EROFS_PAGE_EIO))
172a9a94d93SGao Xiang 			SetPageUptodate(page);
173a9a94d93SGao Xiang 		unlock_page(page);
174a9a94d93SGao Xiang 	}
175a9a94d93SGao Xiang }
176a9a94d93SGao Xiang 
177a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES		32
178a9a94d93SGao Xiang 
17947e4937aSGao Xiang /*
1809f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
1819f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
1829f6cc76eSGao Xiang  */
1839f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
1849f6cc76eSGao Xiang 	struct kmem_cache *slab;
1859f6cc76eSGao Xiang 	unsigned int maxpages;
1869f6cc76eSGao Xiang 	char name[48];
1879f6cc76eSGao Xiang };
1889f6cc76eSGao Xiang 
1899f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
1909f6cc76eSGao Xiang 
1919f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
1929f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
1939f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
1949f6cc76eSGao Xiang };
1959f6cc76eSGao Xiang 
19606a304cdSGao Xiang struct z_erofs_bvec_iter {
19706a304cdSGao Xiang 	struct page *bvpage;
19806a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
19906a304cdSGao Xiang 	unsigned int nr, cur;
20006a304cdSGao Xiang };
20106a304cdSGao Xiang 
20206a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
20306a304cdSGao Xiang {
20406a304cdSGao Xiang 	if (iter->bvpage)
20506a304cdSGao Xiang 		kunmap_local(iter->bvset);
20606a304cdSGao Xiang 	return iter->bvpage;
20706a304cdSGao Xiang }
20806a304cdSGao Xiang 
20906a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
21006a304cdSGao Xiang {
21106a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
21206a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
21306a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
21406a304cdSGao Xiang 	struct page *oldpage;
21506a304cdSGao Xiang 
21606a304cdSGao Xiang 	DBG_BUGON(!nextpage);
21706a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
21806a304cdSGao Xiang 	iter->bvpage = nextpage;
21906a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
22006a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
22106a304cdSGao Xiang 	iter->cur = 0;
22206a304cdSGao Xiang 	return oldpage;
22306a304cdSGao Xiang }
22406a304cdSGao Xiang 
22506a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
22606a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
22706a304cdSGao Xiang 				    unsigned int bootstrap_nr,
22806a304cdSGao Xiang 				    unsigned int cur)
22906a304cdSGao Xiang {
23006a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
23106a304cdSGao Xiang 		.nr = bootstrap_nr,
23206a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
23306a304cdSGao Xiang 	};
23406a304cdSGao Xiang 
23506a304cdSGao Xiang 	while (cur > iter->nr) {
23606a304cdSGao Xiang 		cur -= iter->nr;
23706a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
23806a304cdSGao Xiang 	}
23906a304cdSGao Xiang 	iter->cur = cur;
24006a304cdSGao Xiang }
24106a304cdSGao Xiang 
24206a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
24306a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
24406a304cdSGao Xiang 				struct page **candidate_bvpage)
24506a304cdSGao Xiang {
24606a304cdSGao Xiang 	if (iter->cur == iter->nr) {
24706a304cdSGao Xiang 		if (!*candidate_bvpage)
24806a304cdSGao Xiang 			return -EAGAIN;
24906a304cdSGao Xiang 
25006a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
25106a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
25206a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
25306a304cdSGao Xiang 
25406a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
25506a304cdSGao Xiang 		*candidate_bvpage = NULL;
25606a304cdSGao Xiang 	}
25706a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
25806a304cdSGao Xiang 	return 0;
25906a304cdSGao Xiang }
26006a304cdSGao Xiang 
26106a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
26206a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
26306a304cdSGao Xiang 				 struct page **old_bvpage)
26406a304cdSGao Xiang {
26506a304cdSGao Xiang 	if (iter->cur == iter->nr)
26606a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
26706a304cdSGao Xiang 	else
26806a304cdSGao Xiang 		*old_bvpage = NULL;
26906a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
27006a304cdSGao Xiang }
27106a304cdSGao Xiang 
2729f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2739f6cc76eSGao Xiang {
2749f6cc76eSGao Xiang 	int i;
2759f6cc76eSGao Xiang 
2769f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2779f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2789f6cc76eSGao Xiang 			continue;
2799f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2809f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2819f6cc76eSGao Xiang 	}
2829f6cc76eSGao Xiang }
2839f6cc76eSGao Xiang 
2849f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2859f6cc76eSGao Xiang {
2869f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2879f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2889f6cc76eSGao Xiang 	unsigned int size;
2899f6cc76eSGao Xiang 
2909f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2919f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
292ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2939f6cc76eSGao Xiang 
2949f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
2959f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
2969f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
2979f6cc76eSGao Xiang 		if (pcs->slab)
2989f6cc76eSGao Xiang 			continue;
2999f6cc76eSGao Xiang 
3009f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3019f6cc76eSGao Xiang 		return -ENOMEM;
3029f6cc76eSGao Xiang 	}
3039f6cc76eSGao Xiang 	return 0;
3049f6cc76eSGao Xiang }
3059f6cc76eSGao Xiang 
3069f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3079f6cc76eSGao Xiang {
3089f6cc76eSGao Xiang 	int i;
3099f6cc76eSGao Xiang 
3109f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3119f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3129f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3139f6cc76eSGao Xiang 
3149f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3159f6cc76eSGao Xiang 			continue;
3169f6cc76eSGao Xiang 
3179f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3189f6cc76eSGao Xiang 		if (!pcl)
3199f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3209f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3219f6cc76eSGao Xiang 		return pcl;
3229f6cc76eSGao Xiang 	}
3239f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3249f6cc76eSGao Xiang }
3259f6cc76eSGao Xiang 
3269f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3279f6cc76eSGao Xiang {
328cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3299f6cc76eSGao Xiang 	int i;
3309f6cc76eSGao Xiang 
3319f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3329f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3339f6cc76eSGao Xiang 
334cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3359f6cc76eSGao Xiang 			continue;
3369f6cc76eSGao Xiang 
3379f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3389f6cc76eSGao Xiang 		return;
3399f6cc76eSGao Xiang 	}
3409f6cc76eSGao Xiang 	DBG_BUGON(1);
3419f6cc76eSGao Xiang }
3429f6cc76eSGao Xiang 
34347e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
34447e4937aSGao Xiang 
345*3fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
346*3fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers;
347*3fffb589SSandeep Dhavale 
348*3fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void)
34947e4937aSGao Xiang {
350*3fffb589SSandeep Dhavale 	struct kthread_worker *worker;
351*3fffb589SSandeep Dhavale 	unsigned int cpu;
352*3fffb589SSandeep Dhavale 
353*3fffb589SSandeep Dhavale 	for_each_possible_cpu(cpu) {
354*3fffb589SSandeep Dhavale 		worker = rcu_dereference_protected(
355*3fffb589SSandeep Dhavale 					z_erofs_pcpu_workers[cpu], 1);
356*3fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
357*3fffb589SSandeep Dhavale 		if (worker)
358*3fffb589SSandeep Dhavale 			kthread_destroy_worker(worker);
359*3fffb589SSandeep Dhavale 	}
360*3fffb589SSandeep Dhavale 	kfree(z_erofs_pcpu_workers);
36147e4937aSGao Xiang }
36247e4937aSGao Xiang 
363*3fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu)
36447e4937aSGao Xiang {
365*3fffb589SSandeep Dhavale 	struct kthread_worker *worker =
366*3fffb589SSandeep Dhavale 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
36747e4937aSGao Xiang 
368*3fffb589SSandeep Dhavale 	if (IS_ERR(worker))
369*3fffb589SSandeep Dhavale 		return worker;
370*3fffb589SSandeep Dhavale 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
371*3fffb589SSandeep Dhavale 		sched_set_fifo_low(worker->task);
372*3fffb589SSandeep Dhavale 	else
373*3fffb589SSandeep Dhavale 		sched_set_normal(worker->task, 0);
374*3fffb589SSandeep Dhavale 	return worker;
375*3fffb589SSandeep Dhavale }
376*3fffb589SSandeep Dhavale 
377*3fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void)
378*3fffb589SSandeep Dhavale {
379*3fffb589SSandeep Dhavale 	struct kthread_worker *worker;
380*3fffb589SSandeep Dhavale 	unsigned int cpu;
381*3fffb589SSandeep Dhavale 
382*3fffb589SSandeep Dhavale 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
383*3fffb589SSandeep Dhavale 			sizeof(struct kthread_worker *), GFP_ATOMIC);
384*3fffb589SSandeep Dhavale 	if (!z_erofs_pcpu_workers)
385*3fffb589SSandeep Dhavale 		return -ENOMEM;
386*3fffb589SSandeep Dhavale 
387*3fffb589SSandeep Dhavale 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
388*3fffb589SSandeep Dhavale 		worker = erofs_init_percpu_worker(cpu);
389*3fffb589SSandeep Dhavale 		if (!IS_ERR(worker))
390*3fffb589SSandeep Dhavale 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
391*3fffb589SSandeep Dhavale 	}
392*3fffb589SSandeep Dhavale 	return 0;
393*3fffb589SSandeep Dhavale }
394*3fffb589SSandeep Dhavale #else
395*3fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {}
396*3fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; }
397*3fffb589SSandeep Dhavale #endif
398*3fffb589SSandeep Dhavale 
399*3fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
400*3fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
401*3fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state;
402*3fffb589SSandeep Dhavale 
403*3fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu)
404*3fffb589SSandeep Dhavale {
405*3fffb589SSandeep Dhavale 	struct kthread_worker *worker, *old;
406*3fffb589SSandeep Dhavale 
407*3fffb589SSandeep Dhavale 	worker = erofs_init_percpu_worker(cpu);
408*3fffb589SSandeep Dhavale 	if (IS_ERR(worker))
409*3fffb589SSandeep Dhavale 		return PTR_ERR(worker);
410*3fffb589SSandeep Dhavale 
411*3fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
412*3fffb589SSandeep Dhavale 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
413*3fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
414*3fffb589SSandeep Dhavale 	if (!old)
415*3fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
416*3fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
417*3fffb589SSandeep Dhavale 	if (old)
418*3fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
419*3fffb589SSandeep Dhavale 	return 0;
420*3fffb589SSandeep Dhavale }
421*3fffb589SSandeep Dhavale 
422*3fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu)
423*3fffb589SSandeep Dhavale {
424*3fffb589SSandeep Dhavale 	struct kthread_worker *worker;
425*3fffb589SSandeep Dhavale 
426*3fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
427*3fffb589SSandeep Dhavale 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
428*3fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
429*3fffb589SSandeep Dhavale 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
430*3fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
431*3fffb589SSandeep Dhavale 
432*3fffb589SSandeep Dhavale 	synchronize_rcu();
433*3fffb589SSandeep Dhavale 	if (worker)
434*3fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
435*3fffb589SSandeep Dhavale 	return 0;
436*3fffb589SSandeep Dhavale }
437*3fffb589SSandeep Dhavale 
438*3fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void)
439*3fffb589SSandeep Dhavale {
440*3fffb589SSandeep Dhavale 	int state;
441*3fffb589SSandeep Dhavale 
442*3fffb589SSandeep Dhavale 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
443*3fffb589SSandeep Dhavale 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
444*3fffb589SSandeep Dhavale 	if (state < 0)
445*3fffb589SSandeep Dhavale 		return state;
446*3fffb589SSandeep Dhavale 
447*3fffb589SSandeep Dhavale 	erofs_cpuhp_state = state;
448*3fffb589SSandeep Dhavale 	return 0;
449*3fffb589SSandeep Dhavale }
450*3fffb589SSandeep Dhavale 
451*3fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void)
452*3fffb589SSandeep Dhavale {
453*3fffb589SSandeep Dhavale 	if (erofs_cpuhp_state)
454*3fffb589SSandeep Dhavale 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
455*3fffb589SSandeep Dhavale }
456*3fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
457*3fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; }
458*3fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {}
459*3fffb589SSandeep Dhavale #endif
460*3fffb589SSandeep Dhavale 
461*3fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void)
462*3fffb589SSandeep Dhavale {
463*3fffb589SSandeep Dhavale 	erofs_cpu_hotplug_destroy();
464*3fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
465*3fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
466*3fffb589SSandeep Dhavale 	z_erofs_destroy_pcluster_pool();
46747e4937aSGao Xiang }
46847e4937aSGao Xiang 
46947e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
47047e4937aSGao Xiang {
4719f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
47247e4937aSGao Xiang 
4739f6cc76eSGao Xiang 	if (err)
474*3fffb589SSandeep Dhavale 		goto out_error_pcluster_pool;
475*3fffb589SSandeep Dhavale 
476*3fffb589SSandeep Dhavale 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
477*3fffb589SSandeep Dhavale 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
478*3fffb589SSandeep Dhavale 	if (!z_erofs_workqueue)
479*3fffb589SSandeep Dhavale 		goto out_error_workqueue_init;
480*3fffb589SSandeep Dhavale 
481*3fffb589SSandeep Dhavale 	err = erofs_init_percpu_workers();
4829f6cc76eSGao Xiang 	if (err)
483*3fffb589SSandeep Dhavale 		goto out_error_pcpu_worker;
484*3fffb589SSandeep Dhavale 
485*3fffb589SSandeep Dhavale 	err = erofs_cpu_hotplug_init();
486*3fffb589SSandeep Dhavale 	if (err < 0)
487*3fffb589SSandeep Dhavale 		goto out_error_cpuhp_init;
488*3fffb589SSandeep Dhavale 	return err;
489*3fffb589SSandeep Dhavale 
490*3fffb589SSandeep Dhavale out_error_cpuhp_init:
491*3fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
492*3fffb589SSandeep Dhavale out_error_pcpu_worker:
493*3fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
494*3fffb589SSandeep Dhavale out_error_workqueue_init:
4959f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
496*3fffb589SSandeep Dhavale out_error_pcluster_pool:
4979f6cc76eSGao Xiang 	return err;
49847e4937aSGao Xiang }
49947e4937aSGao Xiang 
500db166fc2SGao Xiang enum z_erofs_pclustermode {
501db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
50247e4937aSGao Xiang 	/*
503db166fc2SGao Xiang 	 * The current pclusters was the tail of an exist chain, in addition
504db166fc2SGao Xiang 	 * that the previous processed chained pclusters are all decided to
50547e4937aSGao Xiang 	 * be hooked up to it.
506db166fc2SGao Xiang 	 * A new chain will be created for the remaining pclusters which are
507db166fc2SGao Xiang 	 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
508db166fc2SGao Xiang 	 * the next pcluster cannot reuse the whole page safely for inplace I/O
509db166fc2SGao Xiang 	 * in the following scenario:
51047e4937aSGao Xiang 	 *  ________________________________________________________________
51147e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
512db166fc2SGao Xiang 	 * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
513db166fc2SGao Xiang 	 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
51447e4937aSGao Xiang 	 */
515db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_HOOKED,
5160b964600SGao Xiang 	/*
517db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
5180b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
5190b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
520387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
5210b964600SGao Xiang 	 */
522db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
52347e4937aSGao Xiang 	/*
52447e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
52547e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
52647e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
52747e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
52847e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
52947e4937aSGao Xiang 	 * illustrated below:
53047e4937aSGao Xiang 	 *  ________________________________________________________________
53147e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
53247e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
533db166fc2SGao Xiang 	 * | PCLUSTER_FOLLOWED or |                                        |
534db166fc2SGao Xiang 	 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
53547e4937aSGao Xiang 	 *
53647e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
53747e4937aSGao Xiang 	 */
538db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
53947e4937aSGao Xiang };
54047e4937aSGao Xiang 
5415c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
5425c6dcc57SGao Xiang 	struct inode *const inode;
5435c6dcc57SGao Xiang 	struct erofs_map_blocks map;
54406a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
54547e4937aSGao Xiang 
54606a304cdSGao Xiang 	struct page *candidate_bvpage;
54747e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
54847e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
549db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
55047e4937aSGao Xiang 
5516ea5aad3SGao Xiang 	bool readahead;
55247e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
55347e4937aSGao Xiang 	bool backmost;
55447e4937aSGao Xiang 	erofs_off_t headoffset;
555ed722fbcSGao Xiang 
556ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
557ed722fbcSGao Xiang 	unsigned int icur;
55847e4937aSGao Xiang };
55947e4937aSGao Xiang 
56047e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
5615c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
562db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
56347e4937aSGao Xiang 
5641282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
5651282dea3SGao Xiang {
5661282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
5671282dea3SGao Xiang 
5681282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
5691282dea3SGao Xiang 		return false;
5701282dea3SGao Xiang 
5711282dea3SGao Xiang 	if (fe->backmost)
5721282dea3SGao Xiang 		return true;
5731282dea3SGao Xiang 
5741282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
5751282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
5761282dea3SGao Xiang 		return true;
5771282dea3SGao Xiang 
5781282dea3SGao Xiang 	return false;
5791282dea3SGao Xiang }
5801282dea3SGao Xiang 
5816f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
582eaa9172aSGao Xiang 			       struct page **pagepool)
58347e4937aSGao Xiang {
5846f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
5855c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
5861282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
58747e4937aSGao Xiang 	bool standalone = true;
5886f39d1e1SGao Xiang 	/*
5896f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
5906f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
5916f39d1e1SGao Xiang 	 */
5921825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
5931825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
594ed722fbcSGao Xiang 	unsigned int i;
59547e4937aSGao Xiang 
596db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
59747e4937aSGao Xiang 		return;
59847e4937aSGao Xiang 
599ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
60047e4937aSGao Xiang 		struct page *page;
601b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
6021825c8d7SGao Xiang 		struct page *newpage = NULL;
60347e4937aSGao Xiang 
60447e4937aSGao Xiang 		/* the compressed page was loaded before */
605ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
60647e4937aSGao Xiang 			continue;
60747e4937aSGao Xiang 
608ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
60947e4937aSGao Xiang 
61047e4937aSGao Xiang 		if (page) {
611b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
6120b964600SGao Xiang 		} else {
6130b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
6140b964600SGao Xiang 			standalone = false;
6151282dea3SGao Xiang 			if (!shouldalloc)
6161282dea3SGao Xiang 				continue;
6171282dea3SGao Xiang 
6181282dea3SGao Xiang 			/*
6191282dea3SGao Xiang 			 * try to use cached I/O if page allocation
6201282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
6211282dea3SGao Xiang 			 * to avoid any direct reclaim.
6221282dea3SGao Xiang 			 */
6231825c8d7SGao Xiang 			newpage = erofs_allocpage(pagepool, gfp);
6241825c8d7SGao Xiang 			if (!newpage)
62547e4937aSGao Xiang 				continue;
6261282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
627b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
62847e4937aSGao Xiang 		}
62947e4937aSGao Xiang 
630b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
63147e4937aSGao Xiang 			continue;
63247e4937aSGao Xiang 
633eaa9172aSGao Xiang 		if (page)
63447e4937aSGao Xiang 			put_page(page);
635eaa9172aSGao Xiang 		else if (newpage)
636eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
63747e4937aSGao Xiang 	}
63847e4937aSGao Xiang 
6390b964600SGao Xiang 	/*
6400b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
6410b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
6420b964600SGao Xiang 	 */
6430b964600SGao Xiang 	if (standalone)
644db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
64547e4937aSGao Xiang }
64647e4937aSGao Xiang 
64747e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
64847e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
64947e4937aSGao Xiang 				       struct erofs_workgroup *grp)
65047e4937aSGao Xiang {
65147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
65247e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
65347e4937aSGao Xiang 	int i;
65447e4937aSGao Xiang 
655cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
65647e4937aSGao Xiang 	/*
65747e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
65847e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
65947e4937aSGao Xiang 	 */
6609f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
661ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
66247e4937aSGao Xiang 
66347e4937aSGao Xiang 		if (!page)
66447e4937aSGao Xiang 			continue;
66547e4937aSGao Xiang 
66647e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
66747e4937aSGao Xiang 		if (!trylock_page(page))
66847e4937aSGao Xiang 			return -EBUSY;
66947e4937aSGao Xiang 
670f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
67147e4937aSGao Xiang 			continue;
67247e4937aSGao Xiang 
67347e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
674ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6756aaa7b06SGao Xiang 		detach_page_private(page);
67647e4937aSGao Xiang 		unlock_page(page);
67747e4937aSGao Xiang 	}
67847e4937aSGao Xiang 	return 0;
67947e4937aSGao Xiang }
68047e4937aSGao Xiang 
681d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
68247e4937aSGao Xiang {
68347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
684ed722fbcSGao Xiang 	int ret, i;
68547e4937aSGao Xiang 
686ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
687ed722fbcSGao Xiang 		return 0;
68847e4937aSGao Xiang 
689ed722fbcSGao Xiang 	ret = 0;
690cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
6919f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
692ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
693ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
69447e4937aSGao Xiang 			ret = 1;
69547e4937aSGao Xiang 			break;
69647e4937aSGao Xiang 		}
69747e4937aSGao Xiang 	}
69847e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
6996aaa7b06SGao Xiang 	if (ret)
7006aaa7b06SGao Xiang 		detach_page_private(page);
70147e4937aSGao Xiang 	return ret;
70247e4937aSGao Xiang }
70347e4937aSGao Xiang 
7045c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
705ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
70647e4937aSGao Xiang {
7075c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
70847e4937aSGao Xiang 
709ed722fbcSGao Xiang 	while (fe->icur > 0) {
710ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
711ed722fbcSGao Xiang 			     NULL, bvec->page)) {
712ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
71347e4937aSGao Xiang 			return true;
714ed722fbcSGao Xiang 		}
715ed722fbcSGao Xiang 	}
71647e4937aSGao Xiang 	return false;
71747e4937aSGao Xiang }
71847e4937aSGao Xiang 
71987ca34a7SGao Xiang /* callers must be with pcluster lock held */
7205c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
7215b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
72247e4937aSGao Xiang {
72347e4937aSGao Xiang 	int ret;
72447e4937aSGao Xiang 
725db166fc2SGao Xiang 	if (exclusive) {
72606a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
727ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
72847e4937aSGao Xiang 			return 0;
72906a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
730db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
73106a304cdSGao Xiang 		    !fe->candidate_bvpage)
73206a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
73306a304cdSGao Xiang 	}
73406a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
73506a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
73606a304cdSGao Xiang 	return ret;
73747e4937aSGao Xiang }
73847e4937aSGao Xiang 
7395c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
74047e4937aSGao Xiang {
7415c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
7425c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
74347e4937aSGao Xiang 
744473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
745473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
746473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
74747e4937aSGao Xiang 		*owned_head = &pcl->next;
748473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
749db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
750473e15b0SGao Xiang 		return;
751473e15b0SGao Xiang 	}
752473e15b0SGao Xiang 
75347e4937aSGao Xiang 	/*
754473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
755473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
75647e4937aSGao Xiang 	 */
757267f2492SGao Xiang 	if (*owned_head != &pcl->next && pcl != f->tailpcl &&
758267f2492SGao Xiang 	    cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
759473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
76047e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
761db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_HOOKED;
7625c6dcc57SGao Xiang 		f->tailpcl = NULL;
763473e15b0SGao Xiang 		return;
76447e4937aSGao Xiang 	}
765473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
766db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
76747e4937aSGao Xiang }
76847e4937aSGao Xiang 
76983a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
77047e4937aSGao Xiang {
77183a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
772cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
77347e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
77464094a04SGao Xiang 	struct erofs_workgroup *grp;
77547e4937aSGao Xiang 	int err;
77647e4937aSGao Xiang 
777c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
778c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
7798f899262SGao Xiang 		DBG_BUGON(1);
7808f899262SGao Xiang 		return -EFSCORRUPTED;
7818f899262SGao Xiang 	}
7828f899262SGao Xiang 
7839f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
784cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
785cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
7869f6cc76eSGao Xiang 	if (IS_ERR(pcl))
7879f6cc76eSGao Xiang 		return PTR_ERR(pcl);
78847e4937aSGao Xiang 
78964094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
7908f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
7912bfab9c0SGao Xiang 	pcl->length = 0;
7922bfab9c0SGao Xiang 	pcl->partial = true;
79347e4937aSGao Xiang 
79447e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
7955c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
79687ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
797db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
79847e4937aSGao Xiang 
79947e4937aSGao Xiang 	/*
80047e4937aSGao Xiang 	 * lock all primary followed works before visible to others
80147e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
80247e4937aSGao Xiang 	 */
80387ca34a7SGao Xiang 	mutex_init(&pcl->lock);
80487ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
80547e4937aSGao Xiang 
806cecf864dSYue Hu 	if (ztailpacking) {
807cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
808cecf864dSYue Hu 		pcl->pageofs_in = erofs_blkoff(map->m_pa);
809cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
810cecf864dSYue Hu 	} else {
811cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
812cecf864dSYue Hu 
81383a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
81464094a04SGao Xiang 		if (IS_ERR(grp)) {
81564094a04SGao Xiang 			err = PTR_ERR(grp);
81664094a04SGao Xiang 			goto err_out;
81764094a04SGao Xiang 		}
81864094a04SGao Xiang 
81964094a04SGao Xiang 		if (grp != &pcl->obj) {
8205c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
821cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
82264094a04SGao Xiang 			err = -EEXIST;
82364094a04SGao Xiang 			goto err_out;
82447e4937aSGao Xiang 		}
825cecf864dSYue Hu 	}
82647e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
8275c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
8285c6dcc57SGao Xiang 		fe->tailpcl = pcl;
8295c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
8305c6dcc57SGao Xiang 	fe->pcl = pcl;
8319e579fc1SGao Xiang 	return 0;
83264094a04SGao Xiang 
83364094a04SGao Xiang err_out:
83487ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
8359f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
83664094a04SGao Xiang 	return err;
83747e4937aSGao Xiang }
83847e4937aSGao Xiang 
83983a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
84047e4937aSGao Xiang {
84183a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
8420d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
8439e579fc1SGao Xiang 	int ret;
84447e4937aSGao Xiang 
84587ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
84647e4937aSGao Xiang 
84787ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
8485c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
8495c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
85047e4937aSGao Xiang 
8510d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
8520d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
8530d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
8540d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
85547e4937aSGao Xiang 		DBG_BUGON(1);
856cecf864dSYue Hu 		return -EFSCORRUPTED;
857cecf864dSYue Hu 	}
85847e4937aSGao Xiang 
85964094a04SGao Xiang 	if (grp) {
8605c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
8610d823b42SGao Xiang 		ret = -EEXIST;
86264094a04SGao Xiang 	} else {
86383a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
86464094a04SGao Xiang 	}
86547e4937aSGao Xiang 
8660d823b42SGao Xiang 	if (ret == -EEXIST) {
867267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
868267f2492SGao Xiang 		/* used to check tail merging loop due to corrupted images */
869267f2492SGao Xiang 		if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
870267f2492SGao Xiang 			fe->tailpcl = fe->pcl;
871267f2492SGao Xiang 
872267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
8730d823b42SGao Xiang 	} else if (ret) {
8740d823b42SGao Xiang 		return ret;
8750d823b42SGao Xiang 	}
87606a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
877387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
87881382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
879ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
88047e4937aSGao Xiang 	return 0;
88147e4937aSGao Xiang }
88247e4937aSGao Xiang 
88347e4937aSGao Xiang /*
88447e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
88547e4937aSGao Xiang  * only after a RCU grace period.
88647e4937aSGao Xiang  */
88747e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
88847e4937aSGao Xiang {
88987ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
89087ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
89147e4937aSGao Xiang }
89247e4937aSGao Xiang 
89347e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
89447e4937aSGao Xiang {
89547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
89647e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
89747e4937aSGao Xiang 
89887ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
89947e4937aSGao Xiang }
90047e4937aSGao Xiang 
9015c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
90247e4937aSGao Xiang {
90387ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
90447e4937aSGao Xiang 
90587ca34a7SGao Xiang 	if (!pcl)
90647e4937aSGao Xiang 		return false;
90747e4937aSGao Xiang 
90806a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
90987ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
91047e4937aSGao Xiang 
91106a304cdSGao Xiang 	if (fe->candidate_bvpage) {
91206a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
91306a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
91406a304cdSGao Xiang 	}
91506a304cdSGao Xiang 
91647e4937aSGao Xiang 	/*
91747e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
91847e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
91947e4937aSGao Xiang 	 */
920db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
92187ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
92247e4937aSGao Xiang 
92387ca34a7SGao Xiang 	fe->pcl = NULL;
92447e4937aSGao Xiang 	return true;
92547e4937aSGao Xiang }
92647e4937aSGao Xiang 
927b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
928b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
929b15b2e30SYue Hu 				 unsigned int len)
930b15b2e30SYue Hu {
931b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
932b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
933b15b2e30SYue Hu 	u8 *src, *dst;
934b15b2e30SYue Hu 	unsigned int i, cnt;
935b15b2e30SYue Hu 
936e5126de1SYue Hu 	if (!packed_inode)
937e5126de1SYue Hu 		return -EFSCORRUPTED;
938e5126de1SYue Hu 
939b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
940b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
941b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
942b15b2e30SYue Hu 			    EROFS_BLKSIZ - erofs_blkoff(pos));
943b15b2e30SYue Hu 		src = erofs_bread(&buf, packed_inode,
944b15b2e30SYue Hu 				  erofs_blknr(pos), EROFS_KMAP);
945b15b2e30SYue Hu 		if (IS_ERR(src)) {
946b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
947b15b2e30SYue Hu 			return PTR_ERR(src);
948b15b2e30SYue Hu 		}
949b15b2e30SYue Hu 
950b15b2e30SYue Hu 		dst = kmap_local_page(page);
951b15b2e30SYue Hu 		memcpy(dst + pageofs + i, src + erofs_blkoff(pos), cnt);
952b15b2e30SYue Hu 		kunmap_local(dst);
953b15b2e30SYue Hu 		pos += cnt;
954b15b2e30SYue Hu 	}
955b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
956b15b2e30SYue Hu 	return 0;
957b15b2e30SYue Hu }
958b15b2e30SYue Hu 
95947e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
960eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
96147e4937aSGao Xiang {
96247e4937aSGao Xiang 	struct inode *const inode = fe->inode;
96347e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
96447e4937aSGao Xiang 	const loff_t offset = page_offset(page);
9655b220b20SGao Xiang 	bool tight = true, exclusive;
9662bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
96747e4937aSGao Xiang 	int err = 0;
96847e4937aSGao Xiang 
96947e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
97047e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
97147e4937aSGao Xiang 
97247e4937aSGao Xiang 	spiltted = 0;
97347e4937aSGao Xiang 	end = PAGE_SIZE;
97447e4937aSGao Xiang repeat:
97547e4937aSGao Xiang 	cur = end - 1;
97647e4937aSGao Xiang 
97739397a46SGao Xiang 	if (offset + cur < map->m_la ||
97839397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
97939397a46SGao Xiang 		erofs_dbg("out-of-range map @ pos %llu", offset + cur);
98047e4937aSGao Xiang 
9815c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
98247e4937aSGao Xiang 			fe->backmost = false;
98347e4937aSGao Xiang 		map->m_la = offset + cur;
98447e4937aSGao Xiang 		map->m_llen = 0;
98547e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
9868d8a09b0SGao Xiang 		if (err)
98767148551SGao Xiang 			goto out;
98839397a46SGao Xiang 	} else {
98939397a46SGao Xiang 		if (fe->pcl)
99039397a46SGao Xiang 			goto hitted;
99139397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
99239397a46SGao Xiang 	}
99347e4937aSGao Xiang 
994b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
995b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
99647e4937aSGao Xiang 		goto hitted;
99747e4937aSGao Xiang 
99883a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
9998d8a09b0SGao Xiang 	if (err)
100067148551SGao Xiang 		goto out;
100147e4937aSGao Xiang 
10025c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
100309c54379SGao Xiang 		void *mp;
1004cecf864dSYue Hu 
100509c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
100609c54379SGao Xiang 					erofs_blknr(map->m_pa), EROFS_NO_KMAP);
100709c54379SGao Xiang 		if (IS_ERR(mp)) {
100809c54379SGao Xiang 			err = PTR_ERR(mp);
1009cecf864dSYue Hu 			erofs_err(inode->i_sb,
1010cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
101167148551SGao Xiang 			goto out;
1012cecf864dSYue Hu 		}
101309c54379SGao Xiang 		get_page(fe->map.buf.page);
1014ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1015ed722fbcSGao Xiang 			   fe->map.buf.page);
1016db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1017cecf864dSYue Hu 	} else {
10186f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
10191282dea3SGao Xiang 		z_erofs_bind_cache(fe, pagepool);
1020cecf864dSYue Hu 	}
102147e4937aSGao Xiang hitted:
1022dc76ea8cSGao Xiang 	/*
1023dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
1024dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
1025dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
1026387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
1027dc76ea8cSGao Xiang 	 */
1028db166fc2SGao Xiang 	tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1029db166fc2SGao Xiang 		  fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1030dc76ea8cSGao Xiang 
103147e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
10328d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
103347e4937aSGao Xiang 		zero_user_segment(page, cur, end);
103447e4937aSGao Xiang 		goto next_part;
103547e4937aSGao Xiang 	}
1036b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
1037b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
1038b15b2e30SYue Hu 
1039b15b2e30SYue Hu 		if (offset > map->m_la) {
1040b15b2e30SYue Hu 			pageofs = 0;
1041b15b2e30SYue Hu 			skip = offset - map->m_la;
1042b15b2e30SYue Hu 		} else {
1043b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
1044b15b2e30SYue Hu 			skip = 0;
1045b15b2e30SYue Hu 		}
1046b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
1047b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1048b15b2e30SYue Hu 		if (err)
1049b15b2e30SYue Hu 			goto out;
1050b15b2e30SYue Hu 		++spiltted;
1051b15b2e30SYue Hu 		tight = false;
1052b15b2e30SYue Hu 		goto next_part;
1053b15b2e30SYue Hu 	}
105447e4937aSGao Xiang 
10555b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
105647e4937aSGao Xiang 	if (cur)
1057db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
105847e4937aSGao Xiang 
105947e4937aSGao Xiang retry:
106006a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
106106a304cdSGao Xiang 					.page = page,
106206a304cdSGao Xiang 					.offset = offset - map->m_la,
106306a304cdSGao Xiang 					.end = end,
10645b220b20SGao Xiang 				  }), exclusive);
106506a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
106606a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
106706a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
106806a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
106906a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
107047e4937aSGao Xiang 		goto retry;
107147e4937aSGao Xiang 	}
107247e4937aSGao Xiang 
107306a304cdSGao Xiang 	if (err) {
107406a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
107567148551SGao Xiang 		goto out;
107606a304cdSGao Xiang 	}
107747e4937aSGao Xiang 
107867148551SGao Xiang 	z_erofs_onlinepage_split(page);
107947e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
108047e4937aSGao Xiang 	++spiltted;
1081267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1082267f2492SGao Xiang 		fe->pcl->multibases = true;
10832bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
10842bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
10852bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
10862bfab9c0SGao Xiang 	}
1087e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1088e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1089e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
1090e7933278SGao Xiang 		fe->pcl->partial = false;
109147e4937aSGao Xiang next_part:
10922bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
109347e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
10942bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
109547e4937aSGao Xiang 
109647e4937aSGao Xiang 	end = cur;
109747e4937aSGao Xiang 	if (end > 0)
109847e4937aSGao Xiang 		goto repeat;
109947e4937aSGao Xiang 
110047e4937aSGao Xiang out:
110167148551SGao Xiang 	if (err)
110267148551SGao Xiang 		z_erofs_page_mark_eio(page);
110347e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
110447e4937aSGao Xiang 
11054f761fa2SGao Xiang 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
110647e4937aSGao Xiang 		  __func__, page, spiltted, map->m_llen);
110747e4937aSGao Xiang 	return err;
110847e4937aSGao Xiang }
110947e4937aSGao Xiang 
111040452ffcSHuang Jianan static bool z_erofs_get_sync_decompress_policy(struct erofs_sb_info *sbi,
111140452ffcSHuang Jianan 				       unsigned int readahead_pages)
111240452ffcSHuang Jianan {
1113a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
111440452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
111540452ffcSHuang Jianan 	    !readahead_pages)
111640452ffcSHuang Jianan 		return true;
111740452ffcSHuang Jianan 
111840452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
111940452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
112040452ffcSHuang Jianan 		return true;
112140452ffcSHuang Jianan 
112240452ffcSHuang Jianan 	return false;
112340452ffcSHuang Jianan }
112440452ffcSHuang Jianan 
11256aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
11266aaa7b06SGao Xiang {
11276aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
11286aaa7b06SGao Xiang }
11296aaa7b06SGao Xiang 
11304f05687fSGao Xiang struct z_erofs_decompress_backend {
11314f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
11324f05687fSGao Xiang 	struct super_block *sb;
11334f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
11344f05687fSGao Xiang 
11354f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
11364f05687fSGao Xiang 	struct page **decompressed_pages;
11374f05687fSGao Xiang 	/* pages to keep the compressed data */
11384f05687fSGao Xiang 	struct page **compressed_pages;
11394f05687fSGao Xiang 
1140267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
11414f05687fSGao Xiang 	struct page **pagepool;
11422bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
11434f05687fSGao Xiang };
11444f05687fSGao Xiang 
1145267f2492SGao Xiang struct z_erofs_bvec_item {
1146267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1147267f2492SGao Xiang 	struct list_head list;
1148267f2492SGao Xiang };
1149267f2492SGao Xiang 
1150267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
11513fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
11523fe96ee0SGao Xiang {
1153267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1154267f2492SGao Xiang 
1155267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1156267f2492SGao Xiang 		unsigned int pgnr;
11573fe96ee0SGao Xiang 
1158267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
11592bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
116063bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
11613fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1162267f2492SGao Xiang 			return;
11633fe96ee0SGao Xiang 		}
116463bbb856SGao Xiang 	}
11653fe96ee0SGao Xiang 
1166267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1167267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1168267f2492SGao Xiang 	item->bvec = *bvec;
1169267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1170267f2492SGao Xiang }
1171267f2492SGao Xiang 
1172267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1173267f2492SGao Xiang 				      int err)
1174267f2492SGao Xiang {
1175267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1176267f2492SGao Xiang 	struct list_head *p, *n;
1177267f2492SGao Xiang 
1178267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1179267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1180267f2492SGao Xiang 		unsigned int end, cur;
1181267f2492SGao Xiang 		void *dst, *src;
1182267f2492SGao Xiang 
1183267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1184267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1185267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1186267f2492SGao Xiang 			    bvi->bvec.end);
1187267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1188267f2492SGao Xiang 		while (cur < end) {
1189267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1190267f2492SGao Xiang 
1191267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1192267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1193267f2492SGao Xiang 
1194267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1195267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1196267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1197267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1198267f2492SGao Xiang 				err = -EFSCORRUPTED;
1199267f2492SGao Xiang 				cur += len;
1200267f2492SGao Xiang 				continue;
1201267f2492SGao Xiang 			}
1202267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1203267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1204267f2492SGao Xiang 			kunmap_local(src);
1205267f2492SGao Xiang 			cur += len;
1206267f2492SGao Xiang 		}
1207267f2492SGao Xiang 		kunmap_local(dst);
1208267f2492SGao Xiang 		if (err)
1209267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
1210267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
1211267f2492SGao Xiang 		list_del(p);
1212267f2492SGao Xiang 		kfree(bvi);
1213267f2492SGao Xiang 	}
1214267f2492SGao Xiang }
1215267f2492SGao Xiang 
1216267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
121742fec235SGao Xiang {
12184f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
121906a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
122006a304cdSGao Xiang 	struct page *old_bvpage;
1221267f2492SGao Xiang 	int i;
122242fec235SGao Xiang 
1223387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
122442fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
122506a304cdSGao Xiang 		struct z_erofs_bvec bvec;
122642fec235SGao Xiang 
122706a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
122842fec235SGao Xiang 
122906a304cdSGao Xiang 		if (old_bvpage)
12304f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
123142fec235SGao Xiang 
123206a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1233267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
123442fec235SGao Xiang 	}
123506a304cdSGao Xiang 
123606a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
123706a304cdSGao Xiang 	if (old_bvpage)
12384f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
123942fec235SGao Xiang }
124042fec235SGao Xiang 
12414f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
12424f05687fSGao Xiang 				  bool *overlapped)
124367139e36SGao Xiang {
12444f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
124567139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
124667139e36SGao Xiang 	int i, err = 0;
124767139e36SGao Xiang 
124867139e36SGao Xiang 	*overlapped = false;
124967139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1250ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1251ed722fbcSGao Xiang 		struct page *page = bvec->page;
125267139e36SGao Xiang 
125367139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
125467139e36SGao Xiang 		if (!page) {
125567139e36SGao Xiang 			DBG_BUGON(1);
125667139e36SGao Xiang 			continue;
125767139e36SGao Xiang 		}
1258fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
125967139e36SGao Xiang 
126067139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
126167139e36SGao Xiang 			if (!PageUptodate(page))
126267139e36SGao Xiang 				err = -EIO;
126367139e36SGao Xiang 			continue;
126467139e36SGao Xiang 		}
126567139e36SGao Xiang 
126667139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
126767139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
12684f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
126967139e36SGao Xiang 				if (!PageUptodate(page))
127067139e36SGao Xiang 					err = -EIO;
127167139e36SGao Xiang 				continue;
127267139e36SGao Xiang 			}
1273267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
127467139e36SGao Xiang 			*overlapped = true;
127567139e36SGao Xiang 		}
127667139e36SGao Xiang 	}
127767139e36SGao Xiang 
1278fe3e5914SGao Xiang 	if (err)
12794f05687fSGao Xiang 		return err;
12804f05687fSGao Xiang 	return 0;
128167139e36SGao Xiang }
128267139e36SGao Xiang 
12834f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12844f05687fSGao Xiang 				       int err)
128547e4937aSGao Xiang {
12864f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
12874f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1288cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
12892bfab9c0SGao Xiang 	unsigned int i, inputsize;
129067148551SGao Xiang 	int err2;
12912bfab9c0SGao Xiang 	struct page *page;
12922bfab9c0SGao Xiang 	bool overlapped;
129347e4937aSGao Xiang 
129487ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
12952bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
129647e4937aSGao Xiang 
1297fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1298fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1299fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1300fe3e5914SGao Xiang 	be->onstack_used = 0;
13012bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
13024f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
13032bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
13044f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
13052bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1306fe3e5914SGao Xiang 	}
1307fe3e5914SGao Xiang 
1308fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1309fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1310fe3e5914SGao Xiang 
1311fe3e5914SGao Xiang 	if (!be->decompressed_pages)
13124f05687fSGao Xiang 		be->decompressed_pages =
131312724ba3SGao Xiang 			kcalloc(be->nr_pages, sizeof(struct page *),
1314e7368187SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
1315fe3e5914SGao Xiang 	if (!be->compressed_pages)
1316fe3e5914SGao Xiang 		be->compressed_pages =
131712724ba3SGao Xiang 			kcalloc(pclusterpages, sizeof(struct page *),
1318fe3e5914SGao Xiang 				GFP_KERNEL | __GFP_NOFAIL);
131947e4937aSGao Xiang 
1320267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
13214f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
13224f05687fSGao Xiang 	if (err2)
13234f05687fSGao Xiang 		err = err2;
13248d8a09b0SGao Xiang 	if (err)
132547e4937aSGao Xiang 		goto out;
132647e4937aSGao Xiang 
1327cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1328cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1329cecf864dSYue Hu 	else
1330cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1331cecf864dSYue Hu 
133247e4937aSGao Xiang 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
13334f05687fSGao Xiang 					.sb = be->sb,
13344f05687fSGao Xiang 					.in = be->compressed_pages,
13354f05687fSGao Xiang 					.out = be->decompressed_pages,
1336cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
133787ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
13389f6cc76eSGao Xiang 					.inputsize = inputsize,
13392bfab9c0SGao Xiang 					.outputsize = pcl->length,
134047e4937aSGao Xiang 					.alg = pcl->algorithmformat,
134147e4937aSGao Xiang 					.inplace_io = overlapped,
13422bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1343267f2492SGao Xiang 					.fillgaps = pcl->multibases,
13444f05687fSGao Xiang 				 }, be->pagepool);
134547e4937aSGao Xiang 
134647e4937aSGao Xiang out:
1347cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1348cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1349ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1350ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1351cecf864dSYue Hu 		put_page(page);
1352cecf864dSYue Hu 	} else {
1353cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1354ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
135547e4937aSGao Xiang 
135647e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
135747e4937aSGao Xiang 				continue;
135847e4937aSGao Xiang 
13596aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
13604f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1361ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
136247e4937aSGao Xiang 		}
1363cecf864dSYue Hu 	}
1364fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1365fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
136612724ba3SGao Xiang 		kfree(be->compressed_pages);
1367267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
136847e4937aSGao Xiang 
13692bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
13704f05687fSGao Xiang 		page = be->decompressed_pages[i];
137147e4937aSGao Xiang 		if (!page)
137247e4937aSGao Xiang 			continue;
137347e4937aSGao Xiang 
13746aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
137547e4937aSGao Xiang 
13766aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
13774f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
137847e4937aSGao Xiang 			continue;
137967148551SGao Xiang 		if (err)
138067148551SGao Xiang 			z_erofs_page_mark_eio(page);
138147e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
138247e4937aSGao Xiang 	}
138347e4937aSGao Xiang 
13844f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
138512724ba3SGao Xiang 		kfree(be->decompressed_pages);
138647e4937aSGao Xiang 
13872bfab9c0SGao Xiang 	pcl->length = 0;
13882bfab9c0SGao Xiang 	pcl->partial = true;
1389267f2492SGao Xiang 	pcl->multibases = false;
139006a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
139187ca34a7SGao Xiang 	pcl->vcnt = 0;
139247e4937aSGao Xiang 
139387ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
139447e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
139587ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
139647e4937aSGao Xiang 	return err;
139747e4937aSGao Xiang }
139847e4937aSGao Xiang 
13990c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1400eaa9172aSGao Xiang 				     struct page **pagepool)
140147e4937aSGao Xiang {
14024f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
14034f05687fSGao Xiang 		.sb = io->sb,
14044f05687fSGao Xiang 		.pagepool = pagepool,
1405267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1406267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
14074f05687fSGao Xiang 	};
140847e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
140947e4937aSGao Xiang 
141047e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
14114f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
141247e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
14134f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
141447e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
141547e4937aSGao Xiang 
14164f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
14174f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
141847e4937aSGao Xiang 
14194f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
14204f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
142147e4937aSGao Xiang 	}
142247e4937aSGao Xiang }
142347e4937aSGao Xiang 
14240c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
142547e4937aSGao Xiang {
1426a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1427a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1428eaa9172aSGao Xiang 	struct page *pagepool = NULL;
142947e4937aSGao Xiang 
1430a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
14310c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
1432eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1433a4b1fab1SGao Xiang 	kvfree(bgq);
143447e4937aSGao Xiang }
143547e4937aSGao Xiang 
1436*3fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1437*3fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1438*3fffb589SSandeep Dhavale {
1439*3fffb589SSandeep Dhavale 	z_erofs_decompressqueue_work((struct work_struct *)work);
1440*3fffb589SSandeep Dhavale }
1441*3fffb589SSandeep Dhavale #endif
1442*3fffb589SSandeep Dhavale 
14437865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1444cdba5506SGao Xiang 				       int bios)
14457865827cSGao Xiang {
14467865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
14477865827cSGao Xiang 
14487865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1449cdba5506SGao Xiang 	if (io->sync) {
14507865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
145160b30050SHongyu Jin 			complete(&io->u.done);
14527865827cSGao Xiang 		return;
14537865827cSGao Xiang 	}
14547865827cSGao Xiang 
14557865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
14567865827cSGao Xiang 		return;
1457*3fffb589SSandeep Dhavale 	/* Use (kthread_)work and sync decompression for atomic contexts only */
14587865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
1459*3fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1460*3fffb589SSandeep Dhavale 		struct kthread_worker *worker;
1461*3fffb589SSandeep Dhavale 
1462*3fffb589SSandeep Dhavale 		rcu_read_lock();
1463*3fffb589SSandeep Dhavale 		worker = rcu_dereference(
1464*3fffb589SSandeep Dhavale 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
1465*3fffb589SSandeep Dhavale 		if (!worker) {
1466*3fffb589SSandeep Dhavale 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
14677865827cSGao Xiang 			queue_work(z_erofs_workqueue, &io->u.work);
1468*3fffb589SSandeep Dhavale 		} else {
1469*3fffb589SSandeep Dhavale 			kthread_queue_work(worker, &io->u.kthread_work);
1470*3fffb589SSandeep Dhavale 		}
1471*3fffb589SSandeep Dhavale 		rcu_read_unlock();
1472*3fffb589SSandeep Dhavale #else
1473*3fffb589SSandeep Dhavale 		queue_work(z_erofs_workqueue, &io->u.work);
1474*3fffb589SSandeep Dhavale #endif
14757865827cSGao Xiang 		/* enable sync decompression for readahead */
14767865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
14777865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
14787865827cSGao Xiang 		return;
14797865827cSGao Xiang 	}
14807865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
14817865827cSGao Xiang }
14827865827cSGao Xiang 
148347e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
148447e4937aSGao Xiang 					       unsigned int nr,
1485eaa9172aSGao Xiang 					       struct page **pagepool,
14869f2731d6SGao Xiang 					       struct address_space *mc)
148747e4937aSGao Xiang {
148847e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
14899f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
149047e4937aSGao Xiang 	bool tocache = false;
149147e4937aSGao Xiang 
149247e4937aSGao Xiang 	struct address_space *mapping;
149347e4937aSGao Xiang 	struct page *oldpage, *page;
149447e4937aSGao Xiang 	int justfound;
149547e4937aSGao Xiang 
149647e4937aSGao Xiang repeat:
1497ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
149847e4937aSGao Xiang 	oldpage = page;
149947e4937aSGao Xiang 
150047e4937aSGao Xiang 	if (!page)
150147e4937aSGao Xiang 		goto out_allocpage;
150247e4937aSGao Xiang 
1503b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1504b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
150547e4937aSGao Xiang 
15061825c8d7SGao Xiang 	/*
15071825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
15081825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
15091825c8d7SGao Xiang 	 */
15101825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1511ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
15121825c8d7SGao Xiang 		set_page_private(page, 0);
15131825c8d7SGao Xiang 		tocache = true;
15141825c8d7SGao Xiang 		goto out_tocache;
15151825c8d7SGao Xiang 	}
151647e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
151747e4937aSGao Xiang 
151847e4937aSGao Xiang 	/*
15196aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
152047e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
152147e4937aSGao Xiang 	 */
152247e4937aSGao Xiang 	if (mapping && mapping != mc)
152347e4937aSGao Xiang 		/* ought to be unmanaged pages */
152447e4937aSGao Xiang 		goto out;
152547e4937aSGao Xiang 
15266aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
15276aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
15286aaa7b06SGao Xiang 		goto out;
15296aaa7b06SGao Xiang 
153047e4937aSGao Xiang 	lock_page(page);
153147e4937aSGao Xiang 
153247e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
153347e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
153447e4937aSGao Xiang 
153547e4937aSGao Xiang 	/* the page is still in manage cache */
153647e4937aSGao Xiang 	if (page->mapping == mc) {
1537ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
153847e4937aSGao Xiang 
153947e4937aSGao Xiang 		if (!PagePrivate(page)) {
154047e4937aSGao Xiang 			/*
154147e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
154247e4937aSGao Xiang 			 * the current restriction as well if
1543ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
154447e4937aSGao Xiang 			 */
154547e4937aSGao Xiang 			DBG_BUGON(!justfound);
154647e4937aSGao Xiang 
154747e4937aSGao Xiang 			justfound = 0;
154847e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
154947e4937aSGao Xiang 			SetPagePrivate(page);
155047e4937aSGao Xiang 		}
155147e4937aSGao Xiang 
155247e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
155347e4937aSGao Xiang 		if (PageUptodate(page)) {
155447e4937aSGao Xiang 			unlock_page(page);
155547e4937aSGao Xiang 			page = NULL;
155647e4937aSGao Xiang 		}
155747e4937aSGao Xiang 		goto out;
155847e4937aSGao Xiang 	}
155947e4937aSGao Xiang 
156047e4937aSGao Xiang 	/*
156147e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
156247e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
156347e4937aSGao Xiang 	 */
156447e4937aSGao Xiang 	DBG_BUGON(page->mapping);
156547e4937aSGao Xiang 	DBG_BUGON(!justfound);
156647e4937aSGao Xiang 
156747e4937aSGao Xiang 	tocache = true;
156847e4937aSGao Xiang 	unlock_page(page);
156947e4937aSGao Xiang 	put_page(page);
157047e4937aSGao Xiang out_allocpage:
15715ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1572ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1573ed722fbcSGao Xiang 			       oldpage, page)) {
1574eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
15755ddcee1fSGao Xiang 		cond_resched();
15765ddcee1fSGao Xiang 		goto repeat;
15775ddcee1fSGao Xiang 	}
15781825c8d7SGao Xiang out_tocache:
1579bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1580bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1581bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1582bf225074SGao Xiang 		goto out;
1583a30573b3SGao Xiang 	}
1584bf225074SGao Xiang 	attach_page_private(page, pcl);
1585bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1586bf225074SGao Xiang 	put_page(page);
1587bf225074SGao Xiang 
158847e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
158947e4937aSGao Xiang 	return page;
159047e4937aSGao Xiang }
159147e4937aSGao Xiang 
1592cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1593a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
159447e4937aSGao Xiang {
1595a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
159647e4937aSGao Xiang 
1597a4b1fab1SGao Xiang 	if (fg && !*fg) {
1598a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1599a4b1fab1SGao Xiang 		if (!q) {
1600a4b1fab1SGao Xiang 			*fg = true;
1601a4b1fab1SGao Xiang 			goto fg_out;
160247e4937aSGao Xiang 		}
1603*3fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1604*3fffb589SSandeep Dhavale 		kthread_init_work(&q->u.kthread_work,
1605*3fffb589SSandeep Dhavale 				  z_erofs_decompressqueue_kthread_work);
1606*3fffb589SSandeep Dhavale #else
16070c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1608*3fffb589SSandeep Dhavale #endif
1609a4b1fab1SGao Xiang 	} else {
1610a4b1fab1SGao Xiang fg_out:
1611a4b1fab1SGao Xiang 		q = fgq;
161260b30050SHongyu Jin 		init_completion(&fgq->u.done);
1613a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
161467148551SGao Xiang 		q->eio = false;
1615cdba5506SGao Xiang 		q->sync = true;
1616a4b1fab1SGao Xiang 	}
1617a4b1fab1SGao Xiang 	q->sb = sb;
1618a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1619a4b1fab1SGao Xiang 	return q;
162047e4937aSGao Xiang }
162147e4937aSGao Xiang 
162247e4937aSGao Xiang /* define decompression jobqueue types */
162347e4937aSGao Xiang enum {
162447e4937aSGao Xiang 	JQ_BYPASS,
162547e4937aSGao Xiang 	JQ_SUBMIT,
162647e4937aSGao Xiang 	NR_JOBQUEUES,
162747e4937aSGao Xiang };
162847e4937aSGao Xiang 
162947e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
163047e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
163147e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
163247e4937aSGao Xiang {
163347e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
163447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
163547e4937aSGao Xiang 
163647e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
163747e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
163847e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
163947e4937aSGao Xiang 
164047e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
164147e4937aSGao Xiang 
164247e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
164347e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
164447e4937aSGao Xiang 
164547e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
164647e4937aSGao Xiang }
164747e4937aSGao Xiang 
16487865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
16497865827cSGao Xiang {
1650cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
16517865827cSGao Xiang 	blk_status_t err = bio->bi_status;
16527865827cSGao Xiang 	struct bio_vec *bvec;
16537865827cSGao Xiang 	struct bvec_iter_all iter_all;
16547865827cSGao Xiang 
16557865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
16567865827cSGao Xiang 		struct page *page = bvec->bv_page;
16577865827cSGao Xiang 
16587865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
16597865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
16607865827cSGao Xiang 
16617865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
16627865827cSGao Xiang 			if (!err)
16637865827cSGao Xiang 				SetPageUptodate(page);
16647865827cSGao Xiang 			unlock_page(page);
16657865827cSGao Xiang 		}
16667865827cSGao Xiang 	}
166767148551SGao Xiang 	if (err)
166867148551SGao Xiang 		q->eio = true;
1669cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
16707865827cSGao Xiang 	bio_put(bio);
16717865827cSGao Xiang }
16727865827cSGao Xiang 
167383a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1674eaa9172aSGao Xiang 				 struct page **pagepool,
1675a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1676a4b1fab1SGao Xiang 				 bool *force_fg)
167747e4937aSGao Xiang {
167883a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
167983a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
168047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1681a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
16825c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1683dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
16843f649ab7SKees Cook 	pgoff_t last_index;
1685dfeab2e9SGao Xiang 	struct block_device *last_bdev;
16861e4a2955SGao Xiang 	unsigned int nr_bios = 0;
16871e4a2955SGao Xiang 	struct bio *bio = NULL;
168882e60d00SJohannes Weiner 	unsigned long pflags;
168982e60d00SJohannes Weiner 	int memstall = 0;
169047e4937aSGao Xiang 
1691cdba5506SGao Xiang 	/*
1692cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1693cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1694cdba5506SGao Xiang 	 */
1695cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1696cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1697cdba5506SGao Xiang 
1698a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1699a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
170047e4937aSGao Xiang 
170147e4937aSGao Xiang 	/* by default, all need io submission */
170247e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
170347e4937aSGao Xiang 
170447e4937aSGao Xiang 	do {
1705dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
170647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
17071e4a2955SGao Xiang 		pgoff_t cur, end;
17081e4a2955SGao Xiang 		unsigned int i = 0;
17091e4a2955SGao Xiang 		bool bypass = true;
171047e4937aSGao Xiang 
171147e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
171247e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
171347e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
171447e4937aSGao Xiang 
171547e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
171647e4937aSGao Xiang 
1717cecf864dSYue Hu 		/* close the main owned chain at first */
1718cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1719cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1720cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1721cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1722cecf864dSYue Hu 			continue;
1723cecf864dSYue Hu 		}
1724cecf864dSYue Hu 
1725dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1726dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
1727dfeab2e9SGao Xiang 			.m_pa = blknr_to_addr(pcl->obj.index),
1728dfeab2e9SGao Xiang 		};
1729dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1730dfeab2e9SGao Xiang 
1731dfeab2e9SGao Xiang 		cur = erofs_blknr(mdev.m_pa);
17329f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
173347e4937aSGao Xiang 
17341e4a2955SGao Xiang 		do {
17351e4a2955SGao Xiang 			struct page *page;
173647e4937aSGao Xiang 
17371e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
173883a386c0SGao Xiang 							  mc);
17391e4a2955SGao Xiang 			if (!page)
17401e4a2955SGao Xiang 				continue;
174147e4937aSGao Xiang 
1742dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1743dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
174447e4937aSGao Xiang submit_bio_retry:
174594e4e153SGao Xiang 				submit_bio(bio);
174682e60d00SJohannes Weiner 				if (memstall) {
174782e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
174882e60d00SJohannes Weiner 					memstall = 0;
174982e60d00SJohannes Weiner 				}
175047e4937aSGao Xiang 				bio = NULL;
175147e4937aSGao Xiang 			}
175247e4937aSGao Xiang 
175382e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
175499486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
175582e60d00SJohannes Weiner 				memstall = 1;
175682e60d00SJohannes Weiner 			}
175799486c51SChristoph Hellwig 
175847e4937aSGao Xiang 			if (!bio) {
175907888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
176007888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
17610c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1762dfeab2e9SGao Xiang 
1763dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
17641e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
1765a5c0b780SGao Xiang 					LOG_SECTORS_PER_BLOCK;
1766cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
17676ea5aad3SGao Xiang 				if (f->readahead)
17686ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
176947e4937aSGao Xiang 				++nr_bios;
177047e4937aSGao Xiang 			}
177147e4937aSGao Xiang 
17726c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
177347e4937aSGao Xiang 				goto submit_bio_retry;
177447e4937aSGao Xiang 
17751e4a2955SGao Xiang 			last_index = cur;
17761e4a2955SGao Xiang 			bypass = false;
17771e4a2955SGao Xiang 		} while (++cur < end);
177847e4937aSGao Xiang 
17791e4a2955SGao Xiang 		if (!bypass)
178047e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
178147e4937aSGao Xiang 		else
178247e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
178347e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
178447e4937aSGao Xiang 
178599486c51SChristoph Hellwig 	if (bio) {
178694e4e153SGao Xiang 		submit_bio(bio);
178782e60d00SJohannes Weiner 		if (memstall)
178882e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
178999486c51SChristoph Hellwig 	}
179047e4937aSGao Xiang 
1791587a67b7SGao Xiang 	/*
1792587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
1793*3fffb589SSandeep Dhavale 	 * don't issue decompression but drop it directly instead.
1794587a67b7SGao Xiang 	 */
1795587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1796587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
17971e4a2955SGao Xiang 		return;
1798587a67b7SGao Xiang 	}
1799cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
180047e4937aSGao Xiang }
180147e4937aSGao Xiang 
180283a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1803eaa9172aSGao Xiang 			     struct page **pagepool, bool force_fg)
180447e4937aSGao Xiang {
1805a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
180647e4937aSGao Xiang 
18075c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
180847e4937aSGao Xiang 		return;
180983a386c0SGao Xiang 	z_erofs_submit_queue(f, pagepool, io, &force_fg);
181047e4937aSGao Xiang 
18110c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
18120c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
181347e4937aSGao Xiang 
181447e4937aSGao Xiang 	if (!force_fg)
181547e4937aSGao Xiang 		return;
181647e4937aSGao Xiang 
181747e4937aSGao Xiang 	/* wait until all bios are completed */
181860b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
181947e4937aSGao Xiang 
18200c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
18210c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
182247e4937aSGao Xiang }
182347e4937aSGao Xiang 
182438629291SGao Xiang /*
182538629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
182638629291SGao Xiang  * approximate readmore strategies as a start.
182738629291SGao Xiang  */
182838629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
182938629291SGao Xiang 				      struct readahead_control *rac,
183038629291SGao Xiang 				      erofs_off_t end,
1831eaa9172aSGao Xiang 				      struct page **pagepool,
183238629291SGao Xiang 				      bool backmost)
183338629291SGao Xiang {
183438629291SGao Xiang 	struct inode *inode = f->inode;
183538629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
183638629291SGao Xiang 	erofs_off_t cur;
183738629291SGao Xiang 	int err;
183838629291SGao Xiang 
183938629291SGao Xiang 	if (backmost) {
184038629291SGao Xiang 		map->m_la = end;
1841622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1842622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
184338629291SGao Xiang 		if (err)
184438629291SGao Xiang 			return;
184538629291SGao Xiang 
184638629291SGao Xiang 		/* expend ra for the trailing edge if readahead */
184738629291SGao Xiang 		if (rac) {
184838629291SGao Xiang 			loff_t newstart = readahead_pos(rac);
184938629291SGao Xiang 
185038629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
185138629291SGao Xiang 			readahead_expand(rac, newstart, cur - newstart);
185238629291SGao Xiang 			return;
185338629291SGao Xiang 		}
185438629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
185538629291SGao Xiang 	} else {
185638629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
185738629291SGao Xiang 
185838629291SGao Xiang 		if (!map->m_llen)
185938629291SGao Xiang 			return;
186038629291SGao Xiang 	}
186138629291SGao Xiang 
186238629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
186338629291SGao Xiang 	while (cur >= end) {
186438629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
186538629291SGao Xiang 		struct page *page;
186638629291SGao Xiang 
186738629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1868aa793b46SGao Xiang 		if (page) {
186938629291SGao Xiang 			if (PageUptodate(page)) {
187038629291SGao Xiang 				unlock_page(page);
1871aa793b46SGao Xiang 			} else {
187238629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
187338629291SGao Xiang 				if (err)
187438629291SGao Xiang 					erofs_err(inode->i_sb,
187538629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
187638629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1877aa793b46SGao Xiang 			}
187838629291SGao Xiang 			put_page(page);
1879aa793b46SGao Xiang 		}
1880aa793b46SGao Xiang 
188138629291SGao Xiang 		if (cur < PAGE_SIZE)
188238629291SGao Xiang 			break;
188338629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
188438629291SGao Xiang 	}
188538629291SGao Xiang }
188638629291SGao Xiang 
1887a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
188847e4937aSGao Xiang {
1889a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
189047e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
189140452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
189247e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1893eaa9172aSGao Xiang 	struct page *pagepool = NULL;
189447e4937aSGao Xiang 	int err;
189547e4937aSGao Xiang 
189647e4937aSGao Xiang 	trace_erofs_readpage(page, false);
189747e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
189847e4937aSGao Xiang 
189938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, f.headoffset + PAGE_SIZE - 1,
190038629291SGao Xiang 				  &pagepool, true);
19011825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
190238629291SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, 0, &pagepool, false);
190338629291SGao Xiang 
19045c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
190547e4937aSGao Xiang 
190647e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
190783a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
190840452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, 0));
190947e4937aSGao Xiang 
191047e4937aSGao Xiang 	if (err)
19114f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
191247e4937aSGao Xiang 
191309c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1914eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
191547e4937aSGao Xiang 	return err;
191647e4937aSGao Xiang }
191747e4937aSGao Xiang 
19180615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
191947e4937aSGao Xiang {
19200615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
192147e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
192247e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1923eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
192438629291SGao Xiang 	unsigned int nr_pages;
192547e4937aSGao Xiang 
19266ea5aad3SGao Xiang 	f.readahead = true;
19270615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
192847e4937aSGao Xiang 
192938629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, f.headoffset +
193038629291SGao Xiang 				  readahead_length(rac) - 1, &pagepool, true);
193138629291SGao Xiang 	nr_pages = readahead_count(rac);
193238629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
193338629291SGao Xiang 
19340615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
193547e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
193647e4937aSGao Xiang 		head = page;
193747e4937aSGao Xiang 	}
193847e4937aSGao Xiang 
193947e4937aSGao Xiang 	while (head) {
194047e4937aSGao Xiang 		struct page *page = head;
194147e4937aSGao Xiang 		int err;
194247e4937aSGao Xiang 
194347e4937aSGao Xiang 		/* traversal in reverse order */
194447e4937aSGao Xiang 		head = (void *)page_private(page);
194547e4937aSGao Xiang 
19461825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1947a5876e24SGao Xiang 		if (err)
19484f761fa2SGao Xiang 			erofs_err(inode->i_sb,
19494f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
19504f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
195147e4937aSGao Xiang 		put_page(page);
195247e4937aSGao Xiang 	}
195338629291SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, 0, &pagepool, false);
19545c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
195547e4937aSGao Xiang 
195683a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
195740452ffcSHuang Jianan 			 z_erofs_get_sync_decompress_policy(sbi, nr_pages));
195809c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1959eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
196047e4937aSGao Xiang }
196147e4937aSGao Xiang 
19620c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1963a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
19640615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
196547e4937aSGao Xiang };
1966