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