1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
6 */
7 #include "zdata.h"
8 #include "compress.h"
9 #include <linux/prefetch.h>
10
11 #include <trace/events/erofs.h>
12
13 /*
14 * since pclustersize is variable for big pcluster feature, introduce slab
15 * pools implementation for different pcluster sizes.
16 */
17 struct z_erofs_pcluster_slab {
18 struct kmem_cache *slab;
19 unsigned int maxpages;
20 char name[48];
21 };
22
23 #define _PCLP(n) { .maxpages = n }
24
25 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
26 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
27 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
28 };
29
z_erofs_destroy_pcluster_pool(void)30 static void z_erofs_destroy_pcluster_pool(void)
31 {
32 int i;
33
34 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
35 if (!pcluster_pool[i].slab)
36 continue;
37 kmem_cache_destroy(pcluster_pool[i].slab);
38 pcluster_pool[i].slab = NULL;
39 }
40 }
41
z_erofs_create_pcluster_pool(void)42 static int z_erofs_create_pcluster_pool(void)
43 {
44 struct z_erofs_pcluster_slab *pcs;
45 struct z_erofs_pcluster *a;
46 unsigned int size;
47
48 for (pcs = pcluster_pool;
49 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
50 size = struct_size(a, compressed_pages, pcs->maxpages);
51
52 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
53 pcs->slab = kmem_cache_create(pcs->name, size, 0,
54 SLAB_RECLAIM_ACCOUNT, NULL);
55 if (pcs->slab)
56 continue;
57
58 z_erofs_destroy_pcluster_pool();
59 return -ENOMEM;
60 }
61 return 0;
62 }
63
z_erofs_alloc_pcluster(unsigned int nrpages)64 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
65 {
66 int i;
67
68 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
69 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
70 struct z_erofs_pcluster *pcl;
71
72 if (nrpages > pcs->maxpages)
73 continue;
74
75 pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
76 if (!pcl)
77 return ERR_PTR(-ENOMEM);
78 pcl->pclusterpages = nrpages;
79 return pcl;
80 }
81 return ERR_PTR(-EINVAL);
82 }
83
z_erofs_free_pcluster(struct z_erofs_pcluster * pcl)84 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
85 {
86 int i;
87
88 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
89 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
90
91 if (pcl->pclusterpages > pcs->maxpages)
92 continue;
93
94 kmem_cache_free(pcs->slab, pcl);
95 return;
96 }
97 DBG_BUGON(1);
98 }
99
100 /*
101 * a compressed_pages[] placeholder in order to avoid
102 * being filled with file pages for in-place decompression.
103 */
104 #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D)
105
106 /* how to allocate cached pages for a pcluster */
107 enum z_erofs_cache_alloctype {
108 DONTALLOC, /* don't allocate any cached pages */
109 DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */
110 /*
111 * try to use cached I/O if page allocation succeeds or fallback
112 * to in-place I/O instead to avoid any direct reclaim.
113 */
114 TRYALLOC,
115 };
116
117 /*
118 * tagged pointer with 1-bit tag for all compressed pages
119 * tag 0 - the page is just found with an extra page reference
120 */
121 typedef tagptr1_t compressed_page_t;
122
123 #define tag_compressed_page_justfound(page) \
124 tagptr_fold(compressed_page_t, page, 1)
125
126 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
127
z_erofs_exit_zip_subsystem(void)128 void z_erofs_exit_zip_subsystem(void)
129 {
130 destroy_workqueue(z_erofs_workqueue);
131 z_erofs_destroy_pcluster_pool();
132 }
133
z_erofs_init_workqueue(void)134 static inline int z_erofs_init_workqueue(void)
135 {
136 const unsigned int onlinecpus = num_possible_cpus();
137
138 /*
139 * no need to spawn too many threads, limiting threads could minimum
140 * scheduling overhead, perhaps per-CPU threads should be better?
141 */
142 z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
143 WQ_UNBOUND | WQ_HIGHPRI,
144 onlinecpus + onlinecpus / 4);
145 return z_erofs_workqueue ? 0 : -ENOMEM;
146 }
147
z_erofs_init_zip_subsystem(void)148 int __init z_erofs_init_zip_subsystem(void)
149 {
150 int err = z_erofs_create_pcluster_pool();
151
152 if (err)
153 return err;
154 err = z_erofs_init_workqueue();
155 if (err)
156 z_erofs_destroy_pcluster_pool();
157 return err;
158 }
159
160 enum z_erofs_collectmode {
161 COLLECT_SECONDARY,
162 COLLECT_PRIMARY,
163 /*
164 * The current collection was the tail of an exist chain, in addition
165 * that the previous processed chained collections are all decided to
166 * be hooked up to it.
167 * A new chain will be created for the remaining collections which are
168 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
169 * the next collection cannot reuse the whole page safely in
170 * the following scenario:
171 * ________________________________________________________________
172 * | tail (partial) page | head (partial) page |
173 * | (belongs to the next cl) | (belongs to the current cl) |
174 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
175 */
176 COLLECT_PRIMARY_HOOKED,
177 /*
178 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
179 * could be dispatched into bypass queue later due to uptodated managed
180 * pages. All related online pages cannot be reused for inplace I/O (or
181 * pagevec) since it can be directly decoded without I/O submission.
182 */
183 COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
184 /*
185 * The current collection has been linked with the owned chain, and
186 * could also be linked with the remaining collections, which means
187 * if the processing page is the tail page of the collection, thus
188 * the current collection can safely use the whole page (since
189 * the previous collection is under control) for in-place I/O, as
190 * illustrated below:
191 * ________________________________________________________________
192 * | tail (partial) page | head (partial) page |
193 * | (of the current cl) | (of the previous collection) |
194 * | PRIMARY_FOLLOWED or | |
195 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
196 *
197 * [ (*) the above page can be used as inplace I/O. ]
198 */
199 COLLECT_PRIMARY_FOLLOWED,
200 };
201
202 struct z_erofs_collector {
203 struct z_erofs_pagevec_ctor vector;
204
205 struct z_erofs_pcluster *pcl, *tailpcl;
206 struct z_erofs_collection *cl;
207 /* a pointer used to pick up inplace I/O pages */
208 struct page **icpage_ptr;
209 z_erofs_next_pcluster_t owned_head;
210
211 enum z_erofs_collectmode mode;
212 };
213
214 struct z_erofs_decompress_frontend {
215 struct inode *const inode;
216
217 struct z_erofs_collector clt;
218 struct erofs_map_blocks map;
219
220 bool readahead;
221 /* used for applying cache strategy on the fly */
222 bool backmost;
223 erofs_off_t headoffset;
224 };
225
226 #define COLLECTOR_INIT() { \
227 .owned_head = Z_EROFS_PCLUSTER_TAIL, \
228 .mode = COLLECT_PRIMARY_FOLLOWED }
229
230 #define DECOMPRESS_FRONTEND_INIT(__i) { \
231 .inode = __i, .clt = COLLECTOR_INIT(), \
232 .backmost = true, }
233
234 static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
235 static DEFINE_MUTEX(z_pagemap_global_lock);
236
preload_compressed_pages(struct z_erofs_collector * clt,struct address_space * mc,enum z_erofs_cache_alloctype type,struct list_head * pagepool)237 static void preload_compressed_pages(struct z_erofs_collector *clt,
238 struct address_space *mc,
239 enum z_erofs_cache_alloctype type,
240 struct list_head *pagepool)
241 {
242 struct z_erofs_pcluster *pcl = clt->pcl;
243 bool standalone = true;
244 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
245 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
246 struct page **pages;
247 pgoff_t index;
248
249 if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
250 return;
251
252 pages = pcl->compressed_pages;
253 index = pcl->obj.index;
254 for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
255 struct page *page;
256 compressed_page_t t;
257 struct page *newpage = NULL;
258
259 /* the compressed page was loaded before */
260 if (READ_ONCE(*pages))
261 continue;
262
263 page = find_get_page(mc, index);
264
265 if (page) {
266 t = tag_compressed_page_justfound(page);
267 } else {
268 /* I/O is needed, no possible to decompress directly */
269 standalone = false;
270 switch (type) {
271 case DELAYEDALLOC:
272 t = tagptr_init(compressed_page_t,
273 PAGE_UNALLOCATED);
274 break;
275 case TRYALLOC:
276 newpage = erofs_allocpage(pagepool, gfp);
277 if (!newpage)
278 continue;
279 set_page_private(newpage,
280 Z_EROFS_PREALLOCATED_PAGE);
281 t = tag_compressed_page_justfound(newpage);
282 break;
283 default: /* DONTALLOC */
284 continue;
285 }
286 }
287
288 if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
289 continue;
290
291 if (page) {
292 put_page(page);
293 } else if (newpage) {
294 set_page_private(newpage, 0);
295 list_add(&newpage->lru, pagepool);
296 }
297 }
298
299 /*
300 * don't do inplace I/O if all compressed pages are available in
301 * managed cache since it can be moved to the bypass queue instead.
302 */
303 if (standalone)
304 clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
305 }
306
307 /* called by erofs_shrinker to get rid of all compressed_pages */
erofs_try_to_free_all_cached_pages(struct erofs_sb_info * sbi,struct erofs_workgroup * grp)308 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
309 struct erofs_workgroup *grp)
310 {
311 struct z_erofs_pcluster *const pcl =
312 container_of(grp, struct z_erofs_pcluster, obj);
313 struct address_space *const mapping = MNGD_MAPPING(sbi);
314 int i;
315
316 /*
317 * refcount of workgroup is now freezed as 1,
318 * therefore no need to worry about available decompression users.
319 */
320 for (i = 0; i < pcl->pclusterpages; ++i) {
321 struct page *page = pcl->compressed_pages[i];
322
323 if (!page)
324 continue;
325
326 /* block other users from reclaiming or migrating the page */
327 if (!trylock_page(page))
328 return -EBUSY;
329
330 if (page->mapping != mapping)
331 continue;
332
333 /* barrier is implied in the following 'unlock_page' */
334 WRITE_ONCE(pcl->compressed_pages[i], NULL);
335 detach_page_private(page);
336 unlock_page(page);
337 }
338 return 0;
339 }
340
erofs_try_to_free_cached_page(struct address_space * mapping,struct page * page)341 int erofs_try_to_free_cached_page(struct address_space *mapping,
342 struct page *page)
343 {
344 struct z_erofs_pcluster *const pcl = (void *)page_private(page);
345 int ret = 0; /* 0 - busy */
346
347 if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
348 unsigned int i;
349
350 for (i = 0; i < pcl->pclusterpages; ++i) {
351 if (pcl->compressed_pages[i] == page) {
352 WRITE_ONCE(pcl->compressed_pages[i], NULL);
353 ret = 1;
354 break;
355 }
356 }
357 erofs_workgroup_unfreeze(&pcl->obj, 1);
358
359 if (ret)
360 detach_page_private(page);
361 }
362 return ret;
363 }
364
365 /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
z_erofs_try_inplace_io(struct z_erofs_collector * clt,struct page * page)366 static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
367 struct page *page)
368 {
369 struct z_erofs_pcluster *const pcl = clt->pcl;
370
371 while (clt->icpage_ptr > pcl->compressed_pages)
372 if (!cmpxchg(--clt->icpage_ptr, NULL, page))
373 return true;
374 return false;
375 }
376
377 /* callers must be with collection lock held */
z_erofs_attach_page(struct z_erofs_collector * clt,struct page * page,enum z_erofs_page_type type,bool pvec_safereuse)378 static int z_erofs_attach_page(struct z_erofs_collector *clt,
379 struct page *page, enum z_erofs_page_type type,
380 bool pvec_safereuse)
381 {
382 int ret;
383
384 /* give priority for inplaceio */
385 if (clt->mode >= COLLECT_PRIMARY &&
386 type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
387 z_erofs_try_inplace_io(clt, page))
388 return 0;
389
390 ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
391 pvec_safereuse);
392 clt->cl->vcnt += (unsigned int)ret;
393 return ret ? 0 : -EAGAIN;
394 }
395
396 static enum z_erofs_collectmode
try_to_claim_pcluster(struct z_erofs_pcluster * pcl,z_erofs_next_pcluster_t * owned_head)397 try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
398 z_erofs_next_pcluster_t *owned_head)
399 {
400 /* let's claim these following types of pclusters */
401 retry:
402 if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
403 /* type 1, nil pcluster */
404 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
405 *owned_head) != Z_EROFS_PCLUSTER_NIL)
406 goto retry;
407
408 *owned_head = &pcl->next;
409 /* lucky, I am the followee :) */
410 return COLLECT_PRIMARY_FOLLOWED;
411 } else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
412 /*
413 * type 2, link to the end of a existing open chain,
414 * be careful that its submission itself is governed
415 * by the original owned chain.
416 */
417 if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
418 *owned_head) != Z_EROFS_PCLUSTER_TAIL)
419 goto retry;
420 *owned_head = Z_EROFS_PCLUSTER_TAIL;
421 return COLLECT_PRIMARY_HOOKED;
422 }
423 return COLLECT_PRIMARY; /* :( better luck next time */
424 }
425
z_erofs_lookup_collection(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)426 static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
427 struct inode *inode,
428 struct erofs_map_blocks *map)
429 {
430 struct z_erofs_pcluster *pcl = clt->pcl;
431 struct z_erofs_collection *cl;
432 unsigned int length;
433
434 /* to avoid unexpected loop formed by corrupted images */
435 if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
436 DBG_BUGON(1);
437 return -EFSCORRUPTED;
438 }
439
440 cl = z_erofs_primarycollection(pcl);
441 if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
442 DBG_BUGON(1);
443 return -EFSCORRUPTED;
444 }
445
446 length = READ_ONCE(pcl->length);
447 if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
448 if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
449 DBG_BUGON(1);
450 return -EFSCORRUPTED;
451 }
452 } else {
453 unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
454
455 if (map->m_flags & EROFS_MAP_FULL_MAPPED)
456 llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
457
458 while (llen > length &&
459 length != cmpxchg_relaxed(&pcl->length, length, llen)) {
460 cpu_relax();
461 length = READ_ONCE(pcl->length);
462 }
463 }
464 mutex_lock(&cl->lock);
465 /* used to check tail merging loop due to corrupted images */
466 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
467 clt->tailpcl = pcl;
468 clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
469 /* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
470 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
471 clt->tailpcl = NULL;
472 clt->cl = cl;
473 return 0;
474 }
475
z_erofs_register_collection(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)476 static int z_erofs_register_collection(struct z_erofs_collector *clt,
477 struct inode *inode,
478 struct erofs_map_blocks *map)
479 {
480 struct z_erofs_pcluster *pcl;
481 struct z_erofs_collection *cl;
482 struct erofs_workgroup *grp;
483 int err;
484
485 /* no available pcluster, let's allocate one */
486 pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
487 if (IS_ERR(pcl))
488 return PTR_ERR(pcl);
489
490 atomic_set(&pcl->obj.refcount, 1);
491 pcl->obj.index = map->m_pa >> PAGE_SHIFT;
492
493 pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
494 (map->m_flags & EROFS_MAP_FULL_MAPPED ?
495 Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
496
497 if (map->m_flags & EROFS_MAP_ZIPPED)
498 pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
499 else
500 pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
501
502 /* new pclusters should be claimed as type 1, primary and followed */
503 pcl->next = clt->owned_head;
504 clt->mode = COLLECT_PRIMARY_FOLLOWED;
505
506 cl = z_erofs_primarycollection(pcl);
507 cl->pageofs = map->m_la & ~PAGE_MASK;
508
509 /*
510 * lock all primary followed works before visible to others
511 * and mutex_trylock *never* fails for a new pcluster.
512 */
513 mutex_init(&cl->lock);
514 DBG_BUGON(!mutex_trylock(&cl->lock));
515
516 grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
517 if (IS_ERR(grp)) {
518 err = PTR_ERR(grp);
519 goto err_out;
520 }
521
522 if (grp != &pcl->obj) {
523 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
524 err = -EEXIST;
525 goto err_out;
526 }
527 /* used to check tail merging loop due to corrupted images */
528 if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
529 clt->tailpcl = pcl;
530 clt->owned_head = &pcl->next;
531 clt->pcl = pcl;
532 clt->cl = cl;
533 return 0;
534
535 err_out:
536 mutex_unlock(&cl->lock);
537 z_erofs_free_pcluster(pcl);
538 return err;
539 }
540
z_erofs_collector_begin(struct z_erofs_collector * clt,struct inode * inode,struct erofs_map_blocks * map)541 static int z_erofs_collector_begin(struct z_erofs_collector *clt,
542 struct inode *inode,
543 struct erofs_map_blocks *map)
544 {
545 struct erofs_workgroup *grp;
546 int ret;
547
548 DBG_BUGON(clt->cl);
549
550 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
551 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
552 DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
553
554 if (!PAGE_ALIGNED(map->m_pa)) {
555 DBG_BUGON(1);
556 return -EINVAL;
557 }
558
559 grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
560 if (grp) {
561 clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
562 } else {
563 ret = z_erofs_register_collection(clt, inode, map);
564
565 if (!ret)
566 goto out;
567 if (ret != -EEXIST)
568 return ret;
569 }
570
571 ret = z_erofs_lookup_collection(clt, inode, map);
572 if (ret) {
573 erofs_workgroup_put(&clt->pcl->obj);
574 return ret;
575 }
576
577 out:
578 z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
579 clt->cl->pagevec, clt->cl->vcnt);
580
581 /* since file-backed online pages are traversed in reverse order */
582 clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
583 return 0;
584 }
585
586 /*
587 * keep in mind that no referenced pclusters will be freed
588 * only after a RCU grace period.
589 */
z_erofs_rcu_callback(struct rcu_head * head)590 static void z_erofs_rcu_callback(struct rcu_head *head)
591 {
592 struct z_erofs_collection *const cl =
593 container_of(head, struct z_erofs_collection, rcu);
594
595 z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
596 primary_collection));
597 }
598
erofs_workgroup_free_rcu(struct erofs_workgroup * grp)599 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
600 {
601 struct z_erofs_pcluster *const pcl =
602 container_of(grp, struct z_erofs_pcluster, obj);
603 struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
604
605 call_rcu(&cl->rcu, z_erofs_rcu_callback);
606 }
607
z_erofs_collection_put(struct z_erofs_collection * cl)608 static void z_erofs_collection_put(struct z_erofs_collection *cl)
609 {
610 struct z_erofs_pcluster *const pcl =
611 container_of(cl, struct z_erofs_pcluster, primary_collection);
612
613 erofs_workgroup_put(&pcl->obj);
614 }
615
z_erofs_collector_end(struct z_erofs_collector * clt)616 static bool z_erofs_collector_end(struct z_erofs_collector *clt)
617 {
618 struct z_erofs_collection *cl = clt->cl;
619
620 if (!cl)
621 return false;
622
623 z_erofs_pagevec_ctor_exit(&clt->vector, false);
624 mutex_unlock(&cl->lock);
625
626 /*
627 * if all pending pages are added, don't hold its reference
628 * any longer if the pcluster isn't hosted by ourselves.
629 */
630 if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
631 z_erofs_collection_put(cl);
632
633 clt->cl = NULL;
634 return true;
635 }
636
should_alloc_managed_pages(struct z_erofs_decompress_frontend * fe,unsigned int cachestrategy,erofs_off_t la)637 static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
638 unsigned int cachestrategy,
639 erofs_off_t la)
640 {
641 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
642 return false;
643
644 if (fe->backmost)
645 return true;
646
647 return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
648 la < fe->headoffset;
649 }
650
z_erofs_do_read_page(struct z_erofs_decompress_frontend * fe,struct page * page,struct list_head * pagepool)651 static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
652 struct page *page, struct list_head *pagepool)
653 {
654 struct inode *const inode = fe->inode;
655 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
656 struct erofs_map_blocks *const map = &fe->map;
657 struct z_erofs_collector *const clt = &fe->clt;
658 const loff_t offset = page_offset(page);
659 bool tight = true;
660
661 enum z_erofs_cache_alloctype cache_strategy;
662 enum z_erofs_page_type page_type;
663 unsigned int cur, end, spiltted, index;
664 int err = 0;
665
666 /* register locked file pages as online pages in pack */
667 z_erofs_onlinepage_init(page);
668
669 spiltted = 0;
670 end = PAGE_SIZE;
671 repeat:
672 cur = end - 1;
673
674 /* lucky, within the range of the current map_blocks */
675 if (offset + cur >= map->m_la &&
676 offset + cur < map->m_la + map->m_llen) {
677 /* didn't get a valid collection previously (very rare) */
678 if (!clt->cl)
679 goto restart_now;
680 goto hitted;
681 }
682
683 /* go ahead the next map_blocks */
684 erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
685
686 if (z_erofs_collector_end(clt))
687 fe->backmost = false;
688
689 map->m_la = offset + cur;
690 map->m_llen = 0;
691 err = z_erofs_map_blocks_iter(inode, map, 0);
692 if (err)
693 goto err_out;
694
695 restart_now:
696 if (!(map->m_flags & EROFS_MAP_MAPPED))
697 goto hitted;
698
699 err = z_erofs_collector_begin(clt, inode, map);
700 if (err)
701 goto err_out;
702
703 /* preload all compressed pages (maybe downgrade role if necessary) */
704 if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
705 cache_strategy = TRYALLOC;
706 else
707 cache_strategy = DONTALLOC;
708
709 preload_compressed_pages(clt, MNGD_MAPPING(sbi),
710 cache_strategy, pagepool);
711
712 hitted:
713 /*
714 * Ensure the current partial page belongs to this submit chain rather
715 * than other concurrent submit chains or the noio(bypass) chain since
716 * those chains are handled asynchronously thus the page cannot be used
717 * for inplace I/O or pagevec (should be processed in strict order.)
718 */
719 tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
720 clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
721
722 cur = end - min_t(unsigned int, offset + end - map->m_la, end);
723 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
724 zero_user_segment(page, cur, end);
725 goto next_part;
726 }
727
728 /* let's derive page type */
729 page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
730 (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
731 (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
732 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
733
734 if (cur)
735 tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
736
737 retry:
738 err = z_erofs_attach_page(clt, page, page_type,
739 clt->mode >= COLLECT_PRIMARY_FOLLOWED);
740 /* should allocate an additional staging page for pagevec */
741 if (err == -EAGAIN) {
742 struct page *const newpage =
743 alloc_page(GFP_NOFS | __GFP_NOFAIL);
744
745 set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
746 err = z_erofs_attach_page(clt, newpage,
747 Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
748 if (!err)
749 goto retry;
750 }
751
752 if (err)
753 goto err_out;
754
755 index = page->index - (map->m_la >> PAGE_SHIFT);
756
757 z_erofs_onlinepage_fixup(page, index, true);
758
759 /* bump up the number of spiltted parts of a page */
760 ++spiltted;
761 /* also update nr_pages */
762 clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
763 next_part:
764 /* can be used for verification */
765 map->m_llen = offset + cur - map->m_la;
766
767 end = cur;
768 if (end > 0)
769 goto repeat;
770
771 out:
772 z_erofs_onlinepage_endio(page);
773
774 erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
775 __func__, page, spiltted, map->m_llen);
776 return err;
777
778 /* if some error occurred while processing this page */
779 err_out:
780 SetPageError(page);
781 goto out;
782 }
783
784 static void z_erofs_decompressqueue_work(struct work_struct *work);
z_erofs_decompress_kickoff(struct z_erofs_decompressqueue * io,bool sync,int bios)785 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
786 bool sync, int bios)
787 {
788 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
789
790 /* wake up the caller thread for sync decompression */
791 if (sync) {
792 if (!atomic_add_return(bios, &io->pending_bios))
793 complete(&io->u.done);
794
795 return;
796 }
797
798 if (atomic_add_return(bios, &io->pending_bios))
799 return;
800 /* Use workqueue and sync decompression for atomic contexts only */
801 if (in_atomic() || irqs_disabled()) {
802 queue_work(z_erofs_workqueue, &io->u.work);
803 sbi->ctx.readahead_sync_decompress = true;
804 return;
805 }
806 z_erofs_decompressqueue_work(&io->u.work);
807 }
808
z_erofs_page_is_invalidated(struct page * page)809 static bool z_erofs_page_is_invalidated(struct page *page)
810 {
811 return !page->mapping && !z_erofs_is_shortlived_page(page);
812 }
813
z_erofs_decompressqueue_endio(struct bio * bio)814 static void z_erofs_decompressqueue_endio(struct bio *bio)
815 {
816 tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
817 struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
818 blk_status_t err = bio->bi_status;
819 struct bio_vec *bvec;
820 struct bvec_iter_all iter_all;
821
822 bio_for_each_segment_all(bvec, bio, iter_all) {
823 struct page *page = bvec->bv_page;
824
825 DBG_BUGON(PageUptodate(page));
826 DBG_BUGON(z_erofs_page_is_invalidated(page));
827
828 if (err)
829 SetPageError(page);
830
831 if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
832 if (!err)
833 SetPageUptodate(page);
834 unlock_page(page);
835 }
836 }
837 z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
838 bio_put(bio);
839 }
840
z_erofs_decompress_pcluster(struct super_block * sb,struct z_erofs_pcluster * pcl,struct list_head * pagepool)841 static int z_erofs_decompress_pcluster(struct super_block *sb,
842 struct z_erofs_pcluster *pcl,
843 struct list_head *pagepool)
844 {
845 struct erofs_sb_info *const sbi = EROFS_SB(sb);
846 struct z_erofs_pagevec_ctor ctor;
847 unsigned int i, inputsize, outputsize, llen, nr_pages;
848 struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
849 struct page **pages, **compressed_pages, *page;
850
851 enum z_erofs_page_type page_type;
852 bool overlapped, partial;
853 struct z_erofs_collection *cl;
854 int err;
855
856 might_sleep();
857 cl = z_erofs_primarycollection(pcl);
858 DBG_BUGON(!READ_ONCE(cl->nr_pages));
859
860 mutex_lock(&cl->lock);
861 nr_pages = cl->nr_pages;
862
863 if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
864 pages = pages_onstack;
865 } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
866 mutex_trylock(&z_pagemap_global_lock)) {
867 pages = z_pagemap_global;
868 } else {
869 gfp_t gfp_flags = GFP_KERNEL;
870
871 if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
872 gfp_flags |= __GFP_NOFAIL;
873
874 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
875 gfp_flags);
876
877 /* fallback to global pagemap for the lowmem scenario */
878 if (!pages) {
879 mutex_lock(&z_pagemap_global_lock);
880 pages = z_pagemap_global;
881 }
882 }
883
884 for (i = 0; i < nr_pages; ++i)
885 pages[i] = NULL;
886
887 err = 0;
888 z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
889 cl->pagevec, 0);
890
891 for (i = 0; i < cl->vcnt; ++i) {
892 unsigned int pagenr;
893
894 page = z_erofs_pagevec_dequeue(&ctor, &page_type);
895
896 /* all pages in pagevec ought to be valid */
897 DBG_BUGON(!page);
898 DBG_BUGON(z_erofs_page_is_invalidated(page));
899
900 if (z_erofs_put_shortlivedpage(pagepool, page))
901 continue;
902
903 if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
904 pagenr = 0;
905 else
906 pagenr = z_erofs_onlinepage_index(page);
907
908 DBG_BUGON(pagenr >= nr_pages);
909
910 /*
911 * currently EROFS doesn't support multiref(dedup),
912 * so here erroring out one multiref page.
913 */
914 if (pages[pagenr]) {
915 DBG_BUGON(1);
916 SetPageError(pages[pagenr]);
917 z_erofs_onlinepage_endio(pages[pagenr]);
918 err = -EFSCORRUPTED;
919 }
920 pages[pagenr] = page;
921 }
922 z_erofs_pagevec_ctor_exit(&ctor, true);
923
924 overlapped = false;
925 compressed_pages = pcl->compressed_pages;
926
927 for (i = 0; i < pcl->pclusterpages; ++i) {
928 unsigned int pagenr;
929
930 page = compressed_pages[i];
931
932 /* all compressed pages ought to be valid */
933 DBG_BUGON(!page);
934 DBG_BUGON(z_erofs_page_is_invalidated(page));
935
936 if (!z_erofs_is_shortlived_page(page)) {
937 if (erofs_page_is_managed(sbi, page)) {
938 if (!PageUptodate(page))
939 err = -EIO;
940 continue;
941 }
942
943 /*
944 * only if non-head page can be selected
945 * for inplace decompression
946 */
947 pagenr = z_erofs_onlinepage_index(page);
948
949 DBG_BUGON(pagenr >= nr_pages);
950 if (pages[pagenr]) {
951 DBG_BUGON(1);
952 SetPageError(pages[pagenr]);
953 z_erofs_onlinepage_endio(pages[pagenr]);
954 err = -EFSCORRUPTED;
955 }
956 pages[pagenr] = page;
957
958 overlapped = true;
959 }
960
961 /* PG_error needs checking for all non-managed pages */
962 if (PageError(page)) {
963 DBG_BUGON(PageUptodate(page));
964 err = -EIO;
965 }
966 }
967
968 if (err)
969 goto out;
970
971 llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
972 if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
973 outputsize = llen;
974 partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
975 } else {
976 outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
977 partial = true;
978 }
979
980 inputsize = pcl->pclusterpages * PAGE_SIZE;
981 err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
982 .sb = sb,
983 .in = compressed_pages,
984 .out = pages,
985 .pageofs_out = cl->pageofs,
986 .inputsize = inputsize,
987 .outputsize = outputsize,
988 .alg = pcl->algorithmformat,
989 .inplace_io = overlapped,
990 .partial_decoding = partial
991 }, pagepool);
992
993 out:
994 /* must handle all compressed pages before ending pages */
995 for (i = 0; i < pcl->pclusterpages; ++i) {
996 page = compressed_pages[i];
997
998 if (erofs_page_is_managed(sbi, page))
999 continue;
1000
1001 /* recycle all individual short-lived pages */
1002 (void)z_erofs_put_shortlivedpage(pagepool, page);
1003
1004 WRITE_ONCE(compressed_pages[i], NULL);
1005 }
1006
1007 for (i = 0; i < nr_pages; ++i) {
1008 page = pages[i];
1009 if (!page)
1010 continue;
1011
1012 DBG_BUGON(z_erofs_page_is_invalidated(page));
1013
1014 /* recycle all individual short-lived pages */
1015 if (z_erofs_put_shortlivedpage(pagepool, page))
1016 continue;
1017
1018 if (err < 0)
1019 SetPageError(page);
1020
1021 z_erofs_onlinepage_endio(page);
1022 }
1023
1024 if (pages == z_pagemap_global)
1025 mutex_unlock(&z_pagemap_global_lock);
1026 else if (pages != pages_onstack)
1027 kvfree(pages);
1028
1029 cl->nr_pages = 0;
1030 cl->vcnt = 0;
1031
1032 /* all cl locks MUST be taken before the following line */
1033 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
1034
1035 /* all cl locks SHOULD be released right now */
1036 mutex_unlock(&cl->lock);
1037
1038 z_erofs_collection_put(cl);
1039 return err;
1040 }
1041
z_erofs_decompress_queue(const struct z_erofs_decompressqueue * io,struct list_head * pagepool)1042 static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1043 struct list_head *pagepool)
1044 {
1045 z_erofs_next_pcluster_t owned = io->head;
1046
1047 while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
1048 struct z_erofs_pcluster *pcl;
1049
1050 /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
1051 DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
1052
1053 /* no possible that 'owned' equals NULL */
1054 DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
1055
1056 pcl = container_of(owned, struct z_erofs_pcluster, next);
1057 owned = READ_ONCE(pcl->next);
1058
1059 z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
1060 }
1061 }
1062
z_erofs_decompressqueue_work(struct work_struct * work)1063 static void z_erofs_decompressqueue_work(struct work_struct *work)
1064 {
1065 struct z_erofs_decompressqueue *bgq =
1066 container_of(work, struct z_erofs_decompressqueue, u.work);
1067 LIST_HEAD(pagepool);
1068
1069 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1070 z_erofs_decompress_queue(bgq, &pagepool);
1071
1072 put_pages_list(&pagepool);
1073 kvfree(bgq);
1074 }
1075
pickup_page_for_submission(struct z_erofs_pcluster * pcl,unsigned int nr,struct list_head * pagepool,struct address_space * mc,gfp_t gfp)1076 static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
1077 unsigned int nr,
1078 struct list_head *pagepool,
1079 struct address_space *mc,
1080 gfp_t gfp)
1081 {
1082 const pgoff_t index = pcl->obj.index;
1083 bool tocache = false;
1084
1085 struct address_space *mapping;
1086 struct page *oldpage, *page;
1087
1088 compressed_page_t t;
1089 int justfound;
1090
1091 repeat:
1092 page = READ_ONCE(pcl->compressed_pages[nr]);
1093 oldpage = page;
1094
1095 if (!page)
1096 goto out_allocpage;
1097
1098 /*
1099 * the cached page has not been allocated and
1100 * an placeholder is out there, prepare it now.
1101 */
1102 if (page == PAGE_UNALLOCATED) {
1103 tocache = true;
1104 goto out_allocpage;
1105 }
1106
1107 /* process the target tagged pointer */
1108 t = tagptr_init(compressed_page_t, page);
1109 justfound = tagptr_unfold_tags(t);
1110 page = tagptr_unfold_ptr(t);
1111
1112 /*
1113 * preallocated cached pages, which is used to avoid direct reclaim
1114 * otherwise, it will go inplace I/O path instead.
1115 */
1116 if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
1117 WRITE_ONCE(pcl->compressed_pages[nr], page);
1118 set_page_private(page, 0);
1119 tocache = true;
1120 goto out_tocache;
1121 }
1122 mapping = READ_ONCE(page->mapping);
1123
1124 /*
1125 * file-backed online pages in plcuster are all locked steady,
1126 * therefore it is impossible for `mapping' to be NULL.
1127 */
1128 if (mapping && mapping != mc)
1129 /* ought to be unmanaged pages */
1130 goto out;
1131
1132 /* directly return for shortlived page as well */
1133 if (z_erofs_is_shortlived_page(page))
1134 goto out;
1135
1136 lock_page(page);
1137
1138 /* only true if page reclaim goes wrong, should never happen */
1139 DBG_BUGON(justfound && PagePrivate(page));
1140
1141 /* the page is still in manage cache */
1142 if (page->mapping == mc) {
1143 WRITE_ONCE(pcl->compressed_pages[nr], page);
1144
1145 ClearPageError(page);
1146 if (!PagePrivate(page)) {
1147 /*
1148 * impossible to be !PagePrivate(page) for
1149 * the current restriction as well if
1150 * the page is already in compressed_pages[].
1151 */
1152 DBG_BUGON(!justfound);
1153
1154 justfound = 0;
1155 set_page_private(page, (unsigned long)pcl);
1156 SetPagePrivate(page);
1157 }
1158
1159 /* no need to submit io if it is already up-to-date */
1160 if (PageUptodate(page)) {
1161 unlock_page(page);
1162 page = NULL;
1163 }
1164 goto out;
1165 }
1166
1167 /*
1168 * the managed page has been truncated, it's unsafe to
1169 * reuse this one, let's allocate a new cache-managed page.
1170 */
1171 DBG_BUGON(page->mapping);
1172 DBG_BUGON(!justfound);
1173
1174 tocache = true;
1175 unlock_page(page);
1176 put_page(page);
1177 out_allocpage:
1178 page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
1179 if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
1180 list_add(&page->lru, pagepool);
1181 cond_resched();
1182 goto repeat;
1183 }
1184 out_tocache:
1185 if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
1186 /* turn into temporary page if fails (1 ref) */
1187 set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
1188 goto out;
1189 }
1190 attach_page_private(page, pcl);
1191 /* drop a refcount added by allocpage (then we have 2 refs here) */
1192 put_page(page);
1193
1194 out: /* the only exit (for tracing and debugging) */
1195 return page;
1196 }
1197
1198 static struct z_erofs_decompressqueue *
jobqueue_init(struct super_block * sb,struct z_erofs_decompressqueue * fgq,bool * fg)1199 jobqueue_init(struct super_block *sb,
1200 struct z_erofs_decompressqueue *fgq, bool *fg)
1201 {
1202 struct z_erofs_decompressqueue *q;
1203
1204 if (fg && !*fg) {
1205 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1206 if (!q) {
1207 *fg = true;
1208 goto fg_out;
1209 }
1210 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1211 } else {
1212 fg_out:
1213 q = fgq;
1214 init_completion(&fgq->u.done);
1215 atomic_set(&fgq->pending_bios, 0);
1216 }
1217 q->sb = sb;
1218 q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1219 return q;
1220 }
1221
1222 /* define decompression jobqueue types */
1223 enum {
1224 JQ_BYPASS,
1225 JQ_SUBMIT,
1226 NR_JOBQUEUES,
1227 };
1228
jobqueueset_init(struct super_block * sb,struct z_erofs_decompressqueue * q[],struct z_erofs_decompressqueue * fgq,bool * fg)1229 static void *jobqueueset_init(struct super_block *sb,
1230 struct z_erofs_decompressqueue *q[],
1231 struct z_erofs_decompressqueue *fgq, bool *fg)
1232 {
1233 /*
1234 * if managed cache is enabled, bypass jobqueue is needed,
1235 * no need to read from device for all pclusters in this queue.
1236 */
1237 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1238 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
1239
1240 return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
1241 }
1242
move_to_bypass_jobqueue(struct z_erofs_pcluster * pcl,z_erofs_next_pcluster_t qtail[],z_erofs_next_pcluster_t owned_head)1243 static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
1244 z_erofs_next_pcluster_t qtail[],
1245 z_erofs_next_pcluster_t owned_head)
1246 {
1247 z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
1248 z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
1249
1250 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1251 if (owned_head == Z_EROFS_PCLUSTER_TAIL)
1252 owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
1253
1254 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
1255
1256 WRITE_ONCE(*submit_qtail, owned_head);
1257 WRITE_ONCE(*bypass_qtail, &pcl->next);
1258
1259 qtail[JQ_BYPASS] = &pcl->next;
1260 }
1261
z_erofs_submit_queue(struct super_block * sb,struct z_erofs_decompress_frontend * f,struct list_head * pagepool,struct z_erofs_decompressqueue * fgq,bool * force_fg)1262 static void z_erofs_submit_queue(struct super_block *sb,
1263 struct z_erofs_decompress_frontend *f,
1264 struct list_head *pagepool,
1265 struct z_erofs_decompressqueue *fgq,
1266 bool *force_fg)
1267 {
1268 struct erofs_sb_info *const sbi = EROFS_SB(sb);
1269 z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
1270 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1271 void *bi_private;
1272 z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
1273 /* since bio will be NULL, no need to initialize last_index */
1274 pgoff_t last_index;
1275 unsigned int nr_bios = 0;
1276 struct bio *bio = NULL;
1277
1278 bi_private = jobqueueset_init(sb, q, fgq, force_fg);
1279 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1280 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1281
1282 /* by default, all need io submission */
1283 q[JQ_SUBMIT]->head = owned_head;
1284
1285 do {
1286 struct z_erofs_pcluster *pcl;
1287 pgoff_t cur, end;
1288 unsigned int i = 0;
1289 bool bypass = true;
1290
1291 /* no possible 'owned_head' equals the following */
1292 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
1293 DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
1294
1295 pcl = container_of(owned_head, struct z_erofs_pcluster, next);
1296
1297 cur = pcl->obj.index;
1298 end = cur + pcl->pclusterpages;
1299
1300 /* close the main owned chain at first */
1301 owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
1302 Z_EROFS_PCLUSTER_TAIL_CLOSED);
1303
1304 do {
1305 struct page *page;
1306
1307 page = pickup_page_for_submission(pcl, i++, pagepool,
1308 MNGD_MAPPING(sbi),
1309 GFP_NOFS);
1310 if (!page)
1311 continue;
1312
1313 if (bio && cur != last_index + 1) {
1314 submit_bio_retry:
1315 submit_bio(bio);
1316 bio = NULL;
1317 }
1318
1319 if (!bio) {
1320 bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
1321
1322 bio->bi_end_io = z_erofs_decompressqueue_endio;
1323 bio_set_dev(bio, sb->s_bdev);
1324 bio->bi_iter.bi_sector = (sector_t)cur <<
1325 LOG_SECTORS_PER_BLOCK;
1326 bio->bi_private = bi_private;
1327 bio->bi_opf = REQ_OP_READ;
1328 if (f->readahead)
1329 bio->bi_opf |= REQ_RAHEAD;
1330 ++nr_bios;
1331 }
1332
1333 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
1334 goto submit_bio_retry;
1335
1336 last_index = cur;
1337 bypass = false;
1338 } while (++cur < end);
1339
1340 if (!bypass)
1341 qtail[JQ_SUBMIT] = &pcl->next;
1342 else
1343 move_to_bypass_jobqueue(pcl, qtail, owned_head);
1344 } while (owned_head != Z_EROFS_PCLUSTER_TAIL);
1345
1346 if (bio)
1347 submit_bio(bio);
1348
1349 /*
1350 * although background is preferred, no one is pending for submission.
1351 * don't issue workqueue for decompression but drop it directly instead.
1352 */
1353 if (!*force_fg && !nr_bios) {
1354 kvfree(q[JQ_SUBMIT]);
1355 return;
1356 }
1357 z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
1358 }
1359
z_erofs_runqueue(struct super_block * sb,struct z_erofs_decompress_frontend * f,struct list_head * pagepool,bool force_fg)1360 static void z_erofs_runqueue(struct super_block *sb,
1361 struct z_erofs_decompress_frontend *f,
1362 struct list_head *pagepool, bool force_fg)
1363 {
1364 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1365
1366 if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
1367 return;
1368 z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
1369
1370 /* handle bypass queue (no i/o pclusters) immediately */
1371 z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
1372
1373 if (!force_fg)
1374 return;
1375
1376 /* wait until all bios are completed */
1377 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1378
1379 /* handle synchronous decompress queue in the caller context */
1380 z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
1381 }
1382
z_erofs_readpage(struct file * file,struct page * page)1383 static int z_erofs_readpage(struct file *file, struct page *page)
1384 {
1385 struct inode *const inode = page->mapping->host;
1386 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1387 int err;
1388 LIST_HEAD(pagepool);
1389
1390 trace_erofs_readpage(page, false);
1391
1392 f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
1393
1394 err = z_erofs_do_read_page(&f, page, &pagepool);
1395 (void)z_erofs_collector_end(&f.clt);
1396
1397 /* if some compressed cluster ready, need submit them anyway */
1398 z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
1399
1400 if (err)
1401 erofs_err(inode->i_sb, "failed to read, err [%d]", err);
1402
1403 if (f.map.mpage)
1404 put_page(f.map.mpage);
1405
1406 /* clean up the remaining free pages */
1407 put_pages_list(&pagepool);
1408 return err;
1409 }
1410
z_erofs_readahead(struct readahead_control * rac)1411 static void z_erofs_readahead(struct readahead_control *rac)
1412 {
1413 struct inode *const inode = rac->mapping->host;
1414 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
1415
1416 unsigned int nr_pages = readahead_count(rac);
1417 bool sync = (sbi->ctx.readahead_sync_decompress &&
1418 nr_pages <= sbi->ctx.max_sync_decompress_pages);
1419 struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
1420 struct page *page, *head = NULL;
1421 LIST_HEAD(pagepool);
1422
1423 trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
1424
1425 f.readahead = true;
1426 f.headoffset = readahead_pos(rac);
1427
1428 while ((page = readahead_page(rac))) {
1429 prefetchw(&page->flags);
1430
1431 /*
1432 * A pure asynchronous readahead is indicated if
1433 * a PG_readahead marked page is hitted at first.
1434 * Let's also do asynchronous decompression for this case.
1435 */
1436 sync &= !(PageReadahead(page) && !head);
1437
1438 set_page_private(page, (unsigned long)head);
1439 head = page;
1440 }
1441
1442 while (head) {
1443 struct page *page = head;
1444 int err;
1445
1446 /* traversal in reverse order */
1447 head = (void *)page_private(page);
1448
1449 err = z_erofs_do_read_page(&f, page, &pagepool);
1450 if (err)
1451 erofs_err(inode->i_sb,
1452 "readahead error at page %lu @ nid %llu",
1453 page->index, EROFS_I(inode)->nid);
1454 put_page(page);
1455 }
1456
1457 (void)z_erofs_collector_end(&f.clt);
1458
1459 z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
1460
1461 if (f.map.mpage)
1462 put_page(f.map.mpage);
1463
1464 /* clean up the remaining free pages */
1465 put_pages_list(&pagepool);
1466 }
1467
1468 const struct address_space_operations z_erofs_aops = {
1469 .readpage = z_erofs_readpage,
1470 .readahead = z_erofs_readahead,
1471 };
1472
1473