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, 10747e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED_NOINPLACE, 10847e4937aSGao Xiang /* 10947e4937aSGao Xiang * The current collection has been linked with the owned chain, and 11047e4937aSGao Xiang * could also be linked with the remaining collections, which means 11147e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 11247e4937aSGao Xiang * the current collection can safely use the whole page (since 11347e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 11447e4937aSGao Xiang * illustrated below: 11547e4937aSGao Xiang * ________________________________________________________________ 11647e4937aSGao Xiang * | tail (partial) page | head (partial) page | 11747e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 11847e4937aSGao Xiang * | PRIMARY_FOLLOWED or | | 11947e4937aSGao Xiang * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| 12047e4937aSGao Xiang * 12147e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 12247e4937aSGao Xiang */ 12347e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED, 12447e4937aSGao Xiang }; 12547e4937aSGao Xiang 12647e4937aSGao Xiang struct z_erofs_collector { 12747e4937aSGao Xiang struct z_erofs_pagevec_ctor vector; 12847e4937aSGao Xiang 12947e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 13047e4937aSGao Xiang struct z_erofs_collection *cl; 13147e4937aSGao Xiang struct page **compressedpages; 13247e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 13347e4937aSGao Xiang 13447e4937aSGao Xiang enum z_erofs_collectmode mode; 13547e4937aSGao Xiang }; 13647e4937aSGao Xiang 13747e4937aSGao Xiang struct z_erofs_decompress_frontend { 13847e4937aSGao Xiang struct inode *const inode; 13947e4937aSGao Xiang 14047e4937aSGao Xiang struct z_erofs_collector clt; 14147e4937aSGao Xiang struct erofs_map_blocks map; 14247e4937aSGao Xiang 1436ea5aad3SGao Xiang bool readahead; 14447e4937aSGao Xiang /* used for applying cache strategy on the fly */ 14547e4937aSGao Xiang bool backmost; 14647e4937aSGao Xiang erofs_off_t headoffset; 14747e4937aSGao Xiang }; 14847e4937aSGao Xiang 14947e4937aSGao Xiang #define COLLECTOR_INIT() { \ 15047e4937aSGao Xiang .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 15147e4937aSGao Xiang .mode = COLLECT_PRIMARY_FOLLOWED } 15247e4937aSGao Xiang 15347e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 15447e4937aSGao Xiang .inode = __i, .clt = COLLECTOR_INIT(), \ 15547e4937aSGao Xiang .backmost = true, } 15647e4937aSGao Xiang 15747e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; 15847e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock); 15947e4937aSGao Xiang 16047e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt, 16147e4937aSGao Xiang struct address_space *mc, 1621825c8d7SGao Xiang enum z_erofs_cache_alloctype type, 1631825c8d7SGao Xiang struct list_head *pagepool) 16447e4937aSGao Xiang { 16547e4937aSGao Xiang const struct z_erofs_pcluster *pcl = clt->pcl; 16647e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 16747e4937aSGao Xiang struct page **pages = clt->compressedpages; 16847e4937aSGao Xiang pgoff_t index = pcl->obj.index + (pages - pcl->compressed_pages); 16947e4937aSGao Xiang bool standalone = true; 1701825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 1711825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 17247e4937aSGao Xiang 17347e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED) 17447e4937aSGao Xiang return; 17547e4937aSGao Xiang 17647e4937aSGao Xiang for (; pages < pcl->compressed_pages + clusterpages; ++pages) { 17747e4937aSGao Xiang struct page *page; 17847e4937aSGao Xiang compressed_page_t t; 1791825c8d7SGao Xiang struct page *newpage = NULL; 18047e4937aSGao Xiang 18147e4937aSGao Xiang /* the compressed page was loaded before */ 18247e4937aSGao Xiang if (READ_ONCE(*pages)) 18347e4937aSGao Xiang continue; 18447e4937aSGao Xiang 18547e4937aSGao Xiang page = find_get_page(mc, index); 18647e4937aSGao Xiang 18747e4937aSGao Xiang if (page) { 18847e4937aSGao Xiang t = tag_compressed_page_justfound(page); 18947e4937aSGao Xiang } else if (type == DELAYEDALLOC) { 19047e4937aSGao Xiang t = tagptr_init(compressed_page_t, PAGE_UNALLOCATED); 1911825c8d7SGao Xiang } else if (type == TRYALLOC) { 1921825c8d7SGao Xiang newpage = erofs_allocpage(pagepool, gfp); 1931825c8d7SGao Xiang if (!newpage) 1941825c8d7SGao Xiang goto dontalloc; 1951825c8d7SGao Xiang 1961825c8d7SGao Xiang set_page_private(newpage, Z_EROFS_PREALLOCATED_PAGE); 1971825c8d7SGao Xiang t = tag_compressed_page_justfound(newpage); 19847e4937aSGao Xiang } else { /* DONTALLOC */ 1991825c8d7SGao Xiang dontalloc: 20047e4937aSGao Xiang if (standalone) 20147e4937aSGao Xiang clt->compressedpages = pages; 20247e4937aSGao Xiang standalone = false; 20347e4937aSGao Xiang continue; 20447e4937aSGao Xiang } 20547e4937aSGao Xiang 20647e4937aSGao Xiang if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) 20747e4937aSGao Xiang continue; 20847e4937aSGao Xiang 2091825c8d7SGao Xiang if (page) { 21047e4937aSGao Xiang put_page(page); 2111825c8d7SGao Xiang } else if (newpage) { 2121825c8d7SGao Xiang set_page_private(newpage, 0); 2131825c8d7SGao Xiang list_add(&newpage->lru, pagepool); 2141825c8d7SGao Xiang } 21547e4937aSGao Xiang } 21647e4937aSGao Xiang 21747e4937aSGao Xiang if (standalone) /* downgrade to PRIMARY_FOLLOWED_NOINPLACE */ 21847e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 21947e4937aSGao Xiang } 22047e4937aSGao Xiang 22147e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 22247e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 22347e4937aSGao Xiang struct erofs_workgroup *grp) 22447e4937aSGao Xiang { 22547e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 22647e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 22747e4937aSGao Xiang struct address_space *const mapping = MNGD_MAPPING(sbi); 22847e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 22947e4937aSGao Xiang int i; 23047e4937aSGao Xiang 23147e4937aSGao Xiang /* 23247e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 23347e4937aSGao Xiang * therefore no need to worry about available decompression users. 23447e4937aSGao Xiang */ 23547e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 23647e4937aSGao Xiang struct page *page = pcl->compressed_pages[i]; 23747e4937aSGao Xiang 23847e4937aSGao Xiang if (!page) 23947e4937aSGao Xiang continue; 24047e4937aSGao Xiang 24147e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 24247e4937aSGao Xiang if (!trylock_page(page)) 24347e4937aSGao Xiang return -EBUSY; 24447e4937aSGao Xiang 2458d8a09b0SGao Xiang if (page->mapping != mapping) 24647e4937aSGao Xiang continue; 24747e4937aSGao Xiang 24847e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 24947e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 2506aaa7b06SGao Xiang detach_page_private(page); 25147e4937aSGao Xiang unlock_page(page); 25247e4937aSGao Xiang } 25347e4937aSGao Xiang return 0; 25447e4937aSGao Xiang } 25547e4937aSGao Xiang 25647e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping, 25747e4937aSGao Xiang struct page *page) 25847e4937aSGao Xiang { 25947e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 26047e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 26147e4937aSGao Xiang int ret = 0; /* 0 - busy */ 26247e4937aSGao Xiang 26347e4937aSGao Xiang if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { 26447e4937aSGao Xiang unsigned int i; 26547e4937aSGao Xiang 26647e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 26747e4937aSGao Xiang if (pcl->compressed_pages[i] == page) { 26847e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 26947e4937aSGao Xiang ret = 1; 27047e4937aSGao Xiang break; 27147e4937aSGao Xiang } 27247e4937aSGao Xiang } 27347e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 27447e4937aSGao Xiang 2756aaa7b06SGao Xiang if (ret) 2766aaa7b06SGao Xiang detach_page_private(page); 27747e4937aSGao Xiang } 27847e4937aSGao Xiang return ret; 27947e4937aSGao Xiang } 28047e4937aSGao Xiang 28147e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ 28299634bf3SGao Xiang static inline bool z_erofs_try_inplace_io(struct z_erofs_collector *clt, 28347e4937aSGao Xiang struct page *page) 28447e4937aSGao Xiang { 28547e4937aSGao Xiang struct z_erofs_pcluster *const pcl = clt->pcl; 28647e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 28747e4937aSGao Xiang 28847e4937aSGao Xiang while (clt->compressedpages < pcl->compressed_pages + clusterpages) { 28947e4937aSGao Xiang if (!cmpxchg(clt->compressedpages++, NULL, page)) 29047e4937aSGao Xiang return true; 29147e4937aSGao Xiang } 29247e4937aSGao Xiang return false; 29347e4937aSGao Xiang } 29447e4937aSGao Xiang 29547e4937aSGao Xiang /* callers must be with collection lock held */ 29647e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt, 29747e4937aSGao Xiang struct page *page, 29847e4937aSGao Xiang enum z_erofs_page_type type) 29947e4937aSGao Xiang { 30047e4937aSGao Xiang int ret; 30147e4937aSGao Xiang bool occupied; 30247e4937aSGao Xiang 30347e4937aSGao Xiang /* give priority for inplaceio */ 30447e4937aSGao Xiang if (clt->mode >= COLLECT_PRIMARY && 30547e4937aSGao Xiang type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && 30699634bf3SGao Xiang z_erofs_try_inplace_io(clt, page)) 30747e4937aSGao Xiang return 0; 30847e4937aSGao Xiang 30947e4937aSGao Xiang ret = z_erofs_pagevec_enqueue(&clt->vector, 31047e4937aSGao Xiang page, type, &occupied); 31147e4937aSGao Xiang clt->cl->vcnt += (unsigned int)ret; 31247e4937aSGao Xiang 31347e4937aSGao Xiang return ret ? 0 : -EAGAIN; 31447e4937aSGao Xiang } 31547e4937aSGao Xiang 316473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt) 31747e4937aSGao Xiang { 318473e15b0SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 319473e15b0SGao Xiang z_erofs_next_pcluster_t *owned_head = &clt->owned_head; 32047e4937aSGao Xiang 321473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 322473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 323473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 32447e4937aSGao Xiang *owned_head = &pcl->next; 325473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 326473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 327473e15b0SGao Xiang return; 328473e15b0SGao Xiang } 329473e15b0SGao Xiang 33047e4937aSGao Xiang /* 331473e15b0SGao Xiang * type 2, link to the end of an existing open chain, be careful 332473e15b0SGao Xiang * that its submission is controlled by the original attached chain. 33347e4937aSGao Xiang */ 33447e4937aSGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 335473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 33647e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 337473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_HOOKED; 338473e15b0SGao Xiang clt->tailpcl = NULL; 339473e15b0SGao Xiang return; 34047e4937aSGao Xiang } 341473e15b0SGao Xiang /* type 3, it belongs to a chain, but it isn't the end of the chain */ 342473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY; 34347e4937aSGao Xiang } 34447e4937aSGao Xiang 3459e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt, 34647e4937aSGao Xiang struct inode *inode, 34747e4937aSGao Xiang struct erofs_map_blocks *map) 34847e4937aSGao Xiang { 34964094a04SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 35047e4937aSGao Xiang struct z_erofs_collection *cl; 35147e4937aSGao Xiang unsigned int length; 35247e4937aSGao Xiang 35364094a04SGao Xiang /* to avoid unexpected loop formed by corrupted images */ 35447e4937aSGao Xiang if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) { 35547e4937aSGao Xiang DBG_BUGON(1); 3569e579fc1SGao Xiang return -EFSCORRUPTED; 35747e4937aSGao Xiang } 35847e4937aSGao Xiang 35947e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 3608d8a09b0SGao Xiang if (cl->pageofs != (map->m_la & ~PAGE_MASK)) { 36147e4937aSGao Xiang DBG_BUGON(1); 3629e579fc1SGao Xiang return -EFSCORRUPTED; 36347e4937aSGao Xiang } 36447e4937aSGao Xiang 36547e4937aSGao Xiang length = READ_ONCE(pcl->length); 36647e4937aSGao Xiang if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { 36747e4937aSGao Xiang if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { 36847e4937aSGao Xiang DBG_BUGON(1); 3699e579fc1SGao Xiang return -EFSCORRUPTED; 37047e4937aSGao Xiang } 37147e4937aSGao Xiang } else { 37247e4937aSGao Xiang unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; 37347e4937aSGao Xiang 37447e4937aSGao Xiang if (map->m_flags & EROFS_MAP_FULL_MAPPED) 37547e4937aSGao Xiang llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; 37647e4937aSGao Xiang 37747e4937aSGao Xiang while (llen > length && 37847e4937aSGao Xiang length != cmpxchg_relaxed(&pcl->length, length, llen)) { 37947e4937aSGao Xiang cpu_relax(); 38047e4937aSGao Xiang length = READ_ONCE(pcl->length); 38147e4937aSGao Xiang } 38247e4937aSGao Xiang } 38347e4937aSGao Xiang mutex_lock(&cl->lock); 38447e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 38547e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 38647e4937aSGao Xiang clt->tailpcl = pcl; 387473e15b0SGao Xiang 388473e15b0SGao Xiang z_erofs_try_to_claim_pcluster(clt); 38947e4937aSGao Xiang clt->cl = cl; 3909e579fc1SGao Xiang return 0; 39147e4937aSGao Xiang } 39247e4937aSGao Xiang 3939e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt, 39447e4937aSGao Xiang struct inode *inode, 39547e4937aSGao Xiang struct erofs_map_blocks *map) 39647e4937aSGao Xiang { 39747e4937aSGao Xiang struct z_erofs_pcluster *pcl; 39847e4937aSGao Xiang struct z_erofs_collection *cl; 39964094a04SGao Xiang struct erofs_workgroup *grp; 40047e4937aSGao Xiang int err; 40147e4937aSGao Xiang 40247e4937aSGao Xiang /* no available workgroup, let's allocate one */ 40347e4937aSGao Xiang pcl = kmem_cache_alloc(pcluster_cachep, GFP_NOFS); 4048d8a09b0SGao Xiang if (!pcl) 4059e579fc1SGao Xiang return -ENOMEM; 40647e4937aSGao Xiang 40764094a04SGao Xiang atomic_set(&pcl->obj.refcount, 1); 40847e4937aSGao Xiang pcl->obj.index = map->m_pa >> PAGE_SHIFT; 40947e4937aSGao Xiang 41047e4937aSGao Xiang pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | 41147e4937aSGao Xiang (map->m_flags & EROFS_MAP_FULL_MAPPED ? 41247e4937aSGao Xiang Z_EROFS_PCLUSTER_FULL_LENGTH : 0); 41347e4937aSGao Xiang 41447e4937aSGao Xiang if (map->m_flags & EROFS_MAP_ZIPPED) 41547e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4; 41647e4937aSGao Xiang else 41747e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; 41847e4937aSGao Xiang 419a5876e24SGao Xiang pcl->clusterbits = EROFS_I(inode)->z_physical_clusterbits[0]; 42047e4937aSGao Xiang pcl->clusterbits -= PAGE_SHIFT; 42147e4937aSGao Xiang 42247e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 42347e4937aSGao Xiang pcl->next = clt->owned_head; 42447e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 42547e4937aSGao Xiang 42647e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 42764094a04SGao Xiang 42864094a04SGao Xiang /* must be cleaned before freeing to slab */ 42964094a04SGao Xiang DBG_BUGON(cl->nr_pages); 43064094a04SGao Xiang DBG_BUGON(cl->vcnt); 43164094a04SGao Xiang 43247e4937aSGao Xiang cl->pageofs = map->m_la & ~PAGE_MASK; 43347e4937aSGao Xiang 43447e4937aSGao Xiang /* 43547e4937aSGao Xiang * lock all primary followed works before visible to others 43647e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 43747e4937aSGao Xiang */ 43864094a04SGao Xiang DBG_BUGON(!mutex_trylock(&cl->lock)); 43947e4937aSGao Xiang 44064094a04SGao Xiang grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj); 44164094a04SGao Xiang if (IS_ERR(grp)) { 44264094a04SGao Xiang err = PTR_ERR(grp); 44364094a04SGao Xiang goto err_out; 44464094a04SGao Xiang } 44564094a04SGao Xiang 44664094a04SGao Xiang if (grp != &pcl->obj) { 44764094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 44864094a04SGao Xiang err = -EEXIST; 44964094a04SGao Xiang goto err_out; 45047e4937aSGao Xiang } 45147e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 45247e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 45347e4937aSGao Xiang clt->tailpcl = pcl; 45447e4937aSGao Xiang clt->owned_head = &pcl->next; 45547e4937aSGao Xiang clt->pcl = pcl; 45647e4937aSGao Xiang clt->cl = cl; 4579e579fc1SGao Xiang return 0; 45864094a04SGao Xiang 45964094a04SGao Xiang err_out: 46064094a04SGao Xiang mutex_unlock(&cl->lock); 46164094a04SGao Xiang kmem_cache_free(pcluster_cachep, pcl); 46264094a04SGao Xiang return err; 46347e4937aSGao Xiang } 46447e4937aSGao Xiang 46547e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt, 46647e4937aSGao Xiang struct inode *inode, 46747e4937aSGao Xiang struct erofs_map_blocks *map) 46847e4937aSGao Xiang { 46964094a04SGao Xiang struct erofs_workgroup *grp; 4709e579fc1SGao Xiang int ret; 47147e4937aSGao Xiang 47247e4937aSGao Xiang DBG_BUGON(clt->cl); 47347e4937aSGao Xiang 47447e4937aSGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */ 47547e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL); 47647e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 47747e4937aSGao Xiang 47847e4937aSGao Xiang if (!PAGE_ALIGNED(map->m_pa)) { 47947e4937aSGao Xiang DBG_BUGON(1); 48047e4937aSGao Xiang return -EINVAL; 48147e4937aSGao Xiang } 48247e4937aSGao Xiang 48364094a04SGao Xiang grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT); 48464094a04SGao Xiang if (grp) { 48564094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 48664094a04SGao Xiang } else { 4879e579fc1SGao Xiang ret = z_erofs_register_collection(clt, inode, map); 48847e4937aSGao Xiang 48964094a04SGao Xiang if (!ret) 49064094a04SGao Xiang goto out; 49164094a04SGao Xiang if (ret != -EEXIST) 4929e579fc1SGao Xiang return ret; 49364094a04SGao Xiang } 49447e4937aSGao Xiang 49564094a04SGao Xiang ret = z_erofs_lookup_collection(clt, inode, map); 49664094a04SGao Xiang if (ret) { 49764094a04SGao Xiang erofs_workgroup_put(&clt->pcl->obj); 49864094a04SGao Xiang return ret; 49964094a04SGao Xiang } 50064094a04SGao Xiang 50164094a04SGao Xiang out: 50247e4937aSGao Xiang z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, 5039e579fc1SGao Xiang clt->cl->pagevec, clt->cl->vcnt); 50447e4937aSGao Xiang 50547e4937aSGao Xiang clt->compressedpages = clt->pcl->compressed_pages; 50647e4937aSGao Xiang if (clt->mode <= COLLECT_PRIMARY) /* cannot do in-place I/O */ 50747e4937aSGao Xiang clt->compressedpages += Z_EROFS_CLUSTER_MAX_PAGES; 50847e4937aSGao Xiang return 0; 50947e4937aSGao Xiang } 51047e4937aSGao Xiang 51147e4937aSGao Xiang /* 51247e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 51347e4937aSGao Xiang * only after a RCU grace period. 51447e4937aSGao Xiang */ 51547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 51647e4937aSGao Xiang { 51747e4937aSGao Xiang struct z_erofs_collection *const cl = 51847e4937aSGao Xiang container_of(head, struct z_erofs_collection, rcu); 51947e4937aSGao Xiang 52047e4937aSGao Xiang kmem_cache_free(pcluster_cachep, 52147e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, 52247e4937aSGao Xiang primary_collection)); 52347e4937aSGao Xiang } 52447e4937aSGao Xiang 52547e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 52647e4937aSGao Xiang { 52747e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 52847e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 52947e4937aSGao Xiang struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); 53047e4937aSGao Xiang 53147e4937aSGao Xiang call_rcu(&cl->rcu, z_erofs_rcu_callback); 53247e4937aSGao Xiang } 53347e4937aSGao Xiang 53447e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl) 53547e4937aSGao Xiang { 53647e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 53747e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, primary_collection); 53847e4937aSGao Xiang 53947e4937aSGao Xiang erofs_workgroup_put(&pcl->obj); 54047e4937aSGao Xiang } 54147e4937aSGao Xiang 54247e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt) 54347e4937aSGao Xiang { 54447e4937aSGao Xiang struct z_erofs_collection *cl = clt->cl; 54547e4937aSGao Xiang 54647e4937aSGao Xiang if (!cl) 54747e4937aSGao Xiang return false; 54847e4937aSGao Xiang 54947e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&clt->vector, false); 55047e4937aSGao Xiang mutex_unlock(&cl->lock); 55147e4937aSGao Xiang 55247e4937aSGao Xiang /* 55347e4937aSGao Xiang * if all pending pages are added, don't hold its reference 55447e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 55547e4937aSGao Xiang */ 55647e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 55747e4937aSGao Xiang z_erofs_collection_put(cl); 55847e4937aSGao Xiang 55947e4937aSGao Xiang clt->cl = NULL; 56047e4937aSGao Xiang return true; 56147e4937aSGao Xiang } 56247e4937aSGao Xiang 56347e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 56447e4937aSGao Xiang unsigned int cachestrategy, 56547e4937aSGao Xiang erofs_off_t la) 56647e4937aSGao Xiang { 56747e4937aSGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 56847e4937aSGao Xiang return false; 56947e4937aSGao Xiang 57047e4937aSGao Xiang if (fe->backmost) 57147e4937aSGao Xiang return true; 57247e4937aSGao Xiang 57347e4937aSGao Xiang return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 57447e4937aSGao Xiang la < fe->headoffset; 57547e4937aSGao Xiang } 57647e4937aSGao Xiang 57747e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 5781825c8d7SGao Xiang struct page *page, struct list_head *pagepool) 57947e4937aSGao Xiang { 58047e4937aSGao Xiang struct inode *const inode = fe->inode; 581bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 58247e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 58347e4937aSGao Xiang struct z_erofs_collector *const clt = &fe->clt; 58447e4937aSGao Xiang const loff_t offset = page_offset(page); 585dc76ea8cSGao Xiang bool tight = true; 58647e4937aSGao Xiang 58747e4937aSGao Xiang enum z_erofs_cache_alloctype cache_strategy; 58847e4937aSGao Xiang enum z_erofs_page_type page_type; 58947e4937aSGao Xiang unsigned int cur, end, spiltted, index; 59047e4937aSGao Xiang int err = 0; 59147e4937aSGao Xiang 59247e4937aSGao Xiang /* register locked file pages as online pages in pack */ 59347e4937aSGao Xiang z_erofs_onlinepage_init(page); 59447e4937aSGao Xiang 59547e4937aSGao Xiang spiltted = 0; 59647e4937aSGao Xiang end = PAGE_SIZE; 59747e4937aSGao Xiang repeat: 59847e4937aSGao Xiang cur = end - 1; 59947e4937aSGao Xiang 60047e4937aSGao Xiang /* lucky, within the range of the current map_blocks */ 60147e4937aSGao Xiang if (offset + cur >= map->m_la && 60247e4937aSGao Xiang offset + cur < map->m_la + map->m_llen) { 60347e4937aSGao Xiang /* didn't get a valid collection previously (very rare) */ 60447e4937aSGao Xiang if (!clt->cl) 60547e4937aSGao Xiang goto restart_now; 60647e4937aSGao Xiang goto hitted; 60747e4937aSGao Xiang } 60847e4937aSGao Xiang 60947e4937aSGao Xiang /* go ahead the next map_blocks */ 6104f761fa2SGao Xiang erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur); 61147e4937aSGao Xiang 61247e4937aSGao Xiang if (z_erofs_collector_end(clt)) 61347e4937aSGao Xiang fe->backmost = false; 61447e4937aSGao Xiang 61547e4937aSGao Xiang map->m_la = offset + cur; 61647e4937aSGao Xiang map->m_llen = 0; 61747e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 6188d8a09b0SGao Xiang if (err) 61947e4937aSGao Xiang goto err_out; 62047e4937aSGao Xiang 62147e4937aSGao Xiang restart_now: 6228d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) 62347e4937aSGao Xiang goto hitted; 62447e4937aSGao Xiang 62547e4937aSGao Xiang err = z_erofs_collector_begin(clt, inode, map); 6268d8a09b0SGao Xiang if (err) 62747e4937aSGao Xiang goto err_out; 62847e4937aSGao Xiang 62947e4937aSGao Xiang /* preload all compressed pages (maybe downgrade role if necessary) */ 630f57a3fe4SChao Yu if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la)) 6311825c8d7SGao Xiang cache_strategy = TRYALLOC; 63247e4937aSGao Xiang else 63347e4937aSGao Xiang cache_strategy = DONTALLOC; 63447e4937aSGao Xiang 6351825c8d7SGao Xiang preload_compressed_pages(clt, MNGD_MAPPING(sbi), 6361825c8d7SGao Xiang cache_strategy, pagepool); 63747e4937aSGao Xiang 63847e4937aSGao Xiang hitted: 639dc76ea8cSGao Xiang /* 640dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 641dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 642dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 643dc76ea8cSGao Xiang * for inplace I/O or pagevec (should be processed in strict order.) 644dc76ea8cSGao Xiang */ 645dc76ea8cSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED && 646dc76ea8cSGao Xiang clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); 647dc76ea8cSGao Xiang 64847e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 6498d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 65047e4937aSGao Xiang zero_user_segment(page, cur, end); 65147e4937aSGao Xiang goto next_part; 65247e4937aSGao Xiang } 65347e4937aSGao Xiang 65447e4937aSGao Xiang /* let's derive page type */ 65547e4937aSGao Xiang page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 65647e4937aSGao Xiang (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 65747e4937aSGao Xiang (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 65847e4937aSGao Xiang Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 65947e4937aSGao Xiang 66047e4937aSGao Xiang if (cur) 66147e4937aSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); 66247e4937aSGao Xiang 66347e4937aSGao Xiang retry: 66447e4937aSGao Xiang err = z_erofs_attach_page(clt, page, page_type); 6656aaa7b06SGao Xiang /* should allocate an additional short-lived page for pagevec */ 66647e4937aSGao Xiang if (err == -EAGAIN) { 66747e4937aSGao Xiang struct page *const newpage = 668e3f78d5eSChao Yu alloc_page(GFP_NOFS | __GFP_NOFAIL); 66947e4937aSGao Xiang 6706aaa7b06SGao Xiang set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE); 67147e4937aSGao Xiang err = z_erofs_attach_page(clt, newpage, 67247e4937aSGao Xiang Z_EROFS_PAGE_TYPE_EXCLUSIVE); 6738d8a09b0SGao Xiang if (!err) 67447e4937aSGao Xiang goto retry; 67547e4937aSGao Xiang } 67647e4937aSGao Xiang 6778d8a09b0SGao Xiang if (err) 67847e4937aSGao Xiang goto err_out; 67947e4937aSGao Xiang 68047e4937aSGao Xiang index = page->index - (map->m_la >> PAGE_SHIFT); 68147e4937aSGao Xiang 68247e4937aSGao Xiang z_erofs_onlinepage_fixup(page, index, true); 68347e4937aSGao Xiang 68447e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 68547e4937aSGao Xiang ++spiltted; 68647e4937aSGao Xiang /* also update nr_pages */ 68747e4937aSGao Xiang clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); 68847e4937aSGao Xiang next_part: 68947e4937aSGao Xiang /* can be used for verification */ 69047e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 69147e4937aSGao Xiang 69247e4937aSGao Xiang end = cur; 69347e4937aSGao Xiang if (end > 0) 69447e4937aSGao Xiang goto repeat; 69547e4937aSGao Xiang 69647e4937aSGao Xiang out: 69747e4937aSGao Xiang z_erofs_onlinepage_endio(page); 69847e4937aSGao Xiang 6994f761fa2SGao Xiang erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 70047e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 70147e4937aSGao Xiang return err; 70247e4937aSGao Xiang 70347e4937aSGao Xiang /* if some error occurred while processing this page */ 70447e4937aSGao Xiang err_out: 70547e4937aSGao Xiang SetPageError(page); 70647e4937aSGao Xiang goto out; 70747e4937aSGao Xiang } 70847e4937aSGao Xiang 709*648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work); 710a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 711a4b1fab1SGao Xiang bool sync, int bios) 71247e4937aSGao Xiang { 713a4b1fab1SGao Xiang /* wake up the caller thread for sync decompression */ 714a4b1fab1SGao Xiang if (sync) { 71547e4937aSGao Xiang unsigned long flags; 71647e4937aSGao Xiang 71747e4937aSGao Xiang spin_lock_irqsave(&io->u.wait.lock, flags); 71847e4937aSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 71947e4937aSGao Xiang wake_up_locked(&io->u.wait); 72047e4937aSGao Xiang spin_unlock_irqrestore(&io->u.wait.lock, flags); 72147e4937aSGao Xiang return; 72247e4937aSGao Xiang } 72347e4937aSGao Xiang 724*648f2de0SHuang Jianan if (atomic_add_return(bios, &io->pending_bios)) 725*648f2de0SHuang Jianan return; 726*648f2de0SHuang Jianan /* Use workqueue decompression for atomic contexts only */ 727*648f2de0SHuang Jianan if (in_atomic() || irqs_disabled()) { 72847e4937aSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 729*648f2de0SHuang Jianan return; 730*648f2de0SHuang Jianan } 731*648f2de0SHuang Jianan z_erofs_decompressqueue_work(&io->u.work); 73247e4937aSGao Xiang } 73347e4937aSGao Xiang 7346aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 7356aaa7b06SGao Xiang { 7366aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 7376aaa7b06SGao Xiang } 7386aaa7b06SGao Xiang 7390c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 74047e4937aSGao Xiang { 741a4b1fab1SGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 742a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 74347e4937aSGao Xiang blk_status_t err = bio->bi_status; 74447e4937aSGao Xiang struct bio_vec *bvec; 74547e4937aSGao Xiang struct bvec_iter_all iter_all; 74647e4937aSGao Xiang 74747e4937aSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 74847e4937aSGao Xiang struct page *page = bvec->bv_page; 74947e4937aSGao Xiang 75047e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 7516aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 75247e4937aSGao Xiang 7538d8a09b0SGao Xiang if (err) 75447e4937aSGao Xiang SetPageError(page); 75547e4937aSGao Xiang 756a4b1fab1SGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 757a4b1fab1SGao Xiang if (!err) 758a4b1fab1SGao Xiang SetPageUptodate(page); 75947e4937aSGao Xiang unlock_page(page); 76047e4937aSGao Xiang } 761a4b1fab1SGao Xiang } 762a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 76347e4937aSGao Xiang bio_put(bio); 76447e4937aSGao Xiang } 76547e4937aSGao Xiang 76647e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb, 76747e4937aSGao Xiang struct z_erofs_pcluster *pcl, 76847e4937aSGao Xiang struct list_head *pagepool) 76947e4937aSGao Xiang { 77047e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 77147e4937aSGao Xiang const unsigned int clusterpages = BIT(pcl->clusterbits); 77247e4937aSGao Xiang struct z_erofs_pagevec_ctor ctor; 77347e4937aSGao Xiang unsigned int i, outputsize, llen, nr_pages; 77447e4937aSGao Xiang struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 77547e4937aSGao Xiang struct page **pages, **compressed_pages, *page; 77647e4937aSGao Xiang 77747e4937aSGao Xiang enum z_erofs_page_type page_type; 77847e4937aSGao Xiang bool overlapped, partial; 77947e4937aSGao Xiang struct z_erofs_collection *cl; 78047e4937aSGao Xiang int err; 78147e4937aSGao Xiang 78247e4937aSGao Xiang might_sleep(); 78347e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 78447e4937aSGao Xiang DBG_BUGON(!READ_ONCE(cl->nr_pages)); 78547e4937aSGao Xiang 78647e4937aSGao Xiang mutex_lock(&cl->lock); 78747e4937aSGao Xiang nr_pages = cl->nr_pages; 78847e4937aSGao Xiang 7898d8a09b0SGao Xiang if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { 79047e4937aSGao Xiang pages = pages_onstack; 79147e4937aSGao Xiang } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 79247e4937aSGao Xiang mutex_trylock(&z_pagemap_global_lock)) { 79347e4937aSGao Xiang pages = z_pagemap_global; 79447e4937aSGao Xiang } else { 79547e4937aSGao Xiang gfp_t gfp_flags = GFP_KERNEL; 79647e4937aSGao Xiang 79747e4937aSGao Xiang if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 79847e4937aSGao Xiang gfp_flags |= __GFP_NOFAIL; 79947e4937aSGao Xiang 80047e4937aSGao Xiang pages = kvmalloc_array(nr_pages, sizeof(struct page *), 80147e4937aSGao Xiang gfp_flags); 80247e4937aSGao Xiang 80347e4937aSGao Xiang /* fallback to global pagemap for the lowmem scenario */ 8048d8a09b0SGao Xiang if (!pages) { 80547e4937aSGao Xiang mutex_lock(&z_pagemap_global_lock); 80647e4937aSGao Xiang pages = z_pagemap_global; 80747e4937aSGao Xiang } 80847e4937aSGao Xiang } 80947e4937aSGao Xiang 81047e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) 81147e4937aSGao Xiang pages[i] = NULL; 81247e4937aSGao Xiang 81347e4937aSGao Xiang err = 0; 81447e4937aSGao Xiang z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, 81547e4937aSGao Xiang cl->pagevec, 0); 81647e4937aSGao Xiang 81747e4937aSGao Xiang for (i = 0; i < cl->vcnt; ++i) { 81847e4937aSGao Xiang unsigned int pagenr; 81947e4937aSGao Xiang 82047e4937aSGao Xiang page = z_erofs_pagevec_dequeue(&ctor, &page_type); 82147e4937aSGao Xiang 82247e4937aSGao Xiang /* all pages in pagevec ought to be valid */ 82347e4937aSGao Xiang DBG_BUGON(!page); 8246aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 82547e4937aSGao Xiang 8266aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 82747e4937aSGao Xiang continue; 82847e4937aSGao Xiang 82947e4937aSGao Xiang if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) 83047e4937aSGao Xiang pagenr = 0; 83147e4937aSGao Xiang else 83247e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 83347e4937aSGao Xiang 83447e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 83547e4937aSGao Xiang 83647e4937aSGao Xiang /* 83747e4937aSGao Xiang * currently EROFS doesn't support multiref(dedup), 83847e4937aSGao Xiang * so here erroring out one multiref page. 83947e4937aSGao Xiang */ 8408d8a09b0SGao Xiang if (pages[pagenr]) { 84147e4937aSGao Xiang DBG_BUGON(1); 84247e4937aSGao Xiang SetPageError(pages[pagenr]); 84347e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 84447e4937aSGao Xiang err = -EFSCORRUPTED; 84547e4937aSGao Xiang } 84647e4937aSGao Xiang pages[pagenr] = page; 84747e4937aSGao Xiang } 84847e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&ctor, true); 84947e4937aSGao Xiang 85047e4937aSGao Xiang overlapped = false; 85147e4937aSGao Xiang compressed_pages = pcl->compressed_pages; 85247e4937aSGao Xiang 85347e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 85447e4937aSGao Xiang unsigned int pagenr; 85547e4937aSGao Xiang 85647e4937aSGao Xiang page = compressed_pages[i]; 85747e4937aSGao Xiang 85847e4937aSGao Xiang /* all compressed pages ought to be valid */ 85947e4937aSGao Xiang DBG_BUGON(!page); 8606aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 86147e4937aSGao Xiang 8626aaa7b06SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 86347e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) { 8648d8a09b0SGao Xiang if (!PageUptodate(page)) 86547e4937aSGao Xiang err = -EIO; 86647e4937aSGao Xiang continue; 86747e4937aSGao Xiang } 86847e4937aSGao Xiang 86947e4937aSGao Xiang /* 87047e4937aSGao Xiang * only if non-head page can be selected 87147e4937aSGao Xiang * for inplace decompression 87247e4937aSGao Xiang */ 87347e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 87447e4937aSGao Xiang 87547e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 8768d8a09b0SGao Xiang if (pages[pagenr]) { 87747e4937aSGao Xiang DBG_BUGON(1); 87847e4937aSGao Xiang SetPageError(pages[pagenr]); 87947e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 88047e4937aSGao Xiang err = -EFSCORRUPTED; 88147e4937aSGao Xiang } 88247e4937aSGao Xiang pages[pagenr] = page; 88347e4937aSGao Xiang 88447e4937aSGao Xiang overlapped = true; 88547e4937aSGao Xiang } 88647e4937aSGao Xiang 8876aaa7b06SGao Xiang /* PG_error needs checking for all non-managed pages */ 8888d8a09b0SGao Xiang if (PageError(page)) { 88947e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 89047e4937aSGao Xiang err = -EIO; 89147e4937aSGao Xiang } 89247e4937aSGao Xiang } 89347e4937aSGao Xiang 8948d8a09b0SGao Xiang if (err) 89547e4937aSGao Xiang goto out; 89647e4937aSGao Xiang 89747e4937aSGao Xiang llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 89847e4937aSGao Xiang if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { 89947e4937aSGao Xiang outputsize = llen; 90047e4937aSGao Xiang partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 90147e4937aSGao Xiang } else { 90247e4937aSGao Xiang outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; 90347e4937aSGao Xiang partial = true; 90447e4937aSGao Xiang } 90547e4937aSGao Xiang 90647e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 90747e4937aSGao Xiang .sb = sb, 90847e4937aSGao Xiang .in = compressed_pages, 90947e4937aSGao Xiang .out = pages, 91047e4937aSGao Xiang .pageofs_out = cl->pageofs, 91147e4937aSGao Xiang .inputsize = PAGE_SIZE, 91247e4937aSGao Xiang .outputsize = outputsize, 91347e4937aSGao Xiang .alg = pcl->algorithmformat, 91447e4937aSGao Xiang .inplace_io = overlapped, 91547e4937aSGao Xiang .partial_decoding = partial 91647e4937aSGao Xiang }, pagepool); 91747e4937aSGao Xiang 91847e4937aSGao Xiang out: 91947e4937aSGao Xiang /* must handle all compressed pages before endding pages */ 92047e4937aSGao Xiang for (i = 0; i < clusterpages; ++i) { 92147e4937aSGao Xiang page = compressed_pages[i]; 92247e4937aSGao Xiang 92347e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 92447e4937aSGao Xiang continue; 92547e4937aSGao Xiang 9266aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 9276aaa7b06SGao Xiang (void)z_erofs_put_shortlivedpage(pagepool, page); 92847e4937aSGao Xiang 92947e4937aSGao Xiang WRITE_ONCE(compressed_pages[i], NULL); 93047e4937aSGao Xiang } 93147e4937aSGao Xiang 93247e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) { 93347e4937aSGao Xiang page = pages[i]; 93447e4937aSGao Xiang if (!page) 93547e4937aSGao Xiang continue; 93647e4937aSGao Xiang 9376aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 93847e4937aSGao Xiang 9396aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 9406aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 94147e4937aSGao Xiang continue; 94247e4937aSGao Xiang 9438d8a09b0SGao Xiang if (err < 0) 94447e4937aSGao Xiang SetPageError(page); 94547e4937aSGao Xiang 94647e4937aSGao Xiang z_erofs_onlinepage_endio(page); 94747e4937aSGao Xiang } 94847e4937aSGao Xiang 94947e4937aSGao Xiang if (pages == z_pagemap_global) 95047e4937aSGao Xiang mutex_unlock(&z_pagemap_global_lock); 9518d8a09b0SGao Xiang else if (pages != pages_onstack) 95247e4937aSGao Xiang kvfree(pages); 95347e4937aSGao Xiang 95447e4937aSGao Xiang cl->nr_pages = 0; 95547e4937aSGao Xiang cl->vcnt = 0; 95647e4937aSGao Xiang 95747e4937aSGao Xiang /* all cl locks MUST be taken before the following line */ 95847e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 95947e4937aSGao Xiang 96047e4937aSGao Xiang /* all cl locks SHOULD be released right now */ 96147e4937aSGao Xiang mutex_unlock(&cl->lock); 96247e4937aSGao Xiang 96347e4937aSGao Xiang z_erofs_collection_put(cl); 96447e4937aSGao Xiang return err; 96547e4937aSGao Xiang } 96647e4937aSGao Xiang 9670c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 96847e4937aSGao Xiang struct list_head *pagepool) 96947e4937aSGao Xiang { 97047e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 97147e4937aSGao Xiang 97247e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 97347e4937aSGao Xiang struct z_erofs_pcluster *pcl; 97447e4937aSGao Xiang 97547e4937aSGao Xiang /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 97647e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 97747e4937aSGao Xiang 97847e4937aSGao Xiang /* no possible that 'owned' equals NULL */ 97947e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 98047e4937aSGao Xiang 98147e4937aSGao Xiang pcl = container_of(owned, struct z_erofs_pcluster, next); 98247e4937aSGao Xiang owned = READ_ONCE(pcl->next); 98347e4937aSGao Xiang 984a4b1fab1SGao Xiang z_erofs_decompress_pcluster(io->sb, pcl, pagepool); 98547e4937aSGao Xiang } 98647e4937aSGao Xiang } 98747e4937aSGao Xiang 9880c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 98947e4937aSGao Xiang { 990a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 991a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 99247e4937aSGao Xiang LIST_HEAD(pagepool); 99347e4937aSGao Xiang 994a4b1fab1SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 9950c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 99647e4937aSGao Xiang 99747e4937aSGao Xiang put_pages_list(&pagepool); 998a4b1fab1SGao Xiang kvfree(bgq); 99947e4937aSGao Xiang } 100047e4937aSGao Xiang 100147e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 100247e4937aSGao Xiang unsigned int nr, 100347e4937aSGao Xiang struct list_head *pagepool, 100447e4937aSGao Xiang struct address_space *mc, 100547e4937aSGao Xiang gfp_t gfp) 100647e4937aSGao Xiang { 100747e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 100847e4937aSGao Xiang bool tocache = false; 100947e4937aSGao Xiang 101047e4937aSGao Xiang struct address_space *mapping; 101147e4937aSGao Xiang struct page *oldpage, *page; 101247e4937aSGao Xiang 101347e4937aSGao Xiang compressed_page_t t; 101447e4937aSGao Xiang int justfound; 101547e4937aSGao Xiang 101647e4937aSGao Xiang repeat: 101747e4937aSGao Xiang page = READ_ONCE(pcl->compressed_pages[nr]); 101847e4937aSGao Xiang oldpage = page; 101947e4937aSGao Xiang 102047e4937aSGao Xiang if (!page) 102147e4937aSGao Xiang goto out_allocpage; 102247e4937aSGao Xiang 102347e4937aSGao Xiang /* 102447e4937aSGao Xiang * the cached page has not been allocated and 102547e4937aSGao Xiang * an placeholder is out there, prepare it now. 102647e4937aSGao Xiang */ 1027bda17a45SGao Xiang if (page == PAGE_UNALLOCATED) { 102847e4937aSGao Xiang tocache = true; 102947e4937aSGao Xiang goto out_allocpage; 103047e4937aSGao Xiang } 103147e4937aSGao Xiang 103247e4937aSGao Xiang /* process the target tagged pointer */ 103347e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 103447e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 103547e4937aSGao Xiang page = tagptr_unfold_ptr(t); 103647e4937aSGao Xiang 10371825c8d7SGao Xiang /* 10381825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 10391825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 10401825c8d7SGao Xiang */ 10411825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 10421825c8d7SGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 10431825c8d7SGao Xiang set_page_private(page, 0); 10441825c8d7SGao Xiang tocache = true; 10451825c8d7SGao Xiang goto out_tocache; 10461825c8d7SGao Xiang } 104747e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 104847e4937aSGao Xiang 104947e4937aSGao Xiang /* 10506aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 105147e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 105247e4937aSGao Xiang */ 105347e4937aSGao Xiang if (mapping && mapping != mc) 105447e4937aSGao Xiang /* ought to be unmanaged pages */ 105547e4937aSGao Xiang goto out; 105647e4937aSGao Xiang 10576aaa7b06SGao Xiang /* directly return for shortlived page as well */ 10586aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 10596aaa7b06SGao Xiang goto out; 10606aaa7b06SGao Xiang 106147e4937aSGao Xiang lock_page(page); 106247e4937aSGao Xiang 106347e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 106447e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 106547e4937aSGao Xiang 106647e4937aSGao Xiang /* the page is still in manage cache */ 106747e4937aSGao Xiang if (page->mapping == mc) { 106847e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 106947e4937aSGao Xiang 107047e4937aSGao Xiang ClearPageError(page); 107147e4937aSGao Xiang if (!PagePrivate(page)) { 107247e4937aSGao Xiang /* 107347e4937aSGao Xiang * impossible to be !PagePrivate(page) for 107447e4937aSGao Xiang * the current restriction as well if 107547e4937aSGao Xiang * the page is already in compressed_pages[]. 107647e4937aSGao Xiang */ 107747e4937aSGao Xiang DBG_BUGON(!justfound); 107847e4937aSGao Xiang 107947e4937aSGao Xiang justfound = 0; 108047e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 108147e4937aSGao Xiang SetPagePrivate(page); 108247e4937aSGao Xiang } 108347e4937aSGao Xiang 108447e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 108547e4937aSGao Xiang if (PageUptodate(page)) { 108647e4937aSGao Xiang unlock_page(page); 108747e4937aSGao Xiang page = NULL; 108847e4937aSGao Xiang } 108947e4937aSGao Xiang goto out; 109047e4937aSGao Xiang } 109147e4937aSGao Xiang 109247e4937aSGao Xiang /* 109347e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 109447e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 109547e4937aSGao Xiang */ 109647e4937aSGao Xiang DBG_BUGON(page->mapping); 109747e4937aSGao Xiang DBG_BUGON(!justfound); 109847e4937aSGao Xiang 109947e4937aSGao Xiang tocache = true; 110047e4937aSGao Xiang unlock_page(page); 110147e4937aSGao Xiang put_page(page); 110247e4937aSGao Xiang out_allocpage: 11035ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 11045ddcee1fSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 11055ddcee1fSGao Xiang list_add(&page->lru, pagepool); 11065ddcee1fSGao Xiang cond_resched(); 11075ddcee1fSGao Xiang goto repeat; 11085ddcee1fSGao Xiang } 11091825c8d7SGao Xiang out_tocache: 1110bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1111bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1112bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1113bf225074SGao Xiang goto out; 1114a30573b3SGao Xiang } 1115bf225074SGao Xiang attach_page_private(page, pcl); 1116bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1117bf225074SGao Xiang put_page(page); 1118bf225074SGao Xiang 111947e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 112047e4937aSGao Xiang return page; 112147e4937aSGao Xiang } 112247e4937aSGao Xiang 1123a4b1fab1SGao Xiang static struct z_erofs_decompressqueue * 1124a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb, 1125a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 112647e4937aSGao Xiang { 1127a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 112847e4937aSGao Xiang 1129a4b1fab1SGao Xiang if (fg && !*fg) { 1130a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1131a4b1fab1SGao Xiang if (!q) { 1132a4b1fab1SGao Xiang *fg = true; 1133a4b1fab1SGao Xiang goto fg_out; 113447e4937aSGao Xiang } 11350c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1136a4b1fab1SGao Xiang } else { 1137a4b1fab1SGao Xiang fg_out: 1138a4b1fab1SGao Xiang q = fgq; 1139a4b1fab1SGao Xiang init_waitqueue_head(&fgq->u.wait); 1140a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 1141a4b1fab1SGao Xiang } 1142a4b1fab1SGao Xiang q->sb = sb; 1143a4b1fab1SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1144a4b1fab1SGao Xiang return q; 114547e4937aSGao Xiang } 114647e4937aSGao Xiang 114747e4937aSGao Xiang /* define decompression jobqueue types */ 114847e4937aSGao Xiang enum { 114947e4937aSGao Xiang JQ_BYPASS, 115047e4937aSGao Xiang JQ_SUBMIT, 115147e4937aSGao Xiang NR_JOBQUEUES, 115247e4937aSGao Xiang }; 115347e4937aSGao Xiang 115447e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 1155a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[], 1156a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 115747e4937aSGao Xiang { 115847e4937aSGao Xiang /* 115947e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 116047e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 116147e4937aSGao Xiang */ 1162a4b1fab1SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1163a4b1fab1SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 116447e4937aSGao Xiang 1165a4b1fab1SGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 116647e4937aSGao Xiang } 116747e4937aSGao Xiang 116847e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 116947e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 117047e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 117147e4937aSGao Xiang { 117247e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 117347e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 117447e4937aSGao Xiang 117547e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 117647e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 117747e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 117847e4937aSGao Xiang 117947e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 118047e4937aSGao Xiang 118147e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 118247e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 118347e4937aSGao Xiang 118447e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 118547e4937aSGao Xiang } 118647e4937aSGao Xiang 11871e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb, 11886ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 118947e4937aSGao Xiang struct list_head *pagepool, 1190a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1191a4b1fab1SGao Xiang bool *force_fg) 119247e4937aSGao Xiang { 1193bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 119447e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1195a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 119647e4937aSGao Xiang void *bi_private; 11976ea5aad3SGao Xiang z_erofs_next_pcluster_t owned_head = f->clt.owned_head; 119847e4937aSGao Xiang /* since bio will be NULL, no need to initialize last_index */ 11993f649ab7SKees Cook pgoff_t last_index; 12001e4a2955SGao Xiang unsigned int nr_bios = 0; 12011e4a2955SGao Xiang struct bio *bio = NULL; 120247e4937aSGao Xiang 1203a4b1fab1SGao Xiang bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1204a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1205a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 120647e4937aSGao Xiang 120747e4937aSGao Xiang /* by default, all need io submission */ 120847e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 120947e4937aSGao Xiang 121047e4937aSGao Xiang do { 121147e4937aSGao Xiang struct z_erofs_pcluster *pcl; 12121e4a2955SGao Xiang pgoff_t cur, end; 12131e4a2955SGao Xiang unsigned int i = 0; 12141e4a2955SGao Xiang bool bypass = true; 121547e4937aSGao Xiang 121647e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 121747e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 121847e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 121947e4937aSGao Xiang 122047e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 122147e4937aSGao Xiang 12221e4a2955SGao Xiang cur = pcl->obj.index; 12231e4a2955SGao Xiang end = cur + BIT(pcl->clusterbits); 122447e4937aSGao Xiang 122547e4937aSGao Xiang /* close the main owned chain at first */ 122647e4937aSGao Xiang owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 122747e4937aSGao Xiang Z_EROFS_PCLUSTER_TAIL_CLOSED); 122847e4937aSGao Xiang 12291e4a2955SGao Xiang do { 12301e4a2955SGao Xiang struct page *page; 123147e4937aSGao Xiang 12321e4a2955SGao Xiang page = pickup_page_for_submission(pcl, i++, pagepool, 123347e4937aSGao Xiang MNGD_MAPPING(sbi), 123447e4937aSGao Xiang GFP_NOFS); 12351e4a2955SGao Xiang if (!page) 12361e4a2955SGao Xiang continue; 123747e4937aSGao Xiang 12381e4a2955SGao Xiang if (bio && cur != last_index + 1) { 123947e4937aSGao Xiang submit_bio_retry: 124094e4e153SGao Xiang submit_bio(bio); 124147e4937aSGao Xiang bio = NULL; 124247e4937aSGao Xiang } 124347e4937aSGao Xiang 124447e4937aSGao Xiang if (!bio) { 1245a8affc03SChristoph Hellwig bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS); 1246a5c0b780SGao Xiang 12470c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1248a5c0b780SGao Xiang bio_set_dev(bio, sb->s_bdev); 12491e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 1250a5c0b780SGao Xiang LOG_SECTORS_PER_BLOCK; 1251a5c0b780SGao Xiang bio->bi_private = bi_private; 125294e4e153SGao Xiang bio->bi_opf = REQ_OP_READ; 12536ea5aad3SGao Xiang if (f->readahead) 12546ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 125547e4937aSGao Xiang ++nr_bios; 125647e4937aSGao Xiang } 125747e4937aSGao Xiang 12586c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 125947e4937aSGao Xiang goto submit_bio_retry; 126047e4937aSGao Xiang 12611e4a2955SGao Xiang last_index = cur; 12621e4a2955SGao Xiang bypass = false; 12631e4a2955SGao Xiang } while (++cur < end); 126447e4937aSGao Xiang 12651e4a2955SGao Xiang if (!bypass) 126647e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 126747e4937aSGao Xiang else 126847e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 126947e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 127047e4937aSGao Xiang 127147e4937aSGao Xiang if (bio) 127294e4e153SGao Xiang submit_bio(bio); 127347e4937aSGao Xiang 1274587a67b7SGao Xiang /* 1275587a67b7SGao Xiang * although background is preferred, no one is pending for submission. 1276587a67b7SGao Xiang * don't issue workqueue for decompression but drop it directly instead. 1277587a67b7SGao Xiang */ 1278587a67b7SGao Xiang if (!*force_fg && !nr_bios) { 1279587a67b7SGao Xiang kvfree(q[JQ_SUBMIT]); 12801e4a2955SGao Xiang return; 1281587a67b7SGao Xiang } 1282a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 128347e4937aSGao Xiang } 128447e4937aSGao Xiang 12850c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb, 12866ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 12870c638f70SGao Xiang struct list_head *pagepool, bool force_fg) 128847e4937aSGao Xiang { 1289a4b1fab1SGao Xiang struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 129047e4937aSGao Xiang 12916ea5aad3SGao Xiang if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL) 129247e4937aSGao Xiang return; 12936ea5aad3SGao Xiang z_erofs_submit_queue(sb, f, pagepool, io, &force_fg); 129447e4937aSGao Xiang 12950c638f70SGao Xiang /* handle bypass queue (no i/o pclusters) immediately */ 12960c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 129747e4937aSGao Xiang 129847e4937aSGao Xiang if (!force_fg) 129947e4937aSGao Xiang return; 130047e4937aSGao Xiang 130147e4937aSGao Xiang /* wait until all bios are completed */ 1302a93f8c36SGao Xiang io_wait_event(io[JQ_SUBMIT].u.wait, 130347e4937aSGao Xiang !atomic_read(&io[JQ_SUBMIT].pending_bios)); 130447e4937aSGao Xiang 13050c638f70SGao Xiang /* handle synchronous decompress queue in the caller context */ 13060c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 130747e4937aSGao Xiang } 130847e4937aSGao Xiang 13090c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page) 131047e4937aSGao Xiang { 131147e4937aSGao Xiang struct inode *const inode = page->mapping->host; 131247e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 131347e4937aSGao Xiang int err; 131447e4937aSGao Xiang LIST_HEAD(pagepool); 131547e4937aSGao Xiang 131647e4937aSGao Xiang trace_erofs_readpage(page, false); 131747e4937aSGao Xiang 131847e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 131947e4937aSGao Xiang 13201825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 132147e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 132247e4937aSGao Xiang 132347e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 13246ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, true); 132547e4937aSGao Xiang 132647e4937aSGao Xiang if (err) 13274f761fa2SGao Xiang erofs_err(inode->i_sb, "failed to read, err [%d]", err); 132847e4937aSGao Xiang 132947e4937aSGao Xiang if (f.map.mpage) 133047e4937aSGao Xiang put_page(f.map.mpage); 133147e4937aSGao Xiang 133247e4937aSGao Xiang /* clean up the remaining free pages */ 133347e4937aSGao Xiang put_pages_list(&pagepool); 133447e4937aSGao Xiang return err; 133547e4937aSGao Xiang } 133647e4937aSGao Xiang 13370615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac) 133847e4937aSGao Xiang { 13390615090cSMatthew Wilcox (Oracle) struct inode *const inode = rac->mapping->host; 134047e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 134147e4937aSGao Xiang 1342bf9a123bSGao Xiang unsigned int nr_pages = readahead_count(rac); 1343bf9a123bSGao Xiang bool sync = (nr_pages <= sbi->ctx.max_sync_decompress_pages); 134447e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 13450615090cSMatthew Wilcox (Oracle) struct page *page, *head = NULL; 134647e4937aSGao Xiang LIST_HEAD(pagepool); 134747e4937aSGao Xiang 1348bf9a123bSGao Xiang trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 134947e4937aSGao Xiang 13506ea5aad3SGao Xiang f.readahead = true; 13510615090cSMatthew Wilcox (Oracle) f.headoffset = readahead_pos(rac); 135247e4937aSGao Xiang 13530615090cSMatthew Wilcox (Oracle) while ((page = readahead_page(rac))) { 135447e4937aSGao Xiang prefetchw(&page->flags); 135547e4937aSGao Xiang 135647e4937aSGao Xiang /* 135747e4937aSGao Xiang * A pure asynchronous readahead is indicated if 135847e4937aSGao Xiang * a PG_readahead marked page is hitted at first. 135947e4937aSGao Xiang * Let's also do asynchronous decompression for this case. 136047e4937aSGao Xiang */ 136147e4937aSGao Xiang sync &= !(PageReadahead(page) && !head); 136247e4937aSGao Xiang 136347e4937aSGao Xiang set_page_private(page, (unsigned long)head); 136447e4937aSGao Xiang head = page; 136547e4937aSGao Xiang } 136647e4937aSGao Xiang 136747e4937aSGao Xiang while (head) { 136847e4937aSGao Xiang struct page *page = head; 136947e4937aSGao Xiang int err; 137047e4937aSGao Xiang 137147e4937aSGao Xiang /* traversal in reverse order */ 137247e4937aSGao Xiang head = (void *)page_private(page); 137347e4937aSGao Xiang 13741825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 1375a5876e24SGao Xiang if (err) 13764f761fa2SGao Xiang erofs_err(inode->i_sb, 13774f761fa2SGao Xiang "readahead error at page %lu @ nid %llu", 13784f761fa2SGao Xiang page->index, EROFS_I(inode)->nid); 137947e4937aSGao Xiang put_page(page); 138047e4937aSGao Xiang } 138147e4937aSGao Xiang 138247e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 138347e4937aSGao Xiang 13846ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync); 138547e4937aSGao Xiang 138647e4937aSGao Xiang if (f.map.mpage) 138747e4937aSGao Xiang put_page(f.map.mpage); 138847e4937aSGao Xiang 138947e4937aSGao Xiang /* clean up the remaining free pages */ 139047e4937aSGao Xiang put_pages_list(&pagepool); 139147e4937aSGao Xiang } 139247e4937aSGao Xiang 13930c638f70SGao Xiang const struct address_space_operations z_erofs_aops = { 13940c638f70SGao Xiang .readpage = z_erofs_readpage, 13950615090cSMatthew Wilcox (Oracle) .readahead = z_erofs_readahead, 139647e4937aSGao Xiang }; 139747e4937aSGao Xiang 1398