147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only 247e4937aSGao Xiang /* 347e4937aSGao Xiang * Copyright (C) 2018 HUAWEI, Inc. 4592e7cd0SAlexander A. Klimov * https://www.huawei.com/ 547e4937aSGao Xiang * Created by Gao Xiang <gaoxiang25@huawei.com> 647e4937aSGao Xiang */ 747e4937aSGao Xiang #include "zdata.h" 847e4937aSGao Xiang #include "compress.h" 947e4937aSGao Xiang #include <linux/prefetch.h> 1047e4937aSGao Xiang 1147e4937aSGao Xiang #include <trace/events/erofs.h> 1247e4937aSGao Xiang 1347e4937aSGao Xiang /* 1447e4937aSGao Xiang * a compressed_pages[] placeholder in order to avoid 1547e4937aSGao Xiang * being filled with file pages for in-place decompression. 1647e4937aSGao Xiang */ 1747e4937aSGao Xiang #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D) 1847e4937aSGao Xiang 1947e4937aSGao Xiang /* how to allocate cached pages for a pcluster */ 2047e4937aSGao Xiang enum z_erofs_cache_alloctype { 2147e4937aSGao Xiang DONTALLOC, /* don't allocate any cached pages */ 2247e4937aSGao Xiang DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */ 231825c8d7SGao Xiang /* 241825c8d7SGao Xiang * try to use cached I/O if page allocation succeeds or fallback 251825c8d7SGao Xiang * to in-place I/O instead to avoid any direct reclaim. 261825c8d7SGao Xiang */ 271825c8d7SGao Xiang TRYALLOC, 2847e4937aSGao Xiang }; 2947e4937aSGao Xiang 3047e4937aSGao Xiang /* 3147e4937aSGao Xiang * tagged pointer with 1-bit tag for all compressed pages 3247e4937aSGao Xiang * tag 0 - the page is just found with an extra page reference 3347e4937aSGao Xiang */ 3447e4937aSGao Xiang typedef tagptr1_t compressed_page_t; 3547e4937aSGao Xiang 3647e4937aSGao Xiang #define tag_compressed_page_justfound(page) \ 3747e4937aSGao Xiang tagptr_fold(compressed_page_t, page, 1) 3847e4937aSGao Xiang 3947e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 4047e4937aSGao Xiang static struct kmem_cache *pcluster_cachep __read_mostly; 4147e4937aSGao Xiang 4247e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void) 4347e4937aSGao Xiang { 4447e4937aSGao Xiang destroy_workqueue(z_erofs_workqueue); 4547e4937aSGao Xiang kmem_cache_destroy(pcluster_cachep); 4647e4937aSGao Xiang } 4747e4937aSGao Xiang 4899634bf3SGao Xiang static inline int z_erofs_init_workqueue(void) 4947e4937aSGao Xiang { 5047e4937aSGao Xiang const unsigned int onlinecpus = num_possible_cpus(); 5147e4937aSGao Xiang 5247e4937aSGao Xiang /* 5347e4937aSGao Xiang * no need to spawn too many threads, limiting threads could minimum 5447e4937aSGao Xiang * scheduling overhead, perhaps per-CPU threads should be better? 5547e4937aSGao Xiang */ 560e62ea33SGao Xiang z_erofs_workqueue = alloc_workqueue("erofs_unzipd", 570e62ea33SGao Xiang WQ_UNBOUND | WQ_HIGHPRI, 5847e4937aSGao Xiang onlinecpus + onlinecpus / 4); 5947e4937aSGao Xiang return z_erofs_workqueue ? 0 : -ENOMEM; 6047e4937aSGao Xiang } 6147e4937aSGao Xiang 6299634bf3SGao Xiang static void z_erofs_pcluster_init_once(void *ptr) 6347e4937aSGao Xiang { 6447e4937aSGao Xiang struct z_erofs_pcluster *pcl = ptr; 6547e4937aSGao Xiang struct z_erofs_collection *cl = z_erofs_primarycollection(pcl); 6647e4937aSGao Xiang unsigned int i; 6747e4937aSGao Xiang 6847e4937aSGao Xiang mutex_init(&cl->lock); 6947e4937aSGao Xiang cl->nr_pages = 0; 7047e4937aSGao Xiang cl->vcnt = 0; 7147e4937aSGao Xiang for (i = 0; i < Z_EROFS_CLUSTER_MAX_PAGES; ++i) 7247e4937aSGao Xiang pcl->compressed_pages[i] = NULL; 7347e4937aSGao Xiang } 7447e4937aSGao Xiang 7547e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 7647e4937aSGao Xiang { 7747e4937aSGao Xiang pcluster_cachep = kmem_cache_create("erofs_compress", 7847e4937aSGao Xiang Z_EROFS_WORKGROUP_SIZE, 0, 7999634bf3SGao Xiang SLAB_RECLAIM_ACCOUNT, 8099634bf3SGao Xiang z_erofs_pcluster_init_once); 8147e4937aSGao Xiang if (pcluster_cachep) { 8299634bf3SGao Xiang if (!z_erofs_init_workqueue()) 8347e4937aSGao Xiang return 0; 8447e4937aSGao Xiang 8547e4937aSGao Xiang kmem_cache_destroy(pcluster_cachep); 8647e4937aSGao Xiang } 8747e4937aSGao Xiang return -ENOMEM; 8847e4937aSGao Xiang } 8947e4937aSGao Xiang 9047e4937aSGao Xiang enum z_erofs_collectmode { 9147e4937aSGao Xiang COLLECT_SECONDARY, 9247e4937aSGao Xiang COLLECT_PRIMARY, 9347e4937aSGao Xiang /* 9447e4937aSGao Xiang * The current collection was the tail of an exist chain, in addition 9547e4937aSGao Xiang * that the previous processed chained collections are all decided to 9647e4937aSGao Xiang * be hooked up to it. 9747e4937aSGao Xiang * A new chain will be created for the remaining collections which are 9847e4937aSGao Xiang * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED, 9947e4937aSGao Xiang * the next collection cannot reuse the whole page safely in 10047e4937aSGao Xiang * the following scenario: 10147e4937aSGao Xiang * ________________________________________________________________ 10247e4937aSGao Xiang * | tail (partial) page | head (partial) page | 10347e4937aSGao Xiang * | (belongs to the next cl) | (belongs to the current cl) | 10447e4937aSGao Xiang * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________| 10547e4937aSGao Xiang */ 10647e4937aSGao Xiang COLLECT_PRIMARY_HOOKED, 107*0b964600SGao Xiang /* 108*0b964600SGao Xiang * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it 109*0b964600SGao Xiang * could be dispatched into bypass queue later due to uptodated managed 110*0b964600SGao Xiang * pages. All related online pages cannot be reused for inplace I/O (or 111*0b964600SGao Xiang * pagevec) since it can be directly decoded without I/O submission. 112*0b964600SGao Xiang */ 11347e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED_NOINPLACE, 11447e4937aSGao Xiang /* 11547e4937aSGao Xiang * The current collection has been linked with the owned chain, and 11647e4937aSGao Xiang * could also be linked with the remaining collections, which means 11747e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 11847e4937aSGao Xiang * the current collection can safely use the whole page (since 11947e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 12047e4937aSGao Xiang * illustrated below: 12147e4937aSGao Xiang * ________________________________________________________________ 12247e4937aSGao Xiang * | tail (partial) page | head (partial) page | 12347e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 12447e4937aSGao Xiang * | PRIMARY_FOLLOWED or | | 12547e4937aSGao Xiang * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| 12647e4937aSGao Xiang * 12747e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 12847e4937aSGao Xiang */ 12947e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED, 13047e4937aSGao Xiang }; 13147e4937aSGao Xiang 13247e4937aSGao Xiang struct z_erofs_collector { 13347e4937aSGao Xiang struct z_erofs_pagevec_ctor vector; 13447e4937aSGao Xiang 13547e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 13647e4937aSGao Xiang struct z_erofs_collection *cl; 13747e4937aSGao Xiang struct page **compressedpages; 13847e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 13947e4937aSGao Xiang 14047e4937aSGao Xiang enum z_erofs_collectmode mode; 14147e4937aSGao Xiang }; 14247e4937aSGao Xiang 14347e4937aSGao Xiang struct z_erofs_decompress_frontend { 14447e4937aSGao Xiang struct inode *const inode; 14547e4937aSGao Xiang 14647e4937aSGao Xiang struct z_erofs_collector clt; 14747e4937aSGao Xiang struct erofs_map_blocks map; 14847e4937aSGao Xiang 1496ea5aad3SGao Xiang bool readahead; 15047e4937aSGao Xiang /* used for applying cache strategy on the fly */ 15147e4937aSGao Xiang bool backmost; 15247e4937aSGao Xiang erofs_off_t headoffset; 15347e4937aSGao Xiang }; 15447e4937aSGao Xiang 15547e4937aSGao Xiang #define COLLECTOR_INIT() { \ 15647e4937aSGao Xiang .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 15747e4937aSGao Xiang .mode = COLLECT_PRIMARY_FOLLOWED } 15847e4937aSGao Xiang 15947e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 16047e4937aSGao Xiang .inode = __i, .clt = COLLECTOR_INIT(), \ 16147e4937aSGao Xiang .backmost = true, } 16247e4937aSGao Xiang 16347e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; 16447e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock); 16547e4937aSGao Xiang 16647e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt, 16747e4937aSGao Xiang struct address_space *mc, 1681825c8d7SGao Xiang enum z_erofs_cache_alloctype type, 1691825c8d7SGao Xiang struct list_head *pagepool) 17047e4937aSGao Xiang { 17147e4937aSGao Xiang const struct z_erofs_pcluster *pcl = clt->pcl; 17247e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 17347e4937aSGao Xiang struct page **pages = clt->compressedpages; 17447e4937aSGao Xiang pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages); 17547e4937aSGao Xiang bool standalone = true; 1761825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 1771825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 17847e4937aSGao Xiang 17947e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED) 18047e4937aSGao Xiang return; 18147e4937aSGao Xiang 18247e4937aSGao Xiang for (; pages < pcl->compressed_pages + clusterpages; ++pages) { 18347e4937aSGao Xiang struct page *page; 18447e4937aSGao Xiang compressed_page_t t; 1851825c8d7SGao Xiang struct page *newpage = NULL; 18647e4937aSGao Xiang 18747e4937aSGao Xiang /* the compressed page was loaded before */ 18847e4937aSGao Xiang if (READ_ONCE(*pages)) 18947e4937aSGao Xiang continue; 19047e4937aSGao Xiang 19147e4937aSGao Xiang page = find_get_page(mc, index); 19247e4937aSGao Xiang 19347e4937aSGao Xiang if (page) { 19447e4937aSGao Xiang t = tag_compressed_page_justfound(page); 195*0b964600SGao Xiang } else { 196*0b964600SGao Xiang /* I/O is needed, no possible to decompress directly */ 197*0b964600SGao Xiang standalone = false; 198*0b964600SGao Xiang switch (type) { 199*0b964600SGao Xiang case DELAYEDALLOC: 200*0b964600SGao Xiang t = tagptr_init(compressed_page_t, 201*0b964600SGao Xiang PAGE_UNALLOCATED); 202*0b964600SGao Xiang break; 203*0b964600SGao Xiang case TRYALLOC: 2041825c8d7SGao Xiang newpage = erofs_allocpage(pagepool, gfp); 2051825c8d7SGao Xiang if (!newpage) 20647e4937aSGao Xiang continue; 207*0b964600SGao Xiang set_page_private(newpage, 208*0b964600SGao Xiang Z_EROFS_PREALLOCATED_PAGE); 209*0b964600SGao Xiang t = tag_compressed_page_justfound(newpage); 210*0b964600SGao Xiang break; 211*0b964600SGao Xiang default: /* DONTALLOC */ 212*0b964600SGao Xiang continue; 213*0b964600SGao Xiang } 21447e4937aSGao Xiang } 21547e4937aSGao Xiang 21647e4937aSGao Xiang if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) 21747e4937aSGao Xiang continue; 21847e4937aSGao Xiang 2191825c8d7SGao Xiang if (page) { 22047e4937aSGao Xiang put_page(page); 2211825c8d7SGao Xiang } else if (newpage) { 2221825c8d7SGao Xiang set_page_private(newpage, 0); 2231825c8d7SGao Xiang list_add(&newpage->lru, pagepool); 2241825c8d7SGao Xiang } 22547e4937aSGao Xiang } 22647e4937aSGao Xiang 227*0b964600SGao Xiang /* 228*0b964600SGao Xiang * don't do inplace I/O if all compressed pages are available in 229*0b964600SGao Xiang * managed cache since it can be moved to the bypass queue instead. 230*0b964600SGao Xiang */ 231*0b964600SGao Xiang if (standalone) 23247e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 23347e4937aSGao Xiang } 23447e4937aSGao Xiang 23547e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 23647e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 23747e4937aSGao Xiang struct erofs_workgroup *grp) 23847e4937aSGao Xiang { 23947e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 24047e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 24147e4937aSGao Xiang struct address_space *const mapping = MNGD_MAPPING(sbi); 24247e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 24347e4937aSGao Xiang int i; 24447e4937aSGao Xiang 24547e4937aSGao Xiang /* 24647e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 24747e4937aSGao Xiang * therefore no need to worry about available decompression users. 24847e4937aSGao Xiang */ 24947e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 25047e4937aSGao Xiang struct page *page = pcl->compressed_pages[i]; 25147e4937aSGao Xiang 25247e4937aSGao Xiang if (!page) 25347e4937aSGao Xiang continue; 25447e4937aSGao Xiang 25547e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 25647e4937aSGao Xiang if (!trylock_page(page)) 25747e4937aSGao Xiang return -EBUSY; 25847e4937aSGao Xiang 2598d8a09b0SGao Xiang if (page->mapping != mapping) 26047e4937aSGao Xiang continue; 26147e4937aSGao Xiang 26247e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 26347e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 2646aaa7b06SGao Xiang detach_page_private(page); 26547e4937aSGao Xiang unlock_page(page); 26647e4937aSGao Xiang } 26747e4937aSGao Xiang return 0; 26847e4937aSGao Xiang } 26947e4937aSGao Xiang 27047e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping, 27147e4937aSGao Xiang struct page *page) 27247e4937aSGao Xiang { 27347e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 27447e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 27547e4937aSGao Xiang int ret = 0; /* 0 - busy */ 27647e4937aSGao Xiang 27747e4937aSGao Xiang if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { 27847e4937aSGao Xiang unsigned int i; 27947e4937aSGao Xiang 28047e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 28147e4937aSGao Xiang if (pcl->compressed_pages[i] == page) { 28247e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 28347e4937aSGao Xiang ret = 1; 28447e4937aSGao Xiang break; 28547e4937aSGao Xiang } 28647e4937aSGao Xiang } 28747e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 28847e4937aSGao Xiang 2896aaa7b06SGao Xiang if (ret) 2906aaa7b06SGao Xiang detach_page_private(page); 29147e4937aSGao Xiang } 29247e4937aSGao Xiang return ret; 29347e4937aSGao Xiang } 29447e4937aSGao Xiang 29547e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ 29699634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt, 29747e4937aSGao Xiang struct page *page) 29847e4937aSGao Xiang { 29947e4937aSGao Xiang struct z_erofs_pcluster *const pcl = clt->pcl; 30047e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 30147e4937aSGao Xiang 30247e4937aSGao Xiang while (clt->compressedpages < pcl->compressed_pages + clusterpages) { 30347e4937aSGao Xiang if (!cmpxchg(clt->compressedpages++, NULL, page)) 30447e4937aSGao Xiang return true; 30547e4937aSGao Xiang } 30647e4937aSGao Xiang return false; 30747e4937aSGao Xiang } 30847e4937aSGao Xiang 30947e4937aSGao Xiang /* callers must be with collection lock held */ 31047e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt, 31147e4937aSGao Xiang struct page *page, 31247e4937aSGao Xiang enum z_erofs_page_type type) 31347e4937aSGao Xiang { 31447e4937aSGao Xiang int ret; 31547e4937aSGao Xiang bool occupied; 31647e4937aSGao Xiang 31747e4937aSGao Xiang /* give priority for inplaceio */ 31847e4937aSGao Xiang if (clt->mode >= COLLECT_PRIMARY && 31947e4937aSGao Xiang type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && 32099634bf3SGao Xiang z_erofs_try_inplace_io(clt, page)) 32147e4937aSGao Xiang return 0; 32247e4937aSGao Xiang 32347e4937aSGao Xiang ret = z_erofs_pagevec_enqueue(&clt->vector, 32447e4937aSGao Xiang page, type, &occupied); 32547e4937aSGao Xiang clt->cl->vcnt += (unsigned int)ret; 32647e4937aSGao Xiang 32747e4937aSGao Xiang return ret ? 0 : -EAGAIN; 32847e4937aSGao Xiang } 32947e4937aSGao Xiang 330473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt) 33147e4937aSGao Xiang { 332473e15b0SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 333473e15b0SGao Xiang z_erofs_next_pcluster_t *owned_head = &clt->owned_head; 33447e4937aSGao Xiang 335473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 336473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 337473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 33847e4937aSGao Xiang *owned_head = &pcl->next; 339473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 340473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 341473e15b0SGao Xiang return; 342473e15b0SGao Xiang } 343473e15b0SGao Xiang 34447e4937aSGao Xiang /* 345473e15b0SGao Xiang * type 2, link to the end of an existing open chain, be careful 346473e15b0SGao Xiang * that its submission is controlled by the original attached chain. 34747e4937aSGao Xiang */ 34847e4937aSGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 349473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 35047e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 351473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_HOOKED; 352473e15b0SGao Xiang clt->tailpcl = NULL; 353473e15b0SGao Xiang return; 35447e4937aSGao Xiang } 355473e15b0SGao Xiang /* type 3, it belongs to a chain, but it isn't the end of the chain */ 356473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY; 35747e4937aSGao Xiang } 35847e4937aSGao Xiang 3599e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt, 36047e4937aSGao Xiang struct inode *inode, 36147e4937aSGao Xiang struct erofs_map_blocks *map) 36247e4937aSGao Xiang { 36364094a04SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 36447e4937aSGao Xiang struct z_erofs_collection *cl; 36547e4937aSGao Xiang unsigned int length; 36647e4937aSGao Xiang 36764094a04SGao Xiang /* to avoid unexpected loop formed by corrupted images */ 36847e4937aSGao Xiang if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) { 36947e4937aSGao Xiang DBG_BUGON(1); 3709e579fc1SGao Xiang return -EFSCORRUPTED; 37147e4937aSGao Xiang } 37247e4937aSGao Xiang 37347e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 3748d8a09b0SGao Xiang if (cl->pageofs != (map->m_la & ~PAGE_MASK)) { 37547e4937aSGao Xiang DBG_BUGON(1); 3769e579fc1SGao Xiang return -EFSCORRUPTED; 37747e4937aSGao Xiang } 37847e4937aSGao Xiang 37947e4937aSGao Xiang length = READ_ONCE(pcl->length); 38047e4937aSGao Xiang if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { 38147e4937aSGao Xiang if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { 38247e4937aSGao Xiang DBG_BUGON(1); 3839e579fc1SGao Xiang return -EFSCORRUPTED; 38447e4937aSGao Xiang } 38547e4937aSGao Xiang } else { 38647e4937aSGao Xiang unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; 38747e4937aSGao Xiang 38847e4937aSGao Xiang if (map->m_flags & EROFS_MAP_FULL_MAPPED) 38947e4937aSGao Xiang llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; 39047e4937aSGao Xiang 39147e4937aSGao Xiang while (llen > length && 39247e4937aSGao Xiang length != cmpxchg_relaxed(&pcl->length, length, llen)) { 39347e4937aSGao Xiang cpu_relax(); 39447e4937aSGao Xiang length = READ_ONCE(pcl->length); 39547e4937aSGao Xiang } 39647e4937aSGao Xiang } 39747e4937aSGao Xiang mutex_lock(&cl->lock); 39847e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 39947e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 40047e4937aSGao Xiang clt->tailpcl = pcl; 401473e15b0SGao Xiang 402473e15b0SGao Xiang z_erofs_try_to_claim_pcluster(clt); 40347e4937aSGao Xiang clt->cl = cl; 4049e579fc1SGao Xiang return 0; 40547e4937aSGao Xiang } 40647e4937aSGao Xiang 4079e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt, 40847e4937aSGao Xiang struct inode *inode, 40947e4937aSGao Xiang struct erofs_map_blocks *map) 41047e4937aSGao Xiang { 41147e4937aSGao Xiang struct z_erofs_pcluster *pcl; 41247e4937aSGao Xiang struct z_erofs_collection *cl; 41364094a04SGao Xiang struct erofs_workgroup *grp; 41447e4937aSGao Xiang int err; 41547e4937aSGao Xiang 41647e4937aSGao Xiang /* no available workgroup, let's allocate one */ 41747e4937aSGao Xiang pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS); 4188d8a09b0SGao Xiang if (!pcl) 4199e579fc1SGao Xiang return -ENOMEM; 42047e4937aSGao Xiang 42164094a04SGao Xiang atomic_set(&pcl->obj.refcount, 1); 42247e4937aSGao Xiang pcl->obj.index = map->m_pa >> PAGE_SHIFT; 42347e4937aSGao Xiang 42447e4937aSGao Xiang pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | 42547e4937aSGao Xiang (map->m_flags & EROFS_MAP_FULL_MAPPED ? 42647e4937aSGao Xiang Z_EROFS_PCLUSTER_FULL_LENGTH : 0); 42747e4937aSGao Xiang 42847e4937aSGao Xiang if (map->m_flags & EROFS_MAP_ZIPPED) 42947e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4; 43047e4937aSGao Xiang else 43147e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; 43247e4937aSGao Xiang 433a5876e24SGao Xiang pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0]; 43447e4937aSGao Xiang pcl->clusterbits -= PAGE_SHIFT; 43547e4937aSGao Xiang 43647e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 43747e4937aSGao Xiang pcl->next = clt->owned_head; 43847e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 43947e4937aSGao Xiang 44047e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 44164094a04SGao Xiang 44264094a04SGao Xiang /* must be cleaned before freeing to slab */ 44364094a04SGao Xiang DBG_BUGON(cl->nr_pages); 44464094a04SGao Xiang DBG_BUGON(cl->vcnt); 44564094a04SGao Xiang 44647e4937aSGao Xiang cl->pageofs = map->m_la & ~PAGE_MASK; 44747e4937aSGao Xiang 44847e4937aSGao Xiang /* 44947e4937aSGao Xiang * lock all primary followed works before visible to others 45047e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 45147e4937aSGao Xiang */ 45264094a04SGao Xiang DBG_BUGON(!mutex_trylock(&cl->lock)); 45347e4937aSGao Xiang 45464094a04SGao Xiang grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj); 45564094a04SGao Xiang if (IS_ERR(grp)) { 45664094a04SGao Xiang err = PTR_ERR(grp); 45764094a04SGao Xiang goto err_out; 45864094a04SGao Xiang } 45964094a04SGao Xiang 46064094a04SGao Xiang if (grp != &pcl->obj) { 46164094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 46264094a04SGao Xiang err = -EEXIST; 46364094a04SGao Xiang goto err_out; 46447e4937aSGao Xiang } 46547e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 46647e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 46747e4937aSGao Xiang clt->tailpcl = pcl; 46847e4937aSGao Xiang clt->owned_head = &pcl->next; 46947e4937aSGao Xiang clt->pcl = pcl; 47047e4937aSGao Xiang clt->cl = cl; 4719e579fc1SGao Xiang return 0; 47264094a04SGao Xiang 47364094a04SGao Xiang err_out: 47464094a04SGao Xiang mutex_unlock(&cl->lock); 47564094a04SGao Xiang kmem_cache_free(pcluster_cachep, pcl); 47664094a04SGao Xiang return err; 47747e4937aSGao Xiang } 47847e4937aSGao Xiang 47947e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt, 48047e4937aSGao Xiang struct inode *inode, 48147e4937aSGao Xiang struct erofs_map_blocks *map) 48247e4937aSGao Xiang { 48364094a04SGao Xiang struct erofs_workgroup *grp; 4849e579fc1SGao Xiang int ret; 48547e4937aSGao Xiang 48647e4937aSGao Xiang DBG_BUGON(clt->cl); 48747e4937aSGao Xiang 48847e4937aSGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */ 48947e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL); 49047e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 49147e4937aSGao Xiang 49247e4937aSGao Xiang if (!PAGE_ALIGNED(map->m_pa)) { 49347e4937aSGao Xiang DBG_BUGON(1); 49447e4937aSGao Xiang return -EINVAL; 49547e4937aSGao Xiang } 49647e4937aSGao Xiang 49764094a04SGao Xiang grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT); 49864094a04SGao Xiang if (grp) { 49964094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 50064094a04SGao Xiang } else { 5019e579fc1SGao Xiang ret = z_erofs_register_collection(clt, inode, map); 50247e4937aSGao Xiang 50364094a04SGao Xiang if (!ret) 50464094a04SGao Xiang goto out; 50564094a04SGao Xiang if (ret != -EEXIST) 5069e579fc1SGao Xiang return ret; 50764094a04SGao Xiang } 50847e4937aSGao Xiang 50964094a04SGao Xiang ret = z_erofs_lookup_collection(clt, inode, map); 51064094a04SGao Xiang if (ret) { 51164094a04SGao Xiang erofs_workgroup_put(&clt->pcl->obj); 51264094a04SGao Xiang return ret; 51364094a04SGao Xiang } 51464094a04SGao Xiang 51564094a04SGao Xiang out: 51647e4937aSGao Xiang z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, 5179e579fc1SGao Xiang clt->cl->pagevec, clt->cl->vcnt); 51847e4937aSGao Xiang 51947e4937aSGao Xiang clt->compressedpages = clt->pcl->compressed_pages; 52047e4937aSGao Xiang if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */ 52147e4937aSGao Xiang clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES; 52247e4937aSGao Xiang return 0; 52347e4937aSGao Xiang } 52447e4937aSGao Xiang 52547e4937aSGao Xiang /* 52647e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 52747e4937aSGao Xiang * only after a RCU grace period. 52847e4937aSGao Xiang */ 52947e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 53047e4937aSGao Xiang { 53147e4937aSGao Xiang struct z_erofs_collection *const cl = 53247e4937aSGao Xiang container_of(head, struct z_erofs_collection, rcu); 53347e4937aSGao Xiang 53447e4937aSGao Xiang kmem_cache_free(pcluster_cachep, 53547e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, 53647e4937aSGao Xiang primary_collection)); 53747e4937aSGao Xiang } 53847e4937aSGao Xiang 53947e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 54047e4937aSGao Xiang { 54147e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 54247e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 54347e4937aSGao Xiang struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); 54447e4937aSGao Xiang 54547e4937aSGao Xiang call_rcu(&cl->rcu, z_erofs_rcu_callback); 54647e4937aSGao Xiang } 54747e4937aSGao Xiang 54847e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl) 54947e4937aSGao Xiang { 55047e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 55147e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, primary_collection); 55247e4937aSGao Xiang 55347e4937aSGao Xiang erofs_workgroup_put(&pcl->obj); 55447e4937aSGao Xiang } 55547e4937aSGao Xiang 55647e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt) 55747e4937aSGao Xiang { 55847e4937aSGao Xiang struct z_erofs_collection *cl = clt->cl; 55947e4937aSGao Xiang 56047e4937aSGao Xiang if (!cl) 56147e4937aSGao Xiang return false; 56247e4937aSGao Xiang 56347e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&clt->vector, false); 56447e4937aSGao Xiang mutex_unlock(&cl->lock); 56547e4937aSGao Xiang 56647e4937aSGao Xiang /* 56747e4937aSGao Xiang * if all pending pages are added, don't hold its reference 56847e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 56947e4937aSGao Xiang */ 57047e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 57147e4937aSGao Xiang z_erofs_collection_put(cl); 57247e4937aSGao Xiang 57347e4937aSGao Xiang clt->cl = NULL; 57447e4937aSGao Xiang return true; 57547e4937aSGao Xiang } 57647e4937aSGao Xiang 57747e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 57847e4937aSGao Xiang unsigned int cachestrategy, 57947e4937aSGao Xiang erofs_off_t la) 58047e4937aSGao Xiang { 58147e4937aSGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 58247e4937aSGao Xiang return false; 58347e4937aSGao Xiang 58447e4937aSGao Xiang if (fe->backmost) 58547e4937aSGao Xiang return true; 58647e4937aSGao Xiang 58747e4937aSGao Xiang return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 58847e4937aSGao Xiang la < fe->headoffset; 58947e4937aSGao Xiang } 59047e4937aSGao Xiang 59147e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 5921825c8d7SGao Xiang struct page *page, struct list_head *pagepool) 59347e4937aSGao Xiang { 59447e4937aSGao Xiang struct inode *const inode = fe->inode; 595bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 59647e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 59747e4937aSGao Xiang struct z_erofs_collector *const clt = &fe->clt; 59847e4937aSGao Xiang const loff_t offset = page_offset(page); 599dc76ea8cSGao Xiang bool tight = true; 60047e4937aSGao Xiang 60147e4937aSGao Xiang enum z_erofs_cache_alloctype cache_strategy; 60247e4937aSGao Xiang enum z_erofs_page_type page_type; 60347e4937aSGao Xiang unsigned int cur, end, spiltted, index; 60447e4937aSGao Xiang int err = 0; 60547e4937aSGao Xiang 60647e4937aSGao Xiang /* register locked file pages as online pages in pack */ 60747e4937aSGao Xiang z_erofs_onlinepage_init(page); 60847e4937aSGao Xiang 60947e4937aSGao Xiang spiltted = 0; 61047e4937aSGao Xiang end = PAGE_SIZE; 61147e4937aSGao Xiang repeat: 61247e4937aSGao Xiang cur = end - 1; 61347e4937aSGao Xiang 61447e4937aSGao Xiang /* lucky, within the range of the current map_blocks */ 61547e4937aSGao Xiang if (offset + cur >= map->m_la && 61647e4937aSGao Xiang offset + cur < map->m_la + map->m_llen) { 61747e4937aSGao Xiang /* didn't get a valid collection previously (very rare) */ 61847e4937aSGao Xiang if (!clt->cl) 61947e4937aSGao Xiang goto restart_now; 62047e4937aSGao Xiang goto hitted; 62147e4937aSGao Xiang } 62247e4937aSGao Xiang 62347e4937aSGao Xiang /* go ahead the next map_blocks */ 6244f761fa2SGao Xiang erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur); 62547e4937aSGao Xiang 62647e4937aSGao Xiang if (z_erofs_collector_end(clt)) 62747e4937aSGao Xiang fe->backmost = false; 62847e4937aSGao Xiang 62947e4937aSGao Xiang map->m_la = offset + cur; 63047e4937aSGao Xiang map->m_llen = 0; 63147e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 6328d8a09b0SGao Xiang if (err) 63347e4937aSGao Xiang goto err_out; 63447e4937aSGao Xiang 63547e4937aSGao Xiang restart_now: 6368d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) 63747e4937aSGao Xiang goto hitted; 63847e4937aSGao Xiang 63947e4937aSGao Xiang err = z_erofs_collector_begin(clt, inode, map); 6408d8a09b0SGao Xiang if (err) 64147e4937aSGao Xiang goto err_out; 64247e4937aSGao Xiang 64347e4937aSGao Xiang /* preload all compressed pages (maybe downgrade role if necessary) */ 644f57a3fe4SChao Yu if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la)) 6451825c8d7SGao Xiang cache_strategy = TRYALLOC; 64647e4937aSGao Xiang else 64747e4937aSGao Xiang cache_strategy = DONTALLOC; 64847e4937aSGao Xiang 6491825c8d7SGao Xiang preload_compressed_pages(clt, MNGD_MAPPING(sbi), 6501825c8d7SGao Xiang cache_strategy, pagepool); 65147e4937aSGao Xiang 65247e4937aSGao Xiang hitted: 653dc76ea8cSGao Xiang /* 654dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 655dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 656dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 657dc76ea8cSGao Xiang * for inplace I/O or pagevec (should be processed in strict order.) 658dc76ea8cSGao Xiang */ 659dc76ea8cSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED && 660dc76ea8cSGao Xiang clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); 661dc76ea8cSGao Xiang 66247e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 6638d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 66447e4937aSGao Xiang zero_user_segment(page, cur, end); 66547e4937aSGao Xiang goto next_part; 66647e4937aSGao Xiang } 66747e4937aSGao Xiang 66847e4937aSGao Xiang /* let's derive page type */ 66947e4937aSGao Xiang page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 67047e4937aSGao Xiang (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 67147e4937aSGao Xiang (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 67247e4937aSGao Xiang Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 67347e4937aSGao Xiang 67447e4937aSGao Xiang if (cur) 67547e4937aSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); 67647e4937aSGao Xiang 67747e4937aSGao Xiang retry: 67847e4937aSGao Xiang err = z_erofs_attach_page(clt, page, page_type); 6796aaa7b06SGao Xiang /* should allocate an additional short-lived page for pagevec */ 68047e4937aSGao Xiang if (err == -EAGAIN) { 68147e4937aSGao Xiang struct page *const newpage = 682e3f78d5eSChao Yu alloc_page(GFP_NOFS | __GFP_NOFAIL); 68347e4937aSGao Xiang 6846aaa7b06SGao Xiang set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE); 68547e4937aSGao Xiang err = z_erofs_attach_page(clt, newpage, 68647e4937aSGao Xiang Z_EROFS_PAGE_TYPE_EXCLUSIVE); 6878d8a09b0SGao Xiang if (!err) 68847e4937aSGao Xiang goto retry; 68947e4937aSGao Xiang } 69047e4937aSGao Xiang 6918d8a09b0SGao Xiang if (err) 69247e4937aSGao Xiang goto err_out; 69347e4937aSGao Xiang 69447e4937aSGao Xiang index = page->index - (map->m_la >> PAGE_SHIFT); 69547e4937aSGao Xiang 69647e4937aSGao Xiang z_erofs_onlinepage_fixup(page, index, true); 69747e4937aSGao Xiang 69847e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 69947e4937aSGao Xiang ++spiltted; 70047e4937aSGao Xiang /* also update nr_pages */ 70147e4937aSGao Xiang clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); 70247e4937aSGao Xiang next_part: 70347e4937aSGao Xiang /* can be used for verification */ 70447e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 70547e4937aSGao Xiang 70647e4937aSGao Xiang end = cur; 70747e4937aSGao Xiang if (end > 0) 70847e4937aSGao Xiang goto repeat; 70947e4937aSGao Xiang 71047e4937aSGao Xiang out: 71147e4937aSGao Xiang z_erofs_onlinepage_endio(page); 71247e4937aSGao Xiang 7134f761fa2SGao Xiang erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 71447e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 71547e4937aSGao Xiang return err; 71647e4937aSGao Xiang 71747e4937aSGao Xiang /* if some error occurred while processing this page */ 71847e4937aSGao Xiang err_out: 71947e4937aSGao Xiang SetPageError(page); 72047e4937aSGao Xiang goto out; 72147e4937aSGao Xiang } 72247e4937aSGao Xiang 723648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work); 724a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 725a4b1fab1SGao Xiang bool sync, int bios) 72647e4937aSGao Xiang { 72730048cdaSHuang Jianan struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 72830048cdaSHuang Jianan 729a4b1fab1SGao Xiang /* wake up the caller thread for sync decompression */ 730a4b1fab1SGao Xiang if (sync) { 73147e4937aSGao Xiang unsigned long flags; 73247e4937aSGao Xiang 73347e4937aSGao Xiang spin_lock_irqsave(&io->u.wait.lock, flags); 73447e4937aSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 73547e4937aSGao Xiang wake_up_locked(&io->u.wait); 73647e4937aSGao Xiang spin_unlock_irqrestore(&io->u.wait.lock, flags); 73747e4937aSGao Xiang return; 73847e4937aSGao Xiang } 73947e4937aSGao Xiang 740648f2de0SHuang Jianan if (atomic_add_return(bios, &io->pending_bios)) 741648f2de0SHuang Jianan return; 74230048cdaSHuang Jianan /* Use workqueue and sync decompression for atomic contexts only */ 743648f2de0SHuang Jianan if (in_atomic() || irqs_disabled()) { 74447e4937aSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 74530048cdaSHuang Jianan sbi->ctx.readahead_sync_decompress = true; 746648f2de0SHuang Jianan return; 747648f2de0SHuang Jianan } 748648f2de0SHuang Jianan z_erofs_decompressqueue_work(&io->u.work); 74947e4937aSGao Xiang } 75047e4937aSGao Xiang 7516aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 7526aaa7b06SGao Xiang { 7536aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 7546aaa7b06SGao Xiang } 7556aaa7b06SGao Xiang 7560c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 75747e4937aSGao Xiang { 758a4b1fab1SGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 759a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 76047e4937aSGao Xiang blk_status_t err = bio->bi_status; 76147e4937aSGao Xiang struct bio_vec *bvec; 76247e4937aSGao Xiang struct bvec_iter_all iter_all; 76347e4937aSGao Xiang 76447e4937aSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 76547e4937aSGao Xiang struct page *page = bvec->bv_page; 76647e4937aSGao Xiang 76747e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 7686aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 76947e4937aSGao Xiang 7708d8a09b0SGao Xiang if (err) 77147e4937aSGao Xiang SetPageError(page); 77247e4937aSGao Xiang 773a4b1fab1SGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 774a4b1fab1SGao Xiang if (!err) 775a4b1fab1SGao Xiang SetPageUptodate(page); 77647e4937aSGao Xiang unlock_page(page); 77747e4937aSGao Xiang } 778a4b1fab1SGao Xiang } 779a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 78047e4937aSGao Xiang bio_put(bio); 78147e4937aSGao Xiang } 78247e4937aSGao Xiang 78347e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb, 78447e4937aSGao Xiang struct z_erofs_pcluster *pcl, 78547e4937aSGao Xiang struct list_head *pagepool) 78647e4937aSGao Xiang { 78747e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 78847e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 78947e4937aSGao Xiang struct z_erofs_pagevec_ctor ctor; 79047e4937aSGao Xiang unsigned int i, outputsize, llen, nr_pages; 79147e4937aSGao Xiang struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 79247e4937aSGao Xiang struct page **pages, **compressed_pages, *page; 79347e4937aSGao Xiang 79447e4937aSGao Xiang enum z_erofs_page_type page_type; 79547e4937aSGao Xiang bool overlapped, partial; 79647e4937aSGao Xiang struct z_erofs_collection *cl; 79747e4937aSGao Xiang int err; 79847e4937aSGao Xiang 79947e4937aSGao Xiang might_sleep(); 80047e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 80147e4937aSGao Xiang DBG_BUGON(!READ_ONCE(cl->nr_pages)); 80247e4937aSGao Xiang 80347e4937aSGao Xiang mutex_lock(&cl->lock); 80447e4937aSGao Xiang nr_pages = cl->nr_pages; 80547e4937aSGao Xiang 8068d8a09b0SGao Xiang if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { 80747e4937aSGao Xiang pages = pages_onstack; 80847e4937aSGao Xiang } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 80947e4937aSGao Xiang mutex_trylock(&z_pagemap_global_lock)) { 81047e4937aSGao Xiang pages = z_pagemap_global; 81147e4937aSGao Xiang } else { 81247e4937aSGao Xiang gfp_t gfp_flags = GFP_KERNEL; 81347e4937aSGao Xiang 81447e4937aSGao Xiang if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 81547e4937aSGao Xiang gfp_flags |= __GFP_NOFAIL; 81647e4937aSGao Xiang 81747e4937aSGao Xiang pages = kvmalloc_array(nr_pages, sizeof(struct page *), 81847e4937aSGao Xiang gfp_flags); 81947e4937aSGao Xiang 82047e4937aSGao Xiang /* fallback to global pagemap for the lowmem scenario */ 8218d8a09b0SGao Xiang if (!pages) { 82247e4937aSGao Xiang mutex_lock(&z_pagemap_global_lock); 82347e4937aSGao Xiang pages = z_pagemap_global; 82447e4937aSGao Xiang } 82547e4937aSGao Xiang } 82647e4937aSGao Xiang 82747e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) 82847e4937aSGao Xiang pages[i] = NULL; 82947e4937aSGao Xiang 83047e4937aSGao Xiang err = 0; 83147e4937aSGao Xiang z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, 83247e4937aSGao Xiang cl->pagevec, 0); 83347e4937aSGao Xiang 83447e4937aSGao Xiang for (i = 0; i < cl->vcnt; ++i) { 83547e4937aSGao Xiang unsigned int pagenr; 83647e4937aSGao Xiang 83747e4937aSGao Xiang page = z_erofs_pagevec_dequeue(&ctor, &page_type); 83847e4937aSGao Xiang 83947e4937aSGao Xiang /* all pages in pagevec ought to be valid */ 84047e4937aSGao Xiang DBG_BUGON(!page); 8416aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 84247e4937aSGao Xiang 8436aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 84447e4937aSGao Xiang continue; 84547e4937aSGao Xiang 84647e4937aSGao Xiang if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) 84747e4937aSGao Xiang pagenr = 0; 84847e4937aSGao Xiang else 84947e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 85047e4937aSGao Xiang 85147e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 85247e4937aSGao Xiang 85347e4937aSGao Xiang /* 85447e4937aSGao Xiang * currently EROFS doesn't support multiref(dedup), 85547e4937aSGao Xiang * so here erroring out one multiref page. 85647e4937aSGao Xiang */ 8578d8a09b0SGao Xiang if (pages[pagenr]) { 85847e4937aSGao Xiang DBG_BUGON(1); 85947e4937aSGao Xiang SetPageError(pages[pagenr]); 86047e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 86147e4937aSGao Xiang err = -EFSCORRUPTED; 86247e4937aSGao Xiang } 86347e4937aSGao Xiang pages[pagenr] = page; 86447e4937aSGao Xiang } 86547e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&ctor, true); 86647e4937aSGao Xiang 86747e4937aSGao Xiang overlapped = false; 86847e4937aSGao Xiang compressed_pages = pcl->compressed_pages; 86947e4937aSGao Xiang 87047e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 87147e4937aSGao Xiang unsigned int pagenr; 87247e4937aSGao Xiang 87347e4937aSGao Xiang page = compressed_pages[i]; 87447e4937aSGao Xiang 87547e4937aSGao Xiang /* all compressed pages ought to be valid */ 87647e4937aSGao Xiang DBG_BUGON(!page); 8776aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 87847e4937aSGao Xiang 8796aaa7b06SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 88047e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) { 8818d8a09b0SGao Xiang if (!PageUptodate(page)) 88247e4937aSGao Xiang err = -EIO; 88347e4937aSGao Xiang continue; 88447e4937aSGao Xiang } 88547e4937aSGao Xiang 88647e4937aSGao Xiang /* 88747e4937aSGao Xiang * only if non-head page can be selected 88847e4937aSGao Xiang * for inplace decompression 88947e4937aSGao Xiang */ 89047e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 89147e4937aSGao Xiang 89247e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 8938d8a09b0SGao Xiang if (pages[pagenr]) { 89447e4937aSGao Xiang DBG_BUGON(1); 89547e4937aSGao Xiang SetPageError(pages[pagenr]); 89647e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 89747e4937aSGao Xiang err = -EFSCORRUPTED; 89847e4937aSGao Xiang } 89947e4937aSGao Xiang pages[pagenr] = page; 90047e4937aSGao Xiang 90147e4937aSGao Xiang overlapped = true; 90247e4937aSGao Xiang } 90347e4937aSGao Xiang 9046aaa7b06SGao Xiang /* PG_error needs checking for all non-managed pages */ 9058d8a09b0SGao Xiang if (PageError(page)) { 90647e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 90747e4937aSGao Xiang err = -EIO; 90847e4937aSGao Xiang } 90947e4937aSGao Xiang } 91047e4937aSGao Xiang 9118d8a09b0SGao Xiang if (err) 91247e4937aSGao Xiang goto out; 91347e4937aSGao Xiang 91447e4937aSGao Xiang llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 91547e4937aSGao Xiang if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { 91647e4937aSGao Xiang outputsize = llen; 91747e4937aSGao Xiang partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 91847e4937aSGao Xiang } else { 91947e4937aSGao Xiang outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; 92047e4937aSGao Xiang partial = true; 92147e4937aSGao Xiang } 92247e4937aSGao Xiang 92347e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 92447e4937aSGao Xiang .sb = sb, 92547e4937aSGao Xiang .in = compressed_pages, 92647e4937aSGao Xiang .out = pages, 92747e4937aSGao Xiang .pageofs_out = cl->pageofs, 92847e4937aSGao Xiang .inputsize = PAGE_SIZE, 92947e4937aSGao Xiang .outputsize = outputsize, 93047e4937aSGao Xiang .alg = pcl->algorithmformat, 93147e4937aSGao Xiang .inplace_io = overlapped, 93247e4937aSGao Xiang .partial_decoding = partial 93347e4937aSGao Xiang }, pagepool); 93447e4937aSGao Xiang 93547e4937aSGao Xiang out: 93647e4937aSGao Xiang /* must handle all compressed pages before endding pages */ 93747e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 93847e4937aSGao Xiang page = compressed_pages[i]; 93947e4937aSGao Xiang 94047e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 94147e4937aSGao Xiang continue; 94247e4937aSGao Xiang 9436aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 9446aaa7b06SGao Xiang (void)z_erofs_put_shortlivedpage(pagepool, page); 94547e4937aSGao Xiang 94647e4937aSGao Xiang WRITE_ONCE(compressed_pages[i], NULL); 94747e4937aSGao Xiang } 94847e4937aSGao Xiang 94947e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) { 95047e4937aSGao Xiang page = pages[i]; 95147e4937aSGao Xiang if (!page) 95247e4937aSGao Xiang continue; 95347e4937aSGao Xiang 9546aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 95547e4937aSGao Xiang 9566aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 9576aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 95847e4937aSGao Xiang continue; 95947e4937aSGao Xiang 9608d8a09b0SGao Xiang if (err < 0) 96147e4937aSGao Xiang SetPageError(page); 96247e4937aSGao Xiang 96347e4937aSGao Xiang z_erofs_onlinepage_endio(page); 96447e4937aSGao Xiang } 96547e4937aSGao Xiang 96647e4937aSGao Xiang if (pages == z_pagemap_global) 96747e4937aSGao Xiang mutex_unlock(&z_pagemap_global_lock); 9688d8a09b0SGao Xiang else if (pages != pages_onstack) 96947e4937aSGao Xiang kvfree(pages); 97047e4937aSGao Xiang 97147e4937aSGao Xiang cl->nr_pages = 0; 97247e4937aSGao Xiang cl->vcnt = 0; 97347e4937aSGao Xiang 97447e4937aSGao Xiang /* all cl locks MUST be taken before the following line */ 97547e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 97647e4937aSGao Xiang 97747e4937aSGao Xiang /* all cl locks SHOULD be released right now */ 97847e4937aSGao Xiang mutex_unlock(&cl->lock); 97947e4937aSGao Xiang 98047e4937aSGao Xiang z_erofs_collection_put(cl); 98147e4937aSGao Xiang return err; 98247e4937aSGao Xiang } 98347e4937aSGao Xiang 9840c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 98547e4937aSGao Xiang struct list_head *pagepool) 98647e4937aSGao Xiang { 98747e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 98847e4937aSGao Xiang 98947e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 99047e4937aSGao Xiang struct z_erofs_pcluster *pcl; 99147e4937aSGao Xiang 99247e4937aSGao Xiang /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 99347e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 99447e4937aSGao Xiang 99547e4937aSGao Xiang /* no possible that 'owned' equals NULL */ 99647e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 99747e4937aSGao Xiang 99847e4937aSGao Xiang pcl = container_of(owned, struct z_erofs_pcluster, next); 99947e4937aSGao Xiang owned = READ_ONCE(pcl->next); 100047e4937aSGao Xiang 1001a4b1fab1SGao Xiang z_erofs_decompress_pcluster(io->sb, pcl, pagepool); 100247e4937aSGao Xiang } 100347e4937aSGao Xiang } 100447e4937aSGao Xiang 10050c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 100647e4937aSGao Xiang { 1007a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 1008a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 100947e4937aSGao Xiang LIST_HEAD(pagepool); 101047e4937aSGao Xiang 1011a4b1fab1SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 10120c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 101347e4937aSGao Xiang 101447e4937aSGao Xiang put_pages_list(&pagepool); 1015a4b1fab1SGao Xiang kvfree(bgq); 101647e4937aSGao Xiang } 101747e4937aSGao Xiang 101847e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 101947e4937aSGao Xiang unsigned int nr, 102047e4937aSGao Xiang struct list_head *pagepool, 102147e4937aSGao Xiang struct address_space *mc, 102247e4937aSGao Xiang gfp_t gfp) 102347e4937aSGao Xiang { 102447e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 102547e4937aSGao Xiang bool tocache = false; 102647e4937aSGao Xiang 102747e4937aSGao Xiang struct address_space *mapping; 102847e4937aSGao Xiang struct page *oldpage, *page; 102947e4937aSGao Xiang 103047e4937aSGao Xiang compressed_page_t t; 103147e4937aSGao Xiang int justfound; 103247e4937aSGao Xiang 103347e4937aSGao Xiang repeat: 103447e4937aSGao Xiang page = READ_ONCE(pcl->compressed_pages[nr]); 103547e4937aSGao Xiang oldpage = page; 103647e4937aSGao Xiang 103747e4937aSGao Xiang if (!page) 103847e4937aSGao Xiang goto out_allocpage; 103947e4937aSGao Xiang 104047e4937aSGao Xiang /* 104147e4937aSGao Xiang * the cached page has not been allocated and 104247e4937aSGao Xiang * an placeholder is out there, prepare it now. 104347e4937aSGao Xiang */ 1044bda17a45SGao Xiang if (page == PAGE_UNALLOCATED) { 104547e4937aSGao Xiang tocache = true; 104647e4937aSGao Xiang goto out_allocpage; 104747e4937aSGao Xiang } 104847e4937aSGao Xiang 104947e4937aSGao Xiang /* process the target tagged pointer */ 105047e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 105147e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 105247e4937aSGao Xiang page = tagptr_unfold_ptr(t); 105347e4937aSGao Xiang 10541825c8d7SGao Xiang /* 10551825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 10561825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 10571825c8d7SGao Xiang */ 10581825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 10591825c8d7SGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 10601825c8d7SGao Xiang set_page_private(page, 0); 10611825c8d7SGao Xiang tocache = true; 10621825c8d7SGao Xiang goto out_tocache; 10631825c8d7SGao Xiang } 106447e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 106547e4937aSGao Xiang 106647e4937aSGao Xiang /* 10676aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 106847e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 106947e4937aSGao Xiang */ 107047e4937aSGao Xiang if (mapping && mapping != mc) 107147e4937aSGao Xiang /* ought to be unmanaged pages */ 107247e4937aSGao Xiang goto out; 107347e4937aSGao Xiang 10746aaa7b06SGao Xiang /* directly return for shortlived page as well */ 10756aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 10766aaa7b06SGao Xiang goto out; 10776aaa7b06SGao Xiang 107847e4937aSGao Xiang lock_page(page); 107947e4937aSGao Xiang 108047e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 108147e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 108247e4937aSGao Xiang 108347e4937aSGao Xiang /* the page is still in manage cache */ 108447e4937aSGao Xiang if (page->mapping == mc) { 108547e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 108647e4937aSGao Xiang 108747e4937aSGao Xiang ClearPageError(page); 108847e4937aSGao Xiang if (!PagePrivate(page)) { 108947e4937aSGao Xiang /* 109047e4937aSGao Xiang * impossible to be !PagePrivate(page) for 109147e4937aSGao Xiang * the current restriction as well if 109247e4937aSGao Xiang * the page is already in compressed_pages[]. 109347e4937aSGao Xiang */ 109447e4937aSGao Xiang DBG_BUGON(!justfound); 109547e4937aSGao Xiang 109647e4937aSGao Xiang justfound = 0; 109747e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 109847e4937aSGao Xiang SetPagePrivate(page); 109947e4937aSGao Xiang } 110047e4937aSGao Xiang 110147e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 110247e4937aSGao Xiang if (PageUptodate(page)) { 110347e4937aSGao Xiang unlock_page(page); 110447e4937aSGao Xiang page = NULL; 110547e4937aSGao Xiang } 110647e4937aSGao Xiang goto out; 110747e4937aSGao Xiang } 110847e4937aSGao Xiang 110947e4937aSGao Xiang /* 111047e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 111147e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 111247e4937aSGao Xiang */ 111347e4937aSGao Xiang DBG_BUGON(page->mapping); 111447e4937aSGao Xiang DBG_BUGON(!justfound); 111547e4937aSGao Xiang 111647e4937aSGao Xiang tocache = true; 111747e4937aSGao Xiang unlock_page(page); 111847e4937aSGao Xiang put_page(page); 111947e4937aSGao Xiang out_allocpage: 11205ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 11215ddcee1fSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 11225ddcee1fSGao Xiang list_add(&page->lru, pagepool); 11235ddcee1fSGao Xiang cond_resched(); 11245ddcee1fSGao Xiang goto repeat; 11255ddcee1fSGao Xiang } 11261825c8d7SGao Xiang out_tocache: 1127bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1128bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1129bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1130bf225074SGao Xiang goto out; 1131a30573b3SGao Xiang } 1132bf225074SGao Xiang attach_page_private(page, pcl); 1133bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1134bf225074SGao Xiang put_page(page); 1135bf225074SGao Xiang 113647e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 113747e4937aSGao Xiang return page; 113847e4937aSGao Xiang } 113947e4937aSGao Xiang 1140a4b1fab1SGao Xiang static struct z_erofs_decompressqueue * 1141a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb, 1142a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 114347e4937aSGao Xiang { 1144a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 114547e4937aSGao Xiang 1146a4b1fab1SGao Xiang if (fg && !*fg) { 1147a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1148a4b1fab1SGao Xiang if (!q) { 1149a4b1fab1SGao Xiang *fg = true; 1150a4b1fab1SGao Xiang goto fg_out; 115147e4937aSGao Xiang } 11520c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1153a4b1fab1SGao Xiang } else { 1154a4b1fab1SGao Xiang fg_out: 1155a4b1fab1SGao Xiang q = fgq; 1156a4b1fab1SGao Xiang init_waitqueue_head(&fgq->u.wait); 1157a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 1158a4b1fab1SGao Xiang } 1159a4b1fab1SGao Xiang q->sb = sb; 1160a4b1fab1SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1161a4b1fab1SGao Xiang return q; 116247e4937aSGao Xiang } 116347e4937aSGao Xiang 116447e4937aSGao Xiang /* define decompression jobqueue types */ 116547e4937aSGao Xiang enum { 116647e4937aSGao Xiang JQ_BYPASS, 116747e4937aSGao Xiang JQ_SUBMIT, 116847e4937aSGao Xiang NR_JOBQUEUES, 116947e4937aSGao Xiang }; 117047e4937aSGao Xiang 117147e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 1172a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[], 1173a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 117447e4937aSGao Xiang { 117547e4937aSGao Xiang /* 117647e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 117747e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 117847e4937aSGao Xiang */ 1179a4b1fab1SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1180a4b1fab1SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 118147e4937aSGao Xiang 1182a4b1fab1SGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 118347e4937aSGao Xiang } 118447e4937aSGao Xiang 118547e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 118647e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 118747e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 118847e4937aSGao Xiang { 118947e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 119047e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 119147e4937aSGao Xiang 119247e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 119347e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 119447e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 119547e4937aSGao Xiang 119647e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 119747e4937aSGao Xiang 119847e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 119947e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 120047e4937aSGao Xiang 120147e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 120247e4937aSGao Xiang } 120347e4937aSGao Xiang 12041e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb, 12056ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 120647e4937aSGao Xiang struct list_head *pagepool, 1207a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1208a4b1fab1SGao Xiang bool *force_fg) 120947e4937aSGao Xiang { 1210bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 121147e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1212a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 121347e4937aSGao Xiang void *bi_private; 12146ea5aad3SGao Xiang z_erofs_next_pcluster_t owned_head = f->clt.owned_head; 121547e4937aSGao Xiang /* since bio will be NULL, no need to initialize last_index */ 12163f649ab7SKees Cook pgoff_t last_index; 12171e4a2955SGao Xiang unsigned int nr_bios = 0; 12181e4a2955SGao Xiang struct bio *bio = NULL; 121947e4937aSGao Xiang 1220a4b1fab1SGao Xiang bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1221a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1222a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 122347e4937aSGao Xiang 122447e4937aSGao Xiang /* by default, all need io submission */ 122547e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 122647e4937aSGao Xiang 122747e4937aSGao Xiang do { 122847e4937aSGao Xiang struct z_erofs_pcluster *pcl; 12291e4a2955SGao Xiang pgoff_t cur, end; 12301e4a2955SGao Xiang unsigned int i = 0; 12311e4a2955SGao Xiang bool bypass = true; 123247e4937aSGao Xiang 123347e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 123447e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 123547e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 123647e4937aSGao Xiang 123747e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 123847e4937aSGao Xiang 12391e4a2955SGao Xiang cur = pcl->obj.index; 12401e4a2955SGao Xiang end = cur + BIT(pcl->clusterbits); 124147e4937aSGao Xiang 124247e4937aSGao Xiang /* close the main owned chain at first */ 124347e4937aSGao Xiang owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 124447e4937aSGao Xiang Z_EROFS_PCLUSTER_TAIL_CLOSED); 124547e4937aSGao Xiang 12461e4a2955SGao Xiang do { 12471e4a2955SGao Xiang struct page *page; 124847e4937aSGao Xiang 12491e4a2955SGao Xiang page = pickup_page_for_submission(pcl, i++, pagepool, 125047e4937aSGao Xiang MNGD_MAPPING(sbi), 125147e4937aSGao Xiang GFP_NOFS); 12521e4a2955SGao Xiang if (!page) 12531e4a2955SGao Xiang continue; 125447e4937aSGao Xiang 12551e4a2955SGao Xiang if (bio && cur != last_index + 1) { 125647e4937aSGao Xiang submit_bio_retry: 125794e4e153SGao Xiang submit_bio(bio); 125847e4937aSGao Xiang bio = NULL; 125947e4937aSGao Xiang } 126047e4937aSGao Xiang 126147e4937aSGao Xiang if (!bio) { 1262a8affc03SChristoph Hellwig bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS); 1263a5c0b780SGao Xiang 12640c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1265a5c0b780SGao Xiang bio_set_dev(bio, sb->s_bdev); 12661e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 1267a5c0b780SGao Xiang LOG_SECTORS_PER_BLOCK; 1268a5c0b780SGao Xiang bio->bi_private = bi_private; 126994e4e153SGao Xiang bio->bi_opf = REQ_OP_READ; 12706ea5aad3SGao Xiang if (f->readahead) 12716ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 127247e4937aSGao Xiang ++nr_bios; 127347e4937aSGao Xiang } 127447e4937aSGao Xiang 12756c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 127647e4937aSGao Xiang goto submit_bio_retry; 127747e4937aSGao Xiang 12781e4a2955SGao Xiang last_index = cur; 12791e4a2955SGao Xiang bypass = false; 12801e4a2955SGao Xiang } while (++cur < end); 128147e4937aSGao Xiang 12821e4a2955SGao Xiang if (!bypass) 128347e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 128447e4937aSGao Xiang else 128547e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 128647e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 128747e4937aSGao Xiang 128847e4937aSGao Xiang if (bio) 128994e4e153SGao Xiang submit_bio(bio); 129047e4937aSGao Xiang 1291587a67b7SGao Xiang /* 1292587a67b7SGao Xiang * although background is preferred, no one is pending for submission. 1293587a67b7SGao Xiang * don't issue workqueue for decompression but drop it directly instead. 1294587a67b7SGao Xiang */ 1295587a67b7SGao Xiang if (!*force_fg && !nr_bios) { 1296587a67b7SGao Xiang kvfree(q[JQ_SUBMIT]); 12971e4a2955SGao Xiang return; 1298587a67b7SGao Xiang } 1299a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 130047e4937aSGao Xiang } 130147e4937aSGao Xiang 13020c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb, 13036ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 13040c638f70SGao Xiang struct list_head *pagepool, bool force_fg) 130547e4937aSGao Xiang { 1306a4b1fab1SGao Xiang struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 130747e4937aSGao Xiang 13086ea5aad3SGao Xiang if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL) 130947e4937aSGao Xiang return; 13106ea5aad3SGao Xiang z_erofs_submit_queue(sb, f, pagepool, io, &force_fg); 131147e4937aSGao Xiang 13120c638f70SGao Xiang /* handle bypass queue (no i/o pclusters) immediately */ 13130c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 131447e4937aSGao Xiang 131547e4937aSGao Xiang if (!force_fg) 131647e4937aSGao Xiang return; 131747e4937aSGao Xiang 131847e4937aSGao Xiang /* wait until all bios are completed */ 1319a93f8c36SGao Xiang io_wait_event(io[JQ_SUBMIT].u.wait, 132047e4937aSGao Xiang !atomic_read(&io[JQ_SUBMIT].pending_bios)); 132147e4937aSGao Xiang 13220c638f70SGao Xiang /* handle synchronous decompress queue in the caller context */ 13230c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 132447e4937aSGao Xiang } 132547e4937aSGao Xiang 13260c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page) 132747e4937aSGao Xiang { 132847e4937aSGao Xiang struct inode *const inode = page->mapping->host; 132947e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 133047e4937aSGao Xiang int err; 133147e4937aSGao Xiang LIST_HEAD(pagepool); 133247e4937aSGao Xiang 133347e4937aSGao Xiang trace_erofs_readpage(page, false); 133447e4937aSGao Xiang 133547e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 133647e4937aSGao Xiang 13371825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 133847e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 133947e4937aSGao Xiang 134047e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 13416ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, true); 134247e4937aSGao Xiang 134347e4937aSGao Xiang if (err) 13444f761fa2SGao Xiang erofs_err(inode->i_sb, "failed to read, err [%d]", err); 134547e4937aSGao Xiang 134647e4937aSGao Xiang if (f.map.mpage) 134747e4937aSGao Xiang put_page(f.map.mpage); 134847e4937aSGao Xiang 134947e4937aSGao Xiang /* clean up the remaining free pages */ 135047e4937aSGao Xiang put_pages_list(&pagepool); 135147e4937aSGao Xiang return err; 135247e4937aSGao Xiang } 135347e4937aSGao Xiang 13540615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac) 135547e4937aSGao Xiang { 13560615090cSMatthew Wilcox (Oracle) struct inode *const inode = rac->mapping->host; 135747e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 135847e4937aSGao Xiang 1359bf9a123bSGao Xiang unsigned int nr_pages = readahead_count(rac); 136030048cdaSHuang Jianan bool sync = (sbi->ctx.readahead_sync_decompress && 136130048cdaSHuang Jianan nr_pages <= sbi->ctx.max_sync_decompress_pages); 136247e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 13630615090cSMatthew Wilcox (Oracle) struct page *page, *head = NULL; 136447e4937aSGao Xiang LIST_HEAD(pagepool); 136547e4937aSGao Xiang 1366bf9a123bSGao Xiang trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 136747e4937aSGao Xiang 13686ea5aad3SGao Xiang f.readahead = true; 13690615090cSMatthew Wilcox (Oracle) f.headoffset = readahead_pos(rac); 137047e4937aSGao Xiang 13710615090cSMatthew Wilcox (Oracle) while ((page = readahead_page(rac))) { 137247e4937aSGao Xiang prefetchw(&page->flags); 137347e4937aSGao Xiang 137447e4937aSGao Xiang /* 137547e4937aSGao Xiang * A pure asynchronous readahead is indicated if 137647e4937aSGao Xiang * a PG_readahead marked page is hitted at first. 137747e4937aSGao Xiang * Let's also do asynchronous decompression for this case. 137847e4937aSGao Xiang */ 137947e4937aSGao Xiang sync &= !(PageReadahead(page) && !head); 138047e4937aSGao Xiang 138147e4937aSGao Xiang set_page_private(page, (unsigned long)head); 138247e4937aSGao Xiang head = page; 138347e4937aSGao Xiang } 138447e4937aSGao Xiang 138547e4937aSGao Xiang while (head) { 138647e4937aSGao Xiang struct page *page = head; 138747e4937aSGao Xiang int err; 138847e4937aSGao Xiang 138947e4937aSGao Xiang /* traversal in reverse order */ 139047e4937aSGao Xiang head = (void *)page_private(page); 139147e4937aSGao Xiang 13921825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 1393a5876e24SGao Xiang if (err) 13944f761fa2SGao Xiang erofs_err(inode->i_sb, 13954f761fa2SGao Xiang "readahead error at page %lu @ nid %llu", 13964f761fa2SGao Xiang page->index, EROFS_I(inode)->nid); 139747e4937aSGao Xiang put_page(page); 139847e4937aSGao Xiang } 139947e4937aSGao Xiang 140047e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 140147e4937aSGao Xiang 14026ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync); 140347e4937aSGao Xiang 140447e4937aSGao Xiang if (f.map.mpage) 140547e4937aSGao Xiang put_page(f.map.mpage); 140647e4937aSGao Xiang 140747e4937aSGao Xiang /* clean up the remaining free pages */ 140847e4937aSGao Xiang put_pages_list(&pagepool); 140947e4937aSGao Xiang } 141047e4937aSGao Xiang 14110c638f70SGao Xiang const struct address_space_operations z_erofs_aops = { 14120c638f70SGao Xiang .readpage = z_erofs_readpage, 14130615090cSMatthew Wilcox (Oracle) .readahead = z_erofs_readahead, 141447e4937aSGao Xiang }; 141547e4937aSGao Xiang 1416