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