xref: /openbmc/linux/fs/erofs/zdata.c (revision 796e9149)
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,
24306a304cdSGao Xiang 				struct page **candidate_bvpage)
24406a304cdSGao Xiang {
24506a304cdSGao Xiang 	if (iter->cur == iter->nr) {
24606a304cdSGao Xiang 		if (!*candidate_bvpage)
24706a304cdSGao Xiang 			return -EAGAIN;
24806a304cdSGao Xiang 
24906a304cdSGao Xiang 		DBG_BUGON(iter->bvset->nextpage);
25006a304cdSGao Xiang 		iter->bvset->nextpage = *candidate_bvpage;
25106a304cdSGao Xiang 		z_erofs_bvset_flip(iter);
25206a304cdSGao Xiang 
25306a304cdSGao Xiang 		iter->bvset->nextpage = NULL;
25406a304cdSGao Xiang 		*candidate_bvpage = NULL;
25506a304cdSGao Xiang 	}
25606a304cdSGao Xiang 	iter->bvset->bvec[iter->cur++] = *bvec;
25706a304cdSGao Xiang 	return 0;
25806a304cdSGao Xiang }
25906a304cdSGao Xiang 
26006a304cdSGao Xiang static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
26106a304cdSGao Xiang 				 struct z_erofs_bvec *bvec,
26206a304cdSGao Xiang 				 struct page **old_bvpage)
26306a304cdSGao Xiang {
26406a304cdSGao Xiang 	if (iter->cur == iter->nr)
26506a304cdSGao Xiang 		*old_bvpage = z_erofs_bvset_flip(iter);
26606a304cdSGao Xiang 	else
26706a304cdSGao Xiang 		*old_bvpage = NULL;
26806a304cdSGao Xiang 	*bvec = iter->bvset->bvec[iter->cur++];
26906a304cdSGao Xiang }
27006a304cdSGao Xiang 
2719f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void)
2729f6cc76eSGao Xiang {
2739f6cc76eSGao Xiang 	int i;
2749f6cc76eSGao Xiang 
2759f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
2769f6cc76eSGao Xiang 		if (!pcluster_pool[i].slab)
2779f6cc76eSGao Xiang 			continue;
2789f6cc76eSGao Xiang 		kmem_cache_destroy(pcluster_pool[i].slab);
2799f6cc76eSGao Xiang 		pcluster_pool[i].slab = NULL;
2809f6cc76eSGao Xiang 	}
2819f6cc76eSGao Xiang }
2829f6cc76eSGao Xiang 
2839f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void)
2849f6cc76eSGao Xiang {
2859f6cc76eSGao Xiang 	struct z_erofs_pcluster_slab *pcs;
2869f6cc76eSGao Xiang 	struct z_erofs_pcluster *a;
2879f6cc76eSGao Xiang 	unsigned int size;
2889f6cc76eSGao Xiang 
2899f6cc76eSGao Xiang 	for (pcs = pcluster_pool;
2909f6cc76eSGao Xiang 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
291ed722fbcSGao Xiang 		size = struct_size(a, compressed_bvecs, pcs->maxpages);
2929f6cc76eSGao Xiang 
2939f6cc76eSGao Xiang 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
2949f6cc76eSGao Xiang 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
2959f6cc76eSGao Xiang 					      SLAB_RECLAIM_ACCOUNT, NULL);
2969f6cc76eSGao Xiang 		if (pcs->slab)
2979f6cc76eSGao Xiang 			continue;
2989f6cc76eSGao Xiang 
2999f6cc76eSGao Xiang 		z_erofs_destroy_pcluster_pool();
3009f6cc76eSGao Xiang 		return -ENOMEM;
3019f6cc76eSGao Xiang 	}
3029f6cc76eSGao Xiang 	return 0;
3039f6cc76eSGao Xiang }
3049f6cc76eSGao Xiang 
3059f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
3069f6cc76eSGao Xiang {
3079f6cc76eSGao Xiang 	int i;
3089f6cc76eSGao Xiang 
3099f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3109f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3119f6cc76eSGao Xiang 		struct z_erofs_pcluster *pcl;
3129f6cc76eSGao Xiang 
3139f6cc76eSGao Xiang 		if (nrpages > pcs->maxpages)
3149f6cc76eSGao Xiang 			continue;
3159f6cc76eSGao Xiang 
3169f6cc76eSGao Xiang 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
3179f6cc76eSGao Xiang 		if (!pcl)
3189f6cc76eSGao Xiang 			return ERR_PTR(-ENOMEM);
3199f6cc76eSGao Xiang 		pcl->pclusterpages = nrpages;
3209f6cc76eSGao Xiang 		return pcl;
3219f6cc76eSGao Xiang 	}
3229f6cc76eSGao Xiang 	return ERR_PTR(-EINVAL);
3239f6cc76eSGao Xiang }
3249f6cc76eSGao Xiang 
3259f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
3269f6cc76eSGao Xiang {
327cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
3289f6cc76eSGao Xiang 	int i;
3299f6cc76eSGao Xiang 
3309f6cc76eSGao Xiang 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
3319f6cc76eSGao Xiang 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
3329f6cc76eSGao Xiang 
333cecf864dSYue Hu 		if (pclusterpages > pcs->maxpages)
3349f6cc76eSGao Xiang 			continue;
3359f6cc76eSGao Xiang 
3369f6cc76eSGao Xiang 		kmem_cache_free(pcs->slab, pcl);
3379f6cc76eSGao Xiang 		return;
3389f6cc76eSGao Xiang 	}
3399f6cc76eSGao Xiang 	DBG_BUGON(1);
3409f6cc76eSGao Xiang }
3419f6cc76eSGao Xiang 
34247e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly;
34347e4937aSGao Xiang 
3443fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
3453fffb589SSandeep Dhavale static struct kthread_worker __rcu **z_erofs_pcpu_workers;
3463fffb589SSandeep Dhavale 
3473fffb589SSandeep Dhavale static void erofs_destroy_percpu_workers(void)
34847e4937aSGao Xiang {
3493fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3503fffb589SSandeep Dhavale 	unsigned int cpu;
3513fffb589SSandeep Dhavale 
3523fffb589SSandeep Dhavale 	for_each_possible_cpu(cpu) {
3533fffb589SSandeep Dhavale 		worker = rcu_dereference_protected(
3543fffb589SSandeep Dhavale 					z_erofs_pcpu_workers[cpu], 1);
3553fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
3563fffb589SSandeep Dhavale 		if (worker)
3573fffb589SSandeep Dhavale 			kthread_destroy_worker(worker);
3583fffb589SSandeep Dhavale 	}
3593fffb589SSandeep Dhavale 	kfree(z_erofs_pcpu_workers);
36047e4937aSGao Xiang }
36147e4937aSGao Xiang 
3623fffb589SSandeep Dhavale static struct kthread_worker *erofs_init_percpu_worker(int cpu)
36347e4937aSGao Xiang {
3643fffb589SSandeep Dhavale 	struct kthread_worker *worker =
3653fffb589SSandeep Dhavale 		kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
36647e4937aSGao Xiang 
3673fffb589SSandeep Dhavale 	if (IS_ERR(worker))
3683fffb589SSandeep Dhavale 		return worker;
3693fffb589SSandeep Dhavale 	if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
3703fffb589SSandeep Dhavale 		sched_set_fifo_low(worker->task);
3713fffb589SSandeep Dhavale 	return worker;
3723fffb589SSandeep Dhavale }
3733fffb589SSandeep Dhavale 
3743fffb589SSandeep Dhavale static int erofs_init_percpu_workers(void)
3753fffb589SSandeep Dhavale {
3763fffb589SSandeep Dhavale 	struct kthread_worker *worker;
3773fffb589SSandeep Dhavale 	unsigned int cpu;
3783fffb589SSandeep Dhavale 
3793fffb589SSandeep Dhavale 	z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
3803fffb589SSandeep Dhavale 			sizeof(struct kthread_worker *), GFP_ATOMIC);
3813fffb589SSandeep Dhavale 	if (!z_erofs_pcpu_workers)
3823fffb589SSandeep Dhavale 		return -ENOMEM;
3833fffb589SSandeep Dhavale 
3843fffb589SSandeep Dhavale 	for_each_online_cpu(cpu) {	/* could miss cpu{off,on}line? */
3853fffb589SSandeep Dhavale 		worker = erofs_init_percpu_worker(cpu);
3863fffb589SSandeep Dhavale 		if (!IS_ERR(worker))
3873fffb589SSandeep Dhavale 			rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
3883fffb589SSandeep Dhavale 	}
3893fffb589SSandeep Dhavale 	return 0;
3903fffb589SSandeep Dhavale }
3913fffb589SSandeep Dhavale #else
3923fffb589SSandeep Dhavale static inline void erofs_destroy_percpu_workers(void) {}
3933fffb589SSandeep Dhavale static inline int erofs_init_percpu_workers(void) { return 0; }
3943fffb589SSandeep Dhavale #endif
3953fffb589SSandeep Dhavale 
3963fffb589SSandeep Dhavale #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD)
3973fffb589SSandeep Dhavale static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
3983fffb589SSandeep Dhavale static enum cpuhp_state erofs_cpuhp_state;
3993fffb589SSandeep Dhavale 
4003fffb589SSandeep Dhavale static int erofs_cpu_online(unsigned int cpu)
4013fffb589SSandeep Dhavale {
4023fffb589SSandeep Dhavale 	struct kthread_worker *worker, *old;
4033fffb589SSandeep Dhavale 
4043fffb589SSandeep Dhavale 	worker = erofs_init_percpu_worker(cpu);
4053fffb589SSandeep Dhavale 	if (IS_ERR(worker))
4063fffb589SSandeep Dhavale 		return PTR_ERR(worker);
4073fffb589SSandeep Dhavale 
4083fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4093fffb589SSandeep Dhavale 	old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4103fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4113fffb589SSandeep Dhavale 	if (!old)
4123fffb589SSandeep Dhavale 		rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
4133fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4143fffb589SSandeep Dhavale 	if (old)
4153fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4163fffb589SSandeep Dhavale 	return 0;
4173fffb589SSandeep Dhavale }
4183fffb589SSandeep Dhavale 
4193fffb589SSandeep Dhavale static int erofs_cpu_offline(unsigned int cpu)
4203fffb589SSandeep Dhavale {
4213fffb589SSandeep Dhavale 	struct kthread_worker *worker;
4223fffb589SSandeep Dhavale 
4233fffb589SSandeep Dhavale 	spin_lock(&z_erofs_pcpu_worker_lock);
4243fffb589SSandeep Dhavale 	worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
4253fffb589SSandeep Dhavale 			lockdep_is_held(&z_erofs_pcpu_worker_lock));
4263fffb589SSandeep Dhavale 	rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
4273fffb589SSandeep Dhavale 	spin_unlock(&z_erofs_pcpu_worker_lock);
4283fffb589SSandeep Dhavale 
4293fffb589SSandeep Dhavale 	synchronize_rcu();
4303fffb589SSandeep Dhavale 	if (worker)
4313fffb589SSandeep Dhavale 		kthread_destroy_worker(worker);
4323fffb589SSandeep Dhavale 	return 0;
4333fffb589SSandeep Dhavale }
4343fffb589SSandeep Dhavale 
4353fffb589SSandeep Dhavale static int erofs_cpu_hotplug_init(void)
4363fffb589SSandeep Dhavale {
4373fffb589SSandeep Dhavale 	int state;
4383fffb589SSandeep Dhavale 
4393fffb589SSandeep Dhavale 	state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
4403fffb589SSandeep Dhavale 			"fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
4413fffb589SSandeep Dhavale 	if (state < 0)
4423fffb589SSandeep Dhavale 		return state;
4433fffb589SSandeep Dhavale 
4443fffb589SSandeep Dhavale 	erofs_cpuhp_state = state;
4453fffb589SSandeep Dhavale 	return 0;
4463fffb589SSandeep Dhavale }
4473fffb589SSandeep Dhavale 
4483fffb589SSandeep Dhavale static void erofs_cpu_hotplug_destroy(void)
4493fffb589SSandeep Dhavale {
4503fffb589SSandeep Dhavale 	if (erofs_cpuhp_state)
4513fffb589SSandeep Dhavale 		cpuhp_remove_state_nocalls(erofs_cpuhp_state);
4523fffb589SSandeep Dhavale }
4533fffb589SSandeep Dhavale #else /* !CONFIG_HOTPLUG_CPU || !CONFIG_EROFS_FS_PCPU_KTHREAD */
4543fffb589SSandeep Dhavale static inline int erofs_cpu_hotplug_init(void) { return 0; }
4553fffb589SSandeep Dhavale static inline void erofs_cpu_hotplug_destroy(void) {}
4563fffb589SSandeep Dhavale #endif
4573fffb589SSandeep Dhavale 
4583fffb589SSandeep Dhavale void z_erofs_exit_zip_subsystem(void)
4593fffb589SSandeep Dhavale {
4603fffb589SSandeep Dhavale 	erofs_cpu_hotplug_destroy();
4613fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4623fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4633fffb589SSandeep Dhavale 	z_erofs_destroy_pcluster_pool();
46447e4937aSGao Xiang }
46547e4937aSGao Xiang 
46647e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void)
46747e4937aSGao Xiang {
4689f6cc76eSGao Xiang 	int err = z_erofs_create_pcluster_pool();
46947e4937aSGao Xiang 
4709f6cc76eSGao Xiang 	if (err)
4713fffb589SSandeep Dhavale 		goto out_error_pcluster_pool;
4723fffb589SSandeep Dhavale 
4733fffb589SSandeep Dhavale 	z_erofs_workqueue = alloc_workqueue("erofs_worker",
4743fffb589SSandeep Dhavale 			WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
4758d1b80a7SDan Carpenter 	if (!z_erofs_workqueue) {
4768d1b80a7SDan Carpenter 		err = -ENOMEM;
4773fffb589SSandeep Dhavale 		goto out_error_workqueue_init;
4788d1b80a7SDan Carpenter 	}
4793fffb589SSandeep Dhavale 
4803fffb589SSandeep Dhavale 	err = erofs_init_percpu_workers();
4819f6cc76eSGao Xiang 	if (err)
4823fffb589SSandeep Dhavale 		goto out_error_pcpu_worker;
4833fffb589SSandeep Dhavale 
4843fffb589SSandeep Dhavale 	err = erofs_cpu_hotplug_init();
4853fffb589SSandeep Dhavale 	if (err < 0)
4863fffb589SSandeep Dhavale 		goto out_error_cpuhp_init;
4873fffb589SSandeep Dhavale 	return err;
4883fffb589SSandeep Dhavale 
4893fffb589SSandeep Dhavale out_error_cpuhp_init:
4903fffb589SSandeep Dhavale 	erofs_destroy_percpu_workers();
4913fffb589SSandeep Dhavale out_error_pcpu_worker:
4923fffb589SSandeep Dhavale 	destroy_workqueue(z_erofs_workqueue);
4933fffb589SSandeep Dhavale out_error_workqueue_init:
4949f6cc76eSGao Xiang 	z_erofs_destroy_pcluster_pool();
4953fffb589SSandeep Dhavale out_error_pcluster_pool:
4969f6cc76eSGao Xiang 	return err;
49747e4937aSGao Xiang }
49847e4937aSGao Xiang 
499db166fc2SGao Xiang enum z_erofs_pclustermode {
500db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_INFLIGHT,
50147e4937aSGao Xiang 	/*
502db166fc2SGao Xiang 	 * The current pclusters was the tail of an exist chain, in addition
503db166fc2SGao Xiang 	 * that the previous processed chained pclusters are all decided to
50447e4937aSGao Xiang 	 * be hooked up to it.
505db166fc2SGao Xiang 	 * A new chain will be created for the remaining pclusters which are
506db166fc2SGao Xiang 	 * not processed yet, so different from Z_EROFS_PCLUSTER_FOLLOWED,
507db166fc2SGao Xiang 	 * the next pcluster cannot reuse the whole page safely for inplace I/O
508db166fc2SGao Xiang 	 * in the following scenario:
50947e4937aSGao Xiang 	 *  ________________________________________________________________
51047e4937aSGao Xiang 	 * |      tail (partial) page     |       head (partial) page       |
511db166fc2SGao Xiang 	 * |   (belongs to the next pcl)  |   (belongs to the current pcl)  |
512db166fc2SGao Xiang 	 * |_______PCLUSTER_FOLLOWED______|________PCLUSTER_HOOKED__________|
51347e4937aSGao Xiang 	 */
514db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_HOOKED,
5150b964600SGao Xiang 	/*
516db166fc2SGao Xiang 	 * a weak form of Z_EROFS_PCLUSTER_FOLLOWED, the difference is that it
5170b964600SGao Xiang 	 * could be dispatched into bypass queue later due to uptodated managed
5180b964600SGao Xiang 	 * pages. All related online pages cannot be reused for inplace I/O (or
519387bab87SGao Xiang 	 * bvpage) since it can be directly decoded without I/O submission.
5200b964600SGao Xiang 	 */
521db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
52247e4937aSGao Xiang 	/*
52347e4937aSGao Xiang 	 * The current collection has been linked with the owned chain, and
52447e4937aSGao Xiang 	 * could also be linked with the remaining collections, which means
52547e4937aSGao Xiang 	 * if the processing page is the tail page of the collection, thus
52647e4937aSGao Xiang 	 * the current collection can safely use the whole page (since
52747e4937aSGao Xiang 	 * the previous collection is under control) for in-place I/O, as
52847e4937aSGao Xiang 	 * illustrated below:
52947e4937aSGao Xiang 	 *  ________________________________________________________________
53047e4937aSGao Xiang 	 * |  tail (partial) page |          head (partial) page           |
53147e4937aSGao Xiang 	 * |  (of the current cl) |      (of the previous collection)      |
532db166fc2SGao Xiang 	 * | PCLUSTER_FOLLOWED or |                                        |
533db166fc2SGao Xiang 	 * |_____PCLUSTER_HOOKED__|___________PCLUSTER_FOLLOWED____________|
53447e4937aSGao Xiang 	 *
53547e4937aSGao Xiang 	 * [  (*) the above page can be used as inplace I/O.               ]
53647e4937aSGao Xiang 	 */
537db166fc2SGao Xiang 	Z_EROFS_PCLUSTER_FOLLOWED,
53847e4937aSGao Xiang };
53947e4937aSGao Xiang 
5405c6dcc57SGao Xiang struct z_erofs_decompress_frontend {
5415c6dcc57SGao Xiang 	struct inode *const inode;
5425c6dcc57SGao Xiang 	struct erofs_map_blocks map;
54306a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
54447e4937aSGao Xiang 
54506a304cdSGao Xiang 	struct page *candidate_bvpage;
54647e4937aSGao Xiang 	struct z_erofs_pcluster *pcl, *tailpcl;
54747e4937aSGao Xiang 	z_erofs_next_pcluster_t owned_head;
548db166fc2SGao Xiang 	enum z_erofs_pclustermode mode;
54947e4937aSGao Xiang 
55047e4937aSGao Xiang 	/* used for applying cache strategy on the fly */
55147e4937aSGao Xiang 	bool backmost;
55247e4937aSGao Xiang 	erofs_off_t headoffset;
553ed722fbcSGao Xiang 
554ed722fbcSGao Xiang 	/* a pointer used to pick up inplace I/O pages */
555ed722fbcSGao Xiang 	unsigned int icur;
55647e4937aSGao Xiang };
55747e4937aSGao Xiang 
55847e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \
5595c6dcc57SGao Xiang 	.inode = __i, .owned_head = Z_EROFS_PCLUSTER_TAIL, \
560db166fc2SGao Xiang 	.mode = Z_EROFS_PCLUSTER_FOLLOWED, .backmost = true }
56147e4937aSGao Xiang 
5621282dea3SGao Xiang static bool z_erofs_should_alloc_cache(struct z_erofs_decompress_frontend *fe)
5631282dea3SGao Xiang {
5641282dea3SGao Xiang 	unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
5651282dea3SGao Xiang 
5661282dea3SGao Xiang 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
5671282dea3SGao Xiang 		return false;
5681282dea3SGao Xiang 
5691282dea3SGao Xiang 	if (fe->backmost)
5701282dea3SGao Xiang 		return true;
5711282dea3SGao Xiang 
5721282dea3SGao Xiang 	if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
5731282dea3SGao Xiang 	    fe->map.m_la < fe->headoffset)
5741282dea3SGao Xiang 		return true;
5751282dea3SGao Xiang 
5761282dea3SGao Xiang 	return false;
5771282dea3SGao Xiang }
5781282dea3SGao Xiang 
5796f39d1e1SGao Xiang static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe,
580eaa9172aSGao Xiang 			       struct page **pagepool)
58147e4937aSGao Xiang {
5826f39d1e1SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
5835c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
5841282dea3SGao Xiang 	bool shouldalloc = z_erofs_should_alloc_cache(fe);
58547e4937aSGao Xiang 	bool standalone = true;
5866f39d1e1SGao Xiang 	/*
5876f39d1e1SGao Xiang 	 * optimistic allocation without direct reclaim since inplace I/O
5886f39d1e1SGao Xiang 	 * can be used if low memory otherwise.
5896f39d1e1SGao Xiang 	 */
5901825c8d7SGao Xiang 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
5911825c8d7SGao Xiang 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
592ed722fbcSGao Xiang 	unsigned int i;
59347e4937aSGao Xiang 
594db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
59547e4937aSGao Xiang 		return;
59647e4937aSGao Xiang 
597ed722fbcSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
59847e4937aSGao Xiang 		struct page *page;
599b1ed220cSGao Xiang 		void *t;	/* mark pages just found for debugging */
6001825c8d7SGao Xiang 		struct page *newpage = NULL;
60147e4937aSGao Xiang 
60247e4937aSGao Xiang 		/* the compressed page was loaded before */
603ed722fbcSGao Xiang 		if (READ_ONCE(pcl->compressed_bvecs[i].page))
60447e4937aSGao Xiang 			continue;
60547e4937aSGao Xiang 
606ed722fbcSGao Xiang 		page = find_get_page(mc, pcl->obj.index + i);
60747e4937aSGao Xiang 
60847e4937aSGao Xiang 		if (page) {
609b1ed220cSGao Xiang 			t = (void *)((unsigned long)page | 1);
6100b964600SGao Xiang 		} else {
6110b964600SGao Xiang 			/* I/O is needed, no possible to decompress directly */
6120b964600SGao Xiang 			standalone = false;
6131282dea3SGao Xiang 			if (!shouldalloc)
6141282dea3SGao Xiang 				continue;
6151282dea3SGao Xiang 
6161282dea3SGao Xiang 			/*
6171282dea3SGao Xiang 			 * try to use cached I/O if page allocation
6181282dea3SGao Xiang 			 * succeeds or fallback to in-place I/O instead
6191282dea3SGao Xiang 			 * to avoid any direct reclaim.
6201282dea3SGao Xiang 			 */
6211825c8d7SGao Xiang 			newpage = erofs_allocpage(pagepool, gfp);
6221825c8d7SGao Xiang 			if (!newpage)
62347e4937aSGao Xiang 				continue;
6241282dea3SGao Xiang 			set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
625b1ed220cSGao Xiang 			t = (void *)((unsigned long)newpage | 1);
62647e4937aSGao Xiang 		}
62747e4937aSGao Xiang 
628b1ed220cSGao Xiang 		if (!cmpxchg_relaxed(&pcl->compressed_bvecs[i].page, NULL, t))
62947e4937aSGao Xiang 			continue;
63047e4937aSGao Xiang 
631eaa9172aSGao Xiang 		if (page)
63247e4937aSGao Xiang 			put_page(page);
633eaa9172aSGao Xiang 		else if (newpage)
634eaa9172aSGao Xiang 			erofs_pagepool_add(pagepool, newpage);
63547e4937aSGao Xiang 	}
63647e4937aSGao Xiang 
6370b964600SGao Xiang 	/*
6380b964600SGao Xiang 	 * don't do inplace I/O if all compressed pages are available in
6390b964600SGao Xiang 	 * managed cache since it can be moved to the bypass queue instead.
6400b964600SGao Xiang 	 */
6410b964600SGao Xiang 	if (standalone)
642db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
64347e4937aSGao Xiang }
64447e4937aSGao Xiang 
64547e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */
64647e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
64747e4937aSGao Xiang 				       struct erofs_workgroup *grp)
64847e4937aSGao Xiang {
64947e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
65047e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
65147e4937aSGao Xiang 	int i;
65247e4937aSGao Xiang 
653cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
65447e4937aSGao Xiang 	/*
65547e4937aSGao Xiang 	 * refcount of workgroup is now freezed as 1,
65647e4937aSGao Xiang 	 * therefore no need to worry about available decompression users.
65747e4937aSGao Xiang 	 */
6589f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
659ed722fbcSGao Xiang 		struct page *page = pcl->compressed_bvecs[i].page;
66047e4937aSGao Xiang 
66147e4937aSGao Xiang 		if (!page)
66247e4937aSGao Xiang 			continue;
66347e4937aSGao Xiang 
66447e4937aSGao Xiang 		/* block other users from reclaiming or migrating the page */
66547e4937aSGao Xiang 		if (!trylock_page(page))
66647e4937aSGao Xiang 			return -EBUSY;
66747e4937aSGao Xiang 
668f4d4e5fcSYue Hu 		if (!erofs_page_is_managed(sbi, page))
66947e4937aSGao Xiang 			continue;
67047e4937aSGao Xiang 
67147e4937aSGao Xiang 		/* barrier is implied in the following 'unlock_page' */
672ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
6736aaa7b06SGao Xiang 		detach_page_private(page);
67447e4937aSGao Xiang 		unlock_page(page);
67547e4937aSGao Xiang 	}
67647e4937aSGao Xiang 	return 0;
67747e4937aSGao Xiang }
67847e4937aSGao Xiang 
679d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page)
68047e4937aSGao Xiang {
68147e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
682ed722fbcSGao Xiang 	int ret, i;
68347e4937aSGao Xiang 
684ed722fbcSGao Xiang 	if (!erofs_workgroup_try_to_freeze(&pcl->obj, 1))
685ed722fbcSGao Xiang 		return 0;
68647e4937aSGao Xiang 
687ed722fbcSGao Xiang 	ret = 0;
688cecf864dSYue Hu 	DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
6899f6cc76eSGao Xiang 	for (i = 0; i < pcl->pclusterpages; ++i) {
690ed722fbcSGao Xiang 		if (pcl->compressed_bvecs[i].page == page) {
691ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
69247e4937aSGao Xiang 			ret = 1;
69347e4937aSGao Xiang 			break;
69447e4937aSGao Xiang 		}
69547e4937aSGao Xiang 	}
69647e4937aSGao Xiang 	erofs_workgroup_unfreeze(&pcl->obj, 1);
6976aaa7b06SGao Xiang 	if (ret)
6986aaa7b06SGao Xiang 		detach_page_private(page);
69947e4937aSGao Xiang 	return ret;
70047e4937aSGao Xiang }
70147e4937aSGao Xiang 
7025c6dcc57SGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_decompress_frontend *fe,
703ed722fbcSGao Xiang 				   struct z_erofs_bvec *bvec)
70447e4937aSGao Xiang {
7055c6dcc57SGao Xiang 	struct z_erofs_pcluster *const pcl = fe->pcl;
70647e4937aSGao Xiang 
707ed722fbcSGao Xiang 	while (fe->icur > 0) {
708ed722fbcSGao Xiang 		if (!cmpxchg(&pcl->compressed_bvecs[--fe->icur].page,
709ed722fbcSGao Xiang 			     NULL, bvec->page)) {
710ed722fbcSGao Xiang 			pcl->compressed_bvecs[fe->icur] = *bvec;
71147e4937aSGao Xiang 			return true;
712ed722fbcSGao Xiang 		}
713ed722fbcSGao Xiang 	}
71447e4937aSGao Xiang 	return false;
71547e4937aSGao Xiang }
71647e4937aSGao Xiang 
71787ca34a7SGao Xiang /* callers must be with pcluster lock held */
7185c6dcc57SGao Xiang static int z_erofs_attach_page(struct z_erofs_decompress_frontend *fe,
7195b220b20SGao Xiang 			       struct z_erofs_bvec *bvec, bool exclusive)
72047e4937aSGao Xiang {
72147e4937aSGao Xiang 	int ret;
72247e4937aSGao Xiang 
723db166fc2SGao Xiang 	if (exclusive) {
72406a304cdSGao Xiang 		/* give priority for inplaceio to use file pages first */
725ed722fbcSGao Xiang 		if (z_erofs_try_inplace_io(fe, bvec))
72647e4937aSGao Xiang 			return 0;
72706a304cdSGao Xiang 		/* otherwise, check if it can be used as a bvpage */
728db166fc2SGao Xiang 		if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
72906a304cdSGao Xiang 		    !fe->candidate_bvpage)
73006a304cdSGao Xiang 			fe->candidate_bvpage = bvec->page;
73106a304cdSGao Xiang 	}
73206a304cdSGao Xiang 	ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage);
73306a304cdSGao Xiang 	fe->pcl->vcnt += (ret >= 0);
73406a304cdSGao Xiang 	return ret;
73547e4937aSGao Xiang }
73647e4937aSGao Xiang 
7375c6dcc57SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_decompress_frontend *f)
73847e4937aSGao Xiang {
7395c6dcc57SGao Xiang 	struct z_erofs_pcluster *pcl = f->pcl;
7405c6dcc57SGao Xiang 	z_erofs_next_pcluster_t *owned_head = &f->owned_head;
74147e4937aSGao Xiang 
742473e15b0SGao Xiang 	/* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */
743473e15b0SGao Xiang 	if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
744473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_NIL) {
74547e4937aSGao Xiang 		*owned_head = &pcl->next;
746473e15b0SGao Xiang 		/* so we can attach this pcluster to our submission chain. */
747db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_FOLLOWED;
748473e15b0SGao Xiang 		return;
749473e15b0SGao Xiang 	}
750473e15b0SGao Xiang 
75147e4937aSGao Xiang 	/*
752473e15b0SGao Xiang 	 * type 2, link to the end of an existing open chain, be careful
753473e15b0SGao Xiang 	 * that its submission is controlled by the original attached chain.
75447e4937aSGao Xiang 	 */
755267f2492SGao Xiang 	if (*owned_head != &pcl->next && pcl != f->tailpcl &&
756267f2492SGao Xiang 	    cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
757473e15b0SGao Xiang 		    *owned_head) == Z_EROFS_PCLUSTER_TAIL) {
75847e4937aSGao Xiang 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
759db166fc2SGao Xiang 		f->mode = Z_EROFS_PCLUSTER_HOOKED;
7605c6dcc57SGao Xiang 		f->tailpcl = NULL;
761473e15b0SGao Xiang 		return;
76247e4937aSGao Xiang 	}
763473e15b0SGao Xiang 	/* type 3, it belongs to a chain, but it isn't the end of the chain */
764db166fc2SGao Xiang 	f->mode = Z_EROFS_PCLUSTER_INFLIGHT;
76547e4937aSGao Xiang }
76647e4937aSGao Xiang 
76783a386c0SGao Xiang static int z_erofs_register_pcluster(struct z_erofs_decompress_frontend *fe)
76847e4937aSGao Xiang {
76983a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
770cecf864dSYue Hu 	bool ztailpacking = map->m_flags & EROFS_MAP_META;
77147e4937aSGao Xiang 	struct z_erofs_pcluster *pcl;
77264094a04SGao Xiang 	struct erofs_workgroup *grp;
77347e4937aSGao Xiang 	int err;
77447e4937aSGao Xiang 
775c42c0ffeSChen Zhongjin 	if (!(map->m_flags & EROFS_MAP_ENCODED) ||
776c42c0ffeSChen Zhongjin 	    (!ztailpacking && !(map->m_pa >> PAGE_SHIFT))) {
7778f899262SGao Xiang 		DBG_BUGON(1);
7788f899262SGao Xiang 		return -EFSCORRUPTED;
7798f899262SGao Xiang 	}
7808f899262SGao Xiang 
7819f6cc76eSGao Xiang 	/* no available pcluster, let's allocate one */
782cecf864dSYue Hu 	pcl = z_erofs_alloc_pcluster(ztailpacking ? 1 :
783cecf864dSYue Hu 				     map->m_plen >> PAGE_SHIFT);
7849f6cc76eSGao Xiang 	if (IS_ERR(pcl))
7859f6cc76eSGao Xiang 		return PTR_ERR(pcl);
78647e4937aSGao Xiang 
78764094a04SGao Xiang 	atomic_set(&pcl->obj.refcount, 1);
7888f899262SGao Xiang 	pcl->algorithmformat = map->m_algorithmformat;
7892bfab9c0SGao Xiang 	pcl->length = 0;
7902bfab9c0SGao Xiang 	pcl->partial = true;
79147e4937aSGao Xiang 
79247e4937aSGao Xiang 	/* new pclusters should be claimed as type 1, primary and followed */
7935c6dcc57SGao Xiang 	pcl->next = fe->owned_head;
79487ca34a7SGao Xiang 	pcl->pageofs_out = map->m_la & ~PAGE_MASK;
795db166fc2SGao Xiang 	fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
79647e4937aSGao Xiang 
79747e4937aSGao Xiang 	/*
79847e4937aSGao Xiang 	 * lock all primary followed works before visible to others
79947e4937aSGao Xiang 	 * and mutex_trylock *never* fails for a new pcluster.
80047e4937aSGao Xiang 	 */
80187ca34a7SGao Xiang 	mutex_init(&pcl->lock);
80287ca34a7SGao Xiang 	DBG_BUGON(!mutex_trylock(&pcl->lock));
80347e4937aSGao Xiang 
804cecf864dSYue Hu 	if (ztailpacking) {
805cecf864dSYue Hu 		pcl->obj.index = 0;	/* which indicates ztailpacking */
8063acea5fcSJingbo Xu 		pcl->pageofs_in = erofs_blkoff(fe->inode->i_sb, map->m_pa);
807cecf864dSYue Hu 		pcl->tailpacking_size = map->m_plen;
808cecf864dSYue Hu 	} else {
809cecf864dSYue Hu 		pcl->obj.index = map->m_pa >> PAGE_SHIFT;
810cecf864dSYue Hu 
81183a386c0SGao Xiang 		grp = erofs_insert_workgroup(fe->inode->i_sb, &pcl->obj);
81264094a04SGao Xiang 		if (IS_ERR(grp)) {
81364094a04SGao Xiang 			err = PTR_ERR(grp);
81464094a04SGao Xiang 			goto err_out;
81564094a04SGao Xiang 		}
81664094a04SGao Xiang 
81764094a04SGao Xiang 		if (grp != &pcl->obj) {
8185c6dcc57SGao Xiang 			fe->pcl = container_of(grp,
819cecf864dSYue Hu 					struct z_erofs_pcluster, obj);
82064094a04SGao Xiang 			err = -EEXIST;
82164094a04SGao Xiang 			goto err_out;
82247e4937aSGao Xiang 		}
823cecf864dSYue Hu 	}
82447e4937aSGao Xiang 	/* used to check tail merging loop due to corrupted images */
8255c6dcc57SGao Xiang 	if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
8265c6dcc57SGao Xiang 		fe->tailpcl = pcl;
8275c6dcc57SGao Xiang 	fe->owned_head = &pcl->next;
8285c6dcc57SGao Xiang 	fe->pcl = pcl;
8299e579fc1SGao Xiang 	return 0;
83064094a04SGao Xiang 
83164094a04SGao Xiang err_out:
83287ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
8339f6cc76eSGao Xiang 	z_erofs_free_pcluster(pcl);
83464094a04SGao Xiang 	return err;
83547e4937aSGao Xiang }
83647e4937aSGao Xiang 
83783a386c0SGao Xiang static int z_erofs_collector_begin(struct z_erofs_decompress_frontend *fe)
83847e4937aSGao Xiang {
83983a386c0SGao Xiang 	struct erofs_map_blocks *map = &fe->map;
8400d823b42SGao Xiang 	struct erofs_workgroup *grp = NULL;
8419e579fc1SGao Xiang 	int ret;
84247e4937aSGao Xiang 
84387ca34a7SGao Xiang 	DBG_BUGON(fe->pcl);
84447e4937aSGao Xiang 
84587ca34a7SGao Xiang 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
8465c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_NIL);
8475c6dcc57SGao Xiang 	DBG_BUGON(fe->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
84847e4937aSGao Xiang 
8490d823b42SGao Xiang 	if (!(map->m_flags & EROFS_MAP_META)) {
8500d823b42SGao Xiang 		grp = erofs_find_workgroup(fe->inode->i_sb,
8510d823b42SGao Xiang 					   map->m_pa >> PAGE_SHIFT);
8520d823b42SGao Xiang 	} else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
85347e4937aSGao Xiang 		DBG_BUGON(1);
854cecf864dSYue Hu 		return -EFSCORRUPTED;
855cecf864dSYue Hu 	}
85647e4937aSGao Xiang 
85764094a04SGao Xiang 	if (grp) {
8585c6dcc57SGao Xiang 		fe->pcl = container_of(grp, struct z_erofs_pcluster, obj);
8590d823b42SGao Xiang 		ret = -EEXIST;
86064094a04SGao Xiang 	} else {
86183a386c0SGao Xiang 		ret = z_erofs_register_pcluster(fe);
86264094a04SGao Xiang 	}
86347e4937aSGao Xiang 
8640d823b42SGao Xiang 	if (ret == -EEXIST) {
865267f2492SGao Xiang 		mutex_lock(&fe->pcl->lock);
866267f2492SGao Xiang 		/* used to check tail merging loop due to corrupted images */
867267f2492SGao Xiang 		if (fe->owned_head == Z_EROFS_PCLUSTER_TAIL)
868267f2492SGao Xiang 			fe->tailpcl = fe->pcl;
869267f2492SGao Xiang 
870267f2492SGao Xiang 		z_erofs_try_to_claim_pcluster(fe);
8710d823b42SGao Xiang 	} else if (ret) {
8720d823b42SGao Xiang 		return ret;
8730d823b42SGao Xiang 	}
87406a304cdSGao Xiang 	z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
875387bab87SGao Xiang 				Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
87681382f5fSGao Xiang 	/* since file-backed online pages are traversed in reverse order */
877ed722fbcSGao Xiang 	fe->icur = z_erofs_pclusterpages(fe->pcl);
87847e4937aSGao Xiang 	return 0;
87947e4937aSGao Xiang }
88047e4937aSGao Xiang 
88147e4937aSGao Xiang /*
88247e4937aSGao Xiang  * keep in mind that no referenced pclusters will be freed
88347e4937aSGao Xiang  * only after a RCU grace period.
88447e4937aSGao Xiang  */
88547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head)
88647e4937aSGao Xiang {
88787ca34a7SGao Xiang 	z_erofs_free_pcluster(container_of(head,
88887ca34a7SGao Xiang 			struct z_erofs_pcluster, rcu));
88947e4937aSGao Xiang }
89047e4937aSGao Xiang 
89147e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
89247e4937aSGao Xiang {
89347e4937aSGao Xiang 	struct z_erofs_pcluster *const pcl =
89447e4937aSGao Xiang 		container_of(grp, struct z_erofs_pcluster, obj);
89547e4937aSGao Xiang 
89687ca34a7SGao Xiang 	call_rcu(&pcl->rcu, z_erofs_rcu_callback);
89747e4937aSGao Xiang }
89847e4937aSGao Xiang 
8995c6dcc57SGao Xiang static bool z_erofs_collector_end(struct z_erofs_decompress_frontend *fe)
90047e4937aSGao Xiang {
90187ca34a7SGao Xiang 	struct z_erofs_pcluster *pcl = fe->pcl;
90247e4937aSGao Xiang 
90387ca34a7SGao Xiang 	if (!pcl)
90447e4937aSGao Xiang 		return false;
90547e4937aSGao Xiang 
90606a304cdSGao Xiang 	z_erofs_bvec_iter_end(&fe->biter);
90787ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
90847e4937aSGao Xiang 
90906a304cdSGao Xiang 	if (fe->candidate_bvpage) {
91006a304cdSGao Xiang 		DBG_BUGON(z_erofs_is_shortlived_page(fe->candidate_bvpage));
91106a304cdSGao Xiang 		fe->candidate_bvpage = NULL;
91206a304cdSGao Xiang 	}
91306a304cdSGao Xiang 
91447e4937aSGao Xiang 	/*
91547e4937aSGao Xiang 	 * if all pending pages are added, don't hold its reference
91647e4937aSGao Xiang 	 * any longer if the pcluster isn't hosted by ourselves.
91747e4937aSGao Xiang 	 */
918db166fc2SGao Xiang 	if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
91987ca34a7SGao Xiang 		erofs_workgroup_put(&pcl->obj);
92047e4937aSGao Xiang 
92187ca34a7SGao Xiang 	fe->pcl = NULL;
92247e4937aSGao Xiang 	return true;
92347e4937aSGao Xiang }
92447e4937aSGao Xiang 
925b15b2e30SYue Hu static int z_erofs_read_fragment(struct inode *inode, erofs_off_t pos,
926b15b2e30SYue Hu 				 struct page *page, unsigned int pageofs,
927b15b2e30SYue Hu 				 unsigned int len)
928b15b2e30SYue Hu {
9293acea5fcSJingbo Xu 	struct super_block *sb = inode->i_sb;
930b15b2e30SYue Hu 	struct inode *packed_inode = EROFS_I_SB(inode)->packed_inode;
931b15b2e30SYue Hu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
932b15b2e30SYue Hu 	u8 *src, *dst;
933b15b2e30SYue Hu 	unsigned int i, cnt;
934b15b2e30SYue Hu 
935e5126de1SYue Hu 	if (!packed_inode)
936e5126de1SYue Hu 		return -EFSCORRUPTED;
937e5126de1SYue Hu 
938eb2c5e41SGao Xiang 	buf.inode = packed_inode;
939b15b2e30SYue Hu 	pos += EROFS_I(inode)->z_fragmentoff;
940b15b2e30SYue Hu 	for (i = 0; i < len; i += cnt) {
941b15b2e30SYue Hu 		cnt = min_t(unsigned int, len - i,
9423acea5fcSJingbo Xu 			    sb->s_blocksize - erofs_blkoff(sb, pos));
943eb2c5e41SGao Xiang 		src = erofs_bread(&buf, erofs_blknr(sb, pos), EROFS_KMAP);
944b15b2e30SYue Hu 		if (IS_ERR(src)) {
945b15b2e30SYue Hu 			erofs_put_metabuf(&buf);
946b15b2e30SYue Hu 			return PTR_ERR(src);
947b15b2e30SYue Hu 		}
948b15b2e30SYue Hu 
949b15b2e30SYue Hu 		dst = kmap_local_page(page);
9503acea5fcSJingbo Xu 		memcpy(dst + pageofs + i, src + erofs_blkoff(sb, pos), cnt);
951b15b2e30SYue Hu 		kunmap_local(dst);
952b15b2e30SYue Hu 		pos += cnt;
953b15b2e30SYue Hu 	}
954b15b2e30SYue Hu 	erofs_put_metabuf(&buf);
955b15b2e30SYue Hu 	return 0;
956b15b2e30SYue Hu }
957b15b2e30SYue Hu 
95847e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
959eaa9172aSGao Xiang 				struct page *page, struct page **pagepool)
96047e4937aSGao Xiang {
96147e4937aSGao Xiang 	struct inode *const inode = fe->inode;
96247e4937aSGao Xiang 	struct erofs_map_blocks *const map = &fe->map;
96347e4937aSGao Xiang 	const loff_t offset = page_offset(page);
9645b220b20SGao Xiang 	bool tight = true, exclusive;
9652bfab9c0SGao Xiang 	unsigned int cur, end, spiltted;
96647e4937aSGao Xiang 	int err = 0;
96747e4937aSGao Xiang 
96847e4937aSGao Xiang 	/* register locked file pages as online pages in pack */
96947e4937aSGao Xiang 	z_erofs_onlinepage_init(page);
97047e4937aSGao Xiang 
97147e4937aSGao Xiang 	spiltted = 0;
97247e4937aSGao Xiang 	end = PAGE_SIZE;
97347e4937aSGao Xiang repeat:
97447e4937aSGao Xiang 	cur = end - 1;
97547e4937aSGao Xiang 
97639397a46SGao Xiang 	if (offset + cur < map->m_la ||
97739397a46SGao Xiang 	    offset + cur >= map->m_la + map->m_llen) {
9785c6dcc57SGao Xiang 		if (z_erofs_collector_end(fe))
97947e4937aSGao Xiang 			fe->backmost = false;
98047e4937aSGao Xiang 		map->m_la = offset + cur;
98147e4937aSGao Xiang 		map->m_llen = 0;
98247e4937aSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map, 0);
9838d8a09b0SGao Xiang 		if (err)
98467148551SGao Xiang 			goto out;
98539397a46SGao Xiang 	} else {
98639397a46SGao Xiang 		if (fe->pcl)
98739397a46SGao Xiang 			goto hitted;
98839397a46SGao Xiang 		/* didn't get a valid pcluster previously (very rare) */
98939397a46SGao Xiang 	}
99047e4937aSGao Xiang 
991b15b2e30SYue Hu 	if (!(map->m_flags & EROFS_MAP_MAPPED) ||
992b15b2e30SYue Hu 	    map->m_flags & EROFS_MAP_FRAGMENT)
99347e4937aSGao Xiang 		goto hitted;
99447e4937aSGao Xiang 
99583a386c0SGao Xiang 	err = z_erofs_collector_begin(fe);
9968d8a09b0SGao Xiang 	if (err)
99767148551SGao Xiang 		goto out;
99847e4937aSGao Xiang 
9995c6dcc57SGao Xiang 	if (z_erofs_is_inline_pcluster(fe->pcl)) {
100009c54379SGao Xiang 		void *mp;
1001cecf864dSYue Hu 
100209c54379SGao Xiang 		mp = erofs_read_metabuf(&fe->map.buf, inode->i_sb,
10033acea5fcSJingbo Xu 					erofs_blknr(inode->i_sb, map->m_pa),
10043acea5fcSJingbo Xu 					EROFS_NO_KMAP);
100509c54379SGao Xiang 		if (IS_ERR(mp)) {
100609c54379SGao Xiang 			err = PTR_ERR(mp);
1007cecf864dSYue Hu 			erofs_err(inode->i_sb,
1008cecf864dSYue Hu 				  "failed to get inline page, err %d", err);
100967148551SGao Xiang 			goto out;
1010cecf864dSYue Hu 		}
101109c54379SGao Xiang 		get_page(fe->map.buf.page);
1012ed722fbcSGao Xiang 		WRITE_ONCE(fe->pcl->compressed_bvecs[0].page,
1013ed722fbcSGao Xiang 			   fe->map.buf.page);
1014db166fc2SGao Xiang 		fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
1015cecf864dSYue Hu 	} else {
10166f39d1e1SGao Xiang 		/* bind cache first when cached decompression is preferred */
10171282dea3SGao Xiang 		z_erofs_bind_cache(fe, pagepool);
1018cecf864dSYue Hu 	}
101947e4937aSGao Xiang hitted:
1020dc76ea8cSGao Xiang 	/*
1021dc76ea8cSGao Xiang 	 * Ensure the current partial page belongs to this submit chain rather
1022dc76ea8cSGao Xiang 	 * than other concurrent submit chains or the noio(bypass) chain since
1023dc76ea8cSGao Xiang 	 * those chains are handled asynchronously thus the page cannot be used
1024387bab87SGao Xiang 	 * for inplace I/O or bvpage (should be processed in a strict order.)
1025dc76ea8cSGao Xiang 	 */
1026db166fc2SGao Xiang 	tight &= (fe->mode >= Z_EROFS_PCLUSTER_HOOKED &&
1027db166fc2SGao Xiang 		  fe->mode != Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE);
1028dc76ea8cSGao Xiang 
102947e4937aSGao Xiang 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
10308d8a09b0SGao Xiang 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
103147e4937aSGao Xiang 		zero_user_segment(page, cur, end);
103247e4937aSGao Xiang 		goto next_part;
103347e4937aSGao Xiang 	}
1034b15b2e30SYue Hu 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
1035b15b2e30SYue Hu 		unsigned int pageofs, skip, len;
1036b15b2e30SYue Hu 
1037b15b2e30SYue Hu 		if (offset > map->m_la) {
1038b15b2e30SYue Hu 			pageofs = 0;
1039b15b2e30SYue Hu 			skip = offset - map->m_la;
1040b15b2e30SYue Hu 		} else {
1041b15b2e30SYue Hu 			pageofs = map->m_la & ~PAGE_MASK;
1042b15b2e30SYue Hu 			skip = 0;
1043b15b2e30SYue Hu 		}
1044b15b2e30SYue Hu 		len = min_t(unsigned int, map->m_llen - skip, end - cur);
1045b15b2e30SYue Hu 		err = z_erofs_read_fragment(inode, skip, page, pageofs, len);
1046b15b2e30SYue Hu 		if (err)
1047b15b2e30SYue Hu 			goto out;
1048b15b2e30SYue Hu 		++spiltted;
1049b15b2e30SYue Hu 		tight = false;
1050b15b2e30SYue Hu 		goto next_part;
1051b15b2e30SYue Hu 	}
105247e4937aSGao Xiang 
10535b220b20SGao Xiang 	exclusive = (!cur && (!spiltted || tight));
105447e4937aSGao Xiang 	if (cur)
1055db166fc2SGao Xiang 		tight &= (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
105647e4937aSGao Xiang 
105747e4937aSGao Xiang retry:
105806a304cdSGao Xiang 	err = z_erofs_attach_page(fe, &((struct z_erofs_bvec) {
105906a304cdSGao Xiang 					.page = page,
106006a304cdSGao Xiang 					.offset = offset - map->m_la,
106106a304cdSGao Xiang 					.end = end,
10625b220b20SGao Xiang 				  }), exclusive);
106306a304cdSGao Xiang 	/* should allocate an additional short-lived page for bvset */
106406a304cdSGao Xiang 	if (err == -EAGAIN && !fe->candidate_bvpage) {
106506a304cdSGao Xiang 		fe->candidate_bvpage = alloc_page(GFP_NOFS | __GFP_NOFAIL);
106606a304cdSGao Xiang 		set_page_private(fe->candidate_bvpage,
106706a304cdSGao Xiang 				 Z_EROFS_SHORTLIVED_PAGE);
106847e4937aSGao Xiang 		goto retry;
106947e4937aSGao Xiang 	}
107047e4937aSGao Xiang 
107106a304cdSGao Xiang 	if (err) {
107206a304cdSGao Xiang 		DBG_BUGON(err == -EAGAIN && fe->candidate_bvpage);
107367148551SGao Xiang 		goto out;
107406a304cdSGao Xiang 	}
107547e4937aSGao Xiang 
107667148551SGao Xiang 	z_erofs_onlinepage_split(page);
107747e4937aSGao Xiang 	/* bump up the number of spiltted parts of a page */
107847e4937aSGao Xiang 	++spiltted;
1079267f2492SGao Xiang 	if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK))
1080267f2492SGao Xiang 		fe->pcl->multibases = true;
10812bfab9c0SGao Xiang 	if (fe->pcl->length < offset + end - map->m_la) {
10822bfab9c0SGao Xiang 		fe->pcl->length = offset + end - map->m_la;
10832bfab9c0SGao Xiang 		fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
10842bfab9c0SGao Xiang 	}
1085e7933278SGao Xiang 	if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1086e7933278SGao Xiang 	    !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1087e7933278SGao Xiang 	    fe->pcl->length == map->m_llen)
1088e7933278SGao Xiang 		fe->pcl->partial = false;
108947e4937aSGao Xiang next_part:
10902bfab9c0SGao Xiang 	/* shorten the remaining extent to update progress */
109147e4937aSGao Xiang 	map->m_llen = offset + cur - map->m_la;
10922bfab9c0SGao Xiang 	map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
109347e4937aSGao Xiang 
109447e4937aSGao Xiang 	end = cur;
109547e4937aSGao Xiang 	if (end > 0)
109647e4937aSGao Xiang 		goto repeat;
109747e4937aSGao Xiang 
109847e4937aSGao Xiang out:
109967148551SGao Xiang 	if (err)
110067148551SGao Xiang 		z_erofs_page_mark_eio(page);
110147e4937aSGao Xiang 	z_erofs_onlinepage_endio(page);
110247e4937aSGao Xiang 	return err;
110347e4937aSGao Xiang }
110447e4937aSGao Xiang 
1105ef4b4b46SYue Hu static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
110640452ffcSHuang Jianan 				       unsigned int readahead_pages)
110740452ffcSHuang Jianan {
1108a2e20a25SMatthew Wilcox (Oracle) 	/* auto: enable for read_folio, disable for readahead */
110940452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
111040452ffcSHuang Jianan 	    !readahead_pages)
111140452ffcSHuang Jianan 		return true;
111240452ffcSHuang Jianan 
111340452ffcSHuang Jianan 	if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
111440452ffcSHuang Jianan 	    (readahead_pages <= sbi->opt.max_sync_decompress_pages))
111540452ffcSHuang Jianan 		return true;
111640452ffcSHuang Jianan 
111740452ffcSHuang Jianan 	return false;
111840452ffcSHuang Jianan }
111940452ffcSHuang Jianan 
11206aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page)
11216aaa7b06SGao Xiang {
11226aaa7b06SGao Xiang 	return !page->mapping && !z_erofs_is_shortlived_page(page);
11236aaa7b06SGao Xiang }
11246aaa7b06SGao Xiang 
11254f05687fSGao Xiang struct z_erofs_decompress_backend {
11264f05687fSGao Xiang 	struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
11274f05687fSGao Xiang 	struct super_block *sb;
11284f05687fSGao Xiang 	struct z_erofs_pcluster *pcl;
11294f05687fSGao Xiang 
11304f05687fSGao Xiang 	/* pages with the longest decompressed length for deduplication */
11314f05687fSGao Xiang 	struct page **decompressed_pages;
11324f05687fSGao Xiang 	/* pages to keep the compressed data */
11334f05687fSGao Xiang 	struct page **compressed_pages;
11344f05687fSGao Xiang 
1135267f2492SGao Xiang 	struct list_head decompressed_secondary_bvecs;
11364f05687fSGao Xiang 	struct page **pagepool;
11372bfab9c0SGao Xiang 	unsigned int onstack_used, nr_pages;
11384f05687fSGao Xiang };
11394f05687fSGao Xiang 
1140267f2492SGao Xiang struct z_erofs_bvec_item {
1141267f2492SGao Xiang 	struct z_erofs_bvec bvec;
1142267f2492SGao Xiang 	struct list_head list;
1143267f2492SGao Xiang };
1144267f2492SGao Xiang 
1145267f2492SGao Xiang static void z_erofs_do_decompressed_bvec(struct z_erofs_decompress_backend *be,
11463fe96ee0SGao Xiang 					 struct z_erofs_bvec *bvec)
11473fe96ee0SGao Xiang {
1148267f2492SGao Xiang 	struct z_erofs_bvec_item *item;
1149267f2492SGao Xiang 
1150267f2492SGao Xiang 	if (!((bvec->offset + be->pcl->pageofs_out) & ~PAGE_MASK)) {
1151267f2492SGao Xiang 		unsigned int pgnr;
11523fe96ee0SGao Xiang 
1153267f2492SGao Xiang 		pgnr = (bvec->offset + be->pcl->pageofs_out) >> PAGE_SHIFT;
11542bfab9c0SGao Xiang 		DBG_BUGON(pgnr >= be->nr_pages);
115563bbb856SGao Xiang 		if (!be->decompressed_pages[pgnr]) {
11563fe96ee0SGao Xiang 			be->decompressed_pages[pgnr] = bvec->page;
1157267f2492SGao Xiang 			return;
11583fe96ee0SGao Xiang 		}
115963bbb856SGao Xiang 	}
11603fe96ee0SGao Xiang 
1161267f2492SGao Xiang 	/* (cold path) one pcluster is requested multiple times */
1162267f2492SGao Xiang 	item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1163267f2492SGao Xiang 	item->bvec = *bvec;
1164267f2492SGao Xiang 	list_add(&item->list, &be->decompressed_secondary_bvecs);
1165267f2492SGao Xiang }
1166267f2492SGao Xiang 
1167267f2492SGao Xiang static void z_erofs_fill_other_copies(struct z_erofs_decompress_backend *be,
1168267f2492SGao Xiang 				      int err)
1169267f2492SGao Xiang {
1170267f2492SGao Xiang 	unsigned int off0 = be->pcl->pageofs_out;
1171267f2492SGao Xiang 	struct list_head *p, *n;
1172267f2492SGao Xiang 
1173267f2492SGao Xiang 	list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1174267f2492SGao Xiang 		struct z_erofs_bvec_item *bvi;
1175267f2492SGao Xiang 		unsigned int end, cur;
1176267f2492SGao Xiang 		void *dst, *src;
1177267f2492SGao Xiang 
1178267f2492SGao Xiang 		bvi = container_of(p, struct z_erofs_bvec_item, list);
1179267f2492SGao Xiang 		cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1180267f2492SGao Xiang 		end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1181267f2492SGao Xiang 			    bvi->bvec.end);
1182267f2492SGao Xiang 		dst = kmap_local_page(bvi->bvec.page);
1183267f2492SGao Xiang 		while (cur < end) {
1184267f2492SGao Xiang 			unsigned int pgnr, scur, len;
1185267f2492SGao Xiang 
1186267f2492SGao Xiang 			pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1187267f2492SGao Xiang 			DBG_BUGON(pgnr >= be->nr_pages);
1188267f2492SGao Xiang 
1189267f2492SGao Xiang 			scur = bvi->bvec.offset + cur -
1190267f2492SGao Xiang 					((pgnr << PAGE_SHIFT) - off0);
1191267f2492SGao Xiang 			len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1192267f2492SGao Xiang 			if (!be->decompressed_pages[pgnr]) {
1193267f2492SGao Xiang 				err = -EFSCORRUPTED;
1194267f2492SGao Xiang 				cur += len;
1195267f2492SGao Xiang 				continue;
1196267f2492SGao Xiang 			}
1197267f2492SGao Xiang 			src = kmap_local_page(be->decompressed_pages[pgnr]);
1198267f2492SGao Xiang 			memcpy(dst + cur, src + scur, len);
1199267f2492SGao Xiang 			kunmap_local(src);
1200267f2492SGao Xiang 			cur += len;
1201267f2492SGao Xiang 		}
1202267f2492SGao Xiang 		kunmap_local(dst);
1203267f2492SGao Xiang 		if (err)
1204267f2492SGao Xiang 			z_erofs_page_mark_eio(bvi->bvec.page);
1205267f2492SGao Xiang 		z_erofs_onlinepage_endio(bvi->bvec.page);
1206267f2492SGao Xiang 		list_del(p);
1207267f2492SGao Xiang 		kfree(bvi);
1208267f2492SGao Xiang 	}
1209267f2492SGao Xiang }
1210267f2492SGao Xiang 
1211267f2492SGao Xiang static void z_erofs_parse_out_bvecs(struct z_erofs_decompress_backend *be)
121242fec235SGao Xiang {
12134f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
121406a304cdSGao Xiang 	struct z_erofs_bvec_iter biter;
121506a304cdSGao Xiang 	struct page *old_bvpage;
1216267f2492SGao Xiang 	int i;
121742fec235SGao Xiang 
1218387bab87SGao Xiang 	z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
121942fec235SGao Xiang 	for (i = 0; i < pcl->vcnt; ++i) {
122006a304cdSGao Xiang 		struct z_erofs_bvec bvec;
122142fec235SGao Xiang 
122206a304cdSGao Xiang 		z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
122342fec235SGao Xiang 
122406a304cdSGao Xiang 		if (old_bvpage)
12254f05687fSGao Xiang 			z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
122642fec235SGao Xiang 
122706a304cdSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1228267f2492SGao Xiang 		z_erofs_do_decompressed_bvec(be, &bvec);
122942fec235SGao Xiang 	}
123006a304cdSGao Xiang 
123106a304cdSGao Xiang 	old_bvpage = z_erofs_bvec_iter_end(&biter);
123206a304cdSGao Xiang 	if (old_bvpage)
12334f05687fSGao Xiang 		z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
123442fec235SGao Xiang }
123542fec235SGao Xiang 
12364f05687fSGao Xiang static int z_erofs_parse_in_bvecs(struct z_erofs_decompress_backend *be,
12374f05687fSGao Xiang 				  bool *overlapped)
123867139e36SGao Xiang {
12394f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
124067139e36SGao Xiang 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
124167139e36SGao Xiang 	int i, err = 0;
124267139e36SGao Xiang 
124367139e36SGao Xiang 	*overlapped = false;
124467139e36SGao Xiang 	for (i = 0; i < pclusterpages; ++i) {
1245ed722fbcSGao Xiang 		struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1246ed722fbcSGao Xiang 		struct page *page = bvec->page;
124767139e36SGao Xiang 
124867139e36SGao Xiang 		/* compressed pages ought to be present before decompressing */
124967139e36SGao Xiang 		if (!page) {
125067139e36SGao Xiang 			DBG_BUGON(1);
125167139e36SGao Xiang 			continue;
125267139e36SGao Xiang 		}
1253fe3e5914SGao Xiang 		be->compressed_pages[i] = page;
125467139e36SGao Xiang 
125567139e36SGao Xiang 		if (z_erofs_is_inline_pcluster(pcl)) {
125667139e36SGao Xiang 			if (!PageUptodate(page))
125767139e36SGao Xiang 				err = -EIO;
125867139e36SGao Xiang 			continue;
125967139e36SGao Xiang 		}
126067139e36SGao Xiang 
126167139e36SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
126267139e36SGao Xiang 		if (!z_erofs_is_shortlived_page(page)) {
12634f05687fSGao Xiang 			if (erofs_page_is_managed(EROFS_SB(be->sb), page)) {
126467139e36SGao Xiang 				if (!PageUptodate(page))
126567139e36SGao Xiang 					err = -EIO;
126667139e36SGao Xiang 				continue;
126767139e36SGao Xiang 			}
1268267f2492SGao Xiang 			z_erofs_do_decompressed_bvec(be, bvec);
126967139e36SGao Xiang 			*overlapped = true;
127067139e36SGao Xiang 		}
127167139e36SGao Xiang 	}
127267139e36SGao Xiang 
1273fe3e5914SGao Xiang 	if (err)
12744f05687fSGao Xiang 		return err;
12754f05687fSGao Xiang 	return 0;
127667139e36SGao Xiang }
127767139e36SGao Xiang 
12784f05687fSGao Xiang static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12794f05687fSGao Xiang 				       int err)
128047e4937aSGao Xiang {
12814f05687fSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
12824f05687fSGao Xiang 	struct z_erofs_pcluster *pcl = be->pcl;
1283cecf864dSYue Hu 	unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1284597e2953SYue Hu 	const struct z_erofs_decompressor *decompressor =
1285597e2953SYue Hu 				&erofs_decompressors[pcl->algorithmformat];
12862bfab9c0SGao Xiang 	unsigned int i, inputsize;
128767148551SGao Xiang 	int err2;
12882bfab9c0SGao Xiang 	struct page *page;
12892bfab9c0SGao Xiang 	bool overlapped;
129047e4937aSGao Xiang 
129187ca34a7SGao Xiang 	mutex_lock(&pcl->lock);
12922bfab9c0SGao Xiang 	be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
129347e4937aSGao Xiang 
1294fe3e5914SGao Xiang 	/* allocate (de)compressed page arrays if cannot be kept on stack */
1295fe3e5914SGao Xiang 	be->decompressed_pages = NULL;
1296fe3e5914SGao Xiang 	be->compressed_pages = NULL;
1297fe3e5914SGao Xiang 	be->onstack_used = 0;
12982bfab9c0SGao Xiang 	if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
12994f05687fSGao Xiang 		be->decompressed_pages = be->onstack_pages;
13002bfab9c0SGao Xiang 		be->onstack_used = be->nr_pages;
13014f05687fSGao Xiang 		memset(be->decompressed_pages, 0,
13022bfab9c0SGao Xiang 		       sizeof(struct page *) * be->nr_pages);
1303fe3e5914SGao Xiang 	}
1304fe3e5914SGao Xiang 
1305fe3e5914SGao Xiang 	if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1306fe3e5914SGao Xiang 		be->compressed_pages = be->onstack_pages + be->onstack_used;
1307fe3e5914SGao Xiang 
1308fe3e5914SGao Xiang 	if (!be->decompressed_pages)
13094f05687fSGao Xiang 		be->decompressed_pages =
1310647dd2c3SGao Xiang 			kvcalloc(be->nr_pages, sizeof(struct page *),
1311e7368187SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
1312fe3e5914SGao Xiang 	if (!be->compressed_pages)
1313fe3e5914SGao Xiang 		be->compressed_pages =
1314647dd2c3SGao Xiang 			kvcalloc(pclusterpages, sizeof(struct page *),
1315fe3e5914SGao Xiang 				 GFP_KERNEL | __GFP_NOFAIL);
131647e4937aSGao Xiang 
1317267f2492SGao Xiang 	z_erofs_parse_out_bvecs(be);
13184f05687fSGao Xiang 	err2 = z_erofs_parse_in_bvecs(be, &overlapped);
13194f05687fSGao Xiang 	if (err2)
13204f05687fSGao Xiang 		err = err2;
13218d8a09b0SGao Xiang 	if (err)
132247e4937aSGao Xiang 		goto out;
132347e4937aSGao Xiang 
1324cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl))
1325cecf864dSYue Hu 		inputsize = pcl->tailpacking_size;
1326cecf864dSYue Hu 	else
1327cecf864dSYue Hu 		inputsize = pclusterpages * PAGE_SIZE;
1328cecf864dSYue Hu 
1329597e2953SYue Hu 	err = decompressor->decompress(&(struct z_erofs_decompress_req) {
13304f05687fSGao Xiang 					.sb = be->sb,
13314f05687fSGao Xiang 					.in = be->compressed_pages,
13324f05687fSGao Xiang 					.out = be->decompressed_pages,
1333cecf864dSYue Hu 					.pageofs_in = pcl->pageofs_in,
133487ca34a7SGao Xiang 					.pageofs_out = pcl->pageofs_out,
13359f6cc76eSGao Xiang 					.inputsize = inputsize,
13362bfab9c0SGao Xiang 					.outputsize = pcl->length,
133747e4937aSGao Xiang 					.alg = pcl->algorithmformat,
133847e4937aSGao Xiang 					.inplace_io = overlapped,
13392bfab9c0SGao Xiang 					.partial_decoding = pcl->partial,
1340267f2492SGao Xiang 					.fillgaps = pcl->multibases,
13414f05687fSGao Xiang 				 }, be->pagepool);
134247e4937aSGao Xiang 
134347e4937aSGao Xiang out:
1344cecf864dSYue Hu 	/* must handle all compressed pages before actual file pages */
1345cecf864dSYue Hu 	if (z_erofs_is_inline_pcluster(pcl)) {
1346ed722fbcSGao Xiang 		page = pcl->compressed_bvecs[0].page;
1347ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1348cecf864dSYue Hu 		put_page(page);
1349cecf864dSYue Hu 	} else {
1350cecf864dSYue Hu 		for (i = 0; i < pclusterpages; ++i) {
1351ed722fbcSGao Xiang 			page = pcl->compressed_bvecs[i].page;
135247e4937aSGao Xiang 
135347e4937aSGao Xiang 			if (erofs_page_is_managed(sbi, page))
135447e4937aSGao Xiang 				continue;
135547e4937aSGao Xiang 
13566aaa7b06SGao Xiang 			/* recycle all individual short-lived pages */
13574f05687fSGao Xiang 			(void)z_erofs_put_shortlivedpage(be->pagepool, page);
1358ed722fbcSGao Xiang 			WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
135947e4937aSGao Xiang 		}
1360cecf864dSYue Hu 	}
1361fe3e5914SGao Xiang 	if (be->compressed_pages < be->onstack_pages ||
1362fe3e5914SGao Xiang 	    be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1363647dd2c3SGao Xiang 		kvfree(be->compressed_pages);
1364267f2492SGao Xiang 	z_erofs_fill_other_copies(be, err);
136547e4937aSGao Xiang 
13662bfab9c0SGao Xiang 	for (i = 0; i < be->nr_pages; ++i) {
13674f05687fSGao Xiang 		page = be->decompressed_pages[i];
136847e4937aSGao Xiang 		if (!page)
136947e4937aSGao Xiang 			continue;
137047e4937aSGao Xiang 
13716aaa7b06SGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
137247e4937aSGao Xiang 
13736aaa7b06SGao Xiang 		/* recycle all individual short-lived pages */
13744f05687fSGao Xiang 		if (z_erofs_put_shortlivedpage(be->pagepool, page))
137547e4937aSGao Xiang 			continue;
137667148551SGao Xiang 		if (err)
137767148551SGao Xiang 			z_erofs_page_mark_eio(page);
137847e4937aSGao Xiang 		z_erofs_onlinepage_endio(page);
137947e4937aSGao Xiang 	}
138047e4937aSGao Xiang 
13814f05687fSGao Xiang 	if (be->decompressed_pages != be->onstack_pages)
1382647dd2c3SGao Xiang 		kvfree(be->decompressed_pages);
138347e4937aSGao Xiang 
13842bfab9c0SGao Xiang 	pcl->length = 0;
13852bfab9c0SGao Xiang 	pcl->partial = true;
1386267f2492SGao Xiang 	pcl->multibases = false;
138706a304cdSGao Xiang 	pcl->bvset.nextpage = NULL;
138887ca34a7SGao Xiang 	pcl->vcnt = 0;
138947e4937aSGao Xiang 
139087ca34a7SGao Xiang 	/* pcluster lock MUST be taken before the following line */
139147e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
139287ca34a7SGao Xiang 	mutex_unlock(&pcl->lock);
139347e4937aSGao Xiang 	return err;
139447e4937aSGao Xiang }
139547e4937aSGao Xiang 
13960c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1397eaa9172aSGao Xiang 				     struct page **pagepool)
139847e4937aSGao Xiang {
13994f05687fSGao Xiang 	struct z_erofs_decompress_backend be = {
14004f05687fSGao Xiang 		.sb = io->sb,
14014f05687fSGao Xiang 		.pagepool = pagepool,
1402267f2492SGao Xiang 		.decompressed_secondary_bvecs =
1403267f2492SGao Xiang 			LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
14044f05687fSGao Xiang 	};
140547e4937aSGao Xiang 	z_erofs_next_pcluster_t owned = io->head;
140647e4937aSGao Xiang 
140747e4937aSGao Xiang 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
14084f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
140947e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
14104f05687fSGao Xiang 		/* impossible that 'owned' equals Z_EROFS_PCLUSTER_NIL */
141147e4937aSGao Xiang 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
141247e4937aSGao Xiang 
14134f05687fSGao Xiang 		be.pcl = container_of(owned, struct z_erofs_pcluster, next);
14144f05687fSGao Xiang 		owned = READ_ONCE(be.pcl->next);
141547e4937aSGao Xiang 
14164f05687fSGao Xiang 		z_erofs_decompress_pcluster(&be, io->eio ? -EIO : 0);
14174f05687fSGao Xiang 		erofs_workgroup_put(&be.pcl->obj);
141847e4937aSGao Xiang 	}
141947e4937aSGao Xiang }
142047e4937aSGao Xiang 
14210c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work)
142247e4937aSGao Xiang {
1423a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *bgq =
1424a4b1fab1SGao Xiang 		container_of(work, struct z_erofs_decompressqueue, u.work);
1425eaa9172aSGao Xiang 	struct page *pagepool = NULL;
142647e4937aSGao Xiang 
1427a4b1fab1SGao Xiang 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
14280c638f70SGao Xiang 	z_erofs_decompress_queue(bgq, &pagepool);
1429eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
1430a4b1fab1SGao Xiang 	kvfree(bgq);
143147e4937aSGao Xiang }
143247e4937aSGao Xiang 
14333fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14343fffb589SSandeep Dhavale static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
14353fffb589SSandeep Dhavale {
14363fffb589SSandeep Dhavale 	z_erofs_decompressqueue_work((struct work_struct *)work);
14373fffb589SSandeep Dhavale }
14383fffb589SSandeep Dhavale #endif
14393fffb589SSandeep Dhavale 
14407865827cSGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1441cdba5506SGao Xiang 				       int bios)
14427865827cSGao Xiang {
14437865827cSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
14447865827cSGao Xiang 
14457865827cSGao Xiang 	/* wake up the caller thread for sync decompression */
1446cdba5506SGao Xiang 	if (io->sync) {
14477865827cSGao Xiang 		if (!atomic_add_return(bios, &io->pending_bios))
144860b30050SHongyu Jin 			complete(&io->u.done);
14497865827cSGao Xiang 		return;
14507865827cSGao Xiang 	}
14517865827cSGao Xiang 
14527865827cSGao Xiang 	if (atomic_add_return(bios, &io->pending_bios))
14537865827cSGao Xiang 		return;
14543fffb589SSandeep Dhavale 	/* Use (kthread_)work and sync decompression for atomic contexts only */
14557865827cSGao Xiang 	if (in_atomic() || irqs_disabled()) {
14563fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
14573fffb589SSandeep Dhavale 		struct kthread_worker *worker;
14583fffb589SSandeep Dhavale 
14593fffb589SSandeep Dhavale 		rcu_read_lock();
14603fffb589SSandeep Dhavale 		worker = rcu_dereference(
14613fffb589SSandeep Dhavale 				z_erofs_pcpu_workers[raw_smp_processor_id()]);
14623fffb589SSandeep Dhavale 		if (!worker) {
14633fffb589SSandeep Dhavale 			INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
14647865827cSGao Xiang 			queue_work(z_erofs_workqueue, &io->u.work);
14653fffb589SSandeep Dhavale 		} else {
14663fffb589SSandeep Dhavale 			kthread_queue_work(worker, &io->u.kthread_work);
14673fffb589SSandeep Dhavale 		}
14683fffb589SSandeep Dhavale 		rcu_read_unlock();
14693fffb589SSandeep Dhavale #else
14703fffb589SSandeep Dhavale 		queue_work(z_erofs_workqueue, &io->u.work);
14713fffb589SSandeep Dhavale #endif
14727865827cSGao Xiang 		/* enable sync decompression for readahead */
14737865827cSGao Xiang 		if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
14747865827cSGao Xiang 			sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
14757865827cSGao Xiang 		return;
14767865827cSGao Xiang 	}
14777865827cSGao Xiang 	z_erofs_decompressqueue_work(&io->u.work);
14787865827cSGao Xiang }
14797865827cSGao Xiang 
148047e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
148147e4937aSGao Xiang 					       unsigned int nr,
1482eaa9172aSGao Xiang 					       struct page **pagepool,
14839f2731d6SGao Xiang 					       struct address_space *mc)
148447e4937aSGao Xiang {
148547e4937aSGao Xiang 	const pgoff_t index = pcl->obj.index;
14869f2731d6SGao Xiang 	gfp_t gfp = mapping_gfp_mask(mc);
148747e4937aSGao Xiang 	bool tocache = false;
148847e4937aSGao Xiang 
148947e4937aSGao Xiang 	struct address_space *mapping;
149047e4937aSGao Xiang 	struct page *oldpage, *page;
149147e4937aSGao Xiang 	int justfound;
149247e4937aSGao Xiang 
149347e4937aSGao Xiang repeat:
1494ed722fbcSGao Xiang 	page = READ_ONCE(pcl->compressed_bvecs[nr].page);
149547e4937aSGao Xiang 	oldpage = page;
149647e4937aSGao Xiang 
149747e4937aSGao Xiang 	if (!page)
149847e4937aSGao Xiang 		goto out_allocpage;
149947e4937aSGao Xiang 
1500b1ed220cSGao Xiang 	justfound = (unsigned long)page & 1UL;
1501b1ed220cSGao Xiang 	page = (struct page *)((unsigned long)page & ~1UL);
150247e4937aSGao Xiang 
15031825c8d7SGao Xiang 	/*
15041825c8d7SGao Xiang 	 * preallocated cached pages, which is used to avoid direct reclaim
15051825c8d7SGao Xiang 	 * otherwise, it will go inplace I/O path instead.
15061825c8d7SGao Xiang 	 */
15071825c8d7SGao Xiang 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1508ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
15091825c8d7SGao Xiang 		set_page_private(page, 0);
15101825c8d7SGao Xiang 		tocache = true;
15111825c8d7SGao Xiang 		goto out_tocache;
15121825c8d7SGao Xiang 	}
151347e4937aSGao Xiang 	mapping = READ_ONCE(page->mapping);
151447e4937aSGao Xiang 
151547e4937aSGao Xiang 	/*
15166aaa7b06SGao Xiang 	 * file-backed online pages in plcuster are all locked steady,
151747e4937aSGao Xiang 	 * therefore it is impossible for `mapping' to be NULL.
151847e4937aSGao Xiang 	 */
151947e4937aSGao Xiang 	if (mapping && mapping != mc)
152047e4937aSGao Xiang 		/* ought to be unmanaged pages */
152147e4937aSGao Xiang 		goto out;
152247e4937aSGao Xiang 
15236aaa7b06SGao Xiang 	/* directly return for shortlived page as well */
15246aaa7b06SGao Xiang 	if (z_erofs_is_shortlived_page(page))
15256aaa7b06SGao Xiang 		goto out;
15266aaa7b06SGao Xiang 
152747e4937aSGao Xiang 	lock_page(page);
152847e4937aSGao Xiang 
152947e4937aSGao Xiang 	/* only true if page reclaim goes wrong, should never happen */
153047e4937aSGao Xiang 	DBG_BUGON(justfound && PagePrivate(page));
153147e4937aSGao Xiang 
153247e4937aSGao Xiang 	/* the page is still in manage cache */
153347e4937aSGao Xiang 	if (page->mapping == mc) {
1534ed722fbcSGao Xiang 		WRITE_ONCE(pcl->compressed_bvecs[nr].page, page);
153547e4937aSGao Xiang 
153647e4937aSGao Xiang 		if (!PagePrivate(page)) {
153747e4937aSGao Xiang 			/*
153847e4937aSGao Xiang 			 * impossible to be !PagePrivate(page) for
153947e4937aSGao Xiang 			 * the current restriction as well if
1540ed722fbcSGao Xiang 			 * the page is already in compressed_bvecs[].
154147e4937aSGao Xiang 			 */
154247e4937aSGao Xiang 			DBG_BUGON(!justfound);
154347e4937aSGao Xiang 
154447e4937aSGao Xiang 			justfound = 0;
154547e4937aSGao Xiang 			set_page_private(page, (unsigned long)pcl);
154647e4937aSGao Xiang 			SetPagePrivate(page);
154747e4937aSGao Xiang 		}
154847e4937aSGao Xiang 
154947e4937aSGao Xiang 		/* no need to submit io if it is already up-to-date */
155047e4937aSGao Xiang 		if (PageUptodate(page)) {
155147e4937aSGao Xiang 			unlock_page(page);
155247e4937aSGao Xiang 			page = NULL;
155347e4937aSGao Xiang 		}
155447e4937aSGao Xiang 		goto out;
155547e4937aSGao Xiang 	}
155647e4937aSGao Xiang 
155747e4937aSGao Xiang 	/*
155847e4937aSGao Xiang 	 * the managed page has been truncated, it's unsafe to
155947e4937aSGao Xiang 	 * reuse this one, let's allocate a new cache-managed page.
156047e4937aSGao Xiang 	 */
156147e4937aSGao Xiang 	DBG_BUGON(page->mapping);
156247e4937aSGao Xiang 	DBG_BUGON(!justfound);
156347e4937aSGao Xiang 
156447e4937aSGao Xiang 	tocache = true;
156547e4937aSGao Xiang 	unlock_page(page);
156647e4937aSGao Xiang 	put_page(page);
156747e4937aSGao Xiang out_allocpage:
15685ddcee1fSGao Xiang 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1569ed722fbcSGao Xiang 	if (oldpage != cmpxchg(&pcl->compressed_bvecs[nr].page,
1570ed722fbcSGao Xiang 			       oldpage, page)) {
1571eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
15725ddcee1fSGao Xiang 		cond_resched();
15735ddcee1fSGao Xiang 		goto repeat;
15745ddcee1fSGao Xiang 	}
15751825c8d7SGao Xiang out_tocache:
1576bf225074SGao Xiang 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1577bf225074SGao Xiang 		/* turn into temporary page if fails (1 ref) */
1578bf225074SGao Xiang 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1579bf225074SGao Xiang 		goto out;
1580a30573b3SGao Xiang 	}
1581bf225074SGao Xiang 	attach_page_private(page, pcl);
1582bf225074SGao Xiang 	/* drop a refcount added by allocpage (then we have 2 refs here) */
1583bf225074SGao Xiang 	put_page(page);
1584bf225074SGao Xiang 
158547e4937aSGao Xiang out:	/* the only exit (for tracing and debugging) */
158647e4937aSGao Xiang 	return page;
158747e4937aSGao Xiang }
158847e4937aSGao Xiang 
1589cdba5506SGao Xiang static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1590a4b1fab1SGao Xiang 			      struct z_erofs_decompressqueue *fgq, bool *fg)
159147e4937aSGao Xiang {
1592a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q;
159347e4937aSGao Xiang 
1594a4b1fab1SGao Xiang 	if (fg && !*fg) {
1595a4b1fab1SGao Xiang 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1596a4b1fab1SGao Xiang 		if (!q) {
1597a4b1fab1SGao Xiang 			*fg = true;
1598a4b1fab1SGao Xiang 			goto fg_out;
159947e4937aSGao Xiang 		}
16003fffb589SSandeep Dhavale #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
16013fffb589SSandeep Dhavale 		kthread_init_work(&q->u.kthread_work,
16023fffb589SSandeep Dhavale 				  z_erofs_decompressqueue_kthread_work);
16033fffb589SSandeep Dhavale #else
16040c638f70SGao Xiang 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
16053fffb589SSandeep Dhavale #endif
1606a4b1fab1SGao Xiang 	} else {
1607a4b1fab1SGao Xiang fg_out:
1608a4b1fab1SGao Xiang 		q = fgq;
160960b30050SHongyu Jin 		init_completion(&fgq->u.done);
1610a4b1fab1SGao Xiang 		atomic_set(&fgq->pending_bios, 0);
161167148551SGao Xiang 		q->eio = false;
1612cdba5506SGao Xiang 		q->sync = true;
1613a4b1fab1SGao Xiang 	}
1614a4b1fab1SGao Xiang 	q->sb = sb;
1615a4b1fab1SGao Xiang 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1616a4b1fab1SGao Xiang 	return q;
161747e4937aSGao Xiang }
161847e4937aSGao Xiang 
161947e4937aSGao Xiang /* define decompression jobqueue types */
162047e4937aSGao Xiang enum {
162147e4937aSGao Xiang 	JQ_BYPASS,
162247e4937aSGao Xiang 	JQ_SUBMIT,
162347e4937aSGao Xiang 	NR_JOBQUEUES,
162447e4937aSGao Xiang };
162547e4937aSGao Xiang 
162647e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
162747e4937aSGao Xiang 				    z_erofs_next_pcluster_t qtail[],
162847e4937aSGao Xiang 				    z_erofs_next_pcluster_t owned_head)
162947e4937aSGao Xiang {
163047e4937aSGao Xiang 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
163147e4937aSGao Xiang 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
163247e4937aSGao Xiang 
163347e4937aSGao Xiang 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
163447e4937aSGao Xiang 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
163547e4937aSGao Xiang 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
163647e4937aSGao Xiang 
163747e4937aSGao Xiang 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
163847e4937aSGao Xiang 
163947e4937aSGao Xiang 	WRITE_ONCE(*submit_qtail, owned_head);
164047e4937aSGao Xiang 	WRITE_ONCE(*bypass_qtail, &pcl->next);
164147e4937aSGao Xiang 
164247e4937aSGao Xiang 	qtail[JQ_BYPASS] = &pcl->next;
164347e4937aSGao Xiang }
164447e4937aSGao Xiang 
16457865827cSGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio)
16467865827cSGao Xiang {
1647cdba5506SGao Xiang 	struct z_erofs_decompressqueue *q = bio->bi_private;
16487865827cSGao Xiang 	blk_status_t err = bio->bi_status;
16497865827cSGao Xiang 	struct bio_vec *bvec;
16507865827cSGao Xiang 	struct bvec_iter_all iter_all;
16517865827cSGao Xiang 
16527865827cSGao Xiang 	bio_for_each_segment_all(bvec, bio, iter_all) {
16537865827cSGao Xiang 		struct page *page = bvec->bv_page;
16547865827cSGao Xiang 
16557865827cSGao Xiang 		DBG_BUGON(PageUptodate(page));
16567865827cSGao Xiang 		DBG_BUGON(z_erofs_page_is_invalidated(page));
16577865827cSGao Xiang 
16587865827cSGao Xiang 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
16597865827cSGao Xiang 			if (!err)
16607865827cSGao Xiang 				SetPageUptodate(page);
16617865827cSGao Xiang 			unlock_page(page);
16627865827cSGao Xiang 		}
16637865827cSGao Xiang 	}
166467148551SGao Xiang 	if (err)
166567148551SGao Xiang 		q->eio = true;
1666cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q, -1);
16677865827cSGao Xiang 	bio_put(bio);
16687865827cSGao Xiang }
16697865827cSGao Xiang 
167083a386c0SGao Xiang static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,
1671eaa9172aSGao Xiang 				 struct page **pagepool,
1672a4b1fab1SGao Xiang 				 struct z_erofs_decompressqueue *fgq,
1673ef4b4b46SYue Hu 				 bool *force_fg, bool readahead)
167447e4937aSGao Xiang {
167583a386c0SGao Xiang 	struct super_block *sb = f->inode->i_sb;
167683a386c0SGao Xiang 	struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
167747e4937aSGao Xiang 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1678a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
16795c6dcc57SGao Xiang 	z_erofs_next_pcluster_t owned_head = f->owned_head;
1680dfeab2e9SGao Xiang 	/* bio is NULL initially, so no need to initialize last_{index,bdev} */
16813f649ab7SKees Cook 	pgoff_t last_index;
1682dfeab2e9SGao Xiang 	struct block_device *last_bdev;
16831e4a2955SGao Xiang 	unsigned int nr_bios = 0;
16841e4a2955SGao Xiang 	struct bio *bio = NULL;
168582e60d00SJohannes Weiner 	unsigned long pflags;
168682e60d00SJohannes Weiner 	int memstall = 0;
168747e4937aSGao Xiang 
1688cdba5506SGao Xiang 	/*
1689cdba5506SGao Xiang 	 * if managed cache is enabled, bypass jobqueue is needed,
1690cdba5506SGao Xiang 	 * no need to read from device for all pclusters in this queue.
1691cdba5506SGao Xiang 	 */
1692cdba5506SGao Xiang 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1693cdba5506SGao Xiang 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1694cdba5506SGao Xiang 
1695a4b1fab1SGao Xiang 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1696a4b1fab1SGao Xiang 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
169747e4937aSGao Xiang 
169847e4937aSGao Xiang 	/* by default, all need io submission */
169947e4937aSGao Xiang 	q[JQ_SUBMIT]->head = owned_head;
170047e4937aSGao Xiang 
170147e4937aSGao Xiang 	do {
1702dfeab2e9SGao Xiang 		struct erofs_map_dev mdev;
170347e4937aSGao Xiang 		struct z_erofs_pcluster *pcl;
17041e4a2955SGao Xiang 		pgoff_t cur, end;
17051e4a2955SGao Xiang 		unsigned int i = 0;
17061e4a2955SGao Xiang 		bool bypass = true;
170747e4937aSGao Xiang 
170847e4937aSGao Xiang 		/* no possible 'owned_head' equals the following */
170947e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
171047e4937aSGao Xiang 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
171147e4937aSGao Xiang 
171247e4937aSGao Xiang 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
171347e4937aSGao Xiang 
1714cecf864dSYue Hu 		/* close the main owned chain at first */
1715cecf864dSYue Hu 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1716cecf864dSYue Hu 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
1717cecf864dSYue Hu 		if (z_erofs_is_inline_pcluster(pcl)) {
1718cecf864dSYue Hu 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
1719cecf864dSYue Hu 			continue;
1720cecf864dSYue Hu 		}
1721cecf864dSYue Hu 
1722dfeab2e9SGao Xiang 		/* no device id here, thus it will always succeed */
1723dfeab2e9SGao Xiang 		mdev = (struct erofs_map_dev) {
17243acea5fcSJingbo Xu 			.m_pa = erofs_pos(sb, pcl->obj.index),
1725dfeab2e9SGao Xiang 		};
1726dfeab2e9SGao Xiang 		(void)erofs_map_dev(sb, &mdev);
1727dfeab2e9SGao Xiang 
17283acea5fcSJingbo Xu 		cur = erofs_blknr(sb, mdev.m_pa);
17299f6cc76eSGao Xiang 		end = cur + pcl->pclusterpages;
173047e4937aSGao Xiang 
17311e4a2955SGao Xiang 		do {
17321e4a2955SGao Xiang 			struct page *page;
173347e4937aSGao Xiang 
17341e4a2955SGao Xiang 			page = pickup_page_for_submission(pcl, i++, pagepool,
173583a386c0SGao Xiang 							  mc);
17361e4a2955SGao Xiang 			if (!page)
17371e4a2955SGao Xiang 				continue;
173847e4937aSGao Xiang 
1739dfeab2e9SGao Xiang 			if (bio && (cur != last_index + 1 ||
1740dfeab2e9SGao Xiang 				    last_bdev != mdev.m_bdev)) {
174147e4937aSGao Xiang submit_bio_retry:
174294e4e153SGao Xiang 				submit_bio(bio);
174382e60d00SJohannes Weiner 				if (memstall) {
174482e60d00SJohannes Weiner 					psi_memstall_leave(&pflags);
174582e60d00SJohannes Weiner 					memstall = 0;
174682e60d00SJohannes Weiner 				}
174747e4937aSGao Xiang 				bio = NULL;
174847e4937aSGao Xiang 			}
174947e4937aSGao Xiang 
175082e60d00SJohannes Weiner 			if (unlikely(PageWorkingset(page)) && !memstall) {
175199486c51SChristoph Hellwig 				psi_memstall_enter(&pflags);
175282e60d00SJohannes Weiner 				memstall = 1;
175382e60d00SJohannes Weiner 			}
175499486c51SChristoph Hellwig 
175547e4937aSGao Xiang 			if (!bio) {
175607888c66SChristoph Hellwig 				bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
175707888c66SChristoph Hellwig 						REQ_OP_READ, GFP_NOIO);
17580c638f70SGao Xiang 				bio->bi_end_io = z_erofs_decompressqueue_endio;
1759dfeab2e9SGao Xiang 
1760dfeab2e9SGao Xiang 				last_bdev = mdev.m_bdev;
17611e4a2955SGao Xiang 				bio->bi_iter.bi_sector = (sector_t)cur <<
17623acea5fcSJingbo Xu 					(sb->s_blocksize_bits - 9);
1763cdba5506SGao Xiang 				bio->bi_private = q[JQ_SUBMIT];
1764ef4b4b46SYue Hu 				if (readahead)
17656ea5aad3SGao Xiang 					bio->bi_opf |= REQ_RAHEAD;
176647e4937aSGao Xiang 				++nr_bios;
176747e4937aSGao Xiang 			}
176847e4937aSGao Xiang 
17696c3e485eSGao Xiang 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
177047e4937aSGao Xiang 				goto submit_bio_retry;
177147e4937aSGao Xiang 
17721e4a2955SGao Xiang 			last_index = cur;
17731e4a2955SGao Xiang 			bypass = false;
17741e4a2955SGao Xiang 		} while (++cur < end);
177547e4937aSGao Xiang 
17761e4a2955SGao Xiang 		if (!bypass)
177747e4937aSGao Xiang 			qtail[JQ_SUBMIT] = &pcl->next;
177847e4937aSGao Xiang 		else
177947e4937aSGao Xiang 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
178047e4937aSGao Xiang 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
178147e4937aSGao Xiang 
178299486c51SChristoph Hellwig 	if (bio) {
178394e4e153SGao Xiang 		submit_bio(bio);
178482e60d00SJohannes Weiner 		if (memstall)
178582e60d00SJohannes Weiner 			psi_memstall_leave(&pflags);
178699486c51SChristoph Hellwig 	}
178747e4937aSGao Xiang 
1788587a67b7SGao Xiang 	/*
1789587a67b7SGao Xiang 	 * although background is preferred, no one is pending for submission.
17903fffb589SSandeep Dhavale 	 * don't issue decompression but drop it directly instead.
1791587a67b7SGao Xiang 	 */
1792587a67b7SGao Xiang 	if (!*force_fg && !nr_bios) {
1793587a67b7SGao Xiang 		kvfree(q[JQ_SUBMIT]);
17941e4a2955SGao Xiang 		return;
1795587a67b7SGao Xiang 	}
1796cdba5506SGao Xiang 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
179747e4937aSGao Xiang }
179847e4937aSGao Xiang 
179983a386c0SGao Xiang static void z_erofs_runqueue(struct z_erofs_decompress_frontend *f,
1800ef4b4b46SYue Hu 			     struct page **pagepool, bool force_fg, bool ra)
180147e4937aSGao Xiang {
1802a4b1fab1SGao Xiang 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
180347e4937aSGao Xiang 
18045c6dcc57SGao Xiang 	if (f->owned_head == Z_EROFS_PCLUSTER_TAIL)
180547e4937aSGao Xiang 		return;
1806ef4b4b46SYue Hu 	z_erofs_submit_queue(f, pagepool, io, &force_fg, ra);
180747e4937aSGao Xiang 
18080c638f70SGao Xiang 	/* handle bypass queue (no i/o pclusters) immediately */
18090c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
181047e4937aSGao Xiang 
181147e4937aSGao Xiang 	if (!force_fg)
181247e4937aSGao Xiang 		return;
181347e4937aSGao Xiang 
181447e4937aSGao Xiang 	/* wait until all bios are completed */
181560b30050SHongyu Jin 	wait_for_completion_io(&io[JQ_SUBMIT].u.done);
181647e4937aSGao Xiang 
18170c638f70SGao Xiang 	/* handle synchronous decompress queue in the caller context */
18180c638f70SGao Xiang 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
181947e4937aSGao Xiang }
182047e4937aSGao Xiang 
182138629291SGao Xiang /*
182238629291SGao Xiang  * Since partial uptodate is still unimplemented for now, we have to use
182338629291SGao Xiang  * approximate readmore strategies as a start.
182438629291SGao Xiang  */
182538629291SGao Xiang static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,
182638629291SGao Xiang 				      struct readahead_control *rac,
1827*796e9149SYue Hu 				      struct page **pagepool, bool backmost)
182838629291SGao Xiang {
182938629291SGao Xiang 	struct inode *inode = f->inode;
183038629291SGao Xiang 	struct erofs_map_blocks *map = &f->map;
1831*796e9149SYue Hu 	erofs_off_t cur, end, headoffset = f->headoffset;
183238629291SGao Xiang 	int err;
183338629291SGao Xiang 
183438629291SGao Xiang 	if (backmost) {
1835*796e9149SYue Hu 		if (rac)
1836*796e9149SYue Hu 			end = headoffset + readahead_length(rac) - 1;
1837*796e9149SYue Hu 		else
1838*796e9149SYue Hu 			end = headoffset + PAGE_SIZE - 1;
183938629291SGao Xiang 		map->m_la = end;
1840622ceaddSGao Xiang 		err = z_erofs_map_blocks_iter(inode, map,
1841622ceaddSGao Xiang 					      EROFS_GET_BLOCKS_READMORE);
184238629291SGao Xiang 		if (err)
184338629291SGao Xiang 			return;
184438629291SGao Xiang 
1845*796e9149SYue Hu 		/* expand ra for the trailing edge if readahead */
184638629291SGao Xiang 		if (rac) {
184738629291SGao Xiang 			cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1848*796e9149SYue Hu 			readahead_expand(rac, headoffset, cur - headoffset);
184938629291SGao Xiang 			return;
185038629291SGao Xiang 		}
185138629291SGao Xiang 		end = round_up(end, PAGE_SIZE);
185238629291SGao Xiang 	} else {
185338629291SGao Xiang 		end = round_up(map->m_la, PAGE_SIZE);
185438629291SGao Xiang 
185538629291SGao Xiang 		if (!map->m_llen)
185638629291SGao Xiang 			return;
185738629291SGao Xiang 	}
185838629291SGao Xiang 
185938629291SGao Xiang 	cur = map->m_la + map->m_llen - 1;
186038629291SGao Xiang 	while (cur >= end) {
186138629291SGao Xiang 		pgoff_t index = cur >> PAGE_SHIFT;
186238629291SGao Xiang 		struct page *page;
186338629291SGao Xiang 
186438629291SGao Xiang 		page = erofs_grab_cache_page_nowait(inode->i_mapping, index);
1865aa793b46SGao Xiang 		if (page) {
186638629291SGao Xiang 			if (PageUptodate(page)) {
186738629291SGao Xiang 				unlock_page(page);
1868aa793b46SGao Xiang 			} else {
186938629291SGao Xiang 				err = z_erofs_do_read_page(f, page, pagepool);
187038629291SGao Xiang 				if (err)
187138629291SGao Xiang 					erofs_err(inode->i_sb,
187238629291SGao Xiang 						  "readmore error at page %lu @ nid %llu",
187338629291SGao Xiang 						  index, EROFS_I(inode)->nid);
1874aa793b46SGao Xiang 			}
187538629291SGao Xiang 			put_page(page);
1876aa793b46SGao Xiang 		}
1877aa793b46SGao Xiang 
187838629291SGao Xiang 		if (cur < PAGE_SIZE)
187938629291SGao Xiang 			break;
188038629291SGao Xiang 		cur = (index << PAGE_SHIFT) - 1;
188138629291SGao Xiang 	}
188238629291SGao Xiang }
188338629291SGao Xiang 
1884a2e20a25SMatthew Wilcox (Oracle) static int z_erofs_read_folio(struct file *file, struct folio *folio)
188547e4937aSGao Xiang {
1886a2e20a25SMatthew Wilcox (Oracle) 	struct page *page = &folio->page;
188747e4937aSGao Xiang 	struct inode *const inode = page->mapping->host;
188840452ffcSHuang Jianan 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
188947e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1890eaa9172aSGao Xiang 	struct page *pagepool = NULL;
189147e4937aSGao Xiang 	int err;
189247e4937aSGao Xiang 
189347e4937aSGao Xiang 	trace_erofs_readpage(page, false);
189447e4937aSGao Xiang 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
189547e4937aSGao Xiang 
1896*796e9149SYue Hu 	z_erofs_pcluster_readmore(&f, NULL, &pagepool, true);
18971825c8d7SGao Xiang 	err = z_erofs_do_read_page(&f, page, &pagepool);
1898*796e9149SYue Hu 	z_erofs_pcluster_readmore(&f, NULL, &pagepool, false);
189938629291SGao Xiang 
19005c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
190147e4937aSGao Xiang 
190247e4937aSGao Xiang 	/* if some compressed cluster ready, need submit them anyway */
1903ef4b4b46SYue Hu 	z_erofs_runqueue(&f, &pagepool, z_erofs_is_sync_decompress(sbi, 0),
1904ef4b4b46SYue Hu 			 false);
190547e4937aSGao Xiang 
190647e4937aSGao Xiang 	if (err)
19074f761fa2SGao Xiang 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
190847e4937aSGao Xiang 
190909c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1910eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
191147e4937aSGao Xiang 	return err;
191247e4937aSGao Xiang }
191347e4937aSGao Xiang 
19140615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac)
191547e4937aSGao Xiang {
19160615090cSMatthew Wilcox (Oracle) 	struct inode *const inode = rac->mapping->host;
191747e4937aSGao Xiang 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
191847e4937aSGao Xiang 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1919eaa9172aSGao Xiang 	struct page *pagepool = NULL, *head = NULL, *page;
192038629291SGao Xiang 	unsigned int nr_pages;
192147e4937aSGao Xiang 
19220615090cSMatthew Wilcox (Oracle) 	f.headoffset = readahead_pos(rac);
192347e4937aSGao Xiang 
1924*796e9149SYue Hu 	z_erofs_pcluster_readmore(&f, rac, &pagepool, true);
192538629291SGao Xiang 	nr_pages = readahead_count(rac);
192638629291SGao Xiang 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
192738629291SGao Xiang 
19280615090cSMatthew Wilcox (Oracle) 	while ((page = readahead_page(rac))) {
192947e4937aSGao Xiang 		set_page_private(page, (unsigned long)head);
193047e4937aSGao Xiang 		head = page;
193147e4937aSGao Xiang 	}
193247e4937aSGao Xiang 
193347e4937aSGao Xiang 	while (head) {
193447e4937aSGao Xiang 		struct page *page = head;
193547e4937aSGao Xiang 		int err;
193647e4937aSGao Xiang 
193747e4937aSGao Xiang 		/* traversal in reverse order */
193847e4937aSGao Xiang 		head = (void *)page_private(page);
193947e4937aSGao Xiang 
19401825c8d7SGao Xiang 		err = z_erofs_do_read_page(&f, page, &pagepool);
1941a5876e24SGao Xiang 		if (err)
19424f761fa2SGao Xiang 			erofs_err(inode->i_sb,
19434f761fa2SGao Xiang 				  "readahead error at page %lu @ nid %llu",
19444f761fa2SGao Xiang 				  page->index, EROFS_I(inode)->nid);
194547e4937aSGao Xiang 		put_page(page);
194647e4937aSGao Xiang 	}
1947*796e9149SYue Hu 	z_erofs_pcluster_readmore(&f, rac, &pagepool, false);
19485c6dcc57SGao Xiang 	(void)z_erofs_collector_end(&f);
194947e4937aSGao Xiang 
195083a386c0SGao Xiang 	z_erofs_runqueue(&f, &pagepool,
1951ef4b4b46SYue Hu 			 z_erofs_is_sync_decompress(sbi, nr_pages), true);
195209c54379SGao Xiang 	erofs_put_metabuf(&f.map.buf);
1953eaa9172aSGao Xiang 	erofs_release_pages(&pagepool);
195447e4937aSGao Xiang }
195547e4937aSGao Xiang 
19560c638f70SGao Xiang const struct address_space_operations z_erofs_aops = {
1957a2e20a25SMatthew Wilcox (Oracle) 	.read_folio = z_erofs_read_folio,
19580615090cSMatthew Wilcox (Oracle) 	.readahead = z_erofs_readahead,
195947e4937aSGao Xiang };
1960