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 */ 647e4937aSGao Xiang #include "zdata.h" 747e4937aSGao Xiang #include "compress.h" 847e4937aSGao Xiang #include <linux/prefetch.h> 947e4937aSGao Xiang 1047e4937aSGao Xiang #include <trace/events/erofs.h> 1147e4937aSGao Xiang 1247e4937aSGao Xiang /* 139f6cc76eSGao Xiang * since pclustersize is variable for big pcluster feature, introduce slab 149f6cc76eSGao Xiang * pools implementation for different pcluster sizes. 159f6cc76eSGao Xiang */ 169f6cc76eSGao Xiang struct z_erofs_pcluster_slab { 179f6cc76eSGao Xiang struct kmem_cache *slab; 189f6cc76eSGao Xiang unsigned int maxpages; 199f6cc76eSGao Xiang char name[48]; 209f6cc76eSGao Xiang }; 219f6cc76eSGao Xiang 229f6cc76eSGao Xiang #define _PCLP(n) { .maxpages = n } 239f6cc76eSGao Xiang 249f6cc76eSGao Xiang static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = { 259f6cc76eSGao Xiang _PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128), 269f6cc76eSGao Xiang _PCLP(Z_EROFS_PCLUSTER_MAX_PAGES) 279f6cc76eSGao Xiang }; 289f6cc76eSGao Xiang 299f6cc76eSGao Xiang static void z_erofs_destroy_pcluster_pool(void) 309f6cc76eSGao Xiang { 319f6cc76eSGao Xiang int i; 329f6cc76eSGao Xiang 339f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 349f6cc76eSGao Xiang if (!pcluster_pool[i].slab) 359f6cc76eSGao Xiang continue; 369f6cc76eSGao Xiang kmem_cache_destroy(pcluster_pool[i].slab); 379f6cc76eSGao Xiang pcluster_pool[i].slab = NULL; 389f6cc76eSGao Xiang } 399f6cc76eSGao Xiang } 409f6cc76eSGao Xiang 419f6cc76eSGao Xiang static int z_erofs_create_pcluster_pool(void) 429f6cc76eSGao Xiang { 439f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs; 449f6cc76eSGao Xiang struct z_erofs_pcluster *a; 459f6cc76eSGao Xiang unsigned int size; 469f6cc76eSGao Xiang 479f6cc76eSGao Xiang for (pcs = pcluster_pool; 489f6cc76eSGao Xiang pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) { 499f6cc76eSGao Xiang size = struct_size(a, compressed_pages, pcs->maxpages); 509f6cc76eSGao Xiang 519f6cc76eSGao Xiang sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages); 529f6cc76eSGao Xiang pcs->slab = kmem_cache_create(pcs->name, size, 0, 539f6cc76eSGao Xiang SLAB_RECLAIM_ACCOUNT, NULL); 549f6cc76eSGao Xiang if (pcs->slab) 559f6cc76eSGao Xiang continue; 569f6cc76eSGao Xiang 579f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 589f6cc76eSGao Xiang return -ENOMEM; 599f6cc76eSGao Xiang } 609f6cc76eSGao Xiang return 0; 619f6cc76eSGao Xiang } 629f6cc76eSGao Xiang 639f6cc76eSGao Xiang static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages) 649f6cc76eSGao Xiang { 659f6cc76eSGao Xiang int i; 669f6cc76eSGao Xiang 679f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 689f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 699f6cc76eSGao Xiang struct z_erofs_pcluster *pcl; 709f6cc76eSGao Xiang 719f6cc76eSGao Xiang if (nrpages > pcs->maxpages) 729f6cc76eSGao Xiang continue; 739f6cc76eSGao Xiang 749f6cc76eSGao Xiang pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS); 759f6cc76eSGao Xiang if (!pcl) 769f6cc76eSGao Xiang return ERR_PTR(-ENOMEM); 779f6cc76eSGao Xiang pcl->pclusterpages = nrpages; 789f6cc76eSGao Xiang return pcl; 799f6cc76eSGao Xiang } 809f6cc76eSGao Xiang return ERR_PTR(-EINVAL); 819f6cc76eSGao Xiang } 829f6cc76eSGao Xiang 839f6cc76eSGao Xiang static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl) 849f6cc76eSGao Xiang { 859f6cc76eSGao Xiang int i; 869f6cc76eSGao Xiang 879f6cc76eSGao Xiang for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) { 889f6cc76eSGao Xiang struct z_erofs_pcluster_slab *pcs = pcluster_pool + i; 899f6cc76eSGao Xiang 909f6cc76eSGao Xiang if (pcl->pclusterpages > pcs->maxpages) 919f6cc76eSGao Xiang continue; 929f6cc76eSGao Xiang 939f6cc76eSGao Xiang kmem_cache_free(pcs->slab, pcl); 949f6cc76eSGao Xiang return; 959f6cc76eSGao Xiang } 969f6cc76eSGao Xiang DBG_BUGON(1); 979f6cc76eSGao Xiang } 989f6cc76eSGao Xiang 999f6cc76eSGao Xiang /* 10047e4937aSGao Xiang * a compressed_pages[] placeholder in order to avoid 10147e4937aSGao Xiang * being filled with file pages for in-place decompression. 10247e4937aSGao Xiang */ 10347e4937aSGao Xiang #define PAGE_UNALLOCATED ((void *)0x5F0E4B1D) 10447e4937aSGao Xiang 10547e4937aSGao Xiang /* how to allocate cached pages for a pcluster */ 10647e4937aSGao Xiang enum z_erofs_cache_alloctype { 10747e4937aSGao Xiang DONTALLOC, /* don't allocate any cached pages */ 10847e4937aSGao Xiang DELAYEDALLOC, /* delayed allocation (at the time of submitting io) */ 1091825c8d7SGao Xiang /* 1101825c8d7SGao Xiang * try to use cached I/O if page allocation succeeds or fallback 1111825c8d7SGao Xiang * to in-place I/O instead to avoid any direct reclaim. 1121825c8d7SGao Xiang */ 1131825c8d7SGao Xiang TRYALLOC, 11447e4937aSGao Xiang }; 11547e4937aSGao Xiang 11647e4937aSGao Xiang /* 11747e4937aSGao Xiang * tagged pointer with 1-bit tag for all compressed pages 11847e4937aSGao Xiang * tag 0 - the page is just found with an extra page reference 11947e4937aSGao Xiang */ 12047e4937aSGao Xiang typedef tagptr1_t compressed_page_t; 12147e4937aSGao Xiang 12247e4937aSGao Xiang #define tag_compressed_page_justfound(page) \ 12347e4937aSGao Xiang tagptr_fold(compressed_page_t, page, 1) 12447e4937aSGao Xiang 12547e4937aSGao Xiang static struct workqueue_struct *z_erofs_workqueue __read_mostly; 12647e4937aSGao Xiang 12747e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void) 12847e4937aSGao Xiang { 12947e4937aSGao Xiang destroy_workqueue(z_erofs_workqueue); 1309f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 13147e4937aSGao Xiang } 13247e4937aSGao Xiang 13399634bf3SGao Xiang static inline int z_erofs_init_workqueue(void) 13447e4937aSGao Xiang { 13547e4937aSGao Xiang const unsigned int onlinecpus = num_possible_cpus(); 13647e4937aSGao Xiang 13747e4937aSGao Xiang /* 13847e4937aSGao Xiang * no need to spawn too many threads, limiting threads could minimum 13947e4937aSGao Xiang * scheduling overhead, perhaps per-CPU threads should be better? 14047e4937aSGao Xiang */ 1410e62ea33SGao Xiang z_erofs_workqueue = alloc_workqueue("erofs_unzipd", 1420e62ea33SGao Xiang WQ_UNBOUND | WQ_HIGHPRI, 14347e4937aSGao Xiang onlinecpus + onlinecpus / 4); 14447e4937aSGao Xiang return z_erofs_workqueue ? 0 : -ENOMEM; 14547e4937aSGao Xiang } 14647e4937aSGao Xiang 14747e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void) 14847e4937aSGao Xiang { 1499f6cc76eSGao Xiang int err = z_erofs_create_pcluster_pool(); 15047e4937aSGao Xiang 1519f6cc76eSGao Xiang if (err) 1529f6cc76eSGao Xiang return err; 1539f6cc76eSGao Xiang err = z_erofs_init_workqueue(); 1549f6cc76eSGao Xiang if (err) 1559f6cc76eSGao Xiang z_erofs_destroy_pcluster_pool(); 1569f6cc76eSGao Xiang return err; 15747e4937aSGao Xiang } 15847e4937aSGao Xiang 15947e4937aSGao Xiang enum z_erofs_collectmode { 16047e4937aSGao Xiang COLLECT_SECONDARY, 16147e4937aSGao Xiang COLLECT_PRIMARY, 16247e4937aSGao Xiang /* 16347e4937aSGao Xiang * The current collection was the tail of an exist chain, in addition 16447e4937aSGao Xiang * that the previous processed chained collections are all decided to 16547e4937aSGao Xiang * be hooked up to it. 16647e4937aSGao Xiang * A new chain will be created for the remaining collections which are 16747e4937aSGao Xiang * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED, 16847e4937aSGao Xiang * the next collection cannot reuse the whole page safely in 16947e4937aSGao Xiang * the following scenario: 17047e4937aSGao Xiang * ________________________________________________________________ 17147e4937aSGao Xiang * | tail (partial) page | head (partial) page | 17247e4937aSGao Xiang * | (belongs to the next cl) | (belongs to the current cl) | 17347e4937aSGao Xiang * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________| 17447e4937aSGao Xiang */ 17547e4937aSGao Xiang COLLECT_PRIMARY_HOOKED, 1760b964600SGao Xiang /* 1770b964600SGao Xiang * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it 1780b964600SGao Xiang * could be dispatched into bypass queue later due to uptodated managed 1790b964600SGao Xiang * pages. All related online pages cannot be reused for inplace I/O (or 1800b964600SGao Xiang * pagevec) since it can be directly decoded without I/O submission. 1810b964600SGao Xiang */ 18247e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED_NOINPLACE, 18347e4937aSGao Xiang /* 18447e4937aSGao Xiang * The current collection has been linked with the owned chain, and 18547e4937aSGao Xiang * could also be linked with the remaining collections, which means 18647e4937aSGao Xiang * if the processing page is the tail page of the collection, thus 18747e4937aSGao Xiang * the current collection can safely use the whole page (since 18847e4937aSGao Xiang * the previous collection is under control) for in-place I/O, as 18947e4937aSGao Xiang * illustrated below: 19047e4937aSGao Xiang * ________________________________________________________________ 19147e4937aSGao Xiang * | tail (partial) page | head (partial) page | 19247e4937aSGao Xiang * | (of the current cl) | (of the previous collection) | 19347e4937aSGao Xiang * | PRIMARY_FOLLOWED or | | 19447e4937aSGao Xiang * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________| 19547e4937aSGao Xiang * 19647e4937aSGao Xiang * [ (*) the above page can be used as inplace I/O. ] 19747e4937aSGao Xiang */ 19847e4937aSGao Xiang COLLECT_PRIMARY_FOLLOWED, 19947e4937aSGao Xiang }; 20047e4937aSGao Xiang 20147e4937aSGao Xiang struct z_erofs_collector { 20247e4937aSGao Xiang struct z_erofs_pagevec_ctor vector; 20347e4937aSGao Xiang 20447e4937aSGao Xiang struct z_erofs_pcluster *pcl, *tailpcl; 20547e4937aSGao Xiang struct z_erofs_collection *cl; 20681382f5fSGao Xiang /* a pointer used to pick up inplace I/O pages */ 20781382f5fSGao Xiang struct page **icpage_ptr; 20847e4937aSGao Xiang z_erofs_next_pcluster_t owned_head; 20947e4937aSGao Xiang 21047e4937aSGao Xiang enum z_erofs_collectmode mode; 21147e4937aSGao Xiang }; 21247e4937aSGao Xiang 21347e4937aSGao Xiang struct z_erofs_decompress_frontend { 21447e4937aSGao Xiang struct inode *const inode; 21547e4937aSGao Xiang 21647e4937aSGao Xiang struct z_erofs_collector clt; 21747e4937aSGao Xiang struct erofs_map_blocks map; 21847e4937aSGao Xiang 2196ea5aad3SGao Xiang bool readahead; 22047e4937aSGao Xiang /* used for applying cache strategy on the fly */ 22147e4937aSGao Xiang bool backmost; 22247e4937aSGao Xiang erofs_off_t headoffset; 22347e4937aSGao Xiang }; 22447e4937aSGao Xiang 22547e4937aSGao Xiang #define COLLECTOR_INIT() { \ 22647e4937aSGao Xiang .owned_head = Z_EROFS_PCLUSTER_TAIL, \ 22747e4937aSGao Xiang .mode = COLLECT_PRIMARY_FOLLOWED } 22847e4937aSGao Xiang 22947e4937aSGao Xiang #define DECOMPRESS_FRONTEND_INIT(__i) { \ 23047e4937aSGao Xiang .inode = __i, .clt = COLLECTOR_INIT(), \ 23147e4937aSGao Xiang .backmost = true, } 23247e4937aSGao Xiang 23347e4937aSGao Xiang static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES]; 23447e4937aSGao Xiang static DEFINE_MUTEX(z_pagemap_global_lock); 23547e4937aSGao Xiang 23647e4937aSGao Xiang static void preload_compressed_pages(struct z_erofs_collector *clt, 23747e4937aSGao Xiang struct address_space *mc, 2381825c8d7SGao Xiang enum z_erofs_cache_alloctype type, 2391825c8d7SGao Xiang struct list_head *pagepool) 24047e4937aSGao Xiang { 24181382f5fSGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 24247e4937aSGao Xiang bool standalone = true; 2431825c8d7SGao Xiang gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) | 2441825c8d7SGao Xiang __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 24581382f5fSGao Xiang struct page **pages; 24681382f5fSGao Xiang pgoff_t index; 24747e4937aSGao Xiang 24847e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED) 24947e4937aSGao Xiang return; 25047e4937aSGao Xiang 25181382f5fSGao Xiang pages = pcl->compressed_pages; 25281382f5fSGao Xiang index = pcl->obj.index; 25381382f5fSGao Xiang for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) { 25447e4937aSGao Xiang struct page *page; 25547e4937aSGao Xiang compressed_page_t t; 2561825c8d7SGao Xiang struct page *newpage = NULL; 25747e4937aSGao Xiang 25847e4937aSGao Xiang /* the compressed page was loaded before */ 25947e4937aSGao Xiang if (READ_ONCE(*pages)) 26047e4937aSGao Xiang continue; 26147e4937aSGao Xiang 26247e4937aSGao Xiang page = find_get_page(mc, index); 26347e4937aSGao Xiang 26447e4937aSGao Xiang if (page) { 26547e4937aSGao Xiang t = tag_compressed_page_justfound(page); 2660b964600SGao Xiang } else { 2670b964600SGao Xiang /* I/O is needed, no possible to decompress directly */ 2680b964600SGao Xiang standalone = false; 2690b964600SGao Xiang switch (type) { 2700b964600SGao Xiang case DELAYEDALLOC: 2710b964600SGao Xiang t = tagptr_init(compressed_page_t, 2720b964600SGao Xiang PAGE_UNALLOCATED); 2730b964600SGao Xiang break; 2740b964600SGao Xiang case TRYALLOC: 2751825c8d7SGao Xiang newpage = erofs_allocpage(pagepool, gfp); 2761825c8d7SGao Xiang if (!newpage) 27747e4937aSGao Xiang continue; 2780b964600SGao Xiang set_page_private(newpage, 2790b964600SGao Xiang Z_EROFS_PREALLOCATED_PAGE); 2800b964600SGao Xiang t = tag_compressed_page_justfound(newpage); 2810b964600SGao Xiang break; 2820b964600SGao Xiang default: /* DONTALLOC */ 2830b964600SGao Xiang continue; 2840b964600SGao Xiang } 28547e4937aSGao Xiang } 28647e4937aSGao Xiang 28747e4937aSGao Xiang if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t))) 28847e4937aSGao Xiang continue; 28947e4937aSGao Xiang 2901825c8d7SGao Xiang if (page) { 29147e4937aSGao Xiang put_page(page); 2921825c8d7SGao Xiang } else if (newpage) { 2931825c8d7SGao Xiang set_page_private(newpage, 0); 2941825c8d7SGao Xiang list_add(&newpage->lru, pagepool); 2951825c8d7SGao Xiang } 29647e4937aSGao Xiang } 29747e4937aSGao Xiang 2980b964600SGao Xiang /* 2990b964600SGao Xiang * don't do inplace I/O if all compressed pages are available in 3000b964600SGao Xiang * managed cache since it can be moved to the bypass queue instead. 3010b964600SGao Xiang */ 3020b964600SGao Xiang if (standalone) 30347e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE; 30447e4937aSGao Xiang } 30547e4937aSGao Xiang 30647e4937aSGao Xiang /* called by erofs_shrinker to get rid of all compressed_pages */ 30747e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 30847e4937aSGao Xiang struct erofs_workgroup *grp) 30947e4937aSGao Xiang { 31047e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 31147e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 31247e4937aSGao Xiang int i; 31347e4937aSGao Xiang 31447e4937aSGao Xiang /* 31547e4937aSGao Xiang * refcount of workgroup is now freezed as 1, 31647e4937aSGao Xiang * therefore no need to worry about available decompression users. 31747e4937aSGao Xiang */ 3189f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 31947e4937aSGao Xiang struct page *page = pcl->compressed_pages[i]; 32047e4937aSGao Xiang 32147e4937aSGao Xiang if (!page) 32247e4937aSGao Xiang continue; 32347e4937aSGao Xiang 32447e4937aSGao Xiang /* block other users from reclaiming or migrating the page */ 32547e4937aSGao Xiang if (!trylock_page(page)) 32647e4937aSGao Xiang return -EBUSY; 32747e4937aSGao Xiang 328*f4d4e5fcSYue Hu if (!erofs_page_is_managed(sbi, page)) 32947e4937aSGao Xiang continue; 33047e4937aSGao Xiang 33147e4937aSGao Xiang /* barrier is implied in the following 'unlock_page' */ 33247e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 3336aaa7b06SGao Xiang detach_page_private(page); 33447e4937aSGao Xiang unlock_page(page); 33547e4937aSGao Xiang } 33647e4937aSGao Xiang return 0; 33747e4937aSGao Xiang } 33847e4937aSGao Xiang 33947e4937aSGao Xiang int erofs_try_to_free_cached_page(struct address_space *mapping, 34047e4937aSGao Xiang struct page *page) 34147e4937aSGao Xiang { 34247e4937aSGao Xiang struct z_erofs_pcluster *const pcl = (void *)page_private(page); 34347e4937aSGao Xiang int ret = 0; /* 0 - busy */ 34447e4937aSGao Xiang 34547e4937aSGao Xiang if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) { 34647e4937aSGao Xiang unsigned int i; 34747e4937aSGao Xiang 3489f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 34947e4937aSGao Xiang if (pcl->compressed_pages[i] == page) { 35047e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[i], NULL); 35147e4937aSGao Xiang ret = 1; 35247e4937aSGao Xiang break; 35347e4937aSGao Xiang } 35447e4937aSGao Xiang } 35547e4937aSGao Xiang erofs_workgroup_unfreeze(&pcl->obj, 1); 35647e4937aSGao Xiang 3576aaa7b06SGao Xiang if (ret) 3586aaa7b06SGao Xiang detach_page_private(page); 35947e4937aSGao Xiang } 36047e4937aSGao Xiang return ret; 36147e4937aSGao Xiang } 36247e4937aSGao Xiang 36347e4937aSGao Xiang /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */ 36481382f5fSGao Xiang static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt, 36547e4937aSGao Xiang struct page *page) 36647e4937aSGao Xiang { 36747e4937aSGao Xiang struct z_erofs_pcluster *const pcl = clt->pcl; 36847e4937aSGao Xiang 36981382f5fSGao Xiang while (clt->icpage_ptr > pcl->compressed_pages) 37081382f5fSGao Xiang if (!cmpxchg(--clt->icpage_ptr, NULL, page)) 37147e4937aSGao Xiang return true; 37247e4937aSGao Xiang return false; 37347e4937aSGao Xiang } 37447e4937aSGao Xiang 37547e4937aSGao Xiang /* callers must be with collection lock held */ 37647e4937aSGao Xiang static int z_erofs_attach_page(struct z_erofs_collector *clt, 37747e4937aSGao Xiang struct page *page, 37847e4937aSGao Xiang enum z_erofs_page_type type) 37947e4937aSGao Xiang { 38047e4937aSGao Xiang int ret; 38147e4937aSGao Xiang 38247e4937aSGao Xiang /* give priority for inplaceio */ 38347e4937aSGao Xiang if (clt->mode >= COLLECT_PRIMARY && 38447e4937aSGao Xiang type == Z_EROFS_PAGE_TYPE_EXCLUSIVE && 38599634bf3SGao Xiang z_erofs_try_inplace_io(clt, page)) 38647e4937aSGao Xiang return 0; 38747e4937aSGao Xiang 3887dea3de7SYue Hu ret = z_erofs_pagevec_enqueue(&clt->vector, page, type); 38947e4937aSGao Xiang clt->cl->vcnt += (unsigned int)ret; 39047e4937aSGao Xiang 39147e4937aSGao Xiang return ret ? 0 : -EAGAIN; 39247e4937aSGao Xiang } 39347e4937aSGao Xiang 394473e15b0SGao Xiang static void z_erofs_try_to_claim_pcluster(struct z_erofs_collector *clt) 39547e4937aSGao Xiang { 396473e15b0SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 397473e15b0SGao Xiang z_erofs_next_pcluster_t *owned_head = &clt->owned_head; 39847e4937aSGao Xiang 399473e15b0SGao Xiang /* type 1, nil pcluster (this pcluster doesn't belong to any chain.) */ 400473e15b0SGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL, 401473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_NIL) { 40247e4937aSGao Xiang *owned_head = &pcl->next; 403473e15b0SGao Xiang /* so we can attach this pcluster to our submission chain. */ 404473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 405473e15b0SGao Xiang return; 406473e15b0SGao Xiang } 407473e15b0SGao Xiang 40847e4937aSGao Xiang /* 409473e15b0SGao Xiang * type 2, link to the end of an existing open chain, be careful 410473e15b0SGao Xiang * that its submission is controlled by the original attached chain. 41147e4937aSGao Xiang */ 41247e4937aSGao Xiang if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 413473e15b0SGao Xiang *owned_head) == Z_EROFS_PCLUSTER_TAIL) { 41447e4937aSGao Xiang *owned_head = Z_EROFS_PCLUSTER_TAIL; 415473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY_HOOKED; 416473e15b0SGao Xiang clt->tailpcl = NULL; 417473e15b0SGao Xiang return; 41847e4937aSGao Xiang } 419473e15b0SGao Xiang /* type 3, it belongs to a chain, but it isn't the end of the chain */ 420473e15b0SGao Xiang clt->mode = COLLECT_PRIMARY; 42147e4937aSGao Xiang } 42247e4937aSGao Xiang 4239e579fc1SGao Xiang static int z_erofs_lookup_collection(struct z_erofs_collector *clt, 42447e4937aSGao Xiang struct inode *inode, 42547e4937aSGao Xiang struct erofs_map_blocks *map) 42647e4937aSGao Xiang { 42764094a04SGao Xiang struct z_erofs_pcluster *pcl = clt->pcl; 42847e4937aSGao Xiang struct z_erofs_collection *cl; 42947e4937aSGao Xiang unsigned int length; 43047e4937aSGao Xiang 43164094a04SGao Xiang /* to avoid unexpected loop formed by corrupted images */ 43247e4937aSGao Xiang if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) { 43347e4937aSGao Xiang DBG_BUGON(1); 4349e579fc1SGao Xiang return -EFSCORRUPTED; 43547e4937aSGao Xiang } 43647e4937aSGao Xiang 43747e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 4388d8a09b0SGao Xiang if (cl->pageofs != (map->m_la & ~PAGE_MASK)) { 43947e4937aSGao Xiang DBG_BUGON(1); 4409e579fc1SGao Xiang return -EFSCORRUPTED; 44147e4937aSGao Xiang } 44247e4937aSGao Xiang 44347e4937aSGao Xiang length = READ_ONCE(pcl->length); 44447e4937aSGao Xiang if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) { 44547e4937aSGao Xiang if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) { 44647e4937aSGao Xiang DBG_BUGON(1); 4479e579fc1SGao Xiang return -EFSCORRUPTED; 44847e4937aSGao Xiang } 44947e4937aSGao Xiang } else { 45047e4937aSGao Xiang unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT; 45147e4937aSGao Xiang 45247e4937aSGao Xiang if (map->m_flags & EROFS_MAP_FULL_MAPPED) 45347e4937aSGao Xiang llen |= Z_EROFS_PCLUSTER_FULL_LENGTH; 45447e4937aSGao Xiang 45547e4937aSGao Xiang while (llen > length && 45647e4937aSGao Xiang length != cmpxchg_relaxed(&pcl->length, length, llen)) { 45747e4937aSGao Xiang cpu_relax(); 45847e4937aSGao Xiang length = READ_ONCE(pcl->length); 45947e4937aSGao Xiang } 46047e4937aSGao Xiang } 46147e4937aSGao Xiang mutex_lock(&cl->lock); 46247e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 46347e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 46447e4937aSGao Xiang clt->tailpcl = pcl; 465473e15b0SGao Xiang 466473e15b0SGao Xiang z_erofs_try_to_claim_pcluster(clt); 46747e4937aSGao Xiang clt->cl = cl; 4689e579fc1SGao Xiang return 0; 46947e4937aSGao Xiang } 47047e4937aSGao Xiang 4719e579fc1SGao Xiang static int z_erofs_register_collection(struct z_erofs_collector *clt, 47247e4937aSGao Xiang struct inode *inode, 47347e4937aSGao Xiang struct erofs_map_blocks *map) 47447e4937aSGao Xiang { 47547e4937aSGao Xiang struct z_erofs_pcluster *pcl; 47647e4937aSGao Xiang struct z_erofs_collection *cl; 47764094a04SGao Xiang struct erofs_workgroup *grp; 47847e4937aSGao Xiang int err; 47947e4937aSGao Xiang 4809f6cc76eSGao Xiang /* no available pcluster, let's allocate one */ 4819f6cc76eSGao Xiang pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT); 4829f6cc76eSGao Xiang if (IS_ERR(pcl)) 4839f6cc76eSGao Xiang return PTR_ERR(pcl); 48447e4937aSGao Xiang 48564094a04SGao Xiang atomic_set(&pcl->obj.refcount, 1); 48647e4937aSGao Xiang pcl->obj.index = map->m_pa >> PAGE_SHIFT; 48747e4937aSGao Xiang 48847e4937aSGao Xiang pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) | 48947e4937aSGao Xiang (map->m_flags & EROFS_MAP_FULL_MAPPED ? 49047e4937aSGao Xiang Z_EROFS_PCLUSTER_FULL_LENGTH : 0); 49147e4937aSGao Xiang 49247e4937aSGao Xiang if (map->m_flags & EROFS_MAP_ZIPPED) 49347e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4; 49447e4937aSGao Xiang else 49547e4937aSGao Xiang pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; 49647e4937aSGao Xiang 49747e4937aSGao Xiang /* new pclusters should be claimed as type 1, primary and followed */ 49847e4937aSGao Xiang pcl->next = clt->owned_head; 49947e4937aSGao Xiang clt->mode = COLLECT_PRIMARY_FOLLOWED; 50047e4937aSGao Xiang 50147e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 50247e4937aSGao Xiang cl->pageofs = map->m_la & ~PAGE_MASK; 50347e4937aSGao Xiang 50447e4937aSGao Xiang /* 50547e4937aSGao Xiang * lock all primary followed works before visible to others 50647e4937aSGao Xiang * and mutex_trylock *never* fails for a new pcluster. 50747e4937aSGao Xiang */ 5089f6cc76eSGao Xiang mutex_init(&cl->lock); 50964094a04SGao Xiang DBG_BUGON(!mutex_trylock(&cl->lock)); 51047e4937aSGao Xiang 51164094a04SGao Xiang grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj); 51264094a04SGao Xiang if (IS_ERR(grp)) { 51364094a04SGao Xiang err = PTR_ERR(grp); 51464094a04SGao Xiang goto err_out; 51564094a04SGao Xiang } 51664094a04SGao Xiang 51764094a04SGao Xiang if (grp != &pcl->obj) { 51864094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 51964094a04SGao Xiang err = -EEXIST; 52064094a04SGao Xiang goto err_out; 52147e4937aSGao Xiang } 52247e4937aSGao Xiang /* used to check tail merging loop due to corrupted images */ 52347e4937aSGao Xiang if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL) 52447e4937aSGao Xiang clt->tailpcl = pcl; 52547e4937aSGao Xiang clt->owned_head = &pcl->next; 52647e4937aSGao Xiang clt->pcl = pcl; 52747e4937aSGao Xiang clt->cl = cl; 5289e579fc1SGao Xiang return 0; 52964094a04SGao Xiang 53064094a04SGao Xiang err_out: 53164094a04SGao Xiang mutex_unlock(&cl->lock); 5329f6cc76eSGao Xiang z_erofs_free_pcluster(pcl); 53364094a04SGao Xiang return err; 53447e4937aSGao Xiang } 53547e4937aSGao Xiang 53647e4937aSGao Xiang static int z_erofs_collector_begin(struct z_erofs_collector *clt, 53747e4937aSGao Xiang struct inode *inode, 53847e4937aSGao Xiang struct erofs_map_blocks *map) 53947e4937aSGao Xiang { 54064094a04SGao Xiang struct erofs_workgroup *grp; 5419e579fc1SGao Xiang int ret; 54247e4937aSGao Xiang 54347e4937aSGao Xiang DBG_BUGON(clt->cl); 54447e4937aSGao Xiang 54547e4937aSGao Xiang /* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */ 54647e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL); 54747e4937aSGao Xiang DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 54847e4937aSGao Xiang 54947e4937aSGao Xiang if (!PAGE_ALIGNED(map->m_pa)) { 55047e4937aSGao Xiang DBG_BUGON(1); 55147e4937aSGao Xiang return -EINVAL; 55247e4937aSGao Xiang } 55347e4937aSGao Xiang 55464094a04SGao Xiang grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT); 55564094a04SGao Xiang if (grp) { 55664094a04SGao Xiang clt->pcl = container_of(grp, struct z_erofs_pcluster, obj); 55764094a04SGao Xiang } else { 5589e579fc1SGao Xiang ret = z_erofs_register_collection(clt, inode, map); 55947e4937aSGao Xiang 56064094a04SGao Xiang if (!ret) 56164094a04SGao Xiang goto out; 56264094a04SGao Xiang if (ret != -EEXIST) 5639e579fc1SGao Xiang return ret; 56464094a04SGao Xiang } 56547e4937aSGao Xiang 56664094a04SGao Xiang ret = z_erofs_lookup_collection(clt, inode, map); 56764094a04SGao Xiang if (ret) { 56864094a04SGao Xiang erofs_workgroup_put(&clt->pcl->obj); 56964094a04SGao Xiang return ret; 57064094a04SGao Xiang } 57164094a04SGao Xiang 57264094a04SGao Xiang out: 57347e4937aSGao Xiang z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS, 5749e579fc1SGao Xiang clt->cl->pagevec, clt->cl->vcnt); 57547e4937aSGao Xiang 57681382f5fSGao Xiang /* since file-backed online pages are traversed in reverse order */ 57781382f5fSGao Xiang clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages; 57847e4937aSGao Xiang return 0; 57947e4937aSGao Xiang } 58047e4937aSGao Xiang 58147e4937aSGao Xiang /* 58247e4937aSGao Xiang * keep in mind that no referenced pclusters will be freed 58347e4937aSGao Xiang * only after a RCU grace period. 58447e4937aSGao Xiang */ 58547e4937aSGao Xiang static void z_erofs_rcu_callback(struct rcu_head *head) 58647e4937aSGao Xiang { 58747e4937aSGao Xiang struct z_erofs_collection *const cl = 58847e4937aSGao Xiang container_of(head, struct z_erofs_collection, rcu); 58947e4937aSGao Xiang 5909f6cc76eSGao Xiang z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster, 59147e4937aSGao Xiang primary_collection)); 59247e4937aSGao Xiang } 59347e4937aSGao Xiang 59447e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp) 59547e4937aSGao Xiang { 59647e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 59747e4937aSGao Xiang container_of(grp, struct z_erofs_pcluster, obj); 59847e4937aSGao Xiang struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl); 59947e4937aSGao Xiang 60047e4937aSGao Xiang call_rcu(&cl->rcu, z_erofs_rcu_callback); 60147e4937aSGao Xiang } 60247e4937aSGao Xiang 60347e4937aSGao Xiang static void z_erofs_collection_put(struct z_erofs_collection *cl) 60447e4937aSGao Xiang { 60547e4937aSGao Xiang struct z_erofs_pcluster *const pcl = 60647e4937aSGao Xiang container_of(cl, struct z_erofs_pcluster, primary_collection); 60747e4937aSGao Xiang 60847e4937aSGao Xiang erofs_workgroup_put(&pcl->obj); 60947e4937aSGao Xiang } 61047e4937aSGao Xiang 61147e4937aSGao Xiang static bool z_erofs_collector_end(struct z_erofs_collector *clt) 61247e4937aSGao Xiang { 61347e4937aSGao Xiang struct z_erofs_collection *cl = clt->cl; 61447e4937aSGao Xiang 61547e4937aSGao Xiang if (!cl) 61647e4937aSGao Xiang return false; 61747e4937aSGao Xiang 61847e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&clt->vector, false); 61947e4937aSGao Xiang mutex_unlock(&cl->lock); 62047e4937aSGao Xiang 62147e4937aSGao Xiang /* 62247e4937aSGao Xiang * if all pending pages are added, don't hold its reference 62347e4937aSGao Xiang * any longer if the pcluster isn't hosted by ourselves. 62447e4937aSGao Xiang */ 62547e4937aSGao Xiang if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE) 62647e4937aSGao Xiang z_erofs_collection_put(cl); 62747e4937aSGao Xiang 62847e4937aSGao Xiang clt->cl = NULL; 62947e4937aSGao Xiang return true; 63047e4937aSGao Xiang } 63147e4937aSGao Xiang 63247e4937aSGao Xiang static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe, 63347e4937aSGao Xiang unsigned int cachestrategy, 63447e4937aSGao Xiang erofs_off_t la) 63547e4937aSGao Xiang { 63647e4937aSGao Xiang if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED) 63747e4937aSGao Xiang return false; 63847e4937aSGao Xiang 63947e4937aSGao Xiang if (fe->backmost) 64047e4937aSGao Xiang return true; 64147e4937aSGao Xiang 64247e4937aSGao Xiang return cachestrategy >= EROFS_ZIP_CACHE_READAROUND && 64347e4937aSGao Xiang la < fe->headoffset; 64447e4937aSGao Xiang } 64547e4937aSGao Xiang 64647e4937aSGao Xiang static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe, 6471825c8d7SGao Xiang struct page *page, struct list_head *pagepool) 64847e4937aSGao Xiang { 64947e4937aSGao Xiang struct inode *const inode = fe->inode; 650bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 65147e4937aSGao Xiang struct erofs_map_blocks *const map = &fe->map; 65247e4937aSGao Xiang struct z_erofs_collector *const clt = &fe->clt; 65347e4937aSGao Xiang const loff_t offset = page_offset(page); 654dc76ea8cSGao Xiang bool tight = true; 65547e4937aSGao Xiang 65647e4937aSGao Xiang enum z_erofs_cache_alloctype cache_strategy; 65747e4937aSGao Xiang enum z_erofs_page_type page_type; 65847e4937aSGao Xiang unsigned int cur, end, spiltted, index; 65947e4937aSGao Xiang int err = 0; 66047e4937aSGao Xiang 66147e4937aSGao Xiang /* register locked file pages as online pages in pack */ 66247e4937aSGao Xiang z_erofs_onlinepage_init(page); 66347e4937aSGao Xiang 66447e4937aSGao Xiang spiltted = 0; 66547e4937aSGao Xiang end = PAGE_SIZE; 66647e4937aSGao Xiang repeat: 66747e4937aSGao Xiang cur = end - 1; 66847e4937aSGao Xiang 66947e4937aSGao Xiang /* lucky, within the range of the current map_blocks */ 67047e4937aSGao Xiang if (offset + cur >= map->m_la && 67147e4937aSGao Xiang offset + cur < map->m_la + map->m_llen) { 67247e4937aSGao Xiang /* didn't get a valid collection previously (very rare) */ 67347e4937aSGao Xiang if (!clt->cl) 67447e4937aSGao Xiang goto restart_now; 67547e4937aSGao Xiang goto hitted; 67647e4937aSGao Xiang } 67747e4937aSGao Xiang 67847e4937aSGao Xiang /* go ahead the next map_blocks */ 6794f761fa2SGao Xiang erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur); 68047e4937aSGao Xiang 68147e4937aSGao Xiang if (z_erofs_collector_end(clt)) 68247e4937aSGao Xiang fe->backmost = false; 68347e4937aSGao Xiang 68447e4937aSGao Xiang map->m_la = offset + cur; 68547e4937aSGao Xiang map->m_llen = 0; 68647e4937aSGao Xiang err = z_erofs_map_blocks_iter(inode, map, 0); 6878d8a09b0SGao Xiang if (err) 68847e4937aSGao Xiang goto err_out; 68947e4937aSGao Xiang 69047e4937aSGao Xiang restart_now: 6918d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) 69247e4937aSGao Xiang goto hitted; 69347e4937aSGao Xiang 69447e4937aSGao Xiang err = z_erofs_collector_begin(clt, inode, map); 6958d8a09b0SGao Xiang if (err) 69647e4937aSGao Xiang goto err_out; 69747e4937aSGao Xiang 69847e4937aSGao Xiang /* preload all compressed pages (maybe downgrade role if necessary) */ 699f57a3fe4SChao Yu if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la)) 7001825c8d7SGao Xiang cache_strategy = TRYALLOC; 70147e4937aSGao Xiang else 70247e4937aSGao Xiang cache_strategy = DONTALLOC; 70347e4937aSGao Xiang 7041825c8d7SGao Xiang preload_compressed_pages(clt, MNGD_MAPPING(sbi), 7051825c8d7SGao Xiang cache_strategy, pagepool); 70647e4937aSGao Xiang 70747e4937aSGao Xiang hitted: 708dc76ea8cSGao Xiang /* 709dc76ea8cSGao Xiang * Ensure the current partial page belongs to this submit chain rather 710dc76ea8cSGao Xiang * than other concurrent submit chains or the noio(bypass) chain since 711dc76ea8cSGao Xiang * those chains are handled asynchronously thus the page cannot be used 712dc76ea8cSGao Xiang * for inplace I/O or pagevec (should be processed in strict order.) 713dc76ea8cSGao Xiang */ 714dc76ea8cSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED && 715dc76ea8cSGao Xiang clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE); 716dc76ea8cSGao Xiang 71747e4937aSGao Xiang cur = end - min_t(unsigned int, offset + end - map->m_la, end); 7188d8a09b0SGao Xiang if (!(map->m_flags & EROFS_MAP_MAPPED)) { 71947e4937aSGao Xiang zero_user_segment(page, cur, end); 72047e4937aSGao Xiang goto next_part; 72147e4937aSGao Xiang } 72247e4937aSGao Xiang 72347e4937aSGao Xiang /* let's derive page type */ 72447e4937aSGao Xiang page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD : 72547e4937aSGao Xiang (!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 72647e4937aSGao Xiang (tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE : 72747e4937aSGao Xiang Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED)); 72847e4937aSGao Xiang 72947e4937aSGao Xiang if (cur) 73047e4937aSGao Xiang tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED); 73147e4937aSGao Xiang 73247e4937aSGao Xiang retry: 73347e4937aSGao Xiang err = z_erofs_attach_page(clt, page, page_type); 7346aaa7b06SGao Xiang /* should allocate an additional short-lived page for pagevec */ 73547e4937aSGao Xiang if (err == -EAGAIN) { 73647e4937aSGao Xiang struct page *const newpage = 737e3f78d5eSChao Yu alloc_page(GFP_NOFS | __GFP_NOFAIL); 73847e4937aSGao Xiang 7396aaa7b06SGao Xiang set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE); 74047e4937aSGao Xiang err = z_erofs_attach_page(clt, newpage, 74147e4937aSGao Xiang Z_EROFS_PAGE_TYPE_EXCLUSIVE); 7428d8a09b0SGao Xiang if (!err) 74347e4937aSGao Xiang goto retry; 74447e4937aSGao Xiang } 74547e4937aSGao Xiang 7468d8a09b0SGao Xiang if (err) 74747e4937aSGao Xiang goto err_out; 74847e4937aSGao Xiang 74947e4937aSGao Xiang index = page->index - (map->m_la >> PAGE_SHIFT); 75047e4937aSGao Xiang 75147e4937aSGao Xiang z_erofs_onlinepage_fixup(page, index, true); 75247e4937aSGao Xiang 75347e4937aSGao Xiang /* bump up the number of spiltted parts of a page */ 75447e4937aSGao Xiang ++spiltted; 75547e4937aSGao Xiang /* also update nr_pages */ 75647e4937aSGao Xiang clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1); 75747e4937aSGao Xiang next_part: 75847e4937aSGao Xiang /* can be used for verification */ 75947e4937aSGao Xiang map->m_llen = offset + cur - map->m_la; 76047e4937aSGao Xiang 76147e4937aSGao Xiang end = cur; 76247e4937aSGao Xiang if (end > 0) 76347e4937aSGao Xiang goto repeat; 76447e4937aSGao Xiang 76547e4937aSGao Xiang out: 76647e4937aSGao Xiang z_erofs_onlinepage_endio(page); 76747e4937aSGao Xiang 7684f761fa2SGao Xiang erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu", 76947e4937aSGao Xiang __func__, page, spiltted, map->m_llen); 77047e4937aSGao Xiang return err; 77147e4937aSGao Xiang 77247e4937aSGao Xiang /* if some error occurred while processing this page */ 77347e4937aSGao Xiang err_out: 77447e4937aSGao Xiang SetPageError(page); 77547e4937aSGao Xiang goto out; 77647e4937aSGao Xiang } 77747e4937aSGao Xiang 778648f2de0SHuang Jianan static void z_erofs_decompressqueue_work(struct work_struct *work); 779a4b1fab1SGao Xiang static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io, 780a4b1fab1SGao Xiang bool sync, int bios) 78147e4937aSGao Xiang { 78230048cdaSHuang Jianan struct erofs_sb_info *const sbi = EROFS_SB(io->sb); 78330048cdaSHuang Jianan 784a4b1fab1SGao Xiang /* wake up the caller thread for sync decompression */ 785a4b1fab1SGao Xiang if (sync) { 78647e4937aSGao Xiang unsigned long flags; 78747e4937aSGao Xiang 78847e4937aSGao Xiang spin_lock_irqsave(&io->u.wait.lock, flags); 78947e4937aSGao Xiang if (!atomic_add_return(bios, &io->pending_bios)) 79047e4937aSGao Xiang wake_up_locked(&io->u.wait); 79147e4937aSGao Xiang spin_unlock_irqrestore(&io->u.wait.lock, flags); 79247e4937aSGao Xiang return; 79347e4937aSGao Xiang } 79447e4937aSGao Xiang 795648f2de0SHuang Jianan if (atomic_add_return(bios, &io->pending_bios)) 796648f2de0SHuang Jianan return; 79730048cdaSHuang Jianan /* Use workqueue and sync decompression for atomic contexts only */ 798648f2de0SHuang Jianan if (in_atomic() || irqs_disabled()) { 79947e4937aSGao Xiang queue_work(z_erofs_workqueue, &io->u.work); 80030048cdaSHuang Jianan sbi->ctx.readahead_sync_decompress = true; 801648f2de0SHuang Jianan return; 802648f2de0SHuang Jianan } 803648f2de0SHuang Jianan z_erofs_decompressqueue_work(&io->u.work); 80447e4937aSGao Xiang } 80547e4937aSGao Xiang 8066aaa7b06SGao Xiang static bool z_erofs_page_is_invalidated(struct page *page) 8076aaa7b06SGao Xiang { 8086aaa7b06SGao Xiang return !page->mapping && !z_erofs_is_shortlived_page(page); 8096aaa7b06SGao Xiang } 8106aaa7b06SGao Xiang 8110c638f70SGao Xiang static void z_erofs_decompressqueue_endio(struct bio *bio) 81247e4937aSGao Xiang { 813a4b1fab1SGao Xiang tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private); 814a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t); 81547e4937aSGao Xiang blk_status_t err = bio->bi_status; 81647e4937aSGao Xiang struct bio_vec *bvec; 81747e4937aSGao Xiang struct bvec_iter_all iter_all; 81847e4937aSGao Xiang 81947e4937aSGao Xiang bio_for_each_segment_all(bvec, bio, iter_all) { 82047e4937aSGao Xiang struct page *page = bvec->bv_page; 82147e4937aSGao Xiang 82247e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 8236aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 82447e4937aSGao Xiang 8258d8a09b0SGao Xiang if (err) 82647e4937aSGao Xiang SetPageError(page); 82747e4937aSGao Xiang 828a4b1fab1SGao Xiang if (erofs_page_is_managed(EROFS_SB(q->sb), page)) { 829a4b1fab1SGao Xiang if (!err) 830a4b1fab1SGao Xiang SetPageUptodate(page); 83147e4937aSGao Xiang unlock_page(page); 83247e4937aSGao Xiang } 833a4b1fab1SGao Xiang } 834a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1); 83547e4937aSGao Xiang bio_put(bio); 83647e4937aSGao Xiang } 83747e4937aSGao Xiang 83847e4937aSGao Xiang static int z_erofs_decompress_pcluster(struct super_block *sb, 83947e4937aSGao Xiang struct z_erofs_pcluster *pcl, 84047e4937aSGao Xiang struct list_head *pagepool) 84147e4937aSGao Xiang { 84247e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 84347e4937aSGao Xiang struct z_erofs_pagevec_ctor ctor; 8449f6cc76eSGao Xiang unsigned int i, inputsize, outputsize, llen, nr_pages; 84547e4937aSGao Xiang struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES]; 84647e4937aSGao Xiang struct page **pages, **compressed_pages, *page; 84747e4937aSGao Xiang 84847e4937aSGao Xiang enum z_erofs_page_type page_type; 84947e4937aSGao Xiang bool overlapped, partial; 85047e4937aSGao Xiang struct z_erofs_collection *cl; 85147e4937aSGao Xiang int err; 85247e4937aSGao Xiang 85347e4937aSGao Xiang might_sleep(); 85447e4937aSGao Xiang cl = z_erofs_primarycollection(pcl); 85547e4937aSGao Xiang DBG_BUGON(!READ_ONCE(cl->nr_pages)); 85647e4937aSGao Xiang 85747e4937aSGao Xiang mutex_lock(&cl->lock); 85847e4937aSGao Xiang nr_pages = cl->nr_pages; 85947e4937aSGao Xiang 8608d8a09b0SGao Xiang if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) { 86147e4937aSGao Xiang pages = pages_onstack; 86247e4937aSGao Xiang } else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES && 86347e4937aSGao Xiang mutex_trylock(&z_pagemap_global_lock)) { 86447e4937aSGao Xiang pages = z_pagemap_global; 86547e4937aSGao Xiang } else { 86647e4937aSGao Xiang gfp_t gfp_flags = GFP_KERNEL; 86747e4937aSGao Xiang 86847e4937aSGao Xiang if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES) 86947e4937aSGao Xiang gfp_flags |= __GFP_NOFAIL; 87047e4937aSGao Xiang 87147e4937aSGao Xiang pages = kvmalloc_array(nr_pages, sizeof(struct page *), 87247e4937aSGao Xiang gfp_flags); 87347e4937aSGao Xiang 87447e4937aSGao Xiang /* fallback to global pagemap for the lowmem scenario */ 8758d8a09b0SGao Xiang if (!pages) { 87647e4937aSGao Xiang mutex_lock(&z_pagemap_global_lock); 87747e4937aSGao Xiang pages = z_pagemap_global; 87847e4937aSGao Xiang } 87947e4937aSGao Xiang } 88047e4937aSGao Xiang 88147e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) 88247e4937aSGao Xiang pages[i] = NULL; 88347e4937aSGao Xiang 88447e4937aSGao Xiang err = 0; 88547e4937aSGao Xiang z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS, 88647e4937aSGao Xiang cl->pagevec, 0); 88747e4937aSGao Xiang 88847e4937aSGao Xiang for (i = 0; i < cl->vcnt; ++i) { 88947e4937aSGao Xiang unsigned int pagenr; 89047e4937aSGao Xiang 89147e4937aSGao Xiang page = z_erofs_pagevec_dequeue(&ctor, &page_type); 89247e4937aSGao Xiang 89347e4937aSGao Xiang /* all pages in pagevec ought to be valid */ 89447e4937aSGao Xiang DBG_BUGON(!page); 8956aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 89647e4937aSGao Xiang 8976aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 89847e4937aSGao Xiang continue; 89947e4937aSGao Xiang 90047e4937aSGao Xiang if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD) 90147e4937aSGao Xiang pagenr = 0; 90247e4937aSGao Xiang else 90347e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 90447e4937aSGao Xiang 90547e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 90647e4937aSGao Xiang 90747e4937aSGao Xiang /* 90847e4937aSGao Xiang * currently EROFS doesn't support multiref(dedup), 90947e4937aSGao Xiang * so here erroring out one multiref page. 91047e4937aSGao Xiang */ 9118d8a09b0SGao Xiang if (pages[pagenr]) { 91247e4937aSGao Xiang DBG_BUGON(1); 91347e4937aSGao Xiang SetPageError(pages[pagenr]); 91447e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 91547e4937aSGao Xiang err = -EFSCORRUPTED; 91647e4937aSGao Xiang } 91747e4937aSGao Xiang pages[pagenr] = page; 91847e4937aSGao Xiang } 91947e4937aSGao Xiang z_erofs_pagevec_ctor_exit(&ctor, true); 92047e4937aSGao Xiang 92147e4937aSGao Xiang overlapped = false; 92247e4937aSGao Xiang compressed_pages = pcl->compressed_pages; 92347e4937aSGao Xiang 9249f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 92547e4937aSGao Xiang unsigned int pagenr; 92647e4937aSGao Xiang 92747e4937aSGao Xiang page = compressed_pages[i]; 92847e4937aSGao Xiang 92947e4937aSGao Xiang /* all compressed pages ought to be valid */ 93047e4937aSGao Xiang DBG_BUGON(!page); 9316aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 93247e4937aSGao Xiang 9336aaa7b06SGao Xiang if (!z_erofs_is_shortlived_page(page)) { 93447e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) { 9358d8a09b0SGao Xiang if (!PageUptodate(page)) 93647e4937aSGao Xiang err = -EIO; 93747e4937aSGao Xiang continue; 93847e4937aSGao Xiang } 93947e4937aSGao Xiang 94047e4937aSGao Xiang /* 94147e4937aSGao Xiang * only if non-head page can be selected 94247e4937aSGao Xiang * for inplace decompression 94347e4937aSGao Xiang */ 94447e4937aSGao Xiang pagenr = z_erofs_onlinepage_index(page); 94547e4937aSGao Xiang 94647e4937aSGao Xiang DBG_BUGON(pagenr >= nr_pages); 9478d8a09b0SGao Xiang if (pages[pagenr]) { 94847e4937aSGao Xiang DBG_BUGON(1); 94947e4937aSGao Xiang SetPageError(pages[pagenr]); 95047e4937aSGao Xiang z_erofs_onlinepage_endio(pages[pagenr]); 95147e4937aSGao Xiang err = -EFSCORRUPTED; 95247e4937aSGao Xiang } 95347e4937aSGao Xiang pages[pagenr] = page; 95447e4937aSGao Xiang 95547e4937aSGao Xiang overlapped = true; 95647e4937aSGao Xiang } 95747e4937aSGao Xiang 9586aaa7b06SGao Xiang /* PG_error needs checking for all non-managed pages */ 9598d8a09b0SGao Xiang if (PageError(page)) { 96047e4937aSGao Xiang DBG_BUGON(PageUptodate(page)); 96147e4937aSGao Xiang err = -EIO; 96247e4937aSGao Xiang } 96347e4937aSGao Xiang } 96447e4937aSGao Xiang 9658d8a09b0SGao Xiang if (err) 96647e4937aSGao Xiang goto out; 96747e4937aSGao Xiang 96847e4937aSGao Xiang llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT; 96947e4937aSGao Xiang if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) { 97047e4937aSGao Xiang outputsize = llen; 97147e4937aSGao Xiang partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH); 97247e4937aSGao Xiang } else { 97347e4937aSGao Xiang outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs; 97447e4937aSGao Xiang partial = true; 97547e4937aSGao Xiang } 97647e4937aSGao Xiang 9779f6cc76eSGao Xiang inputsize = pcl->pclusterpages * PAGE_SIZE; 97847e4937aSGao Xiang err = z_erofs_decompress(&(struct z_erofs_decompress_req) { 97947e4937aSGao Xiang .sb = sb, 98047e4937aSGao Xiang .in = compressed_pages, 98147e4937aSGao Xiang .out = pages, 98247e4937aSGao Xiang .pageofs_out = cl->pageofs, 9839f6cc76eSGao Xiang .inputsize = inputsize, 98447e4937aSGao Xiang .outputsize = outputsize, 98547e4937aSGao Xiang .alg = pcl->algorithmformat, 98647e4937aSGao Xiang .inplace_io = overlapped, 98747e4937aSGao Xiang .partial_decoding = partial 98847e4937aSGao Xiang }, pagepool); 98947e4937aSGao Xiang 99047e4937aSGao Xiang out: 991fe6adcceSRuiqi Gong /* must handle all compressed pages before ending pages */ 9929f6cc76eSGao Xiang for (i = 0; i < pcl->pclusterpages; ++i) { 99347e4937aSGao Xiang page = compressed_pages[i]; 99447e4937aSGao Xiang 99547e4937aSGao Xiang if (erofs_page_is_managed(sbi, page)) 99647e4937aSGao Xiang continue; 99747e4937aSGao Xiang 9986aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 9996aaa7b06SGao Xiang (void)z_erofs_put_shortlivedpage(pagepool, page); 100047e4937aSGao Xiang 100147e4937aSGao Xiang WRITE_ONCE(compressed_pages[i], NULL); 100247e4937aSGao Xiang } 100347e4937aSGao Xiang 100447e4937aSGao Xiang for (i = 0; i < nr_pages; ++i) { 100547e4937aSGao Xiang page = pages[i]; 100647e4937aSGao Xiang if (!page) 100747e4937aSGao Xiang continue; 100847e4937aSGao Xiang 10096aaa7b06SGao Xiang DBG_BUGON(z_erofs_page_is_invalidated(page)); 101047e4937aSGao Xiang 10116aaa7b06SGao Xiang /* recycle all individual short-lived pages */ 10126aaa7b06SGao Xiang if (z_erofs_put_shortlivedpage(pagepool, page)) 101347e4937aSGao Xiang continue; 101447e4937aSGao Xiang 10158d8a09b0SGao Xiang if (err < 0) 101647e4937aSGao Xiang SetPageError(page); 101747e4937aSGao Xiang 101847e4937aSGao Xiang z_erofs_onlinepage_endio(page); 101947e4937aSGao Xiang } 102047e4937aSGao Xiang 102147e4937aSGao Xiang if (pages == z_pagemap_global) 102247e4937aSGao Xiang mutex_unlock(&z_pagemap_global_lock); 10238d8a09b0SGao Xiang else if (pages != pages_onstack) 102447e4937aSGao Xiang kvfree(pages); 102547e4937aSGao Xiang 102647e4937aSGao Xiang cl->nr_pages = 0; 102747e4937aSGao Xiang cl->vcnt = 0; 102847e4937aSGao Xiang 102947e4937aSGao Xiang /* all cl locks MUST be taken before the following line */ 103047e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL); 103147e4937aSGao Xiang 103247e4937aSGao Xiang /* all cl locks SHOULD be released right now */ 103347e4937aSGao Xiang mutex_unlock(&cl->lock); 103447e4937aSGao Xiang 103547e4937aSGao Xiang z_erofs_collection_put(cl); 103647e4937aSGao Xiang return err; 103747e4937aSGao Xiang } 103847e4937aSGao Xiang 10390c638f70SGao Xiang static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io, 104047e4937aSGao Xiang struct list_head *pagepool) 104147e4937aSGao Xiang { 104247e4937aSGao Xiang z_erofs_next_pcluster_t owned = io->head; 104347e4937aSGao Xiang 104447e4937aSGao Xiang while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) { 104547e4937aSGao Xiang struct z_erofs_pcluster *pcl; 104647e4937aSGao Xiang 104747e4937aSGao Xiang /* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */ 104847e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL); 104947e4937aSGao Xiang 105047e4937aSGao Xiang /* no possible that 'owned' equals NULL */ 105147e4937aSGao Xiang DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL); 105247e4937aSGao Xiang 105347e4937aSGao Xiang pcl = container_of(owned, struct z_erofs_pcluster, next); 105447e4937aSGao Xiang owned = READ_ONCE(pcl->next); 105547e4937aSGao Xiang 1056a4b1fab1SGao Xiang z_erofs_decompress_pcluster(io->sb, pcl, pagepool); 105747e4937aSGao Xiang } 105847e4937aSGao Xiang } 105947e4937aSGao Xiang 10600c638f70SGao Xiang static void z_erofs_decompressqueue_work(struct work_struct *work) 106147e4937aSGao Xiang { 1062a4b1fab1SGao Xiang struct z_erofs_decompressqueue *bgq = 1063a4b1fab1SGao Xiang container_of(work, struct z_erofs_decompressqueue, u.work); 106447e4937aSGao Xiang LIST_HEAD(pagepool); 106547e4937aSGao Xiang 1066a4b1fab1SGao Xiang DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 10670c638f70SGao Xiang z_erofs_decompress_queue(bgq, &pagepool); 106847e4937aSGao Xiang 106947e4937aSGao Xiang put_pages_list(&pagepool); 1070a4b1fab1SGao Xiang kvfree(bgq); 107147e4937aSGao Xiang } 107247e4937aSGao Xiang 107347e4937aSGao Xiang static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl, 107447e4937aSGao Xiang unsigned int nr, 107547e4937aSGao Xiang struct list_head *pagepool, 107647e4937aSGao Xiang struct address_space *mc, 107747e4937aSGao Xiang gfp_t gfp) 107847e4937aSGao Xiang { 107947e4937aSGao Xiang const pgoff_t index = pcl->obj.index; 108047e4937aSGao Xiang bool tocache = false; 108147e4937aSGao Xiang 108247e4937aSGao Xiang struct address_space *mapping; 108347e4937aSGao Xiang struct page *oldpage, *page; 108447e4937aSGao Xiang 108547e4937aSGao Xiang compressed_page_t t; 108647e4937aSGao Xiang int justfound; 108747e4937aSGao Xiang 108847e4937aSGao Xiang repeat: 108947e4937aSGao Xiang page = READ_ONCE(pcl->compressed_pages[nr]); 109047e4937aSGao Xiang oldpage = page; 109147e4937aSGao Xiang 109247e4937aSGao Xiang if (!page) 109347e4937aSGao Xiang goto out_allocpage; 109447e4937aSGao Xiang 109547e4937aSGao Xiang /* 109647e4937aSGao Xiang * the cached page has not been allocated and 109747e4937aSGao Xiang * an placeholder is out there, prepare it now. 109847e4937aSGao Xiang */ 1099bda17a45SGao Xiang if (page == PAGE_UNALLOCATED) { 110047e4937aSGao Xiang tocache = true; 110147e4937aSGao Xiang goto out_allocpage; 110247e4937aSGao Xiang } 110347e4937aSGao Xiang 110447e4937aSGao Xiang /* process the target tagged pointer */ 110547e4937aSGao Xiang t = tagptr_init(compressed_page_t, page); 110647e4937aSGao Xiang justfound = tagptr_unfold_tags(t); 110747e4937aSGao Xiang page = tagptr_unfold_ptr(t); 110847e4937aSGao Xiang 11091825c8d7SGao Xiang /* 11101825c8d7SGao Xiang * preallocated cached pages, which is used to avoid direct reclaim 11111825c8d7SGao Xiang * otherwise, it will go inplace I/O path instead. 11121825c8d7SGao Xiang */ 11131825c8d7SGao Xiang if (page->private == Z_EROFS_PREALLOCATED_PAGE) { 11141825c8d7SGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 11151825c8d7SGao Xiang set_page_private(page, 0); 11161825c8d7SGao Xiang tocache = true; 11171825c8d7SGao Xiang goto out_tocache; 11181825c8d7SGao Xiang } 111947e4937aSGao Xiang mapping = READ_ONCE(page->mapping); 112047e4937aSGao Xiang 112147e4937aSGao Xiang /* 11226aaa7b06SGao Xiang * file-backed online pages in plcuster are all locked steady, 112347e4937aSGao Xiang * therefore it is impossible for `mapping' to be NULL. 112447e4937aSGao Xiang */ 112547e4937aSGao Xiang if (mapping && mapping != mc) 112647e4937aSGao Xiang /* ought to be unmanaged pages */ 112747e4937aSGao Xiang goto out; 112847e4937aSGao Xiang 11296aaa7b06SGao Xiang /* directly return for shortlived page as well */ 11306aaa7b06SGao Xiang if (z_erofs_is_shortlived_page(page)) 11316aaa7b06SGao Xiang goto out; 11326aaa7b06SGao Xiang 113347e4937aSGao Xiang lock_page(page); 113447e4937aSGao Xiang 113547e4937aSGao Xiang /* only true if page reclaim goes wrong, should never happen */ 113647e4937aSGao Xiang DBG_BUGON(justfound && PagePrivate(page)); 113747e4937aSGao Xiang 113847e4937aSGao Xiang /* the page is still in manage cache */ 113947e4937aSGao Xiang if (page->mapping == mc) { 114047e4937aSGao Xiang WRITE_ONCE(pcl->compressed_pages[nr], page); 114147e4937aSGao Xiang 114247e4937aSGao Xiang ClearPageError(page); 114347e4937aSGao Xiang if (!PagePrivate(page)) { 114447e4937aSGao Xiang /* 114547e4937aSGao Xiang * impossible to be !PagePrivate(page) for 114647e4937aSGao Xiang * the current restriction as well if 114747e4937aSGao Xiang * the page is already in compressed_pages[]. 114847e4937aSGao Xiang */ 114947e4937aSGao Xiang DBG_BUGON(!justfound); 115047e4937aSGao Xiang 115147e4937aSGao Xiang justfound = 0; 115247e4937aSGao Xiang set_page_private(page, (unsigned long)pcl); 115347e4937aSGao Xiang SetPagePrivate(page); 115447e4937aSGao Xiang } 115547e4937aSGao Xiang 115647e4937aSGao Xiang /* no need to submit io if it is already up-to-date */ 115747e4937aSGao Xiang if (PageUptodate(page)) { 115847e4937aSGao Xiang unlock_page(page); 115947e4937aSGao Xiang page = NULL; 116047e4937aSGao Xiang } 116147e4937aSGao Xiang goto out; 116247e4937aSGao Xiang } 116347e4937aSGao Xiang 116447e4937aSGao Xiang /* 116547e4937aSGao Xiang * the managed page has been truncated, it's unsafe to 116647e4937aSGao Xiang * reuse this one, let's allocate a new cache-managed page. 116747e4937aSGao Xiang */ 116847e4937aSGao Xiang DBG_BUGON(page->mapping); 116947e4937aSGao Xiang DBG_BUGON(!justfound); 117047e4937aSGao Xiang 117147e4937aSGao Xiang tocache = true; 117247e4937aSGao Xiang unlock_page(page); 117347e4937aSGao Xiang put_page(page); 117447e4937aSGao Xiang out_allocpage: 11755ddcee1fSGao Xiang page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL); 11765ddcee1fSGao Xiang if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) { 11775ddcee1fSGao Xiang list_add(&page->lru, pagepool); 11785ddcee1fSGao Xiang cond_resched(); 11795ddcee1fSGao Xiang goto repeat; 11805ddcee1fSGao Xiang } 11811825c8d7SGao Xiang out_tocache: 1182bf225074SGao Xiang if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) { 1183bf225074SGao Xiang /* turn into temporary page if fails (1 ref) */ 1184bf225074SGao Xiang set_page_private(page, Z_EROFS_SHORTLIVED_PAGE); 1185bf225074SGao Xiang goto out; 1186a30573b3SGao Xiang } 1187bf225074SGao Xiang attach_page_private(page, pcl); 1188bf225074SGao Xiang /* drop a refcount added by allocpage (then we have 2 refs here) */ 1189bf225074SGao Xiang put_page(page); 1190bf225074SGao Xiang 119147e4937aSGao Xiang out: /* the only exit (for tracing and debugging) */ 119247e4937aSGao Xiang return page; 119347e4937aSGao Xiang } 119447e4937aSGao Xiang 1195a4b1fab1SGao Xiang static struct z_erofs_decompressqueue * 1196a4b1fab1SGao Xiang jobqueue_init(struct super_block *sb, 1197a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 119847e4937aSGao Xiang { 1199a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q; 120047e4937aSGao Xiang 1201a4b1fab1SGao Xiang if (fg && !*fg) { 1202a4b1fab1SGao Xiang q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN); 1203a4b1fab1SGao Xiang if (!q) { 1204a4b1fab1SGao Xiang *fg = true; 1205a4b1fab1SGao Xiang goto fg_out; 120647e4937aSGao Xiang } 12070c638f70SGao Xiang INIT_WORK(&q->u.work, z_erofs_decompressqueue_work); 1208a4b1fab1SGao Xiang } else { 1209a4b1fab1SGao Xiang fg_out: 1210a4b1fab1SGao Xiang q = fgq; 1211a4b1fab1SGao Xiang init_waitqueue_head(&fgq->u.wait); 1212a4b1fab1SGao Xiang atomic_set(&fgq->pending_bios, 0); 1213a4b1fab1SGao Xiang } 1214a4b1fab1SGao Xiang q->sb = sb; 1215a4b1fab1SGao Xiang q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 1216a4b1fab1SGao Xiang return q; 121747e4937aSGao Xiang } 121847e4937aSGao Xiang 121947e4937aSGao Xiang /* define decompression jobqueue types */ 122047e4937aSGao Xiang enum { 122147e4937aSGao Xiang JQ_BYPASS, 122247e4937aSGao Xiang JQ_SUBMIT, 122347e4937aSGao Xiang NR_JOBQUEUES, 122447e4937aSGao Xiang }; 122547e4937aSGao Xiang 122647e4937aSGao Xiang static void *jobqueueset_init(struct super_block *sb, 1227a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[], 1228a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, bool *fg) 122947e4937aSGao Xiang { 123047e4937aSGao Xiang /* 123147e4937aSGao Xiang * if managed cache is enabled, bypass jobqueue is needed, 123247e4937aSGao Xiang * no need to read from device for all pclusters in this queue. 123347e4937aSGao Xiang */ 1234a4b1fab1SGao Xiang q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL); 1235a4b1fab1SGao Xiang q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg); 123647e4937aSGao Xiang 1237a4b1fab1SGao Xiang return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg)); 123847e4937aSGao Xiang } 123947e4937aSGao Xiang 124047e4937aSGao Xiang static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl, 124147e4937aSGao Xiang z_erofs_next_pcluster_t qtail[], 124247e4937aSGao Xiang z_erofs_next_pcluster_t owned_head) 124347e4937aSGao Xiang { 124447e4937aSGao Xiang z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT]; 124547e4937aSGao Xiang z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS]; 124647e4937aSGao Xiang 124747e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 124847e4937aSGao Xiang if (owned_head == Z_EROFS_PCLUSTER_TAIL) 124947e4937aSGao Xiang owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED; 125047e4937aSGao Xiang 125147e4937aSGao Xiang WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED); 125247e4937aSGao Xiang 125347e4937aSGao Xiang WRITE_ONCE(*submit_qtail, owned_head); 125447e4937aSGao Xiang WRITE_ONCE(*bypass_qtail, &pcl->next); 125547e4937aSGao Xiang 125647e4937aSGao Xiang qtail[JQ_BYPASS] = &pcl->next; 125747e4937aSGao Xiang } 125847e4937aSGao Xiang 12591e4a2955SGao Xiang static void z_erofs_submit_queue(struct super_block *sb, 12606ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 126147e4937aSGao Xiang struct list_head *pagepool, 1262a4b1fab1SGao Xiang struct z_erofs_decompressqueue *fgq, 1263a4b1fab1SGao Xiang bool *force_fg) 126447e4937aSGao Xiang { 1265bda17a45SGao Xiang struct erofs_sb_info *const sbi = EROFS_SB(sb); 126647e4937aSGao Xiang z_erofs_next_pcluster_t qtail[NR_JOBQUEUES]; 1267a4b1fab1SGao Xiang struct z_erofs_decompressqueue *q[NR_JOBQUEUES]; 126847e4937aSGao Xiang void *bi_private; 12696ea5aad3SGao Xiang z_erofs_next_pcluster_t owned_head = f->clt.owned_head; 127047e4937aSGao Xiang /* since bio will be NULL, no need to initialize last_index */ 12713f649ab7SKees Cook pgoff_t last_index; 12721e4a2955SGao Xiang unsigned int nr_bios = 0; 12731e4a2955SGao Xiang struct bio *bio = NULL; 127447e4937aSGao Xiang 1275a4b1fab1SGao Xiang bi_private = jobqueueset_init(sb, q, fgq, force_fg); 1276a4b1fab1SGao Xiang qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head; 1277a4b1fab1SGao Xiang qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head; 127847e4937aSGao Xiang 127947e4937aSGao Xiang /* by default, all need io submission */ 128047e4937aSGao Xiang q[JQ_SUBMIT]->head = owned_head; 128147e4937aSGao Xiang 128247e4937aSGao Xiang do { 128347e4937aSGao Xiang struct z_erofs_pcluster *pcl; 12841e4a2955SGao Xiang pgoff_t cur, end; 12851e4a2955SGao Xiang unsigned int i = 0; 12861e4a2955SGao Xiang bool bypass = true; 128747e4937aSGao Xiang 128847e4937aSGao Xiang /* no possible 'owned_head' equals the following */ 128947e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED); 129047e4937aSGao Xiang DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL); 129147e4937aSGao Xiang 129247e4937aSGao Xiang pcl = container_of(owned_head, struct z_erofs_pcluster, next); 129347e4937aSGao Xiang 12941e4a2955SGao Xiang cur = pcl->obj.index; 12959f6cc76eSGao Xiang end = cur + pcl->pclusterpages; 129647e4937aSGao Xiang 129747e4937aSGao Xiang /* close the main owned chain at first */ 129847e4937aSGao Xiang owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL, 129947e4937aSGao Xiang Z_EROFS_PCLUSTER_TAIL_CLOSED); 130047e4937aSGao Xiang 13011e4a2955SGao Xiang do { 13021e4a2955SGao Xiang struct page *page; 130347e4937aSGao Xiang 13041e4a2955SGao Xiang page = pickup_page_for_submission(pcl, i++, pagepool, 130547e4937aSGao Xiang MNGD_MAPPING(sbi), 130647e4937aSGao Xiang GFP_NOFS); 13071e4a2955SGao Xiang if (!page) 13081e4a2955SGao Xiang continue; 130947e4937aSGao Xiang 13101e4a2955SGao Xiang if (bio && cur != last_index + 1) { 131147e4937aSGao Xiang submit_bio_retry: 131294e4e153SGao Xiang submit_bio(bio); 131347e4937aSGao Xiang bio = NULL; 131447e4937aSGao Xiang } 131547e4937aSGao Xiang 131647e4937aSGao Xiang if (!bio) { 1317a8affc03SChristoph Hellwig bio = bio_alloc(GFP_NOIO, BIO_MAX_VECS); 1318a5c0b780SGao Xiang 13190c638f70SGao Xiang bio->bi_end_io = z_erofs_decompressqueue_endio; 1320a5c0b780SGao Xiang bio_set_dev(bio, sb->s_bdev); 13211e4a2955SGao Xiang bio->bi_iter.bi_sector = (sector_t)cur << 1322a5c0b780SGao Xiang LOG_SECTORS_PER_BLOCK; 1323a5c0b780SGao Xiang bio->bi_private = bi_private; 132494e4e153SGao Xiang bio->bi_opf = REQ_OP_READ; 13256ea5aad3SGao Xiang if (f->readahead) 13266ea5aad3SGao Xiang bio->bi_opf |= REQ_RAHEAD; 132747e4937aSGao Xiang ++nr_bios; 132847e4937aSGao Xiang } 132947e4937aSGao Xiang 13306c3e485eSGao Xiang if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) 133147e4937aSGao Xiang goto submit_bio_retry; 133247e4937aSGao Xiang 13331e4a2955SGao Xiang last_index = cur; 13341e4a2955SGao Xiang bypass = false; 13351e4a2955SGao Xiang } while (++cur < end); 133647e4937aSGao Xiang 13371e4a2955SGao Xiang if (!bypass) 133847e4937aSGao Xiang qtail[JQ_SUBMIT] = &pcl->next; 133947e4937aSGao Xiang else 134047e4937aSGao Xiang move_to_bypass_jobqueue(pcl, qtail, owned_head); 134147e4937aSGao Xiang } while (owned_head != Z_EROFS_PCLUSTER_TAIL); 134247e4937aSGao Xiang 134347e4937aSGao Xiang if (bio) 134494e4e153SGao Xiang submit_bio(bio); 134547e4937aSGao Xiang 1346587a67b7SGao Xiang /* 1347587a67b7SGao Xiang * although background is preferred, no one is pending for submission. 1348587a67b7SGao Xiang * don't issue workqueue for decompression but drop it directly instead. 1349587a67b7SGao Xiang */ 1350587a67b7SGao Xiang if (!*force_fg && !nr_bios) { 1351587a67b7SGao Xiang kvfree(q[JQ_SUBMIT]); 13521e4a2955SGao Xiang return; 1353587a67b7SGao Xiang } 1354a4b1fab1SGao Xiang z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios); 135547e4937aSGao Xiang } 135647e4937aSGao Xiang 13570c638f70SGao Xiang static void z_erofs_runqueue(struct super_block *sb, 13586ea5aad3SGao Xiang struct z_erofs_decompress_frontend *f, 13590c638f70SGao Xiang struct list_head *pagepool, bool force_fg) 136047e4937aSGao Xiang { 1361a4b1fab1SGao Xiang struct z_erofs_decompressqueue io[NR_JOBQUEUES]; 136247e4937aSGao Xiang 13636ea5aad3SGao Xiang if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL) 136447e4937aSGao Xiang return; 13656ea5aad3SGao Xiang z_erofs_submit_queue(sb, f, pagepool, io, &force_fg); 136647e4937aSGao Xiang 13670c638f70SGao Xiang /* handle bypass queue (no i/o pclusters) immediately */ 13680c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool); 136947e4937aSGao Xiang 137047e4937aSGao Xiang if (!force_fg) 137147e4937aSGao Xiang return; 137247e4937aSGao Xiang 137347e4937aSGao Xiang /* wait until all bios are completed */ 1374a93f8c36SGao Xiang io_wait_event(io[JQ_SUBMIT].u.wait, 137547e4937aSGao Xiang !atomic_read(&io[JQ_SUBMIT].pending_bios)); 137647e4937aSGao Xiang 13770c638f70SGao Xiang /* handle synchronous decompress queue in the caller context */ 13780c638f70SGao Xiang z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool); 137947e4937aSGao Xiang } 138047e4937aSGao Xiang 13810c638f70SGao Xiang static int z_erofs_readpage(struct file *file, struct page *page) 138247e4937aSGao Xiang { 138347e4937aSGao Xiang struct inode *const inode = page->mapping->host; 138447e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 138547e4937aSGao Xiang int err; 138647e4937aSGao Xiang LIST_HEAD(pagepool); 138747e4937aSGao Xiang 138847e4937aSGao Xiang trace_erofs_readpage(page, false); 138947e4937aSGao Xiang 139047e4937aSGao Xiang f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT; 139147e4937aSGao Xiang 13921825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 139347e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 139447e4937aSGao Xiang 139547e4937aSGao Xiang /* if some compressed cluster ready, need submit them anyway */ 13966ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, true); 139747e4937aSGao Xiang 139847e4937aSGao Xiang if (err) 13994f761fa2SGao Xiang erofs_err(inode->i_sb, "failed to read, err [%d]", err); 140047e4937aSGao Xiang 140147e4937aSGao Xiang if (f.map.mpage) 140247e4937aSGao Xiang put_page(f.map.mpage); 140347e4937aSGao Xiang 140447e4937aSGao Xiang /* clean up the remaining free pages */ 140547e4937aSGao Xiang put_pages_list(&pagepool); 140647e4937aSGao Xiang return err; 140747e4937aSGao Xiang } 140847e4937aSGao Xiang 14090615090cSMatthew Wilcox (Oracle) static void z_erofs_readahead(struct readahead_control *rac) 141047e4937aSGao Xiang { 14110615090cSMatthew Wilcox (Oracle) struct inode *const inode = rac->mapping->host; 141247e4937aSGao Xiang struct erofs_sb_info *const sbi = EROFS_I_SB(inode); 141347e4937aSGao Xiang 1414bf9a123bSGao Xiang unsigned int nr_pages = readahead_count(rac); 141530048cdaSHuang Jianan bool sync = (sbi->ctx.readahead_sync_decompress && 141630048cdaSHuang Jianan nr_pages <= sbi->ctx.max_sync_decompress_pages); 141747e4937aSGao Xiang struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode); 14180615090cSMatthew Wilcox (Oracle) struct page *page, *head = NULL; 141947e4937aSGao Xiang LIST_HEAD(pagepool); 142047e4937aSGao Xiang 1421bf9a123bSGao Xiang trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false); 142247e4937aSGao Xiang 14236ea5aad3SGao Xiang f.readahead = true; 14240615090cSMatthew Wilcox (Oracle) f.headoffset = readahead_pos(rac); 142547e4937aSGao Xiang 14260615090cSMatthew Wilcox (Oracle) while ((page = readahead_page(rac))) { 142747e4937aSGao Xiang prefetchw(&page->flags); 142847e4937aSGao Xiang 142947e4937aSGao Xiang /* 143047e4937aSGao Xiang * A pure asynchronous readahead is indicated if 143147e4937aSGao Xiang * a PG_readahead marked page is hitted at first. 143247e4937aSGao Xiang * Let's also do asynchronous decompression for this case. 143347e4937aSGao Xiang */ 143447e4937aSGao Xiang sync &= !(PageReadahead(page) && !head); 143547e4937aSGao Xiang 143647e4937aSGao Xiang set_page_private(page, (unsigned long)head); 143747e4937aSGao Xiang head = page; 143847e4937aSGao Xiang } 143947e4937aSGao Xiang 144047e4937aSGao Xiang while (head) { 144147e4937aSGao Xiang struct page *page = head; 144247e4937aSGao Xiang int err; 144347e4937aSGao Xiang 144447e4937aSGao Xiang /* traversal in reverse order */ 144547e4937aSGao Xiang head = (void *)page_private(page); 144647e4937aSGao Xiang 14471825c8d7SGao Xiang err = z_erofs_do_read_page(&f, page, &pagepool); 1448a5876e24SGao Xiang if (err) 14494f761fa2SGao Xiang erofs_err(inode->i_sb, 14504f761fa2SGao Xiang "readahead error at page %lu @ nid %llu", 14514f761fa2SGao Xiang page->index, EROFS_I(inode)->nid); 145247e4937aSGao Xiang put_page(page); 145347e4937aSGao Xiang } 145447e4937aSGao Xiang 145547e4937aSGao Xiang (void)z_erofs_collector_end(&f.clt); 145647e4937aSGao Xiang 14576ea5aad3SGao Xiang z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync); 145847e4937aSGao Xiang 145947e4937aSGao Xiang if (f.map.mpage) 146047e4937aSGao Xiang put_page(f.map.mpage); 146147e4937aSGao Xiang 146247e4937aSGao Xiang /* clean up the remaining free pages */ 146347e4937aSGao Xiang put_pages_list(&pagepool); 146447e4937aSGao Xiang } 146547e4937aSGao Xiang 14660c638f70SGao Xiang const struct address_space_operations z_erofs_aops = { 14670c638f70SGao Xiang .readpage = z_erofs_readpage, 14680615090cSMatthew Wilcox (Oracle) .readahead = z_erofs_readahead, 146947e4937aSGao Xiang }; 1470