erofs: convert z_erofs_pcluster_readmore() to folios
PATCH 1/4 erofs: convert z_erofs_pcluster_readmore() to folios
Unlike pagecache_get_page(), __filemap_get_folio() returns error pointers instead of NULL, thus switching to IS_ERR_OR_NULL.
Apart from that, it's just a straightforward conversion.
--- fs/erofs/internal.h | 14 +++++--------- fs/erofs/zdata.c | 15 +++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-)
fs/erofs/internal.h +5 -9
@@ -312,17 +312,13 @@ static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)return (ifmt >>EROFS_I_DATALAYOUT_BIT ) &EROFS_I_DATALAYOUT_MASK ; } -/* -* Different from grab_cache_page_nowait(), reclaiming is never triggered -* when allocating new pages. -*/ -static inline -struct page *erofs_grab_cache_page_nowait (struct address_space *mapping, -pgoff_t index) +/* reclaiming is never triggered when allocating new folios. */ +static inline struct folio *erofs_grab_folio_nowait (struct address_space *as, +pgoff_t index) { -return pagecache_get_page (mapping, index, +return __filemap_get_folio (as, index,FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , -readahead_gfp_mask (mapping) & ~__GFP_RECLAIM); +readahead_gfp_mask (as) & ~__GFP_RECLAIM); }/* Has a disk mapping */
fs/erofs/zdata.c +7 -8
@@ -1767,7 +1767,6 @@ static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, end =round_up (end,PAGE_SIZE ); }else { end =round_up (map->m_la,PAGE_SIZE ); -if (!map->m_llen)return ; }
@@ -1775,15 +1774,15 @@ static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f, cur = map->m_la + map->m_llen -1 ;while ((cur >= end) && (cur <i_size_read (inode))) {pgoff_t index = cur >>PAGE_SHIFT ; -struct page *page; +struct folio *folio; - page =erofs_grab_cache_page_nowait (inode->i_mapping, index); -if (page) { -if (PageUptodate (page)) -unlock_page (page); + folio =erofs_grab_folio_nowait (inode->i_mapping, index); +if (!IS_ERR_OR_NULL (folio)) { +if (folio_test_uptodate (folio)) +folio_unlock (folio);else -z_erofs_scan_folio (f,page_folio (page), !!rac); -put_page (page); +z_erofs_scan_folio (f, folio, !!rac); +folio_put (folio); }if (cur <PAGE_SIZE )
Signature · 1 line
2.43.5
PATCH 2/4 erofs: convert z_erofs_read_fragment() to folios
Just a straight-forward conversion. No logic changes.
--- fs/erofs/zdata.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
fs/erofs/zdata.c +7 -8
@@ -925,7 +925,7 @@ static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe) fe->pcl =NULL ; } -static int z_erofs_read_fragment (struct super_block *sb,struct page *page, +static int z_erofs_read_fragment (struct super_block *sb,struct folio *folio,unsigned int cur,unsigned int end,erofs_off_t pos) {struct inode *packed_inode =EROFS_SB (sb)->packed_inode;
@@ -938,14 +938,13 @@ static int z_erofs_read_fragment(struct super_block *sb, struct page *page, buf.mapping = packed_inode->i_mapping;for (; cur < end; cur += cnt, pos += cnt) { - cnt = min_t(unsigned int, end - cur, - sb->s_blocksize -erofs_blkoff (sb, pos)); + cnt =min (end - cur, sb->s_blocksize -erofs_blkoff (sb, pos)); src =erofs_bread (&buf, pos,EROFS_KMAP );if (IS_ERR (src)) {erofs_put_metabuf (&buf);return PTR_ERR (src); } -memcpy_to_page (page, cur, src, cnt); +memcpy_to_folio (folio, cur, src, cnt); }erofs_put_metabuf (&buf);return 0 ;
@@ -959,7 +958,7 @@ static int z_erofs_scan_folio(struct z_erofs_decompress_frontend *fe,const loff_t offset =folio_pos (folio);const unsigned int bs =i_blocksize (inode), fs =folio_size (folio);bool tight = true, exclusive; -unsigned int cur, end, len, split; +unsigned int cur, end, split;int err =0 ;z_erofs_onlinefolio_init (folio);
@@ -989,9 +988,9 @@ static int z_erofs_scan_folio(struct z_erofs_decompress_frontend *fe,if (map->m_flags &EROFS_MAP_FRAGMENT ) {erofs_off_t fpos = offset + cur - map->m_la; - len = min_t(unsigned int, map->m_llen - fpos, end - cur); - err =z_erofs_read_fragment (inode->i_sb, &folio->page, cur, - cur + len,EROFS_I (inode)->z_fragmentoff + fpos); + err =z_erofs_read_fragment (inode->i_sb, folio, cur, + cur +min (map->m_llen - fpos, end - cur), +EROFS_I (inode)->z_fragmentoff + fpos);if (err) goto out; tight = false;
Signature · 1 line
2.43.5
PATCH 3/4 erofs: teach z_erofs_scan_folios() to handle multi-page folios
In reply to Gao XiangPreviously, a folio just contains one page. In order to enable large folios, z_erofs_scan_folios() needs to handle multi-page folios.
First, this patch eliminates all gotos. Instead, the new loop deal with multiple parts in each folio. It's simple to handle the parts which belong to unmapped extents or fragment extents; but for encoded extents, the page boundaries needs to be considered for tight and split to keep inplace I/Os work correctly: when a part crosses the page boundary, they needs to be reseted properly.
Besides, simplify tight derivation since Z_EROFS_PCLUSTER_HOOKED has been removed for quite a while.
--- fs/erofs/zdata.c | 167 +++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 85 deletions(-)
fs/erofs/zdata.c +82 -85
@@ -950,100 +950,97 @@ static int z_erofs_read_fragment(struct super_block *sb, struct folio *folio,return 0 ; } -static int z_erofs_scan_folio (struct z_erofs_decompress_frontend *fe, +static int z_erofs_scan_folio (struct z_erofs_decompress_frontend *f,struct folio *folio,bool ra) { -struct inode *const inode = fe->inode; -struct erofs_map_blocks *const map = &fe->map; +struct inode *const inode = f->inode; +struct erofs_map_blocks *const map = &f->map;const loff_t offset =folio_pos (folio); -const unsigned int bs =i_blocksize (inode), fs =folio_size (folio); -bool tight = true, exclusive; -unsigned int cur, end, split; -int err =0 ; +const unsigned int bs =i_blocksize (inode); +unsigned int end =folio_size (folio), split =0 , cur, pgs; +bool tight, excl; +int err; + tight = (bs ==PAGE_SIZE );z_erofs_onlinefolio_init (folio); - split =0 ; - end = fs; -repeat: -if (offset + end -1 < map->m_la || - offset + end -1 >= map->m_la + map->m_llen) { -z_erofs_pcluster_end (fe); - map->m_la = offset + end -1 ; - map->m_llen =0 ; - err =z_erofs_map_blocks_iter (inode, map,0 ); -if (err) - goto out; - } - - cur = offset > map->m_la ?0 : map->m_la - offset; -/* bump split parts first to avoid several separate cases */ - ++split; - -if (!(map->m_flags &EROFS_MAP_MAPPED )) { -folio_zero_segment (folio, cur, end); - tight = false; - goto next_part; - } - -if (map->m_flags &EROFS_MAP_FRAGMENT ) { -erofs_off_t fpos = offset + cur - map->m_la; +do { +if (offset + end -1 < map->m_la || + offset + end -1 >= map->m_la + map->m_llen) { +z_erofs_pcluster_end (f); + map->m_la = offset + end -1 ; + map->m_llen =0 ; + err =z_erofs_map_blocks_iter (inode, map,0 ); +if (err) +break ; + } - err =z_erofs_read_fragment (inode->i_sb, folio, cur, - cur +min (map->m_llen - fpos, end - cur), -EROFS_I (inode)->z_fragmentoff + fpos); -if (err) - goto out; - tight = false; - goto next_part; - } + cur = offset > map->m_la ?0 : map->m_la - offset; + pgs =round_down (cur,PAGE_SIZE ); +/* bump split parts first to avoid several separate cases */ + ++split; + +if (!(map->m_flags &EROFS_MAP_MAPPED )) { +folio_zero_segment (folio, cur, end); + tight = false; + }else if (map->m_flags &EROFS_MAP_FRAGMENT ) { +erofs_off_t fpos = offset + cur - map->m_la; + + err =z_erofs_read_fragment (inode->i_sb, folio, cur, + cur +min (map->m_llen - fpos, end - cur), +EROFS_I (inode)->z_fragmentoff + fpos); +if (err) +break ; + tight = false; + }else { +if (!f->pcl) { + err =z_erofs_pcluster_begin (f); +if (err) +break ; + f->pcl->besteffort |= !ra; + } -if (!fe->pcl) { - err =z_erofs_pcluster_begin (fe); -if (err) - goto out; - fe->pcl->besteffort |= !ra; - } + pgs =round_down (end -1 ,PAGE_SIZE ); +/* +* Ensure this partial page belongs to this submit chain +* rather than other concurrent submit chains or +* noio(bypass) chains since those chains are handled +* asynchronously thus it cannot be used for inplace I/O +* or bvpage (should be processed in the strict order.) +*/ + tight &= (f->mode >=Z_EROFS_PCLUSTER_FOLLOWED ); + excl = false; +if (cur <= pgs) { + excl = (split <=1 ) || tight; + cur = pgs; + } -/* -* Ensure the current partial folio belongs to this submit chain rather -* than other concurrent submit chains or the noio(bypass) chain since -* those chains are handled asynchronously thus the folio cannot be used -* for inplace I/O or bvpage (should be processed in a strict order.) -*/ - tight &= (fe->mode >Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE ); - exclusive = (!cur && ((split <=1 ) || (tight && bs == fs))); -if (cur) - tight &= (fe->mode >=Z_EROFS_PCLUSTER_FOLLOWED ); - - err =z_erofs_attach_page (fe, &((struct z_erofs_bvec ) { - .page = &folio->page, - .offset = offset - map->m_la, - .end = end, - }), exclusive); -if (err) - goto out; - -z_erofs_onlinefolio_split (folio); -if (fe->pcl->pageofs_out != (map->m_la & ~PAGE_MASK )) - fe->pcl->multibases = true; -if (fe->pcl->length < offset + end - map->m_la) { - fe->pcl->length = offset + end - map->m_la; - fe->pcl->pageofs_out = map->m_la & ~PAGE_MASK ; - } -if ((map->m_flags &EROFS_MAP_FULL_MAPPED ) && - !(map->m_flags &EROFS_MAP_PARTIAL_REF ) && - fe->pcl->length == map->m_llen) - fe->pcl->partial = false; -next_part: -/* shorten the remaining extent to update progress */ - map->m_llen = offset + cur - map->m_la; - map->m_flags &= ~EROFS_MAP_FULL_MAPPED ; - - end = cur; -if (end >0 ) - goto repeat; + err =z_erofs_attach_page (f, &((struct z_erofs_bvec ) { + .page =folio_page (folio, pgs >>PAGE_SHIFT ), + .offset = offset + pgs - map->m_la, + .end = end - pgs, }), excl); +if (err) +break ; -out: +z_erofs_onlinefolio_split (folio); +if (f->pcl->pageofs_out != (map->m_la & ~PAGE_MASK )) + f->pcl->multibases = true; +if (f->pcl->length < offset + end - map->m_la) { + f->pcl->length = offset + end - map->m_la; + f->pcl->pageofs_out = map->m_la & ~PAGE_MASK ; + } +if ((map->m_flags &EROFS_MAP_FULL_MAPPED ) && + !(map->m_flags &EROFS_MAP_PARTIAL_REF ) && + f->pcl->length == map->m_llen) + f->pcl->partial = false; + } +/* shorten the remaining extent to update progress */ + map->m_llen = offset + cur - map->m_la; + map->m_flags &= ~EROFS_MAP_FULL_MAPPED ; +if (cur <= pgs) { + split = cur < pgs; + tight = (bs ==PAGE_SIZE ); + } + }while ((end = cur) >0 );z_erofs_onlinefolio_end (folio, err);return err; }
Signature · 1 line
2.43.5
PATCH 4/4 erofs: tidy up `struct z_erofs_bvec`
In reply to Gao XiangAfter revisiting the design, I believe struct z_erofs_bvec should be page-based instead of folio-based due to the reasons below:
- The minimized memory mapping block is a page;
- Under the certain circumstances, only temporary pages needs to be used instead of folios since refcount, mapcount for such pages are unnecessary;
- Decompressors handle all types of pages including temporary pages, not only folios.
When handling struct z_erofs_bvec, all folio-related information is now accessed using the page_folio() helper.
The final goal of this round adaptation is to eliminate direct accesses to struct page in the EROFS codebase, except for some exceptions like z_erofs_is_shortlived_page() and z_erofs_page_is_invalidated(), which require a new helper to determine the memdesc type of an arbitrary page.
Actually large folios of compressed files seems to work now, yet I tend to conduct more tests before officially enabling this for all scenarios.
--- fs/erofs/zdata.c | 101 +++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 52 deletions(-)
fs/erofs/zdata.c +49 -52
@@ -19,10 +19,7 @@typedef void *z_erofs_next_pcluster_t ;struct z_erofs_bvec { -union { -struct page *page; -struct folio *folio; - }; +struct page *page;int offset;unsigned int end; };
@@ -617,32 +614,31 @@ static void z_erofs_bind_cache(struct z_erofs_decompress_frontend *fe) fe->mode =Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE ; } -/* called by erofs_shrinker to get rid of all cached compressed bvecs */ +/* (erofs_shrinker) disconnect cached encoded data with pclusters */ int erofs_try_to_free_all_cached_folios (struct erofs_sb_info *sbi,struct erofs_workgroup *grp) {struct z_erofs_pcluster *const pcl = container_of(grp, struct z_erofs_pcluster, obj);unsigned int pclusterpages =z_erofs_pclusterpages (pcl); +struct folio *folio;int i;DBG_BUGON (z_erofs_is_inline_pcluster (pcl)); -/* There is no actice user since the pcluster is now freezed */ +/* Each cached folio contains one page unless bs > ps is supported */ for (i =0 ; i < pclusterpages; ++i) { -struct folio *folio = pcl->compressed_bvecs[i].folio; +if (pcl->compressed_bvecs[i].page) { + folio =page_folio (pcl->compressed_bvecs[i].page); +/* Avoid reclaiming or migrating this folio */ +if (!folio_trylock (folio)) +return -EBUSY ; -if (!folio) -continue ; - -/* Avoid reclaiming or migrating this folio */ -if (!folio_trylock (folio)) -return -EBUSY ; - -if (!erofs_folio_is_managed (sbi, folio)) -continue ; - pcl->compressed_bvecs[i].folio =NULL ; -folio_detach_private (folio); -folio_unlock (folio); +if (!erofs_folio_is_managed (sbi, folio)) +continue ; + pcl->compressed_bvecs[i].page =NULL ; +folio_detach_private (folio); +folio_unlock (folio); + } }return 0 ; }
@@ -650,9 +646,9 @@ int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,static bool z_erofs_cache_release_folio (struct folio *folio,gfp_t gfp) {struct z_erofs_pcluster *pcl =folio_get_private (folio); -unsigned int pclusterpages =z_erofs_pclusterpages (pcl); +struct z_erofs_bvec *bvec = pcl->compressed_bvecs; +struct z_erofs_bvec *end = bvec +z_erofs_pclusterpages (pcl);bool ret; -int i;if (!folio_test_private (folio))return true;
@@ -661,9 +657,9 @@ static bool z_erofs_cache_release_folio(struct folio *folio, gfp_t gfp)spin_lock (&pcl->obj.lockref.lock);if (pcl->obj.lockref.count <=0 ) {DBG_BUGON (z_erofs_is_inline_pcluster (pcl)); -for (i =0 ; i < pclusterpages; ++i) { -if (pcl->compressed_bvecs[i].folio == folio) { - pcl->compressed_bvecs[i].folio =NULL ; +for (; bvec < end; ++bvec) { +if (bvec->page &&page_folio (bvec->page) == folio) { + bvec->page =NULL ;folio_detach_private (folio); ret = true;break ;
@@ -1062,7 +1058,7 @@ static bool z_erofs_is_sync_decompress(struct erofs_sb_info *sbi,static bool z_erofs_page_is_invalidated (struct page *page) { -return !page->mapping && !z_erofs_is_shortlived_page (page); +return !page_folio (page)->mapping && !z_erofs_is_shortlived_page (page); }struct z_erofs_decompress_backend {
@@ -1415,7 +1411,7 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec,bool tocache = false;struct z_erofs_bvec zbv;struct address_space *mapping; -struct page *page; +struct folio *folio;int bs =i_blocksize (f->inode);/* Except for inplace folios, the entire folio can be used for I/Os */
@@ -1425,23 +1421,25 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec,spin_lock (&pcl->obj.lockref.lock); zbv = pcl->compressed_bvecs[nr];spin_unlock (&pcl->obj.lockref.lock); -if (!zbv.folio) +if (!zbv.page) goto out_allocfolio; - bvec->bv_page = &zbv.folio->page; + bvec->bv_page = zbv.page;DBG_BUGON (z_erofs_is_shortlived_page (bvec->bv_page)); + + folio =page_folio (zbv.page);/* * Handle preallocated cached folios. We tried to allocate such folios * without triggering direct reclaim. If allocation failed, inplace * file-backed folios will be used instead. */ -if (zbv.folio->private == (void *)Z_EROFS_PREALLOCATED_PAGE ) { - zbv.folio->private =0 ; +if (folio->private == (void *)Z_EROFS_PREALLOCATED_PAGE ) { + folio->private =0 ; tocache = true; goto out_tocache; } - mapping =READ_ONCE (zbv.folio->mapping); + mapping =READ_ONCE (folio->mapping);/* * File-backed folios for inplace I/Os are all locked steady, * therefore it is impossible for `mapping` to be NULL.
@@ -1453,21 +1451,21 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec,return ; } -folio_lock (zbv.folio); -if (zbv.folio->mapping == mc) { +folio_lock (folio); +if (folio->mapping == mc) {/* * The cached folio is still in managed cache but without * a valid `->private` pcluster hint. Let's reconnect them. */ -if (!folio_test_private (zbv.folio)) { -folio_attach_private (zbv.folio, pcl); +if (!folio_test_private (folio)) { +folio_attach_private (folio, pcl);/* compressed_bvecs[] already takes a ref before */ -folio_put (zbv.folio); +folio_put (folio); }/* no need to submit if it is already up-to-date */ -if (folio_test_uptodate (zbv.folio)) { -folio_unlock (zbv.folio); +if (folio_test_uptodate (folio)) { +folio_unlock (folio); bvec->bv_page =NULL ; }return ;
@@ -1477,32 +1475,31 @@ static void z_erofs_fill_bio_vec(struct bio_vec *bvec,* It has been truncated, so it's unsafe to reuse this one. Let's * allocate a new page for compressed data. */ -DBG_BUGON (zbv.folio->mapping); +DBG_BUGON (folio->mapping); tocache = true; -folio_unlock (zbv.folio); -folio_put (zbv.folio); +folio_unlock (folio); +folio_put (folio); out_allocfolio: - page =erofs_allocpage (&f->pagepool, gfp | __GFP_NOFAIL); + zbv.page =erofs_allocpage (&f->pagepool, gfp | __GFP_NOFAIL);spin_lock (&pcl->obj.lockref.lock); -if (pcl->compressed_bvecs[nr].folio) { -erofs_pagepool_add (&f->pagepool, page); +if (pcl->compressed_bvecs[nr].page) { +erofs_pagepool_add (&f->pagepool, zbv.page);spin_unlock (&pcl->obj.lockref.lock);cond_resched (); goto repeat; } - pcl->compressed_bvecs[nr].folio = zbv.folio =page_folio (page); + bvec->bv_page = pcl->compressed_bvecs[nr].page = zbv.page; + folio =page_folio (zbv.page); +/* first mark it as a temporary shortlived folio (now 1 ref) */ + folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE ;spin_unlock (&pcl->obj.lockref.lock); - bvec->bv_page = page; out_tocache:if (!tocache || bs !=PAGE_SIZE || -filemap_add_folio (mc, zbv.folio, pcl->obj.index + nr, gfp)) { -/* turn into a temporary shortlived folio (1 ref) */ - zbv.folio->private = (void *)Z_EROFS_SHORTLIVED_PAGE ; +filemap_add_folio (mc, folio, pcl->obj.index + nr, gfp))return ; - } -folio_attach_private (zbv.folio, pcl); +folio_attach_private (folio, pcl);/* drop a refcount added by allocpage (then 2 refs in total here) */ -folio_put (zbv.folio); +folio_put (folio); } static struct z_erofs_decompressqueue *jobqueue_init(struct super_block *sb,
Signature · 1 line
2.43.5