1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022 Alibaba Cloud
6 */
7 #include "compress.h"
8 #include <linux/psi.h>
9 #include <linux/cpuhotplug.h>
10 #include <trace/events/erofs.h>
11
12 #define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
13 #define Z_EROFS_INLINE_BVECS 2
14
15 struct z_erofs_bvec {
16 struct page *page;
17 int offset;
18 unsigned int end;
19 };
20
21 #define __Z_EROFS_BVSET(name, total) \
22 struct name { \
23 /* point to the next page which contains the following bvecs */ \
24 struct page *nextpage; \
25 struct z_erofs_bvec bvec[total]; \
26 }
27 __Z_EROFS_BVSET(z_erofs_bvset,);
28 __Z_EROFS_BVSET(z_erofs_bvset_inline, Z_EROFS_INLINE_BVECS);
29
30 /*
31 * Structure fields follow one of the following exclusion rules.
32 *
33 * I: Modifiable by initialization/destruction paths and read-only
34 * for everyone else;
35 *
36 * L: Field should be protected by the pcluster lock;
37 *
38 * A: Field should be accessed / updated in atomic for parallelized code.
39 */
40 struct z_erofs_pcluster {
41 struct mutex lock;
42 struct lockref lockref;
43
44 /* A: point to next chained pcluster or TAILs */
45 struct z_erofs_pcluster *next;
46
47 /* I: start block address of this pcluster */
48 erofs_off_t index;
49
50 /* L: the maximum decompression size of this round */
51 unsigned int length;
52
53 /* L: total number of bvecs */
54 unsigned int vcnt;
55
56 /* I: pcluster size (compressed size) in bytes */
57 unsigned int pclustersize;
58
59 /* I: page offset of start position of decompression */
60 unsigned short pageofs_out;
61
62 /* I: page offset of inline compressed data */
63 unsigned short pageofs_in;
64
65 union {
66 /* L: inline a certain number of bvec for bootstrap */
67 struct z_erofs_bvset_inline bvset;
68
69 /* I: can be used to free the pcluster by RCU. */
70 struct rcu_head rcu;
71 };
72
73 /* I: compression algorithm format */
74 unsigned char algorithmformat;
75
76 /* L: whether partial decompression or not */
77 bool partial;
78
79 /* L: whether extra buffer allocations are best-effort */
80 bool besteffort;
81
82 /* A: compressed bvecs (can be cached or inplaced pages) */
83 struct z_erofs_bvec compressed_bvecs[];
84 };
85
86 /* the end of a chain of pclusters */
87 #define Z_EROFS_PCLUSTER_TAIL ((void *) 0x700 + POISON_POINTER_DELTA)
88
89 struct z_erofs_decompressqueue {
90 struct super_block *sb;
91 struct z_erofs_pcluster *head;
92 atomic_t pending_bios;
93
94 union {
95 struct completion done;
96 struct work_struct work;
97 struct kthread_work kthread_work;
98 } u;
99 bool eio, sync;
100 };
101
z_erofs_is_inline_pcluster(struct z_erofs_pcluster * pcl)102 static inline bool z_erofs_is_inline_pcluster(struct z_erofs_pcluster *pcl)
103 {
104 return !pcl->index;
105 }
106
z_erofs_pclusterpages(struct z_erofs_pcluster * pcl)107 static inline unsigned int z_erofs_pclusterpages(struct z_erofs_pcluster *pcl)
108 {
109 return PAGE_ALIGN(pcl->pclustersize) >> PAGE_SHIFT;
110 }
111
112 #define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
erofs_folio_is_managed(struct erofs_sb_info * sbi,struct folio * fo)113 static bool erofs_folio_is_managed(struct erofs_sb_info *sbi, struct folio *fo)
114 {
115 return fo->mapping == MNGD_MAPPING(sbi);
116 }
117
118 #define Z_EROFS_ONSTACK_PAGES 32
119
120 /*
121 * since pclustersize is variable for big pcluster feature, introduce slab
122 * pools implementation for different pcluster sizes.
123 */
124 struct z_erofs_pcluster_slab {
125 struct kmem_cache *slab;
126 unsigned int maxpages;
127 char name[48];
128 };
129
130 #define _PCLP(n) { .maxpages = n }
131
132 static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
133 _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
134 _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
135 };
136
137 struct z_erofs_bvec_iter {
138 struct page *bvpage;
139 struct z_erofs_bvset *bvset;
140 unsigned int nr, cur;
141 };
142
z_erofs_bvec_iter_end(struct z_erofs_bvec_iter * iter)143 static struct page *z_erofs_bvec_iter_end(struct z_erofs_bvec_iter *iter)
144 {
145 if (iter->bvpage)
146 kunmap_local(iter->bvset);
147 return iter->bvpage;
148 }
149
z_erofs_bvset_flip(struct z_erofs_bvec_iter * iter)150 static struct page *z_erofs_bvset_flip(struct z_erofs_bvec_iter *iter)
151 {
152 unsigned long base = (unsigned long)((struct z_erofs_bvset *)0)->bvec;
153 /* have to access nextpage in advance, otherwise it will be unmapped */
154 struct page *nextpage = iter->bvset->nextpage;
155 struct page *oldpage;
156
157 DBG_BUGON(!nextpage);
158 oldpage = z_erofs_bvec_iter_end(iter);
159 iter->bvpage = nextpage;
160 iter->bvset = kmap_local_page(nextpage);
161 iter->nr = (PAGE_SIZE - base) / sizeof(struct z_erofs_bvec);
162 iter->cur = 0;
163 return oldpage;
164 }
165
z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter * iter,struct z_erofs_bvset_inline * bvset,unsigned int bootstrap_nr,unsigned int cur)166 static void z_erofs_bvec_iter_begin(struct z_erofs_bvec_iter *iter,
167 struct z_erofs_bvset_inline *bvset,
168 unsigned int bootstrap_nr,
169 unsigned int cur)
170 {
171 *iter = (struct z_erofs_bvec_iter) {
172 .nr = bootstrap_nr,
173 .bvset = (struct z_erofs_bvset *)bvset,
174 };
175
176 while (cur > iter->nr) {
177 cur -= iter->nr;
178 z_erofs_bvset_flip(iter);
179 }
180 iter->cur = cur;
181 }
182
z_erofs_bvec_enqueue(struct z_erofs_bvec_iter * iter,struct z_erofs_bvec * bvec,struct page ** candidate_bvpage,struct page ** pagepool)183 static int z_erofs_bvec_enqueue(struct z_erofs_bvec_iter *iter,
184 struct z_erofs_bvec *bvec,
185 struct page **candidate_bvpage,
186 struct page **pagepool)
187 {
188 if (iter->cur >= iter->nr) {
189 struct page *nextpage = *candidate_bvpage;
190
191 if (!nextpage) {
192 nextpage = __erofs_allocpage(pagepool, GFP_KERNEL,
193 true);
194 if (!nextpage)
195 return -ENOMEM;
196 set_page_private(nextpage, Z_EROFS_SHORTLIVED_PAGE);
197 }
198 DBG_BUGON(iter->bvset->nextpage);
199 iter->bvset->nextpage = nextpage;
200 z_erofs_bvset_flip(iter);
201
202 iter->bvset->nextpage = NULL;
203 *candidate_bvpage = NULL;
204 }
205 iter->bvset->bvec[iter->cur++] = *bvec;
206 return 0;
207 }
208
z_erofs_bvec_dequeue(struct z_erofs_bvec_iter * iter,struct z_erofs_bvec * bvec,struct page ** old_bvpage)209 static void z_erofs_bvec_dequeue(struct z_erofs_bvec_iter *iter,
210 struct z_erofs_bvec *bvec,
211 struct page **old_bvpage)
212 {
213 if (iter->cur == iter->nr)
214 *old_bvpage = z_erofs_bvset_flip(iter);
215 else
216 *old_bvpage = NULL;
217 *bvec = iter->bvset->bvec[iter->cur++];
218 }
219
z_erofs_destroy_pcluster_pool(void)220 static void z_erofs_destroy_pcluster_pool(void)
221 {
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
225 if (!pcluster_pool[i].slab)
226 continue;
227 kmem_cache_destroy(pcluster_pool[i].slab);
228 pcluster_pool[i].slab = NULL;
229 }
230 }
231
z_erofs_create_pcluster_pool(void)232 static int z_erofs_create_pcluster_pool(void)
233 {
234 struct z_erofs_pcluster_slab *pcs;
235 struct z_erofs_pcluster *a;
236 unsigned int size;
237
238 for (pcs = pcluster_pool;
239 pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
240 size = struct_size(a, compressed_bvecs, pcs->maxpages);
241
242 sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
243 pcs->slab = kmem_cache_create(pcs->name, size, 0,
244 SLAB_RECLAIM_ACCOUNT, NULL);
245 if (pcs->slab)
246 continue;
247
248 z_erofs_destroy_pcluster_pool();
249 return -ENOMEM;
250 }
251 return 0;
252 }
253
z_erofs_alloc_pcluster(unsigned int size)254 static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int size)
255 {
256 unsigned int nrpages = PAGE_ALIGN(size) >> PAGE_SHIFT;
257 struct z_erofs_pcluster_slab *pcs = pcluster_pool;
258
259 for (; pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
260 struct z_erofs_pcluster *pcl;
261
262 if (nrpages > pcs->maxpages)
263 continue;
264
265 pcl = kmem_cache_zalloc(pcs->slab, GFP_KERNEL);
266 if (!pcl)
267 return ERR_PTR(-ENOMEM);
268 pcl->pclustersize = size;
269 return pcl;
270 }
271 return ERR_PTR(-EINVAL);
272 }
273
z_erofs_free_pcluster(struct z_erofs_pcluster * pcl)274 static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
275 {
276 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
277 int i;
278
279 for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
280 struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
281
282 if (pclusterpages > pcs->maxpages)
283 continue;
284
285 kmem_cache_free(pcs->slab, pcl);
286 return;
287 }
288 DBG_BUGON(1);
289 }
290
291 static struct workqueue_struct *z_erofs_workqueue __read_mostly;
292
293 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
294 static struct kthread_worker __rcu **z_erofs_pcpu_workers;
295 static atomic_t erofs_percpu_workers_initialized = ATOMIC_INIT(0);
296
erofs_destroy_percpu_workers(void)297 static void erofs_destroy_percpu_workers(void)
298 {
299 struct kthread_worker *worker;
300 unsigned int cpu;
301
302 for_each_possible_cpu(cpu) {
303 worker = rcu_dereference_protected(
304 z_erofs_pcpu_workers[cpu], 1);
305 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
306 if (worker)
307 kthread_destroy_worker(worker);
308 }
309 kfree(z_erofs_pcpu_workers);
310 }
311
erofs_init_percpu_worker(int cpu)312 static struct kthread_worker *erofs_init_percpu_worker(int cpu)
313 {
314 struct kthread_worker *worker =
315 kthread_create_worker_on_cpu(cpu, 0, "erofs_worker/%u", cpu);
316
317 if (IS_ERR(worker))
318 return worker;
319 if (IS_ENABLED(CONFIG_EROFS_FS_PCPU_KTHREAD_HIPRI))
320 sched_set_fifo_low(worker->task);
321 return worker;
322 }
323
erofs_init_percpu_workers(void)324 static int erofs_init_percpu_workers(void)
325 {
326 struct kthread_worker *worker;
327 unsigned int cpu;
328
329 z_erofs_pcpu_workers = kcalloc(num_possible_cpus(),
330 sizeof(struct kthread_worker *), GFP_ATOMIC);
331 if (!z_erofs_pcpu_workers)
332 return -ENOMEM;
333
334 for_each_online_cpu(cpu) { /* could miss cpu{off,on}line? */
335 worker = erofs_init_percpu_worker(cpu);
336 if (!IS_ERR(worker))
337 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
338 }
339 return 0;
340 }
341
342 #ifdef CONFIG_HOTPLUG_CPU
343 static DEFINE_SPINLOCK(z_erofs_pcpu_worker_lock);
344 static enum cpuhp_state erofs_cpuhp_state;
345
erofs_cpu_online(unsigned int cpu)346 static int erofs_cpu_online(unsigned int cpu)
347 {
348 struct kthread_worker *worker, *old;
349
350 worker = erofs_init_percpu_worker(cpu);
351 if (IS_ERR(worker))
352 return PTR_ERR(worker);
353
354 spin_lock(&z_erofs_pcpu_worker_lock);
355 old = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
356 lockdep_is_held(&z_erofs_pcpu_worker_lock));
357 if (!old)
358 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], worker);
359 spin_unlock(&z_erofs_pcpu_worker_lock);
360 if (old)
361 kthread_destroy_worker(worker);
362 return 0;
363 }
364
erofs_cpu_offline(unsigned int cpu)365 static int erofs_cpu_offline(unsigned int cpu)
366 {
367 struct kthread_worker *worker;
368
369 spin_lock(&z_erofs_pcpu_worker_lock);
370 worker = rcu_dereference_protected(z_erofs_pcpu_workers[cpu],
371 lockdep_is_held(&z_erofs_pcpu_worker_lock));
372 rcu_assign_pointer(z_erofs_pcpu_workers[cpu], NULL);
373 spin_unlock(&z_erofs_pcpu_worker_lock);
374
375 synchronize_rcu();
376 if (worker)
377 kthread_destroy_worker(worker);
378 return 0;
379 }
380
erofs_cpu_hotplug_init(void)381 static int erofs_cpu_hotplug_init(void)
382 {
383 int state;
384
385 state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
386 "fs/erofs:online", erofs_cpu_online, erofs_cpu_offline);
387 if (state < 0)
388 return state;
389
390 erofs_cpuhp_state = state;
391 return 0;
392 }
393
erofs_cpu_hotplug_destroy(void)394 static void erofs_cpu_hotplug_destroy(void)
395 {
396 if (erofs_cpuhp_state)
397 cpuhp_remove_state_nocalls(erofs_cpuhp_state);
398 }
399 #else /* !CONFIG_HOTPLUG_CPU */
erofs_cpu_hotplug_init(void)400 static inline int erofs_cpu_hotplug_init(void) { return 0; }
erofs_cpu_hotplug_destroy(void)401 static inline void erofs_cpu_hotplug_destroy(void) {}
402 #endif/* CONFIG_HOTPLUG_CPU */
z_erofs_init_pcpu_workers(struct super_block * sb)403 static int z_erofs_init_pcpu_workers(struct super_block *sb)
404 {
405 int err;
406
407 if (atomic_xchg(&erofs_percpu_workers_initialized, 1))
408 return 0;
409
410 err = erofs_init_percpu_workers();
411 if (err) {
412 erofs_err(sb, "per-cpu workers: failed to allocate.");
413 goto err_init_percpu_workers;
414 }
415
416 err = erofs_cpu_hotplug_init();
417 if (err < 0) {
418 erofs_err(sb, "per-cpu workers: failed CPU hotplug init.");
419 goto err_cpuhp_init;
420 }
421 erofs_info(sb, "initialized per-cpu workers successfully.");
422 return err;
423
424 err_cpuhp_init:
425 erofs_destroy_percpu_workers();
426 err_init_percpu_workers:
427 atomic_set(&erofs_percpu_workers_initialized, 0);
428 return err;
429 }
430
z_erofs_destroy_pcpu_workers(void)431 static void z_erofs_destroy_pcpu_workers(void)
432 {
433 if (!atomic_xchg(&erofs_percpu_workers_initialized, 0))
434 return;
435 erofs_cpu_hotplug_destroy();
436 erofs_destroy_percpu_workers();
437 }
438 #else /* !CONFIG_EROFS_FS_PCPU_KTHREAD */
z_erofs_init_pcpu_workers(struct super_block * sb)439 static inline int z_erofs_init_pcpu_workers(struct super_block *sb) { return 0; }
z_erofs_destroy_pcpu_workers(void)440 static inline void z_erofs_destroy_pcpu_workers(void) {}
441 #endif/* CONFIG_EROFS_FS_PCPU_KTHREAD */
442
z_erofs_exit_subsystem(void)443 void z_erofs_exit_subsystem(void)
444 {
445 z_erofs_destroy_pcpu_workers();
446 destroy_workqueue(z_erofs_workqueue);
447 z_erofs_destroy_pcluster_pool();
448 z_erofs_exit_decompressor();
449 }
450
z_erofs_init_subsystem(void)451 int __init z_erofs_init_subsystem(void)
452 {
453 int err = z_erofs_init_decompressor();
454
455 if (err)
456 goto err_decompressor;
457
458 err = z_erofs_create_pcluster_pool();
459 if (err)
460 goto err_pcluster_pool;
461
462 z_erofs_workqueue = alloc_workqueue("erofs_worker",
463 WQ_UNBOUND | WQ_HIGHPRI, num_possible_cpus());
464 if (!z_erofs_workqueue) {
465 err = -ENOMEM;
466 goto err_workqueue_init;
467 }
468
469 return err;
470
471 err_workqueue_init:
472 z_erofs_destroy_pcluster_pool();
473 err_pcluster_pool:
474 z_erofs_exit_decompressor();
475 err_decompressor:
476 return err;
477 }
478
479 enum z_erofs_pclustermode {
480 /* It has previously been linked into another processing chain */
481 Z_EROFS_PCLUSTER_INFLIGHT,
482 /*
483 * A weaker form of Z_EROFS_PCLUSTER_FOLLOWED; the difference is that it
484 * may be dispatched to the bypass queue later due to uptodated managed
485 * folios. All file-backed folios related to this pcluster cannot be
486 * reused for in-place I/O (or bvpage) since the pcluster may be decoded
487 * in a separate queue (and thus out of order).
488 */
489 Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE,
490 /*
491 * The pcluster has just been linked to our processing chain.
492 * File-backed folios (except for the head page) related to it can be
493 * used for in-place I/O (or bvpage).
494 */
495 Z_EROFS_PCLUSTER_FOLLOWED,
496 };
497
498 struct z_erofs_frontend {
499 struct inode *const inode;
500 struct erofs_map_blocks map;
501 struct z_erofs_bvec_iter biter;
502
503 struct page *pagepool;
504 struct page *candidate_bvpage;
505 struct z_erofs_pcluster *pcl, *head;
506 enum z_erofs_pclustermode mode;
507
508 erofs_off_t headoffset;
509
510 /* a pointer used to pick up inplace I/O pages */
511 unsigned int icur;
512 };
513
514 #define Z_EROFS_DEFINE_FRONTEND(fe, i, ho) struct z_erofs_frontend fe = { \
515 .inode = i, .head = Z_EROFS_PCLUSTER_TAIL, \
516 .mode = Z_EROFS_PCLUSTER_FOLLOWED, .headoffset = ho }
517
z_erofs_should_alloc_cache(struct z_erofs_frontend * fe)518 static bool z_erofs_should_alloc_cache(struct z_erofs_frontend *fe)
519 {
520 unsigned int cachestrategy = EROFS_I_SB(fe->inode)->opt.cache_strategy;
521
522 if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
523 return false;
524
525 if (!(fe->map.m_flags & EROFS_MAP_FULL_MAPPED))
526 return true;
527
528 if (cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
529 fe->map.m_la < fe->headoffset)
530 return true;
531
532 return false;
533 }
534
z_erofs_bind_cache(struct z_erofs_frontend * fe)535 static void z_erofs_bind_cache(struct z_erofs_frontend *fe)
536 {
537 struct address_space *mc = MNGD_MAPPING(EROFS_I_SB(fe->inode));
538 struct z_erofs_pcluster *pcl = fe->pcl;
539 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
540 bool shouldalloc = z_erofs_should_alloc_cache(fe);
541 bool standalone = true;
542 /*
543 * optimistic allocation without direct reclaim since inplace I/O
544 * can be used if low memory otherwise.
545 */
546 gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
547 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
548 unsigned int i;
549
550 if (i_blocksize(fe->inode) != PAGE_SIZE ||
551 fe->mode < Z_EROFS_PCLUSTER_FOLLOWED)
552 return;
553
554 for (i = 0; i < pclusterpages; ++i) {
555 struct page *page, *newpage;
556
557 /* Inaccurate check w/o locking to avoid unneeded lookups */
558 if (READ_ONCE(pcl->compressed_bvecs[i].page))
559 continue;
560
561 page = find_get_page(mc, pcl->index + i);
562 if (!page) {
563 /* I/O is needed, no possible to decompress directly */
564 standalone = false;
565 if (!shouldalloc)
566 continue;
567
568 /*
569 * Try cached I/O if allocation succeeds or fallback to
570 * in-place I/O instead to avoid any direct reclaim.
571 */
572 newpage = erofs_allocpage(&fe->pagepool, gfp);
573 if (!newpage)
574 continue;
575 set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE);
576 }
577 spin_lock(&pcl->lockref.lock);
578 if (!pcl->compressed_bvecs[i].page) {
579 pcl->compressed_bvecs[i].page = page ? page : newpage;
580 spin_unlock(&pcl->lockref.lock);
581 continue;
582 }
583 spin_unlock(&pcl->lockref.lock);
584
585 if (page)
586 put_page(page);
587 else if (newpage)
588 erofs_pagepool_add(&fe->pagepool, newpage);
589 }
590
591 /*
592 * don't do inplace I/O if all compressed pages are available in
593 * managed cache since it can be moved to the bypass queue instead.
594 */
595 if (standalone)
596 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
597 }
598
599 /* (erofs_shrinker) disconnect cached encoded data with pclusters */
erofs_try_to_free_all_cached_folios(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)600 static int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
601 struct z_erofs_pcluster *pcl)
602 {
603 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
604 struct folio *folio;
605 int i;
606
607 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
608 /* Each cached folio contains one page unless bs > ps is supported */
609 for (i = 0; i < pclusterpages; ++i) {
610 if (pcl->compressed_bvecs[i].page) {
611 folio = page_folio(pcl->compressed_bvecs[i].page);
612 /* Avoid reclaiming or migrating this folio */
613 if (!folio_trylock(folio))
614 return -EBUSY;
615
616 if (!erofs_folio_is_managed(sbi, folio))
617 continue;
618 pcl->compressed_bvecs[i].page = NULL;
619 folio_detach_private(folio);
620 folio_unlock(folio);
621 }
622 }
623 return 0;
624 }
625
z_erofs_cache_release_folio(struct folio * folio,gfp_t gfp)626 static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)
627 {
628 struct z_erofs_pcluster *pcl = folio_get_private(folio);
629 struct z_erofs_bvec *bvec = pcl->compressed_bvecs;
630 struct z_erofs_bvec *end = bvec + z_erofs_pclusterpages(pcl);
631 bool ret;
632
633 if (!folio_test_private(folio))
634 return true;
635
636 ret = false;
637 spin_lock(&pcl->lockref.lock);
638 if (pcl->lockref.count <= 0) {
639 DBG_BUGON(z_erofs_is_inline_pcluster(pcl));
640 for (; bvec < end; ++bvec) {
641 if (bvec->page && page_folio(bvec->page) == folio) {
642 bvec->page = NULL;
643 folio_detach_private(folio);
644 ret = true;
645 break;
646 }
647 }
648 }
649 spin_unlock(&pcl->lockref.lock);
650 return ret;
651 }
652
653 /*
654 * It will be called only on inode eviction. In case that there are still some
655 * decompression requests in progress, wait with rescheduling for a bit here.
656 * An extra lock could be introduced instead but it seems unnecessary.
657 */
z_erofs_cache_invalidate_folio(struct folio * folio,size_t offset,size_t length)658 static void z_erofs_cache_invalidate_folio(struct folio *folio,
659 size_t offset, size_t length)
660 {
661 const size_t stop = length + offset;
662
663 /* Check for potential overflow in debug mode */
664 DBG_BUGON(stop > folio_size(folio) || stop < length);
665
666 if (offset == 0 && stop == folio_size(folio))
667 while (!z_erofs_cache_release_folio(folio, 0))
668 cond_resched();
669 }
670
671 static const struct address_space_operations z_erofs_cache_aops = {
672 .release_folio = z_erofs_cache_release_folio,
673 .invalidate_folio = z_erofs_cache_invalidate_folio,
674 };
675
z_erofs_init_super(struct super_block * sb)676 int z_erofs_init_super(struct super_block *sb)
677 {
678 struct inode *inode;
679 int err;
680
681 err = z_erofs_init_pcpu_workers(sb);
682 if (err)
683 return err;
684
685 inode = new_inode(sb);
686 if (!inode)
687 return -ENOMEM;
688 set_nlink(inode, 1);
689 inode->i_size = OFFSET_MAX;
690 inode->i_mapping->a_ops = &z_erofs_cache_aops;
691 mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL);
692 EROFS_SB(sb)->managed_cache = inode;
693 xa_init(&EROFS_SB(sb)->managed_pslots);
694 return 0;
695 }
696
697 /* callers must be with pcluster lock held */
z_erofs_attach_page(struct z_erofs_frontend * fe,struct z_erofs_bvec * bvec,bool exclusive)698 static int z_erofs_attach_page(struct z_erofs_frontend *fe,
699 struct z_erofs_bvec *bvec, bool exclusive)
700 {
701 struct z_erofs_pcluster *pcl = fe->pcl;
702 int ret;
703
704 if (exclusive) {
705 /* give priority for inplaceio to use file pages first */
706 spin_lock(&pcl->lockref.lock);
707 while (fe->icur > 0) {
708 if (pcl->compressed_bvecs[--fe->icur].page)
709 continue;
710 pcl->compressed_bvecs[fe->icur] = *bvec;
711 spin_unlock(&pcl->lockref.lock);
712 return 0;
713 }
714 spin_unlock(&pcl->lockref.lock);
715
716 /* otherwise, check if it can be used as a bvpage */
717 if (fe->mode >= Z_EROFS_PCLUSTER_FOLLOWED &&
718 !fe->candidate_bvpage)
719 fe->candidate_bvpage = bvec->page;
720 }
721 ret = z_erofs_bvec_enqueue(&fe->biter, bvec, &fe->candidate_bvpage,
722 &fe->pagepool);
723 fe->pcl->vcnt += (ret >= 0);
724 return ret;
725 }
726
z_erofs_get_pcluster(struct z_erofs_pcluster * pcl)727 static bool z_erofs_get_pcluster(struct z_erofs_pcluster *pcl)
728 {
729 if (lockref_get_not_zero(&pcl->lockref))
730 return true;
731
732 spin_lock(&pcl->lockref.lock);
733 if (__lockref_is_dead(&pcl->lockref)) {
734 spin_unlock(&pcl->lockref.lock);
735 return false;
736 }
737
738 if (!pcl->lockref.count++)
739 atomic_long_dec(&erofs_global_shrink_cnt);
740 spin_unlock(&pcl->lockref.lock);
741 return true;
742 }
743
z_erofs_register_pcluster(struct z_erofs_frontend * fe)744 static int z_erofs_register_pcluster(struct z_erofs_frontend *fe)
745 {
746 struct erofs_map_blocks *map = &fe->map;
747 struct super_block *sb = fe->inode->i_sb;
748 struct erofs_sb_info *sbi = EROFS_SB(sb);
749 bool ztailpacking = map->m_flags & EROFS_MAP_META;
750 struct z_erofs_pcluster *pcl, *pre;
751 int err;
752
753 if (!(map->m_flags & EROFS_MAP_ENCODED) ||
754 (!ztailpacking && !erofs_blknr(sb, map->m_pa))) {
755 DBG_BUGON(1);
756 return -EFSCORRUPTED;
757 }
758
759 /* no available pcluster, let's allocate one */
760 pcl = z_erofs_alloc_pcluster(map->m_plen);
761 if (IS_ERR(pcl))
762 return PTR_ERR(pcl);
763
764 spin_lock_init(&pcl->lockref.lock);
765 pcl->lockref.count = 1; /* one ref for this request */
766 pcl->algorithmformat = map->m_algorithmformat;
767 pcl->length = 0;
768 pcl->partial = true;
769 pcl->next = fe->head;
770 pcl->pageofs_out = map->m_la & ~PAGE_MASK;
771 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
772
773 /*
774 * lock all primary followed works before visible to others
775 * and mutex_trylock *never* fails for a new pcluster.
776 */
777 mutex_init(&pcl->lock);
778 DBG_BUGON(!mutex_trylock(&pcl->lock));
779
780 if (ztailpacking) {
781 pcl->index = 0; /* which indicates ztailpacking */
782 } else {
783 pcl->index = erofs_blknr(sb, map->m_pa);
784 while (1) {
785 xa_lock(&sbi->managed_pslots);
786 pre = __xa_cmpxchg(&sbi->managed_pslots, pcl->index,
787 NULL, pcl, GFP_KERNEL);
788 if (!pre || xa_is_err(pre) || z_erofs_get_pcluster(pre)) {
789 xa_unlock(&sbi->managed_pslots);
790 break;
791 }
792 /* try to legitimize the current in-tree one */
793 xa_unlock(&sbi->managed_pslots);
794 cond_resched();
795 }
796 if (xa_is_err(pre)) {
797 err = xa_err(pre);
798 goto err_out;
799 } else if (pre) {
800 fe->pcl = pre;
801 err = -EEXIST;
802 goto err_out;
803 }
804 }
805 fe->head = fe->pcl = pcl;
806 return 0;
807
808 err_out:
809 mutex_unlock(&pcl->lock);
810 z_erofs_free_pcluster(pcl);
811 return err;
812 }
813
z_erofs_pcluster_begin(struct z_erofs_frontend * fe)814 static int z_erofs_pcluster_begin(struct z_erofs_frontend *fe)
815 {
816 struct erofs_map_blocks *map = &fe->map;
817 struct super_block *sb = fe->inode->i_sb;
818 erofs_blk_t blknr = erofs_blknr(sb, map->m_pa);
819 struct z_erofs_pcluster *pcl = NULL;
820 int ret;
821
822 DBG_BUGON(fe->pcl);
823 /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous pcluster */
824 DBG_BUGON(!fe->head);
825
826 if (!(map->m_flags & EROFS_MAP_META)) {
827 while (1) {
828 rcu_read_lock();
829 pcl = xa_load(&EROFS_SB(sb)->managed_pslots, blknr);
830 if (!pcl || z_erofs_get_pcluster(pcl)) {
831 DBG_BUGON(pcl && blknr != pcl->index);
832 rcu_read_unlock();
833 break;
834 }
835 rcu_read_unlock();
836 }
837 } else if ((map->m_pa & ~PAGE_MASK) + map->m_plen > PAGE_SIZE) {
838 DBG_BUGON(1);
839 return -EFSCORRUPTED;
840 }
841
842 if (pcl) {
843 fe->pcl = pcl;
844 ret = -EEXIST;
845 } else {
846 ret = z_erofs_register_pcluster(fe);
847 }
848
849 if (ret == -EEXIST) {
850 mutex_lock(&fe->pcl->lock);
851 /* check if this pcluster hasn't been linked into any chain. */
852 if (!cmpxchg(&fe->pcl->next, NULL, fe->head)) {
853 /* .. so it can be attached to our submission chain */
854 fe->head = fe->pcl;
855 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED;
856 } else { /* otherwise, it belongs to an inflight chain */
857 fe->mode = Z_EROFS_PCLUSTER_INFLIGHT;
858 }
859 } else if (ret) {
860 return ret;
861 }
862
863 z_erofs_bvec_iter_begin(&fe->biter, &fe->pcl->bvset,
864 Z_EROFS_INLINE_BVECS, fe->pcl->vcnt);
865 if (!z_erofs_is_inline_pcluster(fe->pcl)) {
866 /* bind cache first when cached decompression is preferred */
867 z_erofs_bind_cache(fe);
868 } else {
869 void *mptr;
870
871 mptr = erofs_read_metabuf(&map->buf, sb, map->m_pa, EROFS_NO_KMAP);
872 if (IS_ERR(mptr)) {
873 ret = PTR_ERR(mptr);
874 erofs_err(sb, "failed to get inline data %d", ret);
875 return ret;
876 }
877 get_page(map->buf.page);
878 WRITE_ONCE(fe->pcl->compressed_bvecs[0].page, map->buf.page);
879 fe->pcl->pageofs_in = map->m_pa & ~PAGE_MASK;
880 fe->mode = Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE;
881 }
882 /* file-backed inplace I/O pages are traversed in reverse order */
883 fe->icur = z_erofs_pclusterpages(fe->pcl);
884 return 0;
885 }
886
z_erofs_rcu_callback(struct rcu_head * head)887 static void z_erofs_rcu_callback(struct rcu_head *head)
888 {
889 z_erofs_free_pcluster(container_of(head, struct z_erofs_pcluster, rcu));
890 }
891
__erofs_try_to_release_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)892 static bool __erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
893 struct z_erofs_pcluster *pcl)
894 {
895 if (pcl->lockref.count)
896 return false;
897
898 /*
899 * Note that all cached folios should be detached before deleted from
900 * the XArray. Otherwise some folios could be still attached to the
901 * orphan old pcluster when the new one is available in the tree.
902 */
903 if (erofs_try_to_free_all_cached_folios(sbi, pcl))
904 return false;
905
906 /*
907 * It's impossible to fail after the pcluster is freezed, but in order
908 * to avoid some race conditions, add a DBG_BUGON to observe this.
909 */
910 DBG_BUGON(__xa_erase(&sbi->managed_pslots, pcl->index) != pcl);
911
912 lockref_mark_dead(&pcl->lockref);
913 return true;
914 }
915
erofs_try_to_release_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl)916 static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
917 struct z_erofs_pcluster *pcl)
918 {
919 bool free;
920
921 spin_lock(&pcl->lockref.lock);
922 free = __erofs_try_to_release_pcluster(sbi, pcl);
923 spin_unlock(&pcl->lockref.lock);
924 if (free) {
925 atomic_long_dec(&erofs_global_shrink_cnt);
926 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
927 }
928 return free;
929 }
930
z_erofs_shrink_scan(struct erofs_sb_info * sbi,unsigned long nr)931 unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi, unsigned long nr)
932 {
933 struct z_erofs_pcluster *pcl;
934 unsigned long index, freed = 0;
935
936 xa_lock(&sbi->managed_pslots);
937 xa_for_each(&sbi->managed_pslots, index, pcl) {
938 /* try to shrink each valid pcluster */
939 if (!erofs_try_to_release_pcluster(sbi, pcl))
940 continue;
941 xa_unlock(&sbi->managed_pslots);
942
943 ++freed;
944 if (!--nr)
945 return freed;
946 xa_lock(&sbi->managed_pslots);
947 }
948 xa_unlock(&sbi->managed_pslots);
949 return freed;
950 }
951
z_erofs_put_pcluster(struct erofs_sb_info * sbi,struct z_erofs_pcluster * pcl,bool try_free)952 static void z_erofs_put_pcluster(struct erofs_sb_info *sbi,
953 struct z_erofs_pcluster *pcl, bool try_free)
954 {
955 bool free = false;
956
957 if (lockref_put_or_lock(&pcl->lockref))
958 return;
959
960 DBG_BUGON(__lockref_is_dead(&pcl->lockref));
961 if (!--pcl->lockref.count) {
962 if (try_free && xa_trylock(&sbi->managed_pslots)) {
963 free = __erofs_try_to_release_pcluster(sbi, pcl);
964 xa_unlock(&sbi->managed_pslots);
965 }
966 atomic_long_add(!free, &erofs_global_shrink_cnt);
967 }
968 spin_unlock(&pcl->lockref.lock);
969 if (free)
970 call_rcu(&pcl->rcu, z_erofs_rcu_callback);
971 }
972
z_erofs_pcluster_end(struct z_erofs_frontend * fe)973 static void z_erofs_pcluster_end(struct z_erofs_frontend *fe)
974 {
975 struct z_erofs_pcluster *pcl = fe->pcl;
976
977 if (!pcl)
978 return;
979
980 z_erofs_bvec_iter_end(&fe->biter);
981 mutex_unlock(&pcl->lock);
982
983 if (fe->candidate_bvpage)
984 fe->candidate_bvpage = NULL;
985
986 /* Drop refcount if it doesn't belong to our processing chain */
987 if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
988 z_erofs_put_pcluster(EROFS_I_SB(fe->inode), pcl, false);
989 fe->pcl = NULL;
990 }
991
z_erofs_read_fragment(struct super_block * sb,struct folio * folio,unsigned int cur,unsigned int end,erofs_off_t pos)992 static int z_erofs_read_fragment(struct super_block *sb, struct folio *folio,
993 unsigned int cur, unsigned int end, erofs_off_t pos)
994 {
995 struct inode *packed_inode = EROFS_SB(sb)->packed_inode;
996 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
997 unsigned int cnt;
998 u8 *src;
999
1000 if (!packed_inode)
1001 return -EFSCORRUPTED;
1002
1003 buf.mapping = packed_inode->i_mapping;
1004 for (; cur < end; cur += cnt, pos += cnt) {
1005 cnt = min(end - cur, sb->s_blocksize - erofs_blkoff(sb, pos));
1006 src = erofs_bread(&buf, pos, EROFS_KMAP);
1007 if (IS_ERR(src)) {
1008 erofs_put_metabuf(&buf);
1009 return PTR_ERR(src);
1010 }
1011 memcpy_to_folio(folio, cur, src, cnt);
1012 }
1013 erofs_put_metabuf(&buf);
1014 return 0;
1015 }
1016
z_erofs_scan_folio(struct z_erofs_frontend * f,struct folio * folio,bool ra)1017 static int z_erofs_scan_folio(struct z_erofs_frontend *f,
1018 struct folio *folio, bool ra)
1019 {
1020 struct inode *const inode = f->inode;
1021 struct erofs_map_blocks *const map = &f->map;
1022 const loff_t offset = folio_pos(folio);
1023 const unsigned int bs = i_blocksize(inode);
1024 unsigned int end = folio_size(folio), split = 0, cur, pgs;
1025 bool tight, excl;
1026 int err = 0;
1027
1028 tight = (bs == PAGE_SIZE);
1029 erofs_onlinefolio_init(folio);
1030 do {
1031 if (offset + end - 1 < map->m_la ||
1032 offset + end - 1 >= map->m_la + map->m_llen) {
1033 z_erofs_pcluster_end(f);
1034 map->m_la = offset + end - 1;
1035 map->m_llen = 0;
1036 err = z_erofs_map_blocks_iter(inode, map, 0);
1037 if (err)
1038 break;
1039 }
1040
1041 cur = offset > map->m_la ? 0 : map->m_la - offset;
1042 pgs = round_down(cur, PAGE_SIZE);
1043 /* bump split parts first to avoid several separate cases */
1044 ++split;
1045
1046 if (!(map->m_flags & EROFS_MAP_MAPPED)) {
1047 folio_zero_segment(folio, cur, end);
1048 tight = false;
1049 } else if (map->m_flags & __EROFS_MAP_FRAGMENT) {
1050 erofs_off_t fpos = offset + cur - map->m_la;
1051
1052 err = z_erofs_read_fragment(inode->i_sb, folio, cur,
1053 cur + min(map->m_llen - fpos, end - cur),
1054 EROFS_I(inode)->z_fragmentoff + fpos);
1055 if (err)
1056 break;
1057 tight = false;
1058 } else {
1059 if (!f->pcl) {
1060 err = z_erofs_pcluster_begin(f);
1061 if (err)
1062 break;
1063 f->pcl->besteffort |= !ra;
1064 }
1065
1066 pgs = round_down(end - 1, PAGE_SIZE);
1067 /*
1068 * Ensure this partial page belongs to this submit chain
1069 * rather than other concurrent submit chains or
1070 * noio(bypass) chains since those chains are handled
1071 * asynchronously thus it cannot be used for inplace I/O
1072 * or bvpage (should be processed in the strict order.)
1073 */
1074 tight &= (f->mode >= Z_EROFS_PCLUSTER_FOLLOWED);
1075 excl = false;
1076 if (cur <= pgs) {
1077 excl = (split <= 1) || tight;
1078 cur = pgs;
1079 }
1080
1081 err = z_erofs_attach_page(f, &((struct z_erofs_bvec) {
1082 .page = folio_page(folio, pgs >> PAGE_SHIFT),
1083 .offset = offset + pgs - map->m_la,
1084 .end = end - pgs, }), excl);
1085 if (err)
1086 break;
1087
1088 erofs_onlinefolio_split(folio);
1089 if (f->pcl->length < offset + end - map->m_la) {
1090 f->pcl->length = offset + end - map->m_la;
1091 f->pcl->pageofs_out = map->m_la & ~PAGE_MASK;
1092 }
1093 if ((map->m_flags & EROFS_MAP_FULL_MAPPED) &&
1094 !(map->m_flags & EROFS_MAP_PARTIAL_REF) &&
1095 f->pcl->length == map->m_llen)
1096 f->pcl->partial = false;
1097 }
1098 /* shorten the remaining extent to update progress */
1099 map->m_llen = offset + cur - map->m_la;
1100 map->m_flags &= ~EROFS_MAP_FULL_MAPPED;
1101 if (cur <= pgs) {
1102 split = cur < pgs;
1103 tight = (bs == PAGE_SIZE);
1104 }
1105 } while ((end = cur) > 0);
1106 erofs_onlinefolio_end(folio, err, false);
1107 return err;
1108 }
1109
z_erofs_is_sync_decompress(struct erofs_sb_info * sbi,unsigned int readahead_pages)1110 static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,
1111 unsigned int readahead_pages)
1112 {
1113 /* auto: enable for read_folio, disable for readahead */
1114 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO) &&
1115 !readahead_pages)
1116 return true;
1117
1118 if ((sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_FORCE_ON) &&
1119 (readahead_pages <= sbi->opt.max_sync_decompress_pages))
1120 return true;
1121
1122 return false;
1123 }
1124
z_erofs_page_is_invalidated(struct page * page)1125 static bool z_erofs_page_is_invalidated(struct page *page)
1126 {
1127 return !page_folio(page)->mapping && !z_erofs_is_shortlived_page(page);
1128 }
1129
1130 struct z_erofs_backend {
1131 struct page *onstack_pages[Z_EROFS_ONSTACK_PAGES];
1132 struct super_block *sb;
1133 struct z_erofs_pcluster *pcl;
1134 /* pages with the longest decompressed length for deduplication */
1135 struct page **decompressed_pages;
1136 /* pages to keep the compressed data */
1137 struct page **compressed_pages;
1138
1139 struct list_head decompressed_secondary_bvecs;
1140 struct page **pagepool;
1141 unsigned int onstack_used, nr_pages;
1142 /* indicate if temporary copies should be preserved for later use */
1143 bool keepxcpy;
1144 };
1145
1146 struct z_erofs_bvec_item {
1147 struct z_erofs_bvec bvec;
1148 struct list_head list;
1149 };
1150
z_erofs_do_decompressed_bvec(struct z_erofs_backend * be,struct z_erofs_bvec * bvec)1151 static void z_erofs_do_decompressed_bvec(struct z_erofs_backend *be,
1152 struct z_erofs_bvec *bvec)
1153 {
1154 int poff = bvec->offset + be->pcl->pageofs_out;
1155 struct z_erofs_bvec_item *item;
1156 struct page **page;
1157
1158 if (!(poff & ~PAGE_MASK) && (bvec->end == PAGE_SIZE ||
1159 bvec->offset + bvec->end == be->pcl->length)) {
1160 DBG_BUGON((poff >> PAGE_SHIFT) >= be->nr_pages);
1161 page = be->decompressed_pages + (poff >> PAGE_SHIFT);
1162 if (!*page) {
1163 *page = bvec->page;
1164 return;
1165 }
1166 } else {
1167 be->keepxcpy = true;
1168 }
1169
1170 /* (cold path) one pcluster is requested multiple times */
1171 item = kmalloc(sizeof(*item), GFP_KERNEL | __GFP_NOFAIL);
1172 item->bvec = *bvec;
1173 list_add(&item->list, &be->decompressed_secondary_bvecs);
1174 }
1175
z_erofs_fill_other_copies(struct z_erofs_backend * be,int err)1176 static void z_erofs_fill_other_copies(struct z_erofs_backend *be, int err)
1177 {
1178 unsigned int off0 = be->pcl->pageofs_out;
1179 struct list_head *p, *n;
1180
1181 list_for_each_safe(p, n, &be->decompressed_secondary_bvecs) {
1182 struct z_erofs_bvec_item *bvi;
1183 unsigned int end, cur;
1184 void *dst, *src;
1185
1186 bvi = container_of(p, struct z_erofs_bvec_item, list);
1187 cur = bvi->bvec.offset < 0 ? -bvi->bvec.offset : 0;
1188 end = min_t(unsigned int, be->pcl->length - bvi->bvec.offset,
1189 bvi->bvec.end);
1190 dst = kmap_local_page(bvi->bvec.page);
1191 while (cur < end) {
1192 unsigned int pgnr, scur, len;
1193
1194 pgnr = (bvi->bvec.offset + cur + off0) >> PAGE_SHIFT;
1195 DBG_BUGON(pgnr >= be->nr_pages);
1196
1197 scur = bvi->bvec.offset + cur -
1198 ((pgnr << PAGE_SHIFT) - off0);
1199 len = min_t(unsigned int, end - cur, PAGE_SIZE - scur);
1200 if (!be->decompressed_pages[pgnr]) {
1201 err = -EFSCORRUPTED;
1202 cur += len;
1203 continue;
1204 }
1205 src = kmap_local_page(be->decompressed_pages[pgnr]);
1206 memcpy(dst + cur, src + scur, len);
1207 kunmap_local(src);
1208 cur += len;
1209 }
1210 kunmap_local(dst);
1211 erofs_onlinefolio_end(page_folio(bvi->bvec.page), err, true);
1212 list_del(p);
1213 kfree(bvi);
1214 }
1215 }
1216
z_erofs_parse_out_bvecs(struct z_erofs_backend * be)1217 static void z_erofs_parse_out_bvecs(struct z_erofs_backend *be)
1218 {
1219 struct z_erofs_pcluster *pcl = be->pcl;
1220 struct z_erofs_bvec_iter biter;
1221 struct page *old_bvpage;
1222 int i;
1223
1224 z_erofs_bvec_iter_begin(&biter, &pcl->bvset, Z_EROFS_INLINE_BVECS, 0);
1225 for (i = 0; i < pcl->vcnt; ++i) {
1226 struct z_erofs_bvec bvec;
1227
1228 z_erofs_bvec_dequeue(&biter, &bvec, &old_bvpage);
1229
1230 if (old_bvpage)
1231 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1232
1233 DBG_BUGON(z_erofs_page_is_invalidated(bvec.page));
1234 z_erofs_do_decompressed_bvec(be, &bvec);
1235 }
1236
1237 old_bvpage = z_erofs_bvec_iter_end(&biter);
1238 if (old_bvpage)
1239 z_erofs_put_shortlivedpage(be->pagepool, old_bvpage);
1240 }
1241
z_erofs_parse_in_bvecs(struct z_erofs_backend * be,bool * overlapped)1242 static int z_erofs_parse_in_bvecs(struct z_erofs_backend *be, bool *overlapped)
1243 {
1244 struct z_erofs_pcluster *pcl = be->pcl;
1245 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1246 int i, err = 0;
1247
1248 *overlapped = false;
1249 for (i = 0; i < pclusterpages; ++i) {
1250 struct z_erofs_bvec *bvec = &pcl->compressed_bvecs[i];
1251 struct page *page = bvec->page;
1252
1253 /* compressed data ought to be valid when decompressing */
1254 if (IS_ERR(page) || !page) {
1255 bvec->page = NULL; /* clear the failure reason */
1256 err = page ? PTR_ERR(page) : -EIO;
1257 continue;
1258 }
1259 be->compressed_pages[i] = page;
1260
1261 if (z_erofs_is_inline_pcluster(pcl) ||
1262 erofs_folio_is_managed(EROFS_SB(be->sb), page_folio(page))) {
1263 if (!PageUptodate(page))
1264 err = -EIO;
1265 continue;
1266 }
1267
1268 DBG_BUGON(z_erofs_page_is_invalidated(page));
1269 if (z_erofs_is_shortlived_page(page))
1270 continue;
1271 z_erofs_do_decompressed_bvec(be, bvec);
1272 *overlapped = true;
1273 }
1274 return err;
1275 }
1276
z_erofs_decompress_pcluster(struct z_erofs_backend * be,int err)1277 static int z_erofs_decompress_pcluster(struct z_erofs_backend *be, int err)
1278 {
1279 struct erofs_sb_info *const sbi = EROFS_SB(be->sb);
1280 struct z_erofs_pcluster *pcl = be->pcl;
1281 unsigned int pclusterpages = z_erofs_pclusterpages(pcl);
1282 const struct z_erofs_decompressor *decomp =
1283 z_erofs_decomp[pcl->algorithmformat];
1284 int i, j, jtop, err2;
1285 struct page *page;
1286 bool overlapped;
1287 bool try_free = true;
1288
1289 mutex_lock(&pcl->lock);
1290 be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
1291
1292 /* allocate (de)compressed page arrays if cannot be kept on stack */
1293 be->decompressed_pages = NULL;
1294 be->compressed_pages = NULL;
1295 be->onstack_used = 0;
1296 if (be->nr_pages <= Z_EROFS_ONSTACK_PAGES) {
1297 be->decompressed_pages = be->onstack_pages;
1298 be->onstack_used = be->nr_pages;
1299 memset(be->decompressed_pages, 0,
1300 sizeof(struct page *) * be->nr_pages);
1301 }
1302
1303 if (pclusterpages + be->onstack_used <= Z_EROFS_ONSTACK_PAGES)
1304 be->compressed_pages = be->onstack_pages + be->onstack_used;
1305
1306 if (!be->decompressed_pages)
1307 be->decompressed_pages =
1308 kvcalloc(be->nr_pages, sizeof(struct page *),
1309 GFP_KERNEL | __GFP_NOFAIL);
1310 if (!be->compressed_pages)
1311 be->compressed_pages =
1312 kvcalloc(pclusterpages, sizeof(struct page *),
1313 GFP_KERNEL | __GFP_NOFAIL);
1314
1315 z_erofs_parse_out_bvecs(be);
1316 err2 = z_erofs_parse_in_bvecs(be, &overlapped);
1317 if (err2)
1318 err = err2;
1319 if (!err)
1320 err = decomp->decompress(&(struct z_erofs_decompress_req) {
1321 .sb = be->sb,
1322 .in = be->compressed_pages,
1323 .out = be->decompressed_pages,
1324 .pageofs_in = pcl->pageofs_in,
1325 .pageofs_out = pcl->pageofs_out,
1326 .inputsize = pcl->pclustersize,
1327 .outputsize = pcl->length,
1328 .alg = pcl->algorithmformat,
1329 .inplace_io = overlapped,
1330 .partial_decoding = pcl->partial,
1331 .fillgaps = be->keepxcpy,
1332 .gfp = pcl->besteffort ? GFP_KERNEL :
1333 GFP_NOWAIT | __GFP_NORETRY
1334 }, be->pagepool);
1335
1336 /* must handle all compressed pages before actual file pages */
1337 if (z_erofs_is_inline_pcluster(pcl)) {
1338 page = pcl->compressed_bvecs[0].page;
1339 WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL);
1340 put_page(page);
1341 } else {
1342 /* managed folios are still left in compressed_bvecs[] */
1343 for (i = 0; i < pclusterpages; ++i) {
1344 page = be->compressed_pages[i];
1345 if (!page)
1346 continue;
1347 if (erofs_folio_is_managed(sbi, page_folio(page))) {
1348 try_free = false;
1349 continue;
1350 }
1351 (void)z_erofs_put_shortlivedpage(be->pagepool, page);
1352 WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
1353 }
1354 }
1355 if (be->compressed_pages < be->onstack_pages ||
1356 be->compressed_pages >= be->onstack_pages + Z_EROFS_ONSTACK_PAGES)
1357 kvfree(be->compressed_pages);
1358
1359 jtop = 0;
1360 z_erofs_fill_other_copies(be, err);
1361 for (i = 0; i < be->nr_pages; ++i) {
1362 page = be->decompressed_pages[i];
1363 if (!page)
1364 continue;
1365
1366 DBG_BUGON(z_erofs_page_is_invalidated(page));
1367 if (!z_erofs_is_shortlived_page(page)) {
1368 erofs_onlinefolio_end(page_folio(page), err, true);
1369 continue;
1370 }
1371 if (pcl->algorithmformat != Z_EROFS_COMPRESSION_LZ4) {
1372 erofs_pagepool_add(be->pagepool, page);
1373 continue;
1374 }
1375 for (j = 0; j < jtop && be->decompressed_pages[j] != page; ++j)
1376 ;
1377 if (j >= jtop) /* this bounce page is newly detected */
1378 be->decompressed_pages[jtop++] = page;
1379 }
1380 while (jtop)
1381 erofs_pagepool_add(be->pagepool,
1382 be->decompressed_pages[--jtop]);
1383 if (be->decompressed_pages != be->onstack_pages)
1384 kvfree(be->decompressed_pages);
1385
1386 pcl->length = 0;
1387 pcl->partial = true;
1388 pcl->besteffort = false;
1389 pcl->bvset.nextpage = NULL;
1390 pcl->vcnt = 0;
1391
1392 /* pcluster lock MUST be taken before the following line */
1393 WRITE_ONCE(pcl->next, NULL);
1394 mutex_unlock(&pcl->lock);
1395
1396 if (z_erofs_is_inline_pcluster(pcl))
1397 z_erofs_free_pcluster(pcl);
1398 else
1399 z_erofs_put_pcluster(sbi, pcl, try_free);
1400 return err;
1401 }
1402
z_erofs_decompress_queue(const struct z_erofs_decompressqueue * io,struct page ** pagepool)1403 static int z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
1404 struct page **pagepool)
1405 {
1406 struct z_erofs_backend be = {
1407 .sb = io->sb,
1408 .pagepool = pagepool,
1409 .decompressed_secondary_bvecs =
1410 LIST_HEAD_INIT(be.decompressed_secondary_bvecs),
1411 .pcl = io->head,
1412 };
1413 struct z_erofs_pcluster *next;
1414 int err = io->eio ? -EIO : 0;
1415
1416 for (; be.pcl != Z_EROFS_PCLUSTER_TAIL; be.pcl = next) {
1417 DBG_BUGON(!be.pcl);
1418 next = READ_ONCE(be.pcl->next);
1419 err = z_erofs_decompress_pcluster(&be, err) ?: err;
1420 }
1421 return err;
1422 }
1423
z_erofs_decompressqueue_work(struct work_struct * work)1424 static void z_erofs_decompressqueue_work(struct work_struct *work)
1425 {
1426 struct z_erofs_decompressqueue *bgq =
1427 container_of(work, struct z_erofs_decompressqueue, u.work);
1428 struct page *pagepool = NULL;
1429
1430 DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL);
1431 z_erofs_decompress_queue(bgq, &pagepool);
1432 erofs_release_pages(&pagepool);
1433 kvfree(bgq);
1434 }
1435
1436 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
z_erofs_decompressqueue_kthread_work(struct kthread_work * work)1437 static void z_erofs_decompressqueue_kthread_work(struct kthread_work *work)
1438 {
1439 z_erofs_decompressqueue_work((struct work_struct *)work);
1440 }
1441 #endif
1442
1443 /* Use (kthread_)work in atomic contexts to minimize scheduling overhead */
z_erofs_in_atomic(void)1444 static inline bool z_erofs_in_atomic(void)
1445 {
1446 if (IS_ENABLED(CONFIG_PREEMPTION) && rcu_preempt_depth())
1447 return true;
1448 if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
1449 return true;
1450 return !preemptible();
1451 }
1452
z_erofs_decompress_kickoff(struct z_erofs_decompressqueue * io,int bios)1453 static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
1454 int bios)
1455 {
1456 struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
1457
1458 /* wake up the caller thread for sync decompression */
1459 if (io->sync) {
1460 if (!atomic_add_return(bios, &io->pending_bios))
1461 complete(&io->u.done);
1462 return;
1463 }
1464
1465 if (atomic_add_return(bios, &io->pending_bios))
1466 return;
1467 if (z_erofs_in_atomic()) {
1468 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1469 struct kthread_worker *worker;
1470
1471 rcu_read_lock();
1472 worker = rcu_dereference(
1473 z_erofs_pcpu_workers[raw_smp_processor_id()]);
1474 if (!worker) {
1475 INIT_WORK(&io->u.work, z_erofs_decompressqueue_work);
1476 queue_work(z_erofs_workqueue, &io->u.work);
1477 } else {
1478 kthread_queue_work(worker, &io->u.kthread_work);
1479 }
1480 rcu_read_unlock();
1481 #else
1482 queue_work(z_erofs_workqueue, &io->u.work);
1483 #endif
1484 /* enable sync decompression for readahead */
1485 if (sbi->opt.sync_decompress == EROFS_SYNC_DECOMPRESS_AUTO)
1486 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_FORCE_ON;
1487 return;
1488 }
1489 z_erofs_decompressqueue_work(&io->u.work);
1490 }
1491
z_erofs_fill_bio_vec(struct bio_vec * bvec,struct z_erofs_frontend * f,struct z_erofs_pcluster * pcl,unsigned int nr,struct address_space * mc)1492 static void z_erofs_fill_bio_vec(struct bio_vec *bvec,
1493 struct z_erofs_frontend *f,
1494 struct z_erofs_pcluster *pcl,
1495 unsigned int nr,
1496 struct address_space *mc)
1497 {
1498 gfp_t gfp = mapping_gfp_mask(mc);
1499 bool tocache = false;
1500 struct z_erofs_bvec zbv;
1501 struct address_space *mapping;
1502 struct folio *folio;
1503 struct page *page;
1504 int bs = i_blocksize(f->inode);
1505
1506 /* Except for inplace folios, the entire folio can be used for I/Os */
1507 bvec->bv_offset = 0;
1508 bvec->bv_len = PAGE_SIZE;
1509 repeat:
1510 spin_lock(&pcl->lockref.lock);
1511 zbv = pcl->compressed_bvecs[nr];
1512 spin_unlock(&pcl->lockref.lock);
1513 if (!zbv.page)
1514 goto out_allocfolio;
1515
1516 bvec->bv_page = zbv.page;
1517 DBG_BUGON(z_erofs_is_shortlived_page(bvec->bv_page));
1518
1519 folio = page_folio(zbv.page);
1520 /*
1521 * Handle preallocated cached folios. We tried to allocate such folios
1522 * without triggering direct reclaim. If allocation failed, inplace
1523 * file-backed folios will be used instead.
1524 */
1525 if (folio->private == (void *)Z_EROFS_PREALLOCATED_PAGE) {
1526 tocache = true;
1527 goto out_tocache;
1528 }
1529
1530 mapping = READ_ONCE(folio->mapping);
1531 /*
1532 * File-backed folios for inplace I/Os are all locked steady,
1533 * therefore it is impossible for `mapping` to be NULL.
1534 */
1535 if (mapping && mapping != mc) {
1536 if (zbv.offset < 0)
1537 bvec->bv_offset = round_up(-zbv.offset, bs);
1538 bvec->bv_len = round_up(zbv.end, bs) - bvec->bv_offset;
1539 return;
1540 }
1541
1542 folio_lock(folio);
1543 if (likely(folio->mapping == mc)) {
1544 /*
1545 * The cached folio is still in managed cache but without
1546 * a valid `->private` pcluster hint. Let's reconnect them.
1547 */
1548 if (!folio_test_private(folio)) {
1549 folio_attach_private(folio, pcl);
1550 /* compressed_bvecs[] already takes a ref before */
1551 folio_put(folio);
1552 }
1553 if (likely(folio->private == pcl)) {
1554 /* don't submit cache I/Os again if already uptodate */
1555 if (folio_test_uptodate(folio)) {
1556 folio_unlock(folio);
1557 bvec->bv_page = NULL;
1558 }
1559 return;
1560 }
1561 /*
1562 * Already linked with another pcluster, which only appears in
1563 * crafted images by fuzzers for now. But handle this anyway.
1564 */
1565 tocache = false; /* use temporary short-lived pages */
1566 } else {
1567 DBG_BUGON(1); /* referenced managed folios can't be truncated */
1568 tocache = true;
1569 }
1570 folio_unlock(folio);
1571 folio_put(folio);
1572 out_allocfolio:
1573 page = __erofs_allocpage(&f->pagepool, gfp, true);
1574 spin_lock(&pcl->lockref.lock);
1575 if (unlikely(pcl->compressed_bvecs[nr].page != zbv.page)) {
1576 if (page)
1577 erofs_pagepool_add(&f->pagepool, page);
1578 spin_unlock(&pcl->lockref.lock);
1579 cond_resched();
1580 goto repeat;
1581 }
1582 pcl->compressed_bvecs[nr].page = page ? page : ERR_PTR(-ENOMEM);
1583 spin_unlock(&pcl->lockref.lock);
1584 bvec->bv_page = page;
1585 if (!page)
1586 return;
1587 folio = page_folio(page);
1588 out_tocache:
1589 if (!tocache || bs != PAGE_SIZE ||
1590 filemap_add_folio(mc, folio, pcl->index + nr, gfp)) {
1591 /* turn into a temporary shortlived folio (1 ref) */
1592 folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE;
1593 return;
1594 }
1595 folio_attach_private(folio, pcl);
1596 /* drop a refcount added by allocpage (then 2 refs in total here) */
1597 folio_put(folio);
1598 }
1599
jobqueue_init(struct super_block * sb,struct z_erofs_decompressqueue * fgq,bool * fg)1600 static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
1601 struct z_erofs_decompressqueue *fgq, bool *fg)
1602 {
1603 struct z_erofs_decompressqueue *q;
1604
1605 if (fg && !*fg) {
1606 q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
1607 if (!q) {
1608 *fg = true;
1609 goto fg_out;
1610 }
1611 #ifdef CONFIG_EROFS_FS_PCPU_KTHREAD
1612 kthread_init_work(&q->u.kthread_work,
1613 z_erofs_decompressqueue_kthread_work);
1614 #else
1615 INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
1616 #endif
1617 } else {
1618 fg_out:
1619 q = fgq;
1620 init_completion(&fgq->u.done);
1621 atomic_set(&fgq->pending_bios, 0);
1622 q->eio = false;
1623 q->sync = true;
1624 }
1625 q->sb = sb;
1626 q->head = Z_EROFS_PCLUSTER_TAIL;
1627 return q;
1628 }
1629
1630 /* define decompression jobqueue types */
1631 enum {
1632 JQ_BYPASS,
1633 JQ_SUBMIT,
1634 NR_JOBQUEUES,
1635 };
1636
z_erofs_move_to_bypass_queue(struct z_erofs_pcluster * pcl,struct z_erofs_pcluster * next,struct z_erofs_pcluster ** qtail[])1637 static void z_erofs_move_to_bypass_queue(struct z_erofs_pcluster *pcl,
1638 struct z_erofs_pcluster *next,
1639 struct z_erofs_pcluster **qtail[])
1640 {
1641 WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL);
1642 WRITE_ONCE(*qtail[JQ_SUBMIT], next);
1643 WRITE_ONCE(*qtail[JQ_BYPASS], pcl);
1644 qtail[JQ_BYPASS] = &pcl->next;
1645 }
1646
z_erofs_endio(struct bio * bio)1647 static void z_erofs_endio(struct bio *bio)
1648 {
1649 struct z_erofs_decompressqueue *q = bio->bi_private;
1650 blk_status_t err = bio->bi_status;
1651 struct folio_iter fi;
1652
1653 bio_for_each_folio_all(fi, bio) {
1654 struct folio *folio = fi.folio;
1655
1656 DBG_BUGON(folio_test_uptodate(folio));
1657 DBG_BUGON(z_erofs_page_is_invalidated(&folio->page));
1658 if (!erofs_folio_is_managed(EROFS_SB(q->sb), folio))
1659 continue;
1660
1661 if (!err)
1662 folio_mark_uptodate(folio);
1663 folio_unlock(folio);
1664 }
1665 if (err)
1666 q->eio = true;
1667 z_erofs_decompress_kickoff(q, -1);
1668 if (bio->bi_bdev)
1669 bio_put(bio);
1670 }
1671
z_erofs_submit_queue(struct z_erofs_frontend * f,struct z_erofs_decompressqueue * fgq,bool * force_fg,bool readahead)1672 static void z_erofs_submit_queue(struct z_erofs_frontend *f,
1673 struct z_erofs_decompressqueue *fgq,
1674 bool *force_fg, bool readahead)
1675 {
1676 struct super_block *sb = f->inode->i_sb;
1677 struct address_space *mc = MNGD_MAPPING(EROFS_SB(sb));
1678 struct z_erofs_pcluster **qtail[NR_JOBQUEUES];
1679 struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
1680 struct z_erofs_pcluster *pcl, *next;
1681 /* bio is NULL initially, so no need to initialize last_{index,bdev} */
1682 erofs_off_t last_pa;
1683 unsigned int nr_bios = 0;
1684 struct bio *bio = NULL;
1685 unsigned long pflags;
1686 int memstall = 0;
1687
1688 /* No need to read from device for pclusters in the bypass queue. */
1689 q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
1690 q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, force_fg);
1691
1692 qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
1693 qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
1694
1695 /* by default, all need io submission */
1696 q[JQ_SUBMIT]->head = next = f->head;
1697
1698 do {
1699 struct erofs_map_dev mdev;
1700 erofs_off_t cur, end;
1701 struct bio_vec bvec;
1702 unsigned int i = 0;
1703 bool bypass = true;
1704
1705 pcl = next;
1706 next = READ_ONCE(pcl->next);
1707 if (z_erofs_is_inline_pcluster(pcl)) {
1708 z_erofs_move_to_bypass_queue(pcl, next, qtail);
1709 continue;
1710 }
1711
1712 /* no device id here, thus it will always succeed */
1713 mdev = (struct erofs_map_dev) {
1714 .m_pa = erofs_pos(sb, pcl->index),
1715 };
1716 (void)erofs_map_dev(sb, &mdev);
1717
1718 cur = mdev.m_pa;
1719 end = cur + pcl->pclustersize;
1720 do {
1721 bvec.bv_page = NULL;
1722 if (bio && (cur != last_pa ||
1723 bio->bi_bdev != mdev.m_bdev)) {
1724 drain_io:
1725 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1726 erofs_fileio_submit_bio(bio);
1727 else if (erofs_is_fscache_mode(sb))
1728 erofs_fscache_submit_bio(bio);
1729 else
1730 submit_bio(bio);
1731
1732 if (memstall) {
1733 psi_memstall_leave(&pflags);
1734 memstall = 0;
1735 }
1736 bio = NULL;
1737 }
1738
1739 if (!bvec.bv_page) {
1740 z_erofs_fill_bio_vec(&bvec, f, pcl, i++, mc);
1741 if (!bvec.bv_page)
1742 continue;
1743 if (cur + bvec.bv_len > end)
1744 bvec.bv_len = end - cur;
1745 DBG_BUGON(bvec.bv_len < sb->s_blocksize);
1746 }
1747
1748 if (unlikely(PageWorkingset(bvec.bv_page)) &&
1749 !memstall) {
1750 psi_memstall_enter(&pflags);
1751 memstall = 1;
1752 }
1753
1754 if (!bio) {
1755 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1756 bio = erofs_fileio_bio_alloc(&mdev);
1757 else if (erofs_is_fscache_mode(sb))
1758 bio = erofs_fscache_bio_alloc(&mdev);
1759 else
1760 bio = bio_alloc(mdev.m_bdev, BIO_MAX_VECS,
1761 REQ_OP_READ, GFP_NOIO);
1762 bio->bi_end_io = z_erofs_endio;
1763 bio->bi_iter.bi_sector =
1764 (mdev.m_dif->fsoff + cur) >> 9;
1765 bio->bi_private = q[JQ_SUBMIT];
1766 if (readahead)
1767 bio->bi_opf |= REQ_RAHEAD;
1768 ++nr_bios;
1769 }
1770
1771 if (!bio_add_page(bio, bvec.bv_page, bvec.bv_len,
1772 bvec.bv_offset))
1773 goto drain_io;
1774 last_pa = cur + bvec.bv_len;
1775 bypass = false;
1776 } while ((cur += bvec.bv_len) < end);
1777
1778 if (!bypass)
1779 qtail[JQ_SUBMIT] = &pcl->next;
1780 else
1781 z_erofs_move_to_bypass_queue(pcl, next, qtail);
1782 } while (next != Z_EROFS_PCLUSTER_TAIL);
1783
1784 if (bio) {
1785 if (erofs_is_fileio_mode(EROFS_SB(sb)))
1786 erofs_fileio_submit_bio(bio);
1787 else if (erofs_is_fscache_mode(sb))
1788 erofs_fscache_submit_bio(bio);
1789 else
1790 submit_bio(bio);
1791 }
1792 if (memstall)
1793 psi_memstall_leave(&pflags);
1794
1795 /*
1796 * although background is preferred, no one is pending for submission.
1797 * don't issue decompression but drop it directly instead.
1798 */
1799 if (!*force_fg && !nr_bios) {
1800 kvfree(q[JQ_SUBMIT]);
1801 return;
1802 }
1803 z_erofs_decompress_kickoff(q[JQ_SUBMIT], nr_bios);
1804 }
1805
z_erofs_runqueue(struct z_erofs_frontend * f,unsigned int rapages)1806 static int z_erofs_runqueue(struct z_erofs_frontend *f, unsigned int rapages)
1807 {
1808 struct z_erofs_decompressqueue io[NR_JOBQUEUES];
1809 struct erofs_sb_info *sbi = EROFS_I_SB(f->inode);
1810 bool force_fg = z_erofs_is_sync_decompress(sbi, rapages);
1811 int err;
1812
1813 if (f->head == Z_EROFS_PCLUSTER_TAIL)
1814 return 0;
1815 z_erofs_submit_queue(f, io, &force_fg, !!rapages);
1816
1817 /* handle bypass queue (no i/o pclusters) immediately */
1818 err = z_erofs_decompress_queue(&io[JQ_BYPASS], &f->pagepool);
1819 if (!force_fg)
1820 return err;
1821
1822 /* wait until all bios are completed */
1823 wait_for_completion_io(&io[JQ_SUBMIT].u.done);
1824
1825 /* handle synchronous decompress queue in the caller context */
1826 return z_erofs_decompress_queue(&io[JQ_SUBMIT], &f->pagepool) ?: err;
1827 }
1828
1829 /*
1830 * Since partial uptodate is still unimplemented for now, we have to use
1831 * approximate readmore strategies as a start.
1832 */
z_erofs_pcluster_readmore(struct z_erofs_frontend * f,struct readahead_control * rac,bool backmost)1833 static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f,
1834 struct readahead_control *rac, bool backmost)
1835 {
1836 struct inode *inode = f->inode;
1837 struct erofs_map_blocks *map = &f->map;
1838 erofs_off_t cur, end, headoffset = f->headoffset;
1839 int err;
1840
1841 if (backmost) {
1842 if (rac)
1843 end = headoffset + readahead_length(rac) - 1;
1844 else
1845 end = headoffset + PAGE_SIZE - 1;
1846 map->m_la = end;
1847 err = z_erofs_map_blocks_iter(inode, map,
1848 EROFS_GET_BLOCKS_READMORE);
1849 if (err)
1850 return;
1851
1852 /* expand ra for the trailing edge if readahead */
1853 if (rac) {
1854 cur = round_up(map->m_la + map->m_llen, PAGE_SIZE);
1855 readahead_expand(rac, headoffset, cur - headoffset);
1856 return;
1857 }
1858 end = round_up(end, PAGE_SIZE);
1859 } else {
1860 end = round_up(map->m_la, PAGE_SIZE);
1861 if (!map->m_llen)
1862 return;
1863 }
1864
1865 cur = map->m_la + map->m_llen - 1;
1866 while ((cur >= end) && (cur < i_size_read(inode))) {
1867 pgoff_t index = cur >> PAGE_SHIFT;
1868 struct folio *folio;
1869
1870 folio = erofs_grab_folio_nowait(inode->i_mapping, index);
1871 if (!IS_ERR_OR_NULL(folio)) {
1872 if (folio_test_uptodate(folio))
1873 folio_unlock(folio);
1874 else
1875 z_erofs_scan_folio(f, folio, !!rac);
1876 folio_put(folio);
1877 }
1878
1879 if (cur < PAGE_SIZE)
1880 break;
1881 cur = (index << PAGE_SHIFT) - 1;
1882 }
1883 }
1884
z_erofs_read_folio(struct file * file,struct folio * folio)1885 static int z_erofs_read_folio(struct file *file, struct folio *folio)
1886 {
1887 struct inode *const inode = folio->mapping->host;
1888 Z_EROFS_DEFINE_FRONTEND(f, inode, folio_pos(folio));
1889 int err;
1890
1891 trace_erofs_read_folio(folio, false);
1892 z_erofs_pcluster_readmore(&f, NULL, true);
1893 err = z_erofs_scan_folio(&f, folio, false);
1894 z_erofs_pcluster_readmore(&f, NULL, false);
1895 z_erofs_pcluster_end(&f);
1896
1897 /* if some pclusters are ready, need submit them anyway */
1898 err = z_erofs_runqueue(&f, 0) ?: err;
1899 if (err && err != -EINTR)
1900 erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
1901 err, folio->index, EROFS_I(inode)->nid);
1902
1903 erofs_put_metabuf(&f.map.buf);
1904 erofs_release_pages(&f.pagepool);
1905 return err;
1906 }
1907
z_erofs_readahead(struct readahead_control * rac)1908 static void z_erofs_readahead(struct readahead_control *rac)
1909 {
1910 struct inode *const inode = rac->mapping->host;
1911 Z_EROFS_DEFINE_FRONTEND(f, inode, readahead_pos(rac));
1912 unsigned int nrpages = readahead_count(rac);
1913 struct folio *head = NULL, *folio;
1914 int err;
1915
1916 trace_erofs_readahead(inode, readahead_index(rac), nrpages, false);
1917 z_erofs_pcluster_readmore(&f, rac, true);
1918 while ((folio = readahead_folio(rac))) {
1919 folio->private = head;
1920 head = folio;
1921 }
1922
1923 /* traverse in reverse order for best metadata I/O performance */
1924 while (head) {
1925 folio = head;
1926 head = folio_get_private(folio);
1927
1928 err = z_erofs_scan_folio(&f, folio, true);
1929 if (err && err != -EINTR)
1930 erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
1931 folio->index, EROFS_I(inode)->nid);
1932 }
1933 z_erofs_pcluster_readmore(&f, rac, false);
1934 z_erofs_pcluster_end(&f);
1935
1936 (void)z_erofs_runqueue(&f, nrpages);
1937 erofs_put_metabuf(&f.map.buf);
1938 erofs_release_pages(&f.pagepool);
1939 }
1940
1941 const struct address_space_operations z_erofs_aops = {
1942 .read_folio = z_erofs_read_folio,
1943 .readahead = z_erofs_readahead,
1944 };
1945