From patchwork Thu Jan 16 21:10:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13942308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51B46C02187 for ; Thu, 16 Jan 2025 21:11:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBD15280004; Thu, 16 Jan 2025 16:11:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6CE6280001; Thu, 16 Jan 2025 16:11:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E624280004; Thu, 16 Jan 2025 16:11:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 80786280001 for ; Thu, 16 Jan 2025 16:11:26 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0E3BE16129E for ; Thu, 16 Jan 2025 21:11:26 +0000 (UTC) X-FDA: 83014560972.17.5B713D8 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2064.outbound.protection.outlook.com [40.107.236.64]) by imf11.hostedemail.com (Postfix) with ESMTP id 3AFF440014 for ; Thu, 16 Jan 2025 21:11:23 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=duZMO+BB; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf11.hostedemail.com: domain of ziy@nvidia.com designates 40.107.236.64 as permitted sender) smtp.mailfrom=ziy@nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1737061883; a=rsa-sha256; cv=pass; b=mLU9e2mzJkgKmoPpFxVrquXQy51e16Z4jouBUlwgtjzDOAgv6anwH0NXXDiCev4FW1zTqG 0Cfs0DCu5sE7vuK6TQdyUQL9cqqN7UE6JmL131kURuXY0ZSLhYQPs31XZZ805aNjPwNjhg P7XW4sk5mdSYGo5Hctt3HNqEqwhA0A0= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=duZMO+BB; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf11.hostedemail.com: domain of ziy@nvidia.com designates 40.107.236.64 as permitted sender) smtp.mailfrom=ziy@nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737061883; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=T6dN5S3nRphg4c7HTWzXe7uTKVAHTiFZ5ttyec/A78c=; b=6rt6aPOQTdiGVkFK7PdER/SR7t03RfBuYpQMs/15ZvjxvtoYanj0/v0td9FBXkYFwxKUi5 QE6jGnouOoOOXL4QttdH/WVRZtKT32mhIpNJjhFqh/snYdwq4AqMEwQhLqVPSqk3vMpEoc CXB7mrZ7xiOmB2qcVg5nc+bkjha9BRM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lGhSGYmThYrb20lplfb3yep17uY/QbVcEUMA6QX3ES+G/yfrMtG6u8TRrbiZsc3pzF63tHYJZVRCCJ7/uFKBZg6UWJqtLYmk78dMj3rlCnKfkeH7Ox9nYhXd2Xk76Aj1iZjxt/swhNnVJbjm635Jn78rLJIylhGEx4kItzw7lxnzyfWvRIAS/zEoSGhD6ZlaGHACJuMj6ReNRvdzVZr8lI6/rKz7LKv5X2/021NLHAXEdZLv3LVa3iZ38Vtivsxe63pMYgieJRyN3o4c5lXxJzhBu9e6NbYVDeQUDF08Y+PCu+V0p1yszPrc32kyn1SS/NPnSn6CJq5cYZojvuOOcw== 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=T6dN5S3nRphg4c7HTWzXe7uTKVAHTiFZ5ttyec/A78c=; b=y+o0OGtYpeUdwa7CkVECq8DTxlQ92J9Qi4K4clUjaDH7NjXXRXGYgwsZJdIYlPVt6yQjSDhauY872JMPLzBa+Sr2ZEEUXL8udyh+loOATo0IMRTX7LgzzHeELfvKFEK82wu39mLYq5zlCFfyeXV0K2Lp+fwxZ8+OQ+uG4GJk6sAQfzbFqRquSPN+UlZFd65yKSqKIzZSfJWzyfGf5ip6umUqIKjOZLN9ShXyJgmyXQeM3ZEF/VsIu9w0HPfr6PnrSwBVn/vO8vv4yX36Eq30xiuwPe0gIRKZb80pRAThiszvrzCQ9HJpHl5jmLxM18V+KvEZWTWOXvl8/pN5GV/XkA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T6dN5S3nRphg4c7HTWzXe7uTKVAHTiFZ5ttyec/A78c=; b=duZMO+BBlKLq3b8HHljzmXtjV1oaI0XJ9jCCoX40YHcnhWcjmwNd/6ekLMGeeDD+65rkWu5Yy6xsL+8+YXNIARD/4BWKh1pOeFPGQE3xe+lMemgRs2yDgN+XRLkG7/Z23b1bHDHkwQHx0dON1T84VvWT4TAzwG14JXFI55JFJx74fPa9T10uLUgj/gRfSEiWMbOm8iycSlIvgrFcazmGsClnZnu/tBa00DjFEkqME+jeQg41uHjMrEX7bTwxMdzxpRpmIwTR1hYsd+903DDH9BH5ZxJS40KLjt2aIxCgiWlsm+K+GHkpFW2LrOqLLhVNpviKataInM7gAFr5wdhIrA== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by LV8PR12MB9232.namprd12.prod.outlook.com (2603:10b6:408:182::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8356.15; Thu, 16 Jan 2025 21:11:04 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%3]) with mapi id 15.20.8356.014; Thu, 16 Jan 2025 21:11:04 +0000 From: Zi Yan To: linux-mm@kvack.org, Andrew Morton , "Kirill A . Shutemov" , "Matthew Wilcox (Oracle)" Cc: Ryan Roberts , Hugh Dickins , David Hildenbrand , Yang Shi , Miaohe Lin , Kefeng Wang , Yu Zhao , John Hubbard , Baolin Wang , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v5 09/10] mm/truncate: use folio_split() for truncate operation. Date: Thu, 16 Jan 2025 16:10:41 -0500 Message-ID: <20250116211042.741543-10-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250116211042.741543-1-ziy@nvidia.com> References: <20250116211042.741543-1-ziy@nvidia.com> X-ClientProxiedBy: MN0P220CA0025.NAMP220.PROD.OUTLOOK.COM (2603:10b6:208:52e::33) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|LV8PR12MB9232:EE_ X-MS-Office365-Filtering-Correlation-Id: ff17b66b-d95b-445d-6d81-08dd36724954 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|1800799024; X-Microsoft-Antispam-Message-Info: 12IiEFxlki9MviFmFafuO5P5+vXaB8G+eMDUqmjsma/ZiLCLNPJ7nbqeNtOspx/EPi33s6kP9vQQTajDInrIWgWxB1v0CaY2H5AjVWMUVUokAt8kIIY2FJXosRUdqyaGvorZhzLUkl5Jzso/UV35TccbvinP+8z9EAaObCCBy/g73bmUFijNgsgt3SCxojBCnp3QiG656kpIOs2oPTKVowdzVNo3A609HreG4QFnIrdEu5Rtnw/KwtUhyFtktQ0uyh/N4Naow5Wmp0HiG0vbJVP/ArE8aTxXhTOToE+LaQvycvE+VULRVgdw77KedrQR5SILS63SAzgb4wBP246/Hcli8s3ww9UyZZu+u/Bka3GnA/8XsI1aAhfkmXyhqygVig1g0ykmvErIG3QtZ+reMX45bjsROMMGTnCznvVTZJTkv2W4Os07ceGYxdbu3PyeGUu5iLFFS2OUqXOuYhaIGa3QDVBRGTSh8h24ukXkTSJTfTGYNCVtrH+SZgSYQNM5bgCW8PX2wqhxcDhbD2TsJtyjkT6aOyHGsuByip03WiRoGbTw2Y7RzE9UjQRnux6bv59cH4l63f3l9UAr10fhurchMAfPzl8KPAhxtr8Xe986cqr53mtVc2tB0f/Sy+dSV8GSSCvW2N0pBvKH1CLWpHL1Ahu92dRig45HA7GLyHOutI5wiC3JYfr26lXovLOL2X289VU3JlkDTuWRRGICqhwMjqzjxNfd5XD40H3lTvBvLTaPnNLr0BNJgORisIUzLSt5kcMLbug1HEjch7kQBYdUTABjo5FS4JopvC5r6LEre4UbaZxnKU7A3DAd9VWMa0XIX1OWjQbsLbiWdB+FZXDIL88AqnVvP2lvJJMeCvkJgQLrPyVnY3/snQz9DEf7lCf986RkUKtg9z4dsnKhmlYbRFufBJ14x8m9oVjc5Lak5zBBOld6/jAslwJppZd4wJJpVYs1fG300Z6BELasvz2TzSp79EXhnsHrrwWpcftP46DTc1SD5s712guUGWNoggBQjPg3vAasDMBGCW8u+gplbyDl3z6OXIegch79Ph0nn64hoeMTFmJPXGCVtqMTePaoL+GQGVTpWo5onBABF49kGqy6rpWnOs2hFBN7X0EghTfWwP2UsENzDc5fkekqwJnih1QdOeiPS7CsXRi9evBtQ2jCq0PrPlmSpn0DKmuxFSiSQNzq4gLaphAk5YWp6yCs35Ugt7GYsFCSeSWzHU7OZF8tlq7eMysSSpR0i4UF5yX7JR1HwE9ubz7doYkHulBxUXrSyEBEiY1BaIsULkVtMjn7tjGT4SAy0k0Fq/hJECd++0v7kCK3Bh5i+5kVJDgkU8ejozNNno+bS3V64XKnofP9EJgdgY8njVc/iwcW4GSoowKuusFBNzKU2uiA X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR12MB9473.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Aj+37zTQXbfX5cLSDo2GVm3uQSh8cQiKvCV2JMjP8Zi+5oL+XcFijwzckiP7M4wlgvsLwv9Z30Qsr+YmP87hbaZ+YauT93J1z5C7JPVmecnldyU/nSbN0Djmn8qdOqRsRpeIuL1p/l7m/9lCQsnTR8UnRFIiZe1KFHGywMyMbR4G1XLO5kquxGklCpwZpueJu6lmOBwPeKGslstdOt/9KbwS4Opck4Wq7B9xMUFy64QMMYTbpLLR8Pbu9wRf7WFqLDLgUmp4L8rLCt7r70aJ/n0RynBT7FOvrXJilOZh/ZVyVKOVZclv4eK3aCiZSuLe2V48KGHbU0sdXWyIxdrp/l1PDD0P8KGDZR3For9A//drC6q2Gjsyq75m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W3JbM7ysRSE/uMmyhTA4iuTINY5n90mRXYODefddUmQLFcow8ImRDwUWqmvhgD0YLAJXxWoZ4ukcnn7MDLr1EHk91S4BjRB/g2b73RMXLvN76yCr8V9AM2mMUgqN1gYzoj1gHUCt6BdW+c+329IdTqNS2LM X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ff17b66b-d95b-445d-6d81-08dd36724954 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jan 2025 21:11:04.2194 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: cFM1RMLo1XIDCs1olJjtZhE0c/D92WD+Cr+phcvGTRfLO+8EoCX18NUkkyyr1dHS X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9232 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 3AFF440014 X-Stat-Signature: yr7xpb9itnicrmswpyahxhouc68ze576 X-HE-Tag: 1737061883-875937 X-HE-Meta: U2FsdGVkX1908pxQZNCSZFNNtmiAxvtv8pZNgJkyreT6kCffGnuxS/CXR7ysWZ9sZpB5GkHWdTB4brZzB7DlhArjoXRahbGEqq3iTM5d/HVeVsc8OuadWfN4jrKmXZVsZSlMR1pu2ULqeIGFuMAR5Mpv8vSFGO+GSGCaH9zXfdjGY2kKt+YO+drTFbIyBMBMNFXVCgpZiLtVoBPmfvt7LNk1fhwJMQmPoIFAGhEesgYB+GJt+0BA+wiRPsDmwf+usl8oyYyPaz/D88OX3CKH0GPqAUrpDc86sg05vE8NFuyD7qmbhjdrrG77MDIKgw+0OR0SQVYNhtJD1vBHHI5kx+SGAiTqNX0VVDPQUl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=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of splitting the large folio uniformly during truncation, try to use buddy allocator like split at the start of truncation range to minimize the number of resulting folios if it is supported. For example, to truncate a order-4 folio [0, 1, 2, 3, 4, 5, ..., 15] between [3, 10] (inclusive), folio_split() splits the folio to [0,1], [2], [3], [4..7], [8..15] and [3], [4..7] can be dropped and [8..15] is kept with zeros in [8..10], then another folio_split() is done at 10, so [8..10] can be dropped. One possible optimization is to make folio_split() to split a folio based on a given range, like [3..10] above. But that complicates folio_split(), so it will be investigated when necessary. Signed-off-by: Zi Yan --- include/linux/huge_mm.h | 24 ++++++++++++++++++++++++ mm/huge_memory.c | 4 ++-- mm/truncate.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 93e509b6c00e..2bdb4959a00f 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -341,6 +341,24 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, unsigned int new_order); int min_order_for_split(struct folio *folio); int split_folio_to_list(struct folio *folio, struct list_head *list); +bool uniform_split_supported(struct folio *folio, unsigned int new_order, + bool warns); +bool non_uniform_split_supported(struct folio *folio, unsigned int new_order, + bool warns); +int folio_split(struct folio *folio, unsigned int new_order, struct page *page, + struct list_head *list); +static inline int split_folio_at(struct folio *folio, struct page *page, + struct list_head *list) +{ + int ret = min_order_for_split(folio); + + if (ret < 0) + return ret; + + if (!non_uniform_split_supported(folio, 0, false)) + return split_folio(folio); + return folio_split(folio, ret, page, list); +} static inline int split_huge_page(struct page *page) { struct folio *folio = page_folio(page); @@ -533,6 +551,12 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lis return 0; } +static inline int split_folio_at(struct folio *folio, struct page *page, + struct list_head *list) +{ + return 0; +} + static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {} #define split_huge_pmd(__vma, __pmd, __address) \ do { } while (0) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ddba3e36b46b..67642c716de1 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3531,7 +3531,7 @@ static int __split_unmapped_folio(struct folio *folio, int new_order, return ret; } -static bool non_uniform_split_supported(struct folio *folio, unsigned int new_order, +bool non_uniform_split_supported(struct folio *folio, unsigned int new_order, bool warns) { /* order-1 is not supported for anonymous THP. */ @@ -3564,7 +3564,7 @@ static bool non_uniform_split_supported(struct folio *folio, unsigned int new_or } /* See comments in non_uniform_split_supported() */ -static bool uniform_split_supported(struct folio *folio, unsigned int new_order, +bool uniform_split_supported(struct folio *folio, unsigned int new_order, bool warns) { if (folio_test_anon(folio) && new_order == 1) { diff --git a/mm/truncate.c b/mm/truncate.c index e2e115adfbc5..a9679696fb93 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -178,6 +178,7 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) { loff_t pos = folio_pos(folio); unsigned int offset, length; + struct page *split_at, *split_at2; if (pos < start) offset = start - pos; @@ -207,8 +208,36 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) folio_invalidate(folio, offset, length); if (!folio_test_large(folio)) return true; - if (split_folio(folio) == 0) + + split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE); + split_at2 = folio_page(folio, + PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE); + + if (!split_folio_at(folio, split_at, NULL)) { + /* + * try to split at offset + length to make sure folios within + * the range can be dropped, especially to avoid memory waste + * for shmem truncate + */ + struct folio *folio2 = page_folio(split_at2); + + if (!folio_try_get(folio2)) + goto no_split; + + if (!folio_test_large(folio2)) + goto out; + + if (!folio_trylock(folio2)) + goto out; + + /* split result does not matter here */ + split_folio_at(folio2, split_at2, NULL); + folio_unlock(folio2); +out: + folio_put(folio2); +no_split: return true; + } if (folio_test_dirty(folio)) return false; truncate_inode_folio(folio->mapping, folio);