Skip to content

Commit f5ad9f9

Browse files
Chunhai Guohsiangkao
authored andcommitted
erofs: free pclusters if no cached folio is attached
Once a pcluster is fully decompressed and there are no attached cached folios, its corresponding `struct z_erofs_pcluster` will be freed. This will significantly reduce the frequency of calls to erofs_shrink_scan() and the memory allocated for `struct z_erofs_pcluster`. The tables below show approximately a 96% reduction in the calls to erofs_shrink_scan() and in the memory allocated for `struct z_erofs_pcluster` after applying this patch. The results were obtained by performing a test to copy a 4.1GB partition on ARM64 Android devices running the 6.6 kernel with an 8-core CPU and 12GB of memory. 1. The reduction in calls to erofs_shrink_scan(): +-----------------+-----------+----------+---------+ | | w/o patch | w/ patch | diff | +-----------------+-----------+----------+---------+ | Average (times) | 11390 | 390 | -96.57% | +-----------------+-----------+----------+---------+ 2. The reduction in memory released by erofs_shrink_scan(): +-----------------+-----------+----------+---------+ | | w/o patch | w/ patch | diff | +-----------------+-----------+----------+---------+ | Average (Byte) | 133612656 | 4434552 | -96.68% | +-----------------+-----------+----------+---------+ Signed-off-by: Chunhai Guo <[email protected]> Reviewed-by: Gao Xiang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Gao Xiang <[email protected]>
1 parent bf1aa03 commit f5ad9f9

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

fs/erofs/zdata.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -885,22 +885,19 @@ static void z_erofs_rcu_callback(struct rcu_head *head)
885885
struct z_erofs_pcluster, rcu));
886886
}
887887

888-
static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
888+
static bool __erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
889889
struct z_erofs_pcluster *pcl)
890890
{
891-
int free = false;
892-
893-
spin_lock(&pcl->lockref.lock);
894891
if (pcl->lockref.count)
895-
goto out;
892+
return false;
896893

897894
/*
898895
* Note that all cached folios should be detached before deleted from
899896
* the XArray. Otherwise some folios could be still attached to the
900897
* orphan old pcluster when the new one is available in the tree.
901898
*/
902899
if (erofs_try_to_free_all_cached_folios(sbi, pcl))
903-
goto out;
900+
return false;
904901

905902
/*
906903
* It's impossible to fail after the pcluster is freezed, but in order
@@ -909,8 +906,16 @@ static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
909906
DBG_BUGON(__xa_erase(&sbi->managed_pslots, pcl->index) != pcl);
910907

911908
lockref_mark_dead(&pcl->lockref);
912-
free = true;
913-
out:
909+
return true;
910+
}
911+
912+
static bool erofs_try_to_release_pcluster(struct erofs_sb_info *sbi,
913+
struct z_erofs_pcluster *pcl)
914+
{
915+
bool free;
916+
917+
spin_lock(&pcl->lockref.lock);
918+
free = __erofs_try_to_release_pcluster(sbi, pcl);
914919
spin_unlock(&pcl->lockref.lock);
915920
if (free) {
916921
atomic_long_dec(&erofs_global_shrink_cnt);
@@ -942,16 +947,25 @@ unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi,
942947
return freed;
943948
}
944949

945-
static void z_erofs_put_pcluster(struct z_erofs_pcluster *pcl)
950+
static void z_erofs_put_pcluster(struct erofs_sb_info *sbi,
951+
struct z_erofs_pcluster *pcl, bool try_free)
946952
{
953+
bool free = false;
954+
947955
if (lockref_put_or_lock(&pcl->lockref))
948956
return;
949957

950958
DBG_BUGON(__lockref_is_dead(&pcl->lockref));
951-
if (pcl->lockref.count == 1)
952-
atomic_long_inc(&erofs_global_shrink_cnt);
953-
--pcl->lockref.count;
959+
if (!--pcl->lockref.count) {
960+
if (try_free && xa_trylock(&sbi->managed_pslots)) {
961+
free = __erofs_try_to_release_pcluster(sbi, pcl);
962+
xa_unlock(&sbi->managed_pslots);
963+
}
964+
atomic_long_add(!free, &erofs_global_shrink_cnt);
965+
}
954966
spin_unlock(&pcl->lockref.lock);
967+
if (free)
968+
call_rcu(&pcl->rcu, z_erofs_rcu_callback);
955969
}
956970

957971
static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
@@ -972,7 +986,7 @@ static void z_erofs_pcluster_end(struct z_erofs_decompress_frontend *fe)
972986
* any longer if the pcluster isn't hosted by ourselves.
973987
*/
974988
if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE)
975-
z_erofs_put_pcluster(pcl);
989+
z_erofs_put_pcluster(EROFS_I_SB(fe->inode), pcl, false);
976990

977991
fe->pcl = NULL;
978992
}
@@ -1274,6 +1288,7 @@ static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
12741288
int i, j, jtop, err2;
12751289
struct page *page;
12761290
bool overlapped;
1291+
bool try_free = true;
12771292

12781293
mutex_lock(&pcl->lock);
12791294
be->nr_pages = PAGE_ALIGN(pcl->length + pcl->pageofs_out) >> PAGE_SHIFT;
@@ -1331,9 +1346,12 @@ static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
13311346
/* managed folios are still left in compressed_bvecs[] */
13321347
for (i = 0; i < pclusterpages; ++i) {
13331348
page = be->compressed_pages[i];
1334-
if (!page ||
1335-
erofs_folio_is_managed(sbi, page_folio(page)))
1349+
if (!page)
13361350
continue;
1351+
if (erofs_folio_is_managed(sbi, page_folio(page))) {
1352+
try_free = false;
1353+
continue;
1354+
}
13371355
(void)z_erofs_put_shortlivedpage(be->pagepool, page);
13381356
WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL);
13391357
}
@@ -1379,6 +1397,11 @@ static int z_erofs_decompress_pcluster(struct z_erofs_decompress_backend *be,
13791397
/* pcluster lock MUST be taken before the following line */
13801398
WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
13811399
mutex_unlock(&pcl->lock);
1400+
1401+
if (z_erofs_is_inline_pcluster(pcl))
1402+
z_erofs_free_pcluster(pcl);
1403+
else
1404+
z_erofs_put_pcluster(sbi, pcl, try_free);
13821405
return err;
13831406
}
13841407

@@ -1401,10 +1424,6 @@ static int z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
14011424
owned = READ_ONCE(be.pcl->next);
14021425

14031426
err = z_erofs_decompress_pcluster(&be, err) ?: err;
1404-
if (z_erofs_is_inline_pcluster(be.pcl))
1405-
z_erofs_free_pcluster(be.pcl);
1406-
else
1407-
z_erofs_put_pcluster(be.pcl);
14081427
}
14091428
return err;
14101429
}

0 commit comments

Comments
 (0)