From nobody Thu Nov 28 15:54:54 2024 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2050.outbound.protection.outlook.com [40.107.117.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2919819047D for ; Mon, 30 Sep 2024 13:49:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727704158; cv=fail; b=JvuVdHYeFQ1dl0GrOfTbjc3U6Su7zNCv1PVwx5vx4HLqp7Lu7Wcx50IEZx36QBgY1N6bco5xN/7wGMBAbFAIxlsOjllrlKoF1aeVAKAGNxaiSse5mVuTRUqoDTVs+i5Y1rr8dafl+DE3eEZoIyoHxInxz9pMa4vhnwZAvIeJHNY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727704158; c=relaxed/simple; bh=jM1TFfcHkRbqiZTqVO294ZI1I0kYgtiBeFmwoxD2gHw=; h=From:To:Cc:Subject:Date:Message-Id:Content-Type:MIME-Version; b=TsaSVZTWwvp1fmqF644kUbs0gr15pxVXb16HCxyxDTTaUoq+MbTWAzM0y/E9gsuaaQSPhNlg9YeBfhqJuLpJFVUF+oiz7tN0KF6ezNq6Te0lChrLXdls4fIcEin4e/D8rS0iulFImYg1Siu+OmZFv5nlGuH3jNTJxvKIYFGcXD8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=V/ePQ6qn; arc=fail smtp.client-ip=40.107.117.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="V/ePQ6qn" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WsDGALWplrYOfrTqf9ozPoiwib2KZaWY55Vncp+viI+y+RufuBJtg6/OryNOMkRNu7VoKif/QvHBG8N+z+Y9eQ9ll51isqIUySWoluxiMPudI/Qv+D+r4eu7/GTJK21nekb/8xP6Cujo3Io4XWZNM2U5DsfrvWL5uLO+v/KbD/OjvL9B7CrDTiSNeWWrPkA/abtbjaKNA7LVvDH6cngYMqsQYqIvL6HNu3612xyLatdbcCI/NnM8Vbg5fnPs/ekRuR5MmBox5PCjsY8wCAdX+cpdE14esAXBimxO+DAkImvmPr0LCFCE3pP2NsZCZwpzCesgxGTR/JEo4SUc1PWNng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=126COAvLR+mfAIPAAtUcG8djqtraYfYxcriyM7aH2Vo=; b=d133PLWedXheXiz1oSnEASvZ24jCnYmblVHT10NE9N4kGlJQ9l7mmgh8i66UUPnIaipHS9L6X07B/hG9apRM13hVFuHjFBaioIK3AxQvPXm6BDrQ8sy3epvyNuntctIe2uekGTdbGIGqYAWemj2c04ExOTj2c78GyQBRavfD8mHR/x4bvs0Mmvm1PQQTIVW29SwuEz6vF9lBxkcW3LV6SY6ZYnL8nAV4zloWueHHKqezliW2UL5yoR6toENknUCxt8QJDqOFIhCCQW69+6s9a/CUzIOqyHbEjmamL4XFuSE3eldm+hgpca01irjGf0YrS4lQ2t6ZITBDJcZE8oTV4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=126COAvLR+mfAIPAAtUcG8djqtraYfYxcriyM7aH2Vo=; b=V/ePQ6qnJ8K3dInoSPM/P7myfu2i/tuzRuF1Wu+9H/0RtP+493uVz97a8v7n2yQwJ2vhxQLXgPtUk4dLRhGGBbaaxjKSU+QzBCy/nXG3QVBgRwzx49y/CTRo/IdIznI0rvEQXgCbDubS7UBaYFVGb35wLgDRqHg8yc+UI8GkqDNAvUx5PCxQFqudDLx7hKxJ5yAY96aRR+mY2yed+4mnNoy6KZylt5Q5HBS1YW/y3qjULdAv9G2FsPBWfRjX1IvVPQ+wnbzjJuUo7ySPqnMqCMEodjEzqTlJsfWC6LkEcXGk1yCq6fkEkSuiew1RSzHBpFd8vhyc2iMGC37L0XIKCA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from TYZPR06MB7096.apcprd06.prod.outlook.com (2603:1096:405:b5::13) by SEZPR06MB5413.apcprd06.prod.outlook.com (2603:1096:101:64::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8005.24; Mon, 30 Sep 2024 13:49:10 +0000 Received: from TYZPR06MB7096.apcprd06.prod.outlook.com ([fe80::6c3a:9f76:c4a5:c2b]) by TYZPR06MB7096.apcprd06.prod.outlook.com ([fe80::6c3a:9f76:c4a5:c2b%2]) with mapi id 15.20.8005.024; Mon, 30 Sep 2024 13:49:10 +0000 From: Chunhai Guo To: xiang@kernel.org Cc: chao@kernel.org, huyue2@coolpad.com, jefflexu@linux.alibaba.com, dhavale@google.com, linux-erofs@lists.ozlabs.org, linux-kernel@vger.kernel.org, Chunhai Guo Subject: [PATCH] erofs: free pcluster right after decompression if possible Date: Mon, 30 Sep 2024 08:04:24 -0600 Message-Id: <20240930140424.4049195-1-guochunhai@vivo.com> X-Mailer: git-send-email 2.25.1 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SG2PR01CA0189.apcprd01.prod.exchangelabs.com (2603:1096:4:189::15) To TYZPR06MB7096.apcprd06.prod.outlook.com (2603:1096:405:b5::13) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYZPR06MB7096:EE_|SEZPR06MB5413:EE_ X-MS-Office365-Filtering-Correlation-Id: 6cbf5a39-ab96-4ad7-9da4-08dce156a904 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|1800799024|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?UfN83kQeHi/eRjEsw6nId6JZ4ouhJbfLKWCprrTDMMZxt0dw8uQ9X2M6Ah/P?= =?us-ascii?Q?HwxYHh5H2EwxdP6Ujnggfpg9utwHOBN3wSbrOVhrCr60p79XLazOzHDebY5e?= =?us-ascii?Q?+xK06mgv2x8yJu4VNEnmYkRIPEvx4TTp+BdyDO1qWVAONmRkKLPQbvn5YbFI?= =?us-ascii?Q?pFHlK7s91GKKG3S1/8uQFkUtMXkh5lr235jUkc+t9ZMdKKRtl7uNaEqVlmNk?= =?us-ascii?Q?d9RWHza3N8Z2dyZhAJxtQYD1GQU0zGiSZTG4YO7geZ4T3U241YDazSq6Yheb?= =?us-ascii?Q?WCh80ixH0PCXEvwLd3Q3EjIGm//Ur6O+zpw/Qz8w1lUZMHQYy/XAT4Uhx3qn?= =?us-ascii?Q?T5bW3QiEtHfc/cJDaxUFRCYOEQC3wYajPUfEr8+JNORrtTvwEHub/vJnObmY?= =?us-ascii?Q?/xAG39Bd0gNt4fO0Yh+LwBMOqBhAueRY/VwKTnkay4+yo6OdIMV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XUTq3BKE+UvSea8DOEgiQ4547mzZPpy?= =?us-ascii?Q?j6bmiXsVoSEhzy9hsznnOJD4r6T+0WbmvNScGFuTgOBnB3JKpw=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TYZPR06MB7096.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(52116014)(376014)(1800799024)(366016)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ycGYBBWnZFoOhBv6otp2qD1X6GenWlKNknrAuzMGd6acDMLrmUmyq3kTbhxr?= =?us-ascii?Q?cdMjA3gNlktRBHBrDqlpYgldR6Z3/HF3eqWeaEfC3fbRtgo0A8t/p95txjjf?= =?us-ascii?Q?S2EF0kWA0ARxiujl2cYTJF0oiFUKs6VT557Nzm8uhAJP3M0OcRVA8kmToEbR?= =?us-ascii?Q?Z03/ZpQbFBe4bHL3l11oAmNjxdHVIR6QBHwtCOPDvMv+9qn/2cEW7q0uyRvf?= =?us-ascii?Q?VkdIKcrCmucljMyjXRqKS6u37pCqb9D9RAVVbGq09tGyJ0COmOs8OTGjua/A?= =?us-ascii?Q?+TcIKfA4Npub/2wCfgKcKAMr/6YdMyoFqzFevded8FFvij+fdw666N0BpqN/?= =?us-ascii?Q?E8I1qJ7JNpgJkObihxjbaY7IaPGgk2dkWCzo0Gn/0A+TAfB1NiHlwwmgX/wT?= =?us-ascii?Q?D0ihGyk4ddXJyq4sVwY/0Hl7J0YKO2JSfTfzztFwE+m+3MZGqYsCJuYjBmv0?= =?us-ascii?Q?Uk2wZ5E2CjIPTdqjK2XlgEusmzVKKyC0SgrpI7yqXKQlePgrrNlbx0RRaRXN?= =?us-ascii?Q?td6l+JC6aKQj4gWgQOVGCPTQbjOZuX0xPY/embTNgZKNdCdn04A0nAr2brHX?= =?us-ascii?Q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xMoC2zXTRAc196vwO7CoMLoVs0LrIbnJC+Lkr?= =?us-ascii?Q?PLslqjEPx11eGs0BxqWiXS+OwOA7QFpZbi9BYjDdRVljXhuHc3CpTGqFCRRu?= =?us-ascii?Q?4Xfx6vti9aBXcr1CcxmzBHBDdhrBe2pMYP8EUjda?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6cbf5a39-ab96-4ad7-9da4-08dce156a904 X-MS-Exchange-CrossTenant-AuthSource: TYZPR06MB7096.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Sep 2024 13:49:10.0137 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kgHDrTOBON5D1E363hINcojUPM49CrOKWsjTXN8EMynrqpLvDsMoQoG65i8gw5rlTybZyuet4e+XwHyICrDkng== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR06MB5413 Content-Type: text/plain; charset="utf-8" Once a pcluster is fully decompressed and there are no attached cached pages, 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 95% 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 2.1 GB partition on ARM64 Android devices running the 5.15 kernel with an 8-core CPU and 8GB of memory. 1. The reduction in calls to erofs_shrink_scan(): +-----------------+-----------+----------+---------+ | | w/o patch | w/ patch | diff | +-----------------+-----------+----------+---------+ | Average (times) | 3152 | 160 | -94.92% | +-----------------+-----------+----------+---------+ 2. The reduction in memory released by erofs_shrink_scan(): +-----------------+-----------+----------+---------+ | | w/o patch | w/ patch | diff | +-----------------+-----------+----------+---------+ | Average (Byte) | 44503200 | 2293760 | -94.84% | +-----------------+-----------+----------+---------+ Signed-off-by: Chunhai Guo --- fs/erofs/internal.h | 3 ++- fs/erofs/zdata.c | 14 ++++++++--- fs/erofs/zutil.c | 58 +++++++++++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/fs/erofs/internal.h b/fs/erofs/internal.h index 4efd578d7c62..17b04bfd743f 100644 --- a/fs/erofs/internal.h +++ b/fs/erofs/internal.h @@ -456,7 +456,8 @@ static inline void erofs_pagepool_add(struct page **pag= epool, struct page *page) void erofs_release_pages(struct page **pagepool); =20 #ifdef CONFIG_EROFS_FS_ZIP -void erofs_workgroup_put(struct erofs_workgroup *grp); +void erofs_workgroup_put(struct erofs_sb_info *sbi, struct erofs_workgroup= *grp, + bool can_released); struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb, pgoff_t index); struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb, diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c index 8936790618c6..656fd65aec33 100644 --- a/fs/erofs/zdata.c +++ b/fs/erofs/zdata.c @@ -888,7 +888,7 @@ static void z_erofs_pcluster_end(struct z_erofs_decompr= ess_frontend *fe) * any longer if the pcluster isn't hosted by ourselves. */ if (fe->mode < Z_EROFS_PCLUSTER_FOLLOWED_NOINPLACE) - erofs_workgroup_put(&pcl->obj); + erofs_workgroup_put(EROFS_I_SB(fe->inode), &pcl->obj, false); =20 fe->pcl =3D NULL; } @@ -1046,6 +1046,9 @@ struct z_erofs_decompress_backend { struct list_head decompressed_secondary_bvecs; struct page **pagepool; unsigned int onstack_used, nr_pages; + + /* whether the pcluster can be released after its decompression */ + bool try_free; }; =20 struct z_erofs_bvec_item { @@ -1244,12 +1247,15 @@ static int z_erofs_decompress_pcluster(struct z_ero= fs_decompress_backend *be, WRITE_ONCE(pcl->compressed_bvecs[0].page, NULL); put_page(page); } else { + be->try_free =3D true; /* managed folios are still left in compressed_bvecs[] */ for (i =3D 0; i < pclusterpages; ++i) { page =3D be->compressed_pages[i]; if (!page || - erofs_folio_is_managed(sbi, page_folio(page))) + erofs_folio_is_managed(sbi, page_folio(page))) { + be->try_free =3D false; continue; + } (void)z_erofs_put_shortlivedpage(be->pagepool, page); WRITE_ONCE(pcl->compressed_bvecs[i].page, NULL); } @@ -1285,6 +1291,7 @@ static int z_erofs_decompress_pcluster(struct z_erofs= _decompress_backend *be, if (be->decompressed_pages !=3D be->onstack_pages) kvfree(be->decompressed_pages); =20 + be->try_free =3D be->try_free && !pcl->partial; pcl->length =3D 0; pcl->partial =3D true; pcl->multibases =3D false; @@ -1320,7 +1327,8 @@ static int z_erofs_decompress_queue(const struct z_er= ofs_decompressqueue *io, if (z_erofs_is_inline_pcluster(be.pcl)) z_erofs_free_pcluster(be.pcl); else - erofs_workgroup_put(&be.pcl->obj); + erofs_workgroup_put(EROFS_SB(io->sb), &be.pcl->obj, + be.try_free); } return err; } diff --git a/fs/erofs/zutil.c b/fs/erofs/zutil.c index 37afe2024840..cf59ba6a2322 100644 --- a/fs/erofs/zutil.c +++ b/fs/erofs/zutil.c @@ -285,26 +285,11 @@ static void __erofs_workgroup_free(struct erofs_work= group *grp) erofs_workgroup_free_rcu(grp); } =20 -void erofs_workgroup_put(struct erofs_workgroup *grp) -{ - if (lockref_put_or_lock(&grp->lockref)) - return; - - DBG_BUGON(__lockref_is_dead(&grp->lockref)); - if (grp->lockref.count =3D=3D 1) - atomic_long_inc(&erofs_global_shrink_cnt); - --grp->lockref.count; - spin_unlock(&grp->lockref.lock); -} - -static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi, +static bool erofs_prepare_to_release_workgroup(struct erofs_sb_info *sbi, struct erofs_workgroup *grp) { - int free =3D false; - - spin_lock(&grp->lockref.lock); if (grp->lockref.count) - goto out; + return false; =20 /* * Note that all cached pages should be detached before deleted from @@ -312,7 +297,7 @@ static bool erofs_try_to_release_workgroup(struct erofs= _sb_info *sbi, * the orphan old workgroup when the new one is available in the tree. */ if (erofs_try_to_free_all_cached_folios(sbi, grp)) - goto out; + return false; =20 /* * It's impossible to fail after the workgroup is freezed, @@ -322,14 +307,47 @@ static bool erofs_try_to_release_workgroup(struct ero= fs_sb_info *sbi, DBG_BUGON(__xa_erase(&sbi->managed_pslots, grp->index) !=3D grp); =20 lockref_mark_dead(&grp->lockref); - free =3D true; -out: + return true; +} + +static bool erofs_try_to_release_workgroup(struct erofs_sb_info *sbi, + struct erofs_workgroup *grp) +{ + bool free =3D false; + + /* Using trylock to avoid deadlock with erofs_workgroup_put() */ + if (!spin_trylock(&grp->lockref.lock)) + return free; + free =3D erofs_prepare_to_release_workgroup(sbi, grp); spin_unlock(&grp->lockref.lock); if (free) __erofs_workgroup_free(grp); return free; } =20 +void erofs_workgroup_put(struct erofs_sb_info *sbi, struct erofs_workgroup= *grp, + bool try_free) +{ + bool free =3D false; + + if (lockref_put_or_lock(&grp->lockref)) + return; + + DBG_BUGON(__lockref_is_dead(&grp->lockref)); + if (--grp->lockref.count =3D=3D 0) { + atomic_long_inc(&erofs_global_shrink_cnt); + + if (try_free) { + xa_lock(&sbi->managed_pslots); + free =3D erofs_prepare_to_release_workgroup(sbi, grp); + xa_unlock(&sbi->managed_pslots); + } + } + spin_unlock(&grp->lockref.lock); + if (free) + __erofs_workgroup_free(grp); +} + static unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi, unsigned long nr_shrink) { --=20 2.25.1