xref: /openbmc/linux/fs/erofs/zdata.c (revision 6ab5eed6)
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"
899486c51SChristoph Hellwig #include <linux/psi.h>
93fffb589SSandeep Dhavale #include <linux/cpuhotplug.h>
1047e4937aSGao Xiang #include <trace/events/erofs.h>
1147e4937aSGao Xiang 
12a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_MAX_PAGES	(Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13a9a94d93SGao Xiang #define Z_EROFS_INLINE_BVECS		2
14a9a94d93SGao Xiang 
15a9a94d93SGao Xiang /*
16a9a94d93SGao Xiang  * let's leave a type here in case of introducing
17a9a94d93SGao Xiang  * another tagged pointer later.
18a9a94d93SGao Xiang  */
19a9a94d93SGao Xiang typedef void *z_erofs_next_pcluster_t;
20a9a94d93SGao Xiang 
21a9a94d93SGao Xiang struct z_erofs_bvec {
22a9a94d93SGao Xiang 	struct page *page;
23a9a94d93SGao Xiang 	int offset;
24a9a94d93SGao Xiang 	unsigned int end;
25a9a94d93SGao Xiang };
26a9a94d93SGao Xiang 
27a9a94d93SGao Xiang #define __Z_EROFS_BVSET(name, total) \
28a9a94d93SGao Xiang struct name { \
29a9a94d93SGao Xiang 	/* point to the next page which contains the following bvecs */ \
30a9a94d93SGao Xiang 	struct page *nextpage; \
31a9a94d93SGao Xiang 	struct z_erofs_bvec bvec[total]; \
32a9a94d93SGao Xiang }
33a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset,);
34a9a94d93SGao Xiang __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
35a9a94d93SGao Xiang 
36a9a94d93SGao Xiang /*
37a9a94d93SGao Xiang  * Structure fields follow one of the following exclusion rules.
38a9a94d93SGao Xiang  *
39a9a94d93SGao Xiang  * I: Modifiable by initialization/destruction paths and read-only
40a9a94d93SGao Xiang  *    for everyone else;
41a9a94d93SGao Xiang  *
42a9a94d93SGao Xiang  * L: Field should be protected by the pcluster lock;
43a9a94d93SGao Xiang  *
44a9a94d93SGao Xiang  * A: Field should be accessed / updated in atomic for parallelized code.
45a9a94d93SGao Xiang  */
46a9a94d93SGao Xiang struct z_erofs_pcluster {
47a9a94d93SGao Xiang 	struct erofs_workgroup obj;
48a9a94d93SGao Xiang 	struct mutex lock;
49a9a94d93SGao Xiang 
50a9a94d93SGao Xiang 	/* A: point to next chained pcluster or TAILs */
51a9a94d93SGao Xiang 	z_erofs_next_pcluster_t next;
52a9a94d93SGao Xiang 
53a9a94d93SGao Xiang 	/* L: the maximum decompression size of this round */
54a9a94d93SGao Xiang 	unsigned int length;
55a9a94d93SGao Xiang 
56a9a94d93SGao Xiang 	/* L: total number of bvecs */
57a9a94d93SGao Xiang 	unsigned int vcnt;
58a9a94d93SGao Xiang 
59a9a94d93SGao Xiang 	/* I: page offset of start position of decompression */
60a9a94d93SGao Xiang 	unsigned short pageofs_out;
61a9a94d93SGao Xiang 
62a9a94d93SGao Xiang 	/* I: page offset of inline compressed data */
63a9a94d93SGao Xiang 	unsigned short pageofs_in;
64a9a94d93SGao Xiang 
65a9a94d93SGao Xiang 	union {
66a9a94d93SGao Xiang 		/* L: inline a certain number of bvec for bootstrap */
67a9a94d93SGao Xiang 		struct z_erofs_bvset_inline bvset;
68a9a94d93SGao Xiang 
69a9a94d93SGao Xiang 		/* I: can be used to free the pcluster by RCU. */
70a9a94d93SGao Xiang 		struct rcu_head rcu;
71a9a94d93SGao Xiang 	};
72a9a94d93SGao Xiang 
73a9a94d93SGao Xiang 	union {
74a9a94d93SGao Xiang 		/* I: physical cluster size in pages */
75a9a94d93SGao Xiang 		unsigned short pclusterpages;
76a9a94d93SGao Xiang 
77a9a94d93SGao Xiang 		/* I: tailpacking inline compressed size */
78a9a94d93SGao Xiang 		unsigned short tailpacking_size;
79a9a94d93SGao Xiang 	};
80a9a94d93SGao Xiang 
81a9a94d93SGao Xiang 	/* I: compression algorithm format */
82a9a94d93SGao Xiang 	unsigned char algorithmformat;
83a9a94d93SGao Xiang 
84a9a94d93SGao Xiang 	/* L: whether partial decompression or not */
85a9a94d93SGao Xiang 	bool partial;
86a9a94d93SGao Xiang 
87a9a94d93SGao Xiang 	/* L: indicate several pageofs_outs or not */
88a9a94d93SGao Xiang 	bool multibases;
89a9a94d93SGao Xiang 
90a9a94d93SGao Xiang 	/* A: compressed bvecs (can be cached or inplaced pages) */
91a9a94d93SGao Xiang 	struct z_erofs_bvec compressed_bvecs[];
92a9a94d93SGao Xiang };
93a9a94d93SGao Xiang 
94a9a94d93SGao Xiang /* let's avoid the valid 32-bit kernel addresses */
95a9a94d93SGao Xiang 
96a9a94d93SGao Xiang /* the chained workgroup has't submitted io (still open) */
97a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL           ((void *)0x5F0ECAFE)
98a9a94d93SGao Xiang /* the chained workgroup has already submitted io */
99a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_TAIL_CLOSED    ((void *)0x5F0EDEAD)
100a9a94d93SGao Xiang 
101a9a94d93SGao Xiang #define Z_EROFS_PCLUSTER_NIL            (NULL)
102a9a94d93SGao Xiang 
103a9a94d93SGao Xiang struct z_erofs_decompressqueue {
104a9a94d93SGao Xiang 	struct super_block *sb;
105a9a94d93SGao Xiang 	atomic_t pending_bios;
106a9a94d93SGao Xiang 	z_erofs_next_pcluster_t head;
107a9a94d93SGao Xiang 
108a9a94d93SGao Xiang 	union {
109a9a94d93SGao Xiang 		struct completion done;
110a9a94d93SGao Xiang 		struct work_struct work;
1113fffb589SSandeep Dhavale 		struct kthread_work kthread_work;
112a9a94d93SGao Xiang 	} u;
113a9a94d93SGao Xiang 	bool eio, sync;
114a9a94d93SGao Xiang };
115a9a94d93SGao Xiang 
116a9a94d93SGao Xiang static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
117a9a94d93SGao Xiang {
118a9a94d93SGao Xiang 	return !pcl->obj.index;
119a9a94d93SGao Xiang }
120a9a94d93SGao Xiang 
121a9a94d93SGao Xiang static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
122a9a94d93SGao Xiang {
123a9a94d93SGao Xiang 	if (z_erofs_is_inline_pcluster(pcl))
124a9a94d93SGao Xiang 		return 1;
125a9a94d93SGao Xiang 	return pcl->pclusterpages;
126a9a94d93SGao Xiang }
127a9a94d93SGao Xiang 
128a9a94d93SGao Xiang /*
129a9a94d93SGao Xiang  * bit 30: I/O error occurred on this page
130a9a94d93SGao Xiang  * bit 0 - 29: remaining parts to complete this page
131a9a94d93SGao Xiang  */
132a9a94d93SGao Xiang #define Z_EROFS_PAGE_EIO			(1 << 30)
133a9a94d93SGao Xiang 
134a9a94d93SGao Xiang static inline void z_erofs_onlinepage_init(struct page *page)
135a9a94d93SGao Xiang {
136a9a94d93SGao Xiang 	union {
137a9a94d93SGao Xiang 		atomic_t o;
138a9a94d93SGao Xiang 		unsigned long v;
139a9a94d93SGao Xiang 	} u = { .o = ATOMIC_INIT(1) };
140a9a94d93SGao Xiang 
141a9a94d93SGao Xiang 	set_page_private(page, u.v);
142a9a94d93SGao Xiang 	smp_wmb();
143a9a94d93SGao Xiang 	SetPagePrivate(page);
144a9a94d93SGao Xiang }
145a9a94d93SGao Xiang 
146a9a94d93SGao Xiang static inline void z_erofs_onlinepage_split(struct page *page)
147a9a94d93SGao Xiang {
148a9a94d93SGao Xiang 	atomic_inc((atomic_t *)&page->private);
149a9a94d93SGao Xiang }
150a9a94d93SGao Xiang 
151a9a94d93SGao Xiang static inline void z_erofs_page_mark_eio(struct page *page)
152a9a94d93SGao Xiang {
153a9a94d93SGao Xiang 	int orig;
154a9a94d93SGao Xiang 
155a9a94d93SGao Xiang 	do {
156a9a94d93SGao Xiang 		orig = atomic_read((atomic_t *)&page->private);
157a9a94d93SGao Xiang 	} while (atomic_cmpxchg((atomic_t *)&page->private, orig,
158a9a94d93SGao Xiang 				orig | Z_EROFS_PAGE_EIO) != orig);
159a9a94d93SGao Xiang }
160a9a94d93SGao Xiang 
161a9a94d93SGao Xiang static inline void z_erofs_onlinepage_endio(struct page *page)
162a9a94d93SGao Xiang {
163a9a94d93SGao Xiang 	unsigned int v;
164a9a94d93SGao Xiang 
165a9a94d93SGao Xiang 	DBG_BUGON(!PagePrivate(page));
166a9a94d93SGao Xiang 	v = atomic_dec_return((atomic_t *)&page->private);
167a9a94d93SGao Xiang 	if (!(v & ~Z_EROFS_PAGE_EIO)) {
168a9a94d93SGao Xiang 		set_page_private(page, 0);
169a9a94d93SGao Xiang 		ClearPagePrivate(page);
170a9a94d93SGao Xiang 		if (!(v & Z_EROFS_PAGE_EIO))
171a9a94d93SGao Xiang 			SetPageUptodate(page);
172a9a94d93SGao Xiang 		unlock_page(page);
173a9a94d93SGao Xiang 	}
174a9a94d93SGao Xiang }
175a9a94d93SGao Xiang 
176a9a94d93SGao Xiang #define Z_EROFS_ONSTACK_PAGES		32
177a9a94d93SGao Xiang 
17847e4937aSGao Xiang /*
1799f6cc76eSGao Xiang  * since pclustersize is variable for big pcluster feature, introduce slab
1809f6cc76eSGao Xiang  * pools implementation for different pcluster sizes.
1819f6cc76eSGao Xiang  */
1829f6cc76eSGao Xiang struct z_erofs_pcluster_slab {
1839f6cc76eSGao Xiang 	struct kmem_cache *slab;
1849f6cc76eSGao Xiang 	unsigned int maxpages;
1859f6cc76eSGao Xiang 	char name[48];
1869f6cc76eSGao Xiang };
1879f6cc76eSGao Xiang 
1889f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n }
1899f6cc76eSGao Xiang 
1909f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
1919f6cc76eSGao Xiang 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
1929f6cc76eSGao Xiang 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
1939f6cc76eSGao Xiang };
1949f6cc76eSGao Xiang 
19506a304cdSGao Xiang struct z_erofs_bvec_iter {
19606a304cdSGao Xiang 	struct page *bvpage;
19706a304cdSGao Xiang 	struct z_erofs_bvset *bvset;
19806a304cdSGao Xiang 	unsigned int nr, cur;
19906a304cdSGao Xiang };
20006a304cdSGao Xiang 
20106a304cdSGao Xiang static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
20206a304cdSGao Xiang {
20306a304cdSGao Xiang 	if (iter->bvpage)
20406a304cdSGao Xiang 		kunmap_local(iter->bvset);
20506a304cdSGao Xiang 	return iter->bvpage;
20606a304cdSGao Xiang }
20706a304cdSGao Xiang 
20806a304cdSGao Xiang static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
20906a304cdSGao Xiang {
21006a304cdSGao Xiang 	unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
21106a304cdSGao Xiang 	/* have to access nextpage in advance, otherwise it will be unmapped */
21206a304cdSGao Xiang 	struct page *nextpage = iter->bvset->nextpage;
21306a304cdSGao Xiang 	struct page *oldpage;
21406a304cdSGao Xiang 
21506a304cdSGao Xiang 	DBG_BUGON(!nextpage);
21606a304cdSGao Xiang 	oldpage = z_erofs_bvec_iter_end(iter);
21706a304cdSGao Xiang 	iter->bvpage = nextpage;
21806a304cdSGao Xiang 	iter->bvset = kmap_local_page(nextpage);
21906a304cdSGao Xiang 	iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
22006a304cdSGao Xiang 	iter->cur = 0;
22106a304cdSGao Xiang 	return oldpage;
22206a304cdSGao Xiang }
22306a304cdSGao Xiang 
22406a304cdSGao Xiang static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
22506a304cdSGao Xiang 				    struct z_erofs_bvset_inline *bvset,
22606a304cdSGao Xiang 				    unsigned int bootstrap_nr,
22706a304cdSGao Xiang 				    unsigned int cur)
22806a304cdSGao Xiang {
22906a304cdSGao Xiang 	*iter = (struct z_erofs_bvec_iter) {
23006a304cdSGao Xiang 		.nr = bootstrap_nr,
23106a304cdSGao Xiang 		.bvset = (struct z_erofs_bvset *)bvset,
23206a304cdSGao Xiang 	};
23306a304cdSGao Xiang 
23406a304cdSGao Xiang 	while (cur > iter->nr) {
23506a304cdSGao Xiang 		cur -= iter->nr;
23606a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
23706a304cdSGao Xiang 	}
23806a304cdSGao Xiang 	iter->cur = cur;
23906a304cdSGao Xiang }
24006a304cdSGao Xiang 
24106a304cdSGao Xiang static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
24206a304cdSGao Xiang 				struct z_erofs_bvec *bvec,
243*6ab5eed6SGao Xiang 				struct page **candidate_bvpage,
244*6ab5eed6SGao Xiang 				struct page **pagepool)
24506a304cdSGao Xiang {
24605b63d2bSGao Xiang 	if (iter->cur >= iter->nr) {
24705b63d2bSGao Xiang 		struct page *nextpage = *candidate_bvpage;
24806a304cdSGao Xiang 
24905b63d2bSGao Xiang 		if (!nextpage) {
250*6ab5eed6SGao Xiang 			nextpage = erofs_allocpage(pagepool, GFP_NOFS);
25105b63d2bSGao Xiang 			if (!nextpage)
25205b63d2bSGao Xiang 				return -ENOMEM;
25305b63d2bSGao Xiang 			set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
25405b63d2bSGao Xiang 		}
25506a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
25605b63d2bSGao Xiang 		iter->bvset->nextpage = nextpage;
25706a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
25806a304cdSGao Xiang 
25906a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
26006a304cdSGao Xiang 		*candidate_bvpage = NULL;
26106a304cdSGao Xiang 	}
26206a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
26306a304cdSGao Xiang 	return 0;
26406a304cdSGao Xiang }
26506a304cdSGao Xiang 
26606a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
26706a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
26806a304cdSGao Xiang 				 struct page **old_bvpage)
26906a304cdSGao Xiang {
27006a304cdSGao Xiang 	if (iter->cur == iter->nr)
27106a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
27206a304cdSGao Xiang 	else
27306a304cdSGao Xiang 		*old_bvpage = NULL;
27406a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
27506a304cdSGao Xiang }
27606a304cdSGao Xiang 
2779f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2789f6cc76eSGao Xiang {
2799f6cc76eSGao Xiang 	int i;
2809f6cc76eSGao Xiang 
2819f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2829f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2839f6cc76eSGao Xiang 			continue;
2849f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2859f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2869f6cc76eSGao Xiang 	}
2879f6cc76eSGao Xiang }
2889f6cc76eSGao Xiang 
2899f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2909f6cc76eSGao Xiang {
2919f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2929f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2939f6cc76eSGao Xiang 	unsigned int size;
2949f6cc76eSGao Xiang 
2959f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2969f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
297ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2989f6cc76eSGao Xiang 
2999f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
3009f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
3019f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
3029f6cc76eSGao Xiang 		if (pcs->slab)
3039f6cc76eSGao Xiang 			continue;
3049f6cc76eSGao Xiang 
3059f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3069f6cc76eSGao Xiang 		return -ENOMEM;
3079f6cc76eSGao Xiang 	}
3089f6cc76eSGao Xiang 	return 0;
3099f6cc76eSGao Xiang }
3109f6cc76eSGao Xiang 
3119f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3129f6cc76eSGao Xiang {
3139f6cc76eSGao Xiang 	int i;
3149f6cc76eSGao Xiang 
3159f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3169f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3179f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3189f6cc76eSGao Xiang 
3199f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3209f6cc76eSGao Xiang 			continue;
3219f6cc76eSGao Xiang 
3229f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3239f6cc76eSGao Xiang 		if (!pcl)
3249f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3259f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3269f6cc76eSGao Xiang 		return pcl;
3279f6cc76eSGao Xiang 	}
3289f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3299f6cc76eSGao Xiang }
3309f6cc76eSGao Xiang 
3319f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3329f6cc76eSGao Xiang {
333cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3349f6cc76eSGao Xiang 	int i;
3359f6cc76eSGao Xiang 
3369f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3379f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3389f6cc76eSGao Xiang 
339cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3409f6cc76eSGao Xiang 			continue;
3419f6cc76eSGao Xiang 
3429f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3439f6cc76eSGao Xiang 		return;
3449f6cc76eSGao Xiang 	}
3459f6cc76eSGao Xiang 	DBG_BUGON(1);
3469f6cc76eSGao Xiang }
3479f6cc76eSGao Xiang 
34847e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
34947e4937aSGao Xiang 
3503fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
3513fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers;
3523fffb589SSandeep Dhavale 
3533fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void)
35447e4937aSGao Xiang {
3553fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3563fffb589SSandeep Dhavale 	unsigned int cpu;
3573fffb589SSandeep Dhavale 
3583fffb589SSandeep Dhavale 	for_each_possible_cpu(cpu) {
3593fffb589SSandeep Dhavale 		worker = rcu_dereference_protected(
3603fffb589SSandeep Dhavale 					z_erofs_pcpu_workers[cpu], 1);
3613fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
3623fffb589SSandeep Dhavale 		if (worker)
3633fffb589SSandeep Dhavale 			kthread_destroy_worker(worker);
3643fffb589SSandeep Dhavale 	}
3653fffb589SSandeep Dhavale 	kfree(z_erofs_pcpu_workers);
36647e4937aSGao Xiang }
36747e4937aSGao Xiang 
3683fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu)
36947e4937aSGao Xiang {
3703fffb589SSandeep Dhavale 	struct kthread_worker *worker =
3713fffb589SSandeep Dhavale 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
37247e4937aSGao Xiang 
3733fffb589SSandeep Dhavale 	if (IS_ERR(worker))
3743fffb589SSandeep Dhavale 		return worker;
3753fffb589SSandeep Dhavale 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
3763fffb589SSandeep Dhavale 		sched_set_fifo_low(worker->task);
3773fffb589SSandeep Dhavale 	return worker;
3783fffb589SSandeep Dhavale }
3793fffb589SSandeep Dhavale 
3803fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void)
3813fffb589SSandeep Dhavale {
3823fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3833fffb589SSandeep Dhavale 	unsigned int cpu;
3843fffb589SSandeep Dhavale 
3853fffb589SSandeep Dhavale 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
3863fffb589SSandeep Dhavale 			sizeof(struct kthread_worker *), GFP_ATOMIC);
3873fffb589SSandeep Dhavale 	if (!z_erofs_pcpu_workers)
3883fffb589SSandeep Dhavale 		return -ENOMEM;
3893fffb589SSandeep Dhavale 
3903fffb589SSandeep Dhavale 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
3913fffb589SSandeep Dhavale 		worker = erofs_init_percpu_worker(cpu);
3923fffb589SSandeep Dhavale 		if (!IS_ERR(worker))
3933fffb589SSandeep Dhavale 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
3943fffb589SSandeep Dhavale 	}
3953fffb589SSandeep Dhavale 	return 0;
3963fffb589SSandeep Dhavale }
3973fffb589SSandeep Dhavale #else
3983fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {}
3993fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; }
4003fffb589SSandeep Dhavale #endif
4013fffb589SSandeep Dhavale 
4023fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
4033fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
4043fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state;
4053fffb589SSandeep Dhavale 
4063fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu)
4073fffb589SSandeep Dhavale {
4083fffb589SSandeep Dhavale 	struct kthread_worker *worker, *old;
4093fffb589SSandeep Dhavale 
4103fffb589SSandeep Dhavale 	worker = erofs_init_percpu_worker(cpu);
4113fffb589SSandeep Dhavale 	if (IS_ERR(worker))
4123fffb589SSandeep Dhavale 		return PTR_ERR(worker);
4133fffb589SSandeep Dhavale 
4143fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4153fffb589SSandeep Dhavale 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4163fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4173fffb589SSandeep Dhavale 	if (!old)
4183fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
4193fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4203fffb589SSandeep Dhavale 	if (old)
4213fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4223fffb589SSandeep Dhavale 	return 0;
4233fffb589SSandeep Dhavale }
4243fffb589SSandeep Dhavale 
4253fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu)
4263fffb589SSandeep Dhavale {
4273fffb589SSandeep Dhavale 	struct kthread_worker *worker;
4283fffb589SSandeep Dhavale 
4293fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4303fffb589SSandeep Dhavale 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4313fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4323fffb589SSandeep Dhavale 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
4333fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4343fffb589SSandeep Dhavale 
4353fffb589SSandeep Dhavale 	synchronize_rcu();
4363fffb589SSandeep Dhavale 	if (worker)
4373fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4383fffb589SSandeep Dhavale 	return 0;
4393fffb589SSandeep Dhavale }
4403fffb589SSandeep Dhavale 
4413fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void)
4423fffb589SSandeep Dhavale {
4433fffb589SSandeep Dhavale 	int state;
4443fffb589SSandeep Dhavale 
4453fffb589SSandeep Dhavale 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
4463fffb589SSandeep Dhavale 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
4473fffb589SSandeep Dhavale 	if (state < 0)
4483fffb589SSandeep Dhavale 		return state;
4493fffb589SSandeep Dhavale 
4503fffb589SSandeep Dhavale 	erofs_cpuhp_state = state;
4513fffb589SSandeep Dhavale 	return 0;
4523fffb589SSandeep Dhavale }
4533fffb589SSandeep Dhavale 
4543fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void)
4553fffb589SSandeep Dhavale {
4563fffb589SSandeep Dhavale 	if (erofs_cpuhp_state)
4573fffb589SSandeep Dhavale 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
4583fffb589SSandeep Dhavale }
4593fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
4603fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; }
4613fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {}
4623fffb589SSandeep Dhavale #endif
4633fffb589SSandeep Dhavale 
4643fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void)
4653fffb589SSandeep Dhavale {
4663fffb589SSandeep Dhavale 	erofs_cpu_hotplug_destroy();
4673fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4683fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4693fffb589SSandeep Dhavale 	z_erofs_destroy_pcluster_pool();
47047e4937aSGao Xiang }
47147e4937aSGao Xiang 
47247e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
47347e4937aSGao Xiang {
4749f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
47547e4937aSGao Xiang 
4769f6cc76eSGao Xiang 	if (err)
4773fffb589SSandeep Dhavale 		goto out_error_pcluster_pool;
4783fffb589SSandeep Dhavale 
4793fffb589SSandeep Dhavale 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
4803fffb589SSandeep Dhavale 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
4818d1b80a7SDan Carpenter 	if (!z_erofs_workqueue) {
4828d1b80a7SDan Carpenter 		err = -ENOMEM;
4833fffb589SSandeep Dhavale 		goto out_error_workqueue_init;
4848d1b80a7SDan Carpenter 	}
4853fffb589SSandeep Dhavale 
4863fffb589SSandeep Dhavale 	err = erofs_init_percpu_workers();
4879f6cc76eSGao Xiang 	if (err)
4883fffb589SSandeep Dhavale 		goto out_error_pcpu_worker;
4893fffb589SSandeep Dhavale 
4903fffb589SSandeep Dhavale 	err = erofs_cpu_hotplug_init();
4913fffb589SSandeep Dhavale 	if (err < 0)
4923fffb589SSandeep Dhavale 		goto out_error_cpuhp_init;
4933fffb589SSandeep Dhavale 	return err;
4943fffb589SSandeep Dhavale 
4953fffb589SSandeep Dhavale out_error_cpuhp_init:
4963fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4973fffb589SSandeep Dhavale out_error_pcpu_worker:
4983fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4993fffb589SSandeep Dhavale out_error_workqueue_init:
5009f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
5013fffb589SSandeep Dhavale out_error_pcluster_pool:
5029f6cc76eSGao Xiang 	return err;
50347e4937aSGao Xiang }
50447e4937aSGao Xiang 
505db166fc2SGao Xiang enum z_erofs_pclustermode {
506db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
50747e4937aSGao Xiang 	/*
508db166fc2SGao Xiang 	 * The current pclusters was the tail of an exist chain, in addition
509db166fc2SGao Xiang 	 * that the previous processed chained pclusters are all decided to
51047e4937aSGao Xiang 	 * be hooked up to it.
511db166fc2SGao Xiang 	 * A new chain will be created for the remaining pclusters which are
512db166fc2SGao Xiang 	 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
513db166fc2SGao Xiang 	 * the next pcluster cannot reuse the whole page safely for inplace I/O
514db166fc2SGao Xiang 	 * in the following scenario:
51547e4937aSGao Xiang 	 *  ________________________________________________________________
51647e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
517db166fc2SGao Xiang 	 * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
518db166fc2SGao Xiang 	 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
51947e4937aSGao Xiang 	 */
520db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_HOOKED,
5210b964600SGao Xiang 	/*
522db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
5230b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
5240b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
525387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
5260b964600SGao Xiang 	 */
527db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
52847e4937aSGao Xiang 	/*
52947e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
53047e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
53147e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
53247e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
53347e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
53447e4937aSGao Xiang 	 * illustrated below:
53547e4937aSGao Xiang 	 *  ________________________________________________________________
53647e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
53747e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
538db166fc2SGao Xiang 	 * | PCLUSTER_FOLLOWED or |                                        |
539db166fc2SGao Xiang 	 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
54047e4937aSGao Xiang 	 *
54147e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
54247e4937aSGao Xiang 	 */
543db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
54447e4937aSGao Xiang };
54547e4937aSGao Xiang 
5465c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
5475c6dcc57SGao Xiang 	struct inode *const inode;
5485c6dcc57SGao Xiang 	struct erofs_map_blocks map;
54906a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
55047e4937aSGao Xiang 
551*6ab5eed6SGao Xiang 	struct page *pagepool;
55206a304cdSGao Xiang 	struct page *candidate_bvpage;
55347e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
55447e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
555db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
55647e4937aSGao Xiang 
55747e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
55847e4937aSGao Xiang 	bool backmost;
55947e4937aSGao Xiang 	erofs_off_t headoffset;
560ed722fbcSGao Xiang 
561ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
562ed722fbcSGao Xiang 	unsigned int icur;
56347e4937aSGao Xiang };
56447e4937aSGao Xiang 
56547e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
5665c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
567db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
56847e4937aSGao Xiang 
5691282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
5701282dea3SGao Xiang {
5711282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
5721282dea3SGao Xiang 
5731282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
5741282dea3SGao Xiang 		return false;
5751282dea3SGao Xiang 
5761282dea3SGao Xiang 	if (fe->backmost)
5771282dea3SGao Xiang 		return true;
5781282dea3SGao Xiang 
5791282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
5801282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
5811282dea3SGao Xiang 		return true;
5821282dea3SGao Xiang 
5831282dea3SGao Xiang 	return false;
5841282dea3SGao Xiang }
5851282dea3SGao Xiang 
586*6ab5eed6SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe)
58747e4937aSGao Xiang {
5886f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
5895c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
5901282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
59147e4937aSGao Xiang 	bool standalone = true;
5926f39d1e1SGao Xiang 	/*
5936f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
5946f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
5956f39d1e1SGao Xiang 	 */
5961825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
5971825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
598ed722fbcSGao Xiang 	unsigned int i;
59947e4937aSGao Xiang 
600db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
60147e4937aSGao Xiang 		return;
60247e4937aSGao Xiang 
603ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
60447e4937aSGao Xiang 		struct page *page;
605b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
6061825c8d7SGao Xiang 		struct page *newpage = NULL;
60747e4937aSGao Xiang 
60847e4937aSGao Xiang 		/* the compressed page was loaded before */
609ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
61047e4937aSGao Xiang 			continue;
61147e4937aSGao Xiang 
612ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
61347e4937aSGao Xiang 
61447e4937aSGao Xiang 		if (page) {
615b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
6160b964600SGao Xiang 		} else {
6170b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
6180b964600SGao Xiang 			standalone = false;
6191282dea3SGao Xiang 			if (!shouldalloc)
6201282dea3SGao Xiang 				continue;
6211282dea3SGao Xiang 
6221282dea3SGao Xiang 			/*
6231282dea3SGao Xiang 			 * try to use cached I/O if page allocation
6241282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
6251282dea3SGao Xiang 			 * to avoid any direct reclaim.
6261282dea3SGao Xiang 			 */
627*6ab5eed6SGao Xiang 			newpage = erofs_allocpage(&fe->pagepool, gfp);
6281825c8d7SGao Xiang 			if (!newpage)
62947e4937aSGao Xiang 				continue;
6301282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
631b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
63247e4937aSGao Xiang 		}
63347e4937aSGao Xiang 
634b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
63547e4937aSGao Xiang 			continue;
63647e4937aSGao Xiang 
637eaa9172aSGao Xiang 		if (page)
63847e4937aSGao Xiang 			put_page(page);
639eaa9172aSGao Xiang 		else if (newpage)
640*6ab5eed6SGao Xiang 			erofs_pagepool_add(&fe->pagepool, newpage);
64147e4937aSGao Xiang 	}
64247e4937aSGao Xiang 
6430b964600SGao Xiang 	/*
6440b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
6450b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
6460b964600SGao Xiang 	 */
6470b964600SGao Xiang 	if (standalone)
648db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
64947e4937aSGao Xiang }
65047e4937aSGao Xiang 
65147e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
65247e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
65347e4937aSGao Xiang 				       struct erofs_workgroup *grp)
65447e4937aSGao Xiang {
65547e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
65647e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
65747e4937aSGao Xiang 	int i;
65847e4937aSGao Xiang 
659cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
66047e4937aSGao Xiang 	/*
66147e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
66247e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
66347e4937aSGao Xiang 	 */
6649f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
665ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
66647e4937aSGao Xiang 
66747e4937aSGao Xiang 		if (!page)
66847e4937aSGao Xiang 			continue;
66947e4937aSGao Xiang 
67047e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
67147e4937aSGao Xiang 		if (!trylock_page(page))
67247e4937aSGao Xiang 			return -EBUSY;
67347e4937aSGao Xiang 
674f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
67547e4937aSGao Xiang 			continue;
67647e4937aSGao Xiang 
67747e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
678ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6796aaa7b06SGao Xiang 		detach_page_private(page);
68047e4937aSGao Xiang 		unlock_page(page);
68147e4937aSGao Xiang 	}
68247e4937aSGao Xiang 	return 0;
68347e4937aSGao Xiang }
68447e4937aSGao Xiang 
685d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
68647e4937aSGao Xiang {
68747e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
688ed722fbcSGao Xiang 	int ret, i;
68947e4937aSGao Xiang 
690ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
691ed722fbcSGao Xiang 		return 0;
69247e4937aSGao Xiang 
693ed722fbcSGao Xiang 	ret = 0;
694cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
6959f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
696ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
697ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
69847e4937aSGao Xiang 			ret = 1;
69947e4937aSGao Xiang 			break;
70047e4937aSGao Xiang 		}
70147e4937aSGao Xiang 	}
70247e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
7036aaa7b06SGao Xiang 	if (ret)
7046aaa7b06SGao Xiang 		detach_page_private(page);
70547e4937aSGao Xiang 	return ret;
70647e4937aSGao Xiang }
70747e4937aSGao Xiang 
7085c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
709ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
71047e4937aSGao Xiang {
7115c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
71247e4937aSGao Xiang 
713ed722fbcSGao Xiang 	while (fe->icur > 0) {
714ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
715ed722fbcSGao Xiang 			     NULL, bvec->page)) {
716ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
71747e4937aSGao Xiang 			return true;
718ed722fbcSGao Xiang 		}
719ed722fbcSGao Xiang 	}
72047e4937aSGao Xiang 	return false;
72147e4937aSGao Xiang }
72247e4937aSGao Xiang 
72387ca34a7SGao Xiang /* callers must be with pcluster lock held */
7245c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
7255b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
72647e4937aSGao Xiang {
72747e4937aSGao Xiang 	int ret;
72847e4937aSGao Xiang 
729db166fc2SGao Xiang 	if (exclusive) {
73006a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
731ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
73247e4937aSGao Xiang 			return 0;
73306a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
734db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
73506a304cdSGao Xiang 		    !fe->candidate_bvpage)
73606a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
73706a304cdSGao Xiang 	}
738*6ab5eed6SGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
739*6ab5eed6SGao Xiang 				   &fe->pagepool);
74006a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
74106a304cdSGao Xiang 	return ret;
74247e4937aSGao Xiang }
74347e4937aSGao Xiang 
7445c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
74547e4937aSGao Xiang {
7465c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
7475c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
74847e4937aSGao Xiang 
749473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
750473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
751473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
75247e4937aSGao Xiang 		*owned_head = &pcl->next;
753473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
754db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
755473e15b0SGao Xiang 		return;
756473e15b0SGao Xiang 	}
757473e15b0SGao Xiang 
75847e4937aSGao Xiang 	/*
759473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
760473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
76147e4937aSGao Xiang 	 */
762267f2492SGao Xiang 	if (*owned_head != &pcl->next && pcl != f->tailpcl &&
763267f2492SGao Xiang 	    cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
764473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
76547e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
766db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_HOOKED;
7675c6dcc57SGao Xiang 		f->tailpcl = NULL;
768473e15b0SGao Xiang 		return;
76947e4937aSGao Xiang 	}
770473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
771db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
77247e4937aSGao Xiang }
77347e4937aSGao Xiang 
77483a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
77547e4937aSGao Xiang {
77683a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
777cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
77847e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
77964094a04SGao Xiang 	struct erofs_workgroup *grp;
78047e4937aSGao Xiang 	int err;
78147e4937aSGao Xiang 
782c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
783c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
7848f899262SGao Xiang 		DBG_BUGON(1);
7858f899262SGao Xiang 		return -EFSCORRUPTED;
7868f899262SGao Xiang 	}
7878f899262SGao Xiang 
7889f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
789cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
790cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
7919f6cc76eSGao Xiang 	if (IS_ERR(pcl))
7929f6cc76eSGao Xiang 		return PTR_ERR(pcl);
79347e4937aSGao Xiang 
79464094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
7958f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
7962bfab9c0SGao Xiang 	pcl->length = 0;
7972bfab9c0SGao Xiang 	pcl->partial = true;
79847e4937aSGao Xiang 
79947e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
8005c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
80187ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
802db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
80347e4937aSGao Xiang 
80447e4937aSGao Xiang 	/*
80547e4937aSGao Xiang 	 * lock all primary followed works before visible to others
80647e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
80747e4937aSGao Xiang 	 */
80887ca34a7SGao Xiang 	mutex_init(&pcl->lock);
80987ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
81047e4937aSGao Xiang 
811cecf864dSYue Hu 	if (ztailpacking) {
812cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
8133acea5fcSJingbo Xu 		pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
814cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
815cecf864dSYue Hu 	} else {
816cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
817cecf864dSYue Hu 
81883a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
81964094a04SGao Xiang 		if (IS_ERR(grp)) {
82064094a04SGao Xiang 			err = PTR_ERR(grp);
82164094a04SGao Xiang 			goto err_out;
82264094a04SGao Xiang 		}
82364094a04SGao Xiang 
82464094a04SGao Xiang 		if (grp != &pcl->obj) {
8255c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
826cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
82764094a04SGao Xiang 			err = -EEXIST;
82864094a04SGao Xiang 			goto err_out;
82947e4937aSGao Xiang 		}
830cecf864dSYue Hu 	}
83147e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
8325c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
8335c6dcc57SGao Xiang 		fe->tailpcl = pcl;
8345c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
8355c6dcc57SGao Xiang 	fe->pcl = pcl;
8369e579fc1SGao Xiang 	return 0;
83764094a04SGao Xiang 
83864094a04SGao Xiang err_out:
83987ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
8409f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
84164094a04SGao Xiang 	return err;
84247e4937aSGao Xiang }
84347e4937aSGao Xiang 
84483a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
84547e4937aSGao Xiang {
84683a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
8470d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
8489e579fc1SGao Xiang 	int ret;
84947e4937aSGao Xiang 
85087ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
85147e4937aSGao Xiang 
85287ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
8535c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
8545c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
85547e4937aSGao Xiang 
8560d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
8570d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
8580d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
8590d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
86047e4937aSGao Xiang 		DBG_BUGON(1);
861cecf864dSYue Hu 		return -EFSCORRUPTED;
862cecf864dSYue Hu 	}
86347e4937aSGao Xiang 
86464094a04SGao Xiang 	if (grp) {
8655c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
8660d823b42SGao Xiang 		ret = -EEXIST;
86764094a04SGao Xiang 	} else {
86883a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
86964094a04SGao Xiang 	}
87047e4937aSGao Xiang 
8710d823b42SGao Xiang 	if (ret == -EEXIST) {
872267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
873267f2492SGao Xiang 		/* used to check tail merging loop due to corrupted images */
874267f2492SGao Xiang 		if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
875267f2492SGao Xiang 			fe->tailpcl = fe->pcl;
876267f2492SGao Xiang 
877267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
8780d823b42SGao Xiang 	} else if (ret) {
8790d823b42SGao Xiang 		return ret;
8800d823b42SGao Xiang 	}
88106a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
882387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
88381382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
884ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
88547e4937aSGao Xiang 	return 0;
88647e4937aSGao Xiang }
88747e4937aSGao Xiang 
88847e4937aSGao Xiang /*
88947e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
89047e4937aSGao Xiang  * only after a RCU grace period.
89147e4937aSGao Xiang  */
89247e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
89347e4937aSGao Xiang {
89487ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
89587ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
89647e4937aSGao Xiang }
89747e4937aSGao Xiang 
89847e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
89947e4937aSGao Xiang {
90047e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
90147e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
90247e4937aSGao Xiang 
90387ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
90447e4937aSGao Xiang }
90547e4937aSGao Xiang 
9065c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
90747e4937aSGao Xiang {
90887ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
90947e4937aSGao Xiang 
91087ca34a7SGao Xiang 	if (!pcl)
91147e4937aSGao Xiang 		return false;
91247e4937aSGao Xiang 
91306a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
91487ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
91547e4937aSGao Xiang 
91605b63d2bSGao Xiang 	if (fe->candidate_bvpage)
91706a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
91806a304cdSGao Xiang 
91947e4937aSGao Xiang 	/*
92047e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
92147e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
92247e4937aSGao Xiang 	 */
923db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
92487ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
92547e4937aSGao Xiang 
92687ca34a7SGao Xiang 	fe->pcl = NULL;
92747e4937aSGao Xiang 	return true;
92847e4937aSGao Xiang }
92947e4937aSGao Xiang 
930b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
931b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
932b15b2e30SYue Hu 				 unsigned int len)
933b15b2e30SYue Hu {
9343acea5fcSJingbo Xu 	struct super_block *sb = inode->i_sb;
935b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
936b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
937b15b2e30SYue Hu 	u8 *src, *dst;
938b15b2e30SYue Hu 	unsigned int i, cnt;
939b15b2e30SYue Hu 
940e5126de1SYue Hu 	if (!packed_inode)
941e5126de1SYue Hu 		return -EFSCORRUPTED;
942e5126de1SYue Hu 
943eb2c5e41SGao Xiang 	buf.inode = packed_inode;
944b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
945b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
946b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
9473acea5fcSJingbo Xu 			    sb->s_blocksize - erofs_blkoff(sb, pos));
948eb2c5e41SGao Xiang 		src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
949b15b2e30SYue Hu 		if (IS_ERR(src)) {
950b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
951b15b2e30SYue Hu 			return PTR_ERR(src);
952b15b2e30SYue Hu 		}
953b15b2e30SYue Hu 
954b15b2e30SYue Hu 		dst = kmap_local_page(page);
9553acea5fcSJingbo Xu 		memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
956b15b2e30SYue Hu 		kunmap_local(dst);
957b15b2e30SYue Hu 		pos += cnt;
958b15b2e30SYue Hu 	}
959b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
960b15b2e30SYue Hu 	return 0;
961b15b2e30SYue Hu }
962b15b2e30SYue Hu 
96347e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
964*6ab5eed6SGao Xiang 				struct page *page)
96547e4937aSGao Xiang {
96647e4937aSGao Xiang 	struct inode *const inode = fe->inode;
96747e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
96847e4937aSGao Xiang 	const loff_t offset = page_offset(page);
9695b220b20SGao Xiang 	bool tight = true, exclusive;
9702bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
97147e4937aSGao Xiang 	int err = 0;
97247e4937aSGao Xiang 
97347e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
97447e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
97547e4937aSGao Xiang 
97647e4937aSGao Xiang 	spiltted = 0;
97747e4937aSGao Xiang 	end = PAGE_SIZE;
97847e4937aSGao Xiang repeat:
97947e4937aSGao Xiang 	cur = end - 1;
98047e4937aSGao Xiang 
98139397a46SGao Xiang 	if (offset + cur < map->m_la ||
98239397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
9835c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
98447e4937aSGao Xiang 			fe->backmost = false;
98547e4937aSGao Xiang 		map->m_la = offset + cur;
98647e4937aSGao Xiang 		map->m_llen = 0;
98747e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
9888d8a09b0SGao Xiang 		if (err)
98967148551SGao Xiang 			goto out;
99039397a46SGao Xiang 	} else {
99139397a46SGao Xiang 		if (fe->pcl)
99239397a46SGao Xiang 			goto hitted;
99339397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
99439397a46SGao Xiang 	}
99547e4937aSGao Xiang 
996b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
997b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
99847e4937aSGao Xiang 		goto hitted;
99947e4937aSGao Xiang 
100083a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
10018d8a09b0SGao Xiang 	if (err)
100267148551SGao Xiang 		goto out;
100347e4937aSGao Xiang 
10045c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
100509c54379SGao Xiang 		void *mp;
1006cecf864dSYue Hu 
100709c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
10083acea5fcSJingbo Xu 					erofs_blknr(inode->i_sb, map->m_pa),
10093acea5fcSJingbo Xu 					EROFS_NO_KMAP);
101009c54379SGao Xiang 		if (IS_ERR(mp)) {
101109c54379SGao Xiang 			err = PTR_ERR(mp);
1012cecf864dSYue Hu 			erofs_err(inode->i_sb,
1013cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
101467148551SGao Xiang 			goto out;
1015cecf864dSYue Hu 		}
101609c54379SGao Xiang 		get_page(fe->map.buf.page);
1017ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1018ed722fbcSGao Xiang 			   fe->map.buf.page);
1019db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1020cecf864dSYue Hu 	} else {
10216f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
1022*6ab5eed6SGao Xiang 		z_erofs_bind_cache(fe);
1023cecf864dSYue Hu 	}
102447e4937aSGao Xiang hitted:
1025dc76ea8cSGao Xiang 	/*
1026dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
1027dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
1028dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
1029387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
1030dc76ea8cSGao Xiang 	 */
1031db166fc2SGao Xiang 	tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1032db166fc2SGao Xiang 		  fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1033dc76ea8cSGao Xiang 
103447e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
10358d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
103647e4937aSGao Xiang 		zero_user_segment(page, cur, end);
103747e4937aSGao Xiang 		goto next_part;
103847e4937aSGao Xiang 	}
1039b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
1040b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
1041b15b2e30SYue Hu 
1042b15b2e30SYue Hu 		if (offset > map->m_la) {
1043b15b2e30SYue Hu 			pageofs = 0;
1044b15b2e30SYue Hu 			skip = offset - map->m_la;
1045b15b2e30SYue Hu 		} else {
1046b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
1047b15b2e30SYue Hu 			skip = 0;
1048b15b2e30SYue Hu 		}
1049b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
1050b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1051b15b2e30SYue Hu 		if (err)
1052b15b2e30SYue Hu 			goto out;
1053b15b2e30SYue Hu 		++spiltted;
1054b15b2e30SYue Hu 		tight = false;
1055b15b2e30SYue Hu 		goto next_part;
1056b15b2e30SYue Hu 	}
105747e4937aSGao Xiang 
10585b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
105947e4937aSGao Xiang 	if (cur)
1060db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
106147e4937aSGao Xiang 
106206a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
106306a304cdSGao Xiang 					.page = page,
106406a304cdSGao Xiang 					.offset = offset - map->m_la,
106506a304cdSGao Xiang 					.end = end,
10665b220b20SGao Xiang 				  }), exclusive);
106705b63d2bSGao Xiang 	if (err)
106867148551SGao Xiang 		goto out;
106947e4937aSGao Xiang 
107067148551SGao Xiang 	z_erofs_onlinepage_split(page);
107147e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
107247e4937aSGao Xiang 	++spiltted;
1073267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1074267f2492SGao Xiang 		fe->pcl->multibases = true;
10752bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
10762bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
10772bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
10782bfab9c0SGao Xiang 	}
1079e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1080e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1081e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
1082e7933278SGao Xiang 		fe->pcl->partial = false;
108347e4937aSGao Xiang next_part:
10842bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
108547e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
10862bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
108747e4937aSGao Xiang 
108847e4937aSGao Xiang 	end = cur;
108947e4937aSGao Xiang 	if (end > 0)
109047e4937aSGao Xiang 		goto repeat;
109147e4937aSGao Xiang 
109247e4937aSGao Xiang out:
109367148551SGao Xiang 	if (err)
109467148551SGao Xiang 		z_erofs_page_mark_eio(page);
109547e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
109647e4937aSGao Xiang 	return err;
109747e4937aSGao Xiang }
109847e4937aSGao Xiang 
1099ef4b4b46SYue Hu static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
110040452ffcSHuang Jianan 				       unsigned int readahead_pages)
110140452ffcSHuang Jianan {
1102a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
110340452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
110440452ffcSHuang Jianan 	    !readahead_pages)
110540452ffcSHuang Jianan 		return true;
110640452ffcSHuang Jianan 
110740452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
110840452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
110940452ffcSHuang Jianan 		return true;
111040452ffcSHuang Jianan 
111140452ffcSHuang Jianan 	return false;
111240452ffcSHuang Jianan }
111340452ffcSHuang Jianan 
11146aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
11156aaa7b06SGao Xiang {
11166aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
11176aaa7b06SGao Xiang }
11186aaa7b06SGao Xiang 
11194f05687fSGao Xiang struct z_erofs_decompress_backend {
11204f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
11214f05687fSGao Xiang 	struct super_block *sb;
11224f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
11234f05687fSGao Xiang 
11244f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
11254f05687fSGao Xiang 	struct page **decompressed_pages;
11264f05687fSGao Xiang 	/* pages to keep the compressed data */
11274f05687fSGao Xiang 	struct page **compressed_pages;
11284f05687fSGao Xiang 
1129267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
11304f05687fSGao Xiang 	struct page **pagepool;
11312bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
11324f05687fSGao Xiang };
11334f05687fSGao Xiang 
1134267f2492SGao Xiang struct z_erofs_bvec_item {
1135267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1136267f2492SGao Xiang 	struct list_head list;
1137267f2492SGao Xiang };
1138267f2492SGao Xiang 
1139267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
11403fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
11413fe96ee0SGao Xiang {
1142267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1143267f2492SGao Xiang 
1144267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1145267f2492SGao Xiang 		unsigned int pgnr;
11463fe96ee0SGao Xiang 
1147267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
11482bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
114963bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
11503fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1151267f2492SGao Xiang 			return;
11523fe96ee0SGao Xiang 		}
115363bbb856SGao Xiang 	}
11543fe96ee0SGao Xiang 
1155267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1156267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1157267f2492SGao Xiang 	item->bvec = *bvec;
1158267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1159267f2492SGao Xiang }
1160267f2492SGao Xiang 
1161267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1162267f2492SGao Xiang 				      int err)
1163267f2492SGao Xiang {
1164267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1165267f2492SGao Xiang 	struct list_head *p, *n;
1166267f2492SGao Xiang 
1167267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1168267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1169267f2492SGao Xiang 		unsigned int end, cur;
1170267f2492SGao Xiang 		void *dst, *src;
1171267f2492SGao Xiang 
1172267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1173267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1174267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1175267f2492SGao Xiang 			    bvi->bvec.end);
1176267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1177267f2492SGao Xiang 		while (cur < end) {
1178267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1179267f2492SGao Xiang 
1180267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1181267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1182267f2492SGao Xiang 
1183267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1184267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1185267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1186267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1187267f2492SGao Xiang 				err = -EFSCORRUPTED;
1188267f2492SGao Xiang 				cur += len;
1189267f2492SGao Xiang 				continue;
1190267f2492SGao Xiang 			}
1191267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1192267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1193267f2492SGao Xiang 			kunmap_local(src);
1194267f2492SGao Xiang 			cur += len;
1195267f2492SGao Xiang 		}
1196267f2492SGao Xiang 		kunmap_local(dst);
1197267f2492SGao Xiang 		if (err)
1198267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
1199267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
1200267f2492SGao Xiang 		list_del(p);
1201267f2492SGao Xiang 		kfree(bvi);
1202267f2492SGao Xiang 	}
1203267f2492SGao Xiang }
1204267f2492SGao Xiang 
1205267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
120642fec235SGao Xiang {
12074f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
120806a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
120906a304cdSGao Xiang 	struct page *old_bvpage;
1210267f2492SGao Xiang 	int i;
121142fec235SGao Xiang 
1212387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
121342fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
121406a304cdSGao Xiang 		struct z_erofs_bvec bvec;
121542fec235SGao Xiang 
121606a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
121742fec235SGao Xiang 
121806a304cdSGao Xiang 		if (old_bvpage)
12194f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
122042fec235SGao Xiang 
122106a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1222267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
122342fec235SGao Xiang 	}
122406a304cdSGao Xiang 
122506a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
122606a304cdSGao Xiang 	if (old_bvpage)
12274f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
122842fec235SGao Xiang }
122942fec235SGao Xiang 
12304f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
12314f05687fSGao Xiang 				  bool *overlapped)
123267139e36SGao Xiang {
12334f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
123467139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
123567139e36SGao Xiang 	int i, err = 0;
123667139e36SGao Xiang 
123767139e36SGao Xiang 	*overlapped = false;
123867139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1239ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1240ed722fbcSGao Xiang 		struct page *page = bvec->page;
124167139e36SGao Xiang 
124267139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
124367139e36SGao Xiang 		if (!page) {
124467139e36SGao Xiang 			DBG_BUGON(1);
124567139e36SGao Xiang 			continue;
124667139e36SGao Xiang 		}
1247fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
124867139e36SGao Xiang 
124967139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
125067139e36SGao Xiang 			if (!PageUptodate(page))
125167139e36SGao Xiang 				err = -EIO;
125267139e36SGao Xiang 			continue;
125367139e36SGao Xiang 		}
125467139e36SGao Xiang 
125567139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
125667139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
12574f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
125867139e36SGao Xiang 				if (!PageUptodate(page))
125967139e36SGao Xiang 					err = -EIO;
126067139e36SGao Xiang 				continue;
126167139e36SGao Xiang 			}
1262267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
126367139e36SGao Xiang 			*overlapped = true;
126467139e36SGao Xiang 		}
126567139e36SGao Xiang 	}
126667139e36SGao Xiang 
1267fe3e5914SGao Xiang 	if (err)
12684f05687fSGao Xiang 		return err;
12694f05687fSGao Xiang 	return 0;
127067139e36SGao Xiang }
127167139e36SGao Xiang 
12724f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12734f05687fSGao Xiang 				       int err)
127447e4937aSGao Xiang {
12754f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
12764f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1277cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1278597e2953SYue Hu 	const struct z_erofs_decompressor *decompressor =
1279597e2953SYue Hu 				&erofs_decompressors[pcl->algorithmformat];
12802bfab9c0SGao Xiang 	unsigned int i, inputsize;
128167148551SGao Xiang 	int err2;
12822bfab9c0SGao Xiang 	struct page *page;
12832bfab9c0SGao Xiang 	bool overlapped;
128447e4937aSGao Xiang 
128587ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
12862bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
128747e4937aSGao Xiang 
1288fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1289fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1290fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1291fe3e5914SGao Xiang 	be->onstack_used = 0;
12922bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
12934f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
12942bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
12954f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
12962bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1297fe3e5914SGao Xiang 	}
1298fe3e5914SGao Xiang 
1299fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1300fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1301fe3e5914SGao Xiang 
1302fe3e5914SGao Xiang 	if (!be->decompressed_pages)
13034f05687fSGao Xiang 		be->decompressed_pages =
1304647dd2c3SGao Xiang 			kvcalloc(be->nr_pages, sizeof(struct page *),
1305e7368187SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
1306fe3e5914SGao Xiang 	if (!be->compressed_pages)
1307fe3e5914SGao Xiang 		be->compressed_pages =
1308647dd2c3SGao Xiang 			kvcalloc(pclusterpages, sizeof(struct page *),
1309fe3e5914SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
131047e4937aSGao Xiang 
1311267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
13124f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
13134f05687fSGao Xiang 	if (err2)
13144f05687fSGao Xiang 		err = err2;
13158d8a09b0SGao Xiang 	if (err)
131647e4937aSGao Xiang 		goto out;
131747e4937aSGao Xiang 
1318cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1319cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1320cecf864dSYue Hu 	else
1321cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1322cecf864dSYue Hu 
1323597e2953SYue Hu 	err = decompressor->decompress(&(struct z_erofs_decompress_req) {
13244f05687fSGao Xiang 					.sb = be->sb,
13254f05687fSGao Xiang 					.in = be->compressed_pages,
13264f05687fSGao Xiang 					.out = be->decompressed_pages,
1327cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
132887ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
13299f6cc76eSGao Xiang 					.inputsize = inputsize,
13302bfab9c0SGao Xiang 					.outputsize = pcl->length,
133147e4937aSGao Xiang 					.alg = pcl->algorithmformat,
133247e4937aSGao Xiang 					.inplace_io = overlapped,
13332bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1334267f2492SGao Xiang 					.fillgaps = pcl->multibases,
13354f05687fSGao Xiang 				 }, be->pagepool);
133647e4937aSGao Xiang 
133747e4937aSGao Xiang out:
1338cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1339cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1340ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1341ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1342cecf864dSYue Hu 		put_page(page);
1343cecf864dSYue Hu 	} else {
1344cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1345ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
134647e4937aSGao Xiang 
134747e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
134847e4937aSGao Xiang 				continue;
134947e4937aSGao Xiang 
13506aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
13514f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1352ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
135347e4937aSGao Xiang 		}
1354cecf864dSYue Hu 	}
1355fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1356fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1357647dd2c3SGao Xiang 		kvfree(be->compressed_pages);
1358267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
135947e4937aSGao Xiang 
13602bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
13614f05687fSGao Xiang 		page = be->decompressed_pages[i];
136247e4937aSGao Xiang 		if (!page)
136347e4937aSGao Xiang 			continue;
136447e4937aSGao Xiang 
13656aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
136647e4937aSGao Xiang 
13676aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
13684f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
136947e4937aSGao Xiang 			continue;
137067148551SGao Xiang 		if (err)
137167148551SGao Xiang 			z_erofs_page_mark_eio(page);
137247e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
137347e4937aSGao Xiang 	}
137447e4937aSGao Xiang 
13754f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
1376647dd2c3SGao Xiang 		kvfree(be->decompressed_pages);
137747e4937aSGao Xiang 
13782bfab9c0SGao Xiang 	pcl->length = 0;
13792bfab9c0SGao Xiang 	pcl->partial = true;
1380267f2492SGao Xiang 	pcl->multibases = false;
138106a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
138287ca34a7SGao Xiang 	pcl->vcnt = 0;
138347e4937aSGao Xiang 
138487ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
138547e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
138687ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
138747e4937aSGao Xiang 	return err;
138847e4937aSGao Xiang }
138947e4937aSGao Xiang 
13900c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1391eaa9172aSGao Xiang 				     struct page **pagepool)
139247e4937aSGao Xiang {
13934f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
13944f05687fSGao Xiang 		.sb = io->sb,
13954f05687fSGao Xiang 		.pagepool = pagepool,
1396267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1397267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
13984f05687fSGao Xiang 	};
139947e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
140047e4937aSGao Xiang 
140147e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
14024f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
140347e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
14044f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
140547e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
140647e4937aSGao Xiang 
14074f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
14084f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
140947e4937aSGao Xiang 
14104f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
14114f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
141247e4937aSGao Xiang 	}
141347e4937aSGao Xiang }
141447e4937aSGao Xiang 
14150c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
141647e4937aSGao Xiang {
1417a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1418a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1419eaa9172aSGao Xiang 	struct page *pagepool = NULL;
142047e4937aSGao Xiang 
1421a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
14220c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
1423eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1424a4b1fab1SGao Xiang 	kvfree(bgq);
142547e4937aSGao Xiang }
142647e4937aSGao Xiang 
14273fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14283fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
14293fffb589SSandeep Dhavale {
14303fffb589SSandeep Dhavale 	z_erofs_decompressqueue_work((struct work_struct *)work);
14313fffb589SSandeep Dhavale }
14323fffb589SSandeep Dhavale #endif
14333fffb589SSandeep Dhavale 
14347865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1435cdba5506SGao Xiang 				       int bios)
14367865827cSGao Xiang {
14377865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
14387865827cSGao Xiang 
14397865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1440cdba5506SGao Xiang 	if (io->sync) {
14417865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
144260b30050SHongyu Jin 			complete(&io->u.done);
14437865827cSGao Xiang 		return;
14447865827cSGao Xiang 	}
14457865827cSGao Xiang 
14467865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
14477865827cSGao Xiang 		return;
14483fffb589SSandeep Dhavale 	/* Use (kthread_)work and sync decompression for atomic contexts only */
14497865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
14503fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14513fffb589SSandeep Dhavale 		struct kthread_worker *worker;
14523fffb589SSandeep Dhavale 
14533fffb589SSandeep Dhavale 		rcu_read_lock();
14543fffb589SSandeep Dhavale 		worker = rcu_dereference(
14553fffb589SSandeep Dhavale 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
14563fffb589SSandeep Dhavale 		if (!worker) {
14573fffb589SSandeep Dhavale 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
14587865827cSGao Xiang 			queue_work(z_erofs_workqueue, &io->u.work);
14593fffb589SSandeep Dhavale 		} else {
14603fffb589SSandeep Dhavale 			kthread_queue_work(worker, &io->u.kthread_work);
14613fffb589SSandeep Dhavale 		}
14623fffb589SSandeep Dhavale 		rcu_read_unlock();
14633fffb589SSandeep Dhavale #else
14643fffb589SSandeep Dhavale 		queue_work(z_erofs_workqueue, &io->u.work);
14653fffb589SSandeep Dhavale #endif
14667865827cSGao Xiang 		/* enable sync decompression for readahead */
14677865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
14687865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
14697865827cSGao Xiang 		return;
14707865827cSGao Xiang 	}
14717865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
14727865827cSGao Xiang }
14737865827cSGao Xiang 
147447e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
147547e4937aSGao Xiang 					       unsigned int nr,
1476eaa9172aSGao Xiang 					       struct page **pagepool,
14779f2731d6SGao Xiang 					       struct address_space *mc)
147847e4937aSGao Xiang {
147947e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
14809f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
148147e4937aSGao Xiang 	bool tocache = false;
148247e4937aSGao Xiang 
148347e4937aSGao Xiang 	struct address_space *mapping;
148447e4937aSGao Xiang 	struct page *oldpage, *page;
148547e4937aSGao Xiang 	int justfound;
148647e4937aSGao Xiang 
148747e4937aSGao Xiang repeat:
1488ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
148947e4937aSGao Xiang 	oldpage = page;
149047e4937aSGao Xiang 
149147e4937aSGao Xiang 	if (!page)
149247e4937aSGao Xiang 		goto out_allocpage;
149347e4937aSGao Xiang 
1494b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1495b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
149647e4937aSGao Xiang 
14971825c8d7SGao Xiang 	/*
14981825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
14991825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
15001825c8d7SGao Xiang 	 */
15011825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1502ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
15031825c8d7SGao Xiang 		set_page_private(page, 0);
15041825c8d7SGao Xiang 		tocache = true;
15051825c8d7SGao Xiang 		goto out_tocache;
15061825c8d7SGao Xiang 	}
150747e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
150847e4937aSGao Xiang 
150947e4937aSGao Xiang 	/*
15106aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
151147e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
151247e4937aSGao Xiang 	 */
151347e4937aSGao Xiang 	if (mapping && mapping != mc)
151447e4937aSGao Xiang 		/* ought to be unmanaged pages */
151547e4937aSGao Xiang 		goto out;
151647e4937aSGao Xiang 
15176aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
15186aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
15196aaa7b06SGao Xiang 		goto out;
15206aaa7b06SGao Xiang 
152147e4937aSGao Xiang 	lock_page(page);
152247e4937aSGao Xiang 
152347e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
152447e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
152547e4937aSGao Xiang 
152647e4937aSGao Xiang 	/* the page is still in manage cache */
152747e4937aSGao Xiang 	if (page->mapping == mc) {
1528ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
152947e4937aSGao Xiang 
153047e4937aSGao Xiang 		if (!PagePrivate(page)) {
153147e4937aSGao Xiang 			/*
153247e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
153347e4937aSGao Xiang 			 * the current restriction as well if
1534ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
153547e4937aSGao Xiang 			 */
153647e4937aSGao Xiang 			DBG_BUGON(!justfound);
153747e4937aSGao Xiang 
153847e4937aSGao Xiang 			justfound = 0;
153947e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
154047e4937aSGao Xiang 			SetPagePrivate(page);
154147e4937aSGao Xiang 		}
154247e4937aSGao Xiang 
154347e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
154447e4937aSGao Xiang 		if (PageUptodate(page)) {
154547e4937aSGao Xiang 			unlock_page(page);
154647e4937aSGao Xiang 			page = NULL;
154747e4937aSGao Xiang 		}
154847e4937aSGao Xiang 		goto out;
154947e4937aSGao Xiang 	}
155047e4937aSGao Xiang 
155147e4937aSGao Xiang 	/*
155247e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
155347e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
155447e4937aSGao Xiang 	 */
155547e4937aSGao Xiang 	DBG_BUGON(page->mapping);
155647e4937aSGao Xiang 	DBG_BUGON(!justfound);
155747e4937aSGao Xiang 
155847e4937aSGao Xiang 	tocache = true;
155947e4937aSGao Xiang 	unlock_page(page);
156047e4937aSGao Xiang 	put_page(page);
156147e4937aSGao Xiang out_allocpage:
15625ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1563ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1564ed722fbcSGao Xiang 			       oldpage, page)) {
1565eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
15665ddcee1fSGao Xiang 		cond_resched();
15675ddcee1fSGao Xiang 		goto repeat;
15685ddcee1fSGao Xiang 	}
15691825c8d7SGao Xiang out_tocache:
1570bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1571bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1572bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1573bf225074SGao Xiang 		goto out;
1574a30573b3SGao Xiang 	}
1575bf225074SGao Xiang 	attach_page_private(page, pcl);
1576bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1577bf225074SGao Xiang 	put_page(page);
1578bf225074SGao Xiang 
157947e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
158047e4937aSGao Xiang 	return page;
158147e4937aSGao Xiang }
158247e4937aSGao Xiang 
1583cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1584a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
158547e4937aSGao Xiang {
1586a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
158747e4937aSGao Xiang 
1588a4b1fab1SGao Xiang 	if (fg && !*fg) {
1589a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1590a4b1fab1SGao Xiang 		if (!q) {
1591a4b1fab1SGao Xiang 			*fg = true;
1592a4b1fab1SGao Xiang 			goto fg_out;
159347e4937aSGao Xiang 		}
15943fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
15953fffb589SSandeep Dhavale 		kthread_init_work(&q->u.kthread_work,
15963fffb589SSandeep Dhavale 				  z_erofs_decompressqueue_kthread_work);
15973fffb589SSandeep Dhavale #else
15980c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
15993fffb589SSandeep Dhavale #endif
1600a4b1fab1SGao Xiang 	} else {
1601a4b1fab1SGao Xiang fg_out:
1602a4b1fab1SGao Xiang 		q = fgq;
160360b30050SHongyu Jin 		init_completion(&fgq->u.done);
1604a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
160567148551SGao Xiang 		q->eio = false;
1606cdba5506SGao Xiang 		q->sync = true;
1607a4b1fab1SGao Xiang 	}
1608a4b1fab1SGao Xiang 	q->sb = sb;
1609a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1610a4b1fab1SGao Xiang 	return q;
161147e4937aSGao Xiang }
161247e4937aSGao Xiang 
161347e4937aSGao Xiang /* define decompression jobqueue types */
161447e4937aSGao Xiang enum {
161547e4937aSGao Xiang 	JQ_BYPASS,
161647e4937aSGao Xiang 	JQ_SUBMIT,
161747e4937aSGao Xiang 	NR_JOBQUEUES,
161847e4937aSGao Xiang };
161947e4937aSGao Xiang 
162047e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
162147e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
162247e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
162347e4937aSGao Xiang {
162447e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
162547e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
162647e4937aSGao Xiang 
162747e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
162847e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
162947e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
163047e4937aSGao Xiang 
163147e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
163247e4937aSGao Xiang 
163347e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
163447e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
163547e4937aSGao Xiang 
163647e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
163747e4937aSGao Xiang }
163847e4937aSGao Xiang 
16397865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
16407865827cSGao Xiang {
1641cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
16427865827cSGao Xiang 	blk_status_t err = bio->bi_status;
16437865827cSGao Xiang 	struct bio_vec *bvec;
16447865827cSGao Xiang 	struct bvec_iter_all iter_all;
16457865827cSGao Xiang 
16467865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
16477865827cSGao Xiang 		struct page *page = bvec->bv_page;
16487865827cSGao Xiang 
16497865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
16507865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
16517865827cSGao Xiang 
16527865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
16537865827cSGao Xiang 			if (!err)
16547865827cSGao Xiang 				SetPageUptodate(page);
16557865827cSGao Xiang 			unlock_page(page);
16567865827cSGao Xiang 		}
16577865827cSGao Xiang 	}
165867148551SGao Xiang 	if (err)
165967148551SGao Xiang 		q->eio = true;
1660cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
16617865827cSGao Xiang 	bio_put(bio);
16627865827cSGao Xiang }
16637865827cSGao Xiang 
166483a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1665a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1666ef4b4b46SYue Hu 				 bool *force_fg, bool readahead)
166747e4937aSGao Xiang {
166883a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
166983a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
167047e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1671a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
16725c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1673dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
16743f649ab7SKees Cook 	pgoff_t last_index;
1675dfeab2e9SGao Xiang 	struct block_device *last_bdev;
16761e4a2955SGao Xiang 	unsigned int nr_bios = 0;
16771e4a2955SGao Xiang 	struct bio *bio = NULL;
167882e60d00SJohannes Weiner 	unsigned long pflags;
167982e60d00SJohannes Weiner 	int memstall = 0;
168047e4937aSGao Xiang 
1681cdba5506SGao Xiang 	/*
1682cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1683cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1684cdba5506SGao Xiang 	 */
1685cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1686cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1687cdba5506SGao Xiang 
1688a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1689a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
169047e4937aSGao Xiang 
169147e4937aSGao Xiang 	/* by default, all need io submission */
169247e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
169347e4937aSGao Xiang 
169447e4937aSGao Xiang 	do {
1695dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
169647e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
16971e4a2955SGao Xiang 		pgoff_t cur, end;
16981e4a2955SGao Xiang 		unsigned int i = 0;
16991e4a2955SGao Xiang 		bool bypass = true;
170047e4937aSGao Xiang 
170147e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
170247e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
170347e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
170447e4937aSGao Xiang 
170547e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
170647e4937aSGao Xiang 
1707cecf864dSYue Hu 		/* close the main owned chain at first */
1708cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1709cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1710cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1711cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1712cecf864dSYue Hu 			continue;
1713cecf864dSYue Hu 		}
1714cecf864dSYue Hu 
1715dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1716dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
17173acea5fcSJingbo Xu 			.m_pa = erofs_pos(sb, pcl->obj.index),
1718dfeab2e9SGao Xiang 		};
1719dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1720dfeab2e9SGao Xiang 
17213acea5fcSJingbo Xu 		cur = erofs_blknr(sb, mdev.m_pa);
17229f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
172347e4937aSGao Xiang 
17241e4a2955SGao Xiang 		do {
17251e4a2955SGao Xiang 			struct page *page;
172647e4937aSGao Xiang 
1727*6ab5eed6SGao Xiang 			page = pickup_page_for_submission(pcl, i++,
1728*6ab5eed6SGao Xiang 					&f->pagepool, mc);
17291e4a2955SGao Xiang 			if (!page)
17301e4a2955SGao Xiang 				continue;
173147e4937aSGao Xiang 
1732dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1733dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
173447e4937aSGao Xiang submit_bio_retry:
173594e4e153SGao Xiang 				submit_bio(bio);
173682e60d00SJohannes Weiner 				if (memstall) {
173782e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
173882e60d00SJohannes Weiner 					memstall = 0;
173982e60d00SJohannes Weiner 				}
174047e4937aSGao Xiang 				bio = NULL;
174147e4937aSGao Xiang 			}
174247e4937aSGao Xiang 
174382e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
174499486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
174582e60d00SJohannes Weiner 				memstall = 1;
174682e60d00SJohannes Weiner 			}
174799486c51SChristoph Hellwig 
174847e4937aSGao Xiang 			if (!bio) {
174907888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
175007888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
17510c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1752dfeab2e9SGao Xiang 
1753dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
17541e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
17553acea5fcSJingbo Xu 					(sb->s_blocksize_bits - 9);
1756cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
1757ef4b4b46SYue Hu 				if (readahead)
17586ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
175947e4937aSGao Xiang 				++nr_bios;
176047e4937aSGao Xiang 			}
176147e4937aSGao Xiang 
17626c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
176347e4937aSGao Xiang 				goto submit_bio_retry;
176447e4937aSGao Xiang 
17651e4a2955SGao Xiang 			last_index = cur;
17661e4a2955SGao Xiang 			bypass = false;
17671e4a2955SGao Xiang 		} while (++cur < end);
176847e4937aSGao Xiang 
17691e4a2955SGao Xiang 		if (!bypass)
177047e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
177147e4937aSGao Xiang 		else
177247e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
177347e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
177447e4937aSGao Xiang 
177599486c51SChristoph Hellwig 	if (bio) {
177694e4e153SGao Xiang 		submit_bio(bio);
177782e60d00SJohannes Weiner 		if (memstall)
177882e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
177999486c51SChristoph Hellwig 	}
178047e4937aSGao Xiang 
1781587a67b7SGao Xiang 	/*
1782587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
17833fffb589SSandeep Dhavale 	 * don't issue decompression but drop it directly instead.
1784587a67b7SGao Xiang 	 */
1785587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1786587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
17871e4a2955SGao Xiang 		return;
1788587a67b7SGao Xiang 	}
1789cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
179047e4937aSGao Xiang }
179147e4937aSGao Xiang 
179283a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1793*6ab5eed6SGao Xiang 			     bool force_fg, bool ra)
179447e4937aSGao Xiang {
1795a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
179647e4937aSGao Xiang 
17975c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
179847e4937aSGao Xiang 		return;
1799*6ab5eed6SGao Xiang 	z_erofs_submit_queue(f, io, &force_fg, ra);
180047e4937aSGao Xiang 
18010c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
1802*6ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
180347e4937aSGao Xiang 
180447e4937aSGao Xiang 	if (!force_fg)
180547e4937aSGao Xiang 		return;
180647e4937aSGao Xiang 
180747e4937aSGao Xiang 	/* wait until all bios are completed */
180860b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
180947e4937aSGao Xiang 
18100c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
1811*6ab5eed6SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool);
181247e4937aSGao Xiang }
181347e4937aSGao Xiang 
181438629291SGao Xiang /*
181538629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
181638629291SGao Xiang  * approximate readmore strategies as a start.
181738629291SGao Xiang  */
181838629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
1819*6ab5eed6SGao Xiang 		struct readahead_control *rac, bool backmost)
182038629291SGao Xiang {
182138629291SGao Xiang 	struct inode *inode = f->inode;
182238629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
1823796e9149SYue Hu 	erofs_off_t cur, end, headoffset = f->headoffset;
182438629291SGao Xiang 	int err;
182538629291SGao Xiang 
182638629291SGao Xiang 	if (backmost) {
1827796e9149SYue Hu 		if (rac)
1828796e9149SYue Hu 			end = headoffset + readahead_length(rac) - 1;
1829796e9149SYue Hu 		else
1830796e9149SYue Hu 			end = headoffset + PAGE_SIZE - 1;
183138629291SGao Xiang 		map->m_la = end;
1832622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1833622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
183438629291SGao Xiang 		if (err)
183538629291SGao Xiang 			return;
183638629291SGao Xiang 
1837796e9149SYue Hu 		/* expand ra for the trailing edge if readahead */
183838629291SGao Xiang 		if (rac) {
183938629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1840796e9149SYue Hu 			readahead_expand(rac, headoffset, cur - headoffset);
184138629291SGao Xiang 			return;
184238629291SGao Xiang 		}
184338629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
184438629291SGao Xiang 	} else {
184538629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
184638629291SGao Xiang 
184738629291SGao Xiang 		if (!map->m_llen)
184838629291SGao Xiang 			return;
184938629291SGao Xiang 	}
185038629291SGao Xiang 
185138629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
185238629291SGao Xiang 	while (cur >= end) {
185338629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
185438629291SGao Xiang 		struct page *page;
185538629291SGao Xiang 
185638629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1857aa793b46SGao Xiang 		if (page) {
185838629291SGao Xiang 			if (PageUptodate(page)) {
185938629291SGao Xiang 				unlock_page(page);
1860aa793b46SGao Xiang 			} else {
1861*6ab5eed6SGao Xiang 				err = z_erofs_do_read_page(f, page);
186238629291SGao Xiang 				if (err)
186338629291SGao Xiang 					erofs_err(inode->i_sb,
186438629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
186538629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1866aa793b46SGao Xiang 			}
186738629291SGao Xiang 			put_page(page);
1868aa793b46SGao Xiang 		}
1869aa793b46SGao Xiang 
187038629291SGao Xiang 		if (cur < PAGE_SIZE)
187138629291SGao Xiang 			break;
187238629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
187338629291SGao Xiang 	}
187438629291SGao Xiang }
187538629291SGao Xiang 
1876a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
187747e4937aSGao Xiang {
1878a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
187947e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
188040452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
188147e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
188247e4937aSGao Xiang 	int err;
188347e4937aSGao Xiang 
188447e4937aSGao Xiang 	trace_erofs_readpage(page, false);
188547e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
188647e4937aSGao Xiang 
1887*6ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, true);
1888*6ab5eed6SGao Xiang 	err = z_erofs_do_read_page(&f, page);
1889*6ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, NULL, false);
18905c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
189147e4937aSGao Xiang 
189247e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
1893*6ab5eed6SGao Xiang 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, 0), false);
189447e4937aSGao Xiang 
189547e4937aSGao Xiang 	if (err)
18964f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
189747e4937aSGao Xiang 
189809c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1899*6ab5eed6SGao Xiang 	erofs_release_pages(&f.pagepool);
190047e4937aSGao Xiang 	return err;
190147e4937aSGao Xiang }
190247e4937aSGao Xiang 
19030615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
190447e4937aSGao Xiang {
19050615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
190647e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
190747e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1908*6ab5eed6SGao Xiang 	struct page *head = NULL, *page;
190938629291SGao Xiang 	unsigned int nr_pages;
191047e4937aSGao Xiang 
19110615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
191247e4937aSGao Xiang 
1913*6ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, true);
191438629291SGao Xiang 	nr_pages = readahead_count(rac);
191538629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
191638629291SGao Xiang 
19170615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
191847e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
191947e4937aSGao Xiang 		head = page;
192047e4937aSGao Xiang 	}
192147e4937aSGao Xiang 
192247e4937aSGao Xiang 	while (head) {
192347e4937aSGao Xiang 		struct page *page = head;
192447e4937aSGao Xiang 		int err;
192547e4937aSGao Xiang 
192647e4937aSGao Xiang 		/* traversal in reverse order */
192747e4937aSGao Xiang 		head = (void *)page_private(page);
192847e4937aSGao Xiang 
1929*6ab5eed6SGao Xiang 		err = z_erofs_do_read_page(&f, page);
1930a5876e24SGao Xiang 		if (err)
19314f761fa2SGao Xiang 			erofs_err(inode->i_sb,
19324f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
19334f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
193447e4937aSGao Xiang 		put_page(page);
193547e4937aSGao Xiang 	}
1936*6ab5eed6SGao Xiang 	z_erofs_pcluster_readmore(&f, rac, false);
19375c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
193847e4937aSGao Xiang 
1939*6ab5eed6SGao Xiang 	z_erofs_runqueue(&f, z_erofs_is_sync_decompress(sbi, nr_pages), true);
194009c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1941*6ab5eed6SGao Xiang 	erofs_release_pages(&f.pagepool);
194247e4937aSGao Xiang }
194347e4937aSGao Xiang 
19440c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1945a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
19460615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
194747e4937aSGao Xiang };
1948