From patchwork Mon Jan 6 16:55:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13927682 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 F041CE77199 for ; Mon, 6 Jan 2025 16:56:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 824E26B0099; Mon, 6 Jan 2025 11:56:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D5CB6B009A; Mon, 6 Jan 2025 11:56:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64ED96B009B; Mon, 6 Jan 2025 11:56:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 47F3D6B0099 for ; Mon, 6 Jan 2025 11:56:04 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id EE5A740DCA for ; Mon, 6 Jan 2025 16:56:03 +0000 (UTC) X-FDA: 82977629406.03.49EDB6B Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2070.outbound.protection.outlook.com [40.107.96.70]) by imf01.hostedemail.com (Postfix) with ESMTP id 0A1D540007 for ; Mon, 6 Jan 2025 16:56:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=JPaYvEby; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf01.hostedemail.com: domain of ziy@nvidia.com designates 40.107.96.70 as permitted sender) smtp.mailfrom=ziy@nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736182561; 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=Ufgh35zpABvHcri2rikCwKTSNZOwz1LsK/G4HcgWwbc=; b=xYIwwYOHMxJonMKaKrojcBg6BeFLXt2MeFrOncef2oTDSqi5w71afnTw6YqQbUSdX1reGk rLZRlnzyXN6XPcpG7VsEM15NJdQ3p51RYJSAfjolbBESGWDPEvEmMbFj/4V8iO4CyDALl9 24wIYZli4Z72vKZHKAwDDgSZ1AUAdZk= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1736182561; a=rsa-sha256; cv=pass; b=TSyPoMCpnqI4fY6677MMVtUWyy5N3iOpILno+H4ERPa80m6epxNUhXDHIjKuUwfkDCL4Sa LXIjxOOdrQjpXIAsg1fhC2G0iUFukGCfhFTQkGJh/HvCPEKeDneRqbTH6LEBXjK+qZXupS K1PL8LlFO4EzYiQkY2mHJ6kn9iOq5io= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=JPaYvEby; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf01.hostedemail.com: domain of ziy@nvidia.com designates 40.107.96.70 as permitted sender) smtp.mailfrom=ziy@nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Fc8N1zZGzV1jertEE3r2Ax/C8iTJjDE8lzjlYMZAtcRFpfQedykj/0xQebOu9uFS06Ie8qKJg1s5kSs+kjeN4mE0RqBbW7Ko6vcn6Fzw0ICju57H9Bbh5VbSQLjw+bKtT4Y7rwjffyKZS+52I4FJJlPyLtmhVgJto3gdSvUtCuPvLu44P7dkXp6Qsb123JzandKdrLD0gIbizpdDExiDSlKdQ39PhrYH1XeMqWL+bLmG+5aTHFN728cT8WCWAkR5/Bjck4Frgwb9TRZWBahQltX1XszDn6qbu+87nuq9TLp9gIdeNqgGeyZNcnGnhQf5hdZXNX7BotKkvuxvsmWFtw== 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=Ufgh35zpABvHcri2rikCwKTSNZOwz1LsK/G4HcgWwbc=; b=RqmChzOtBGSHLAdxU6sQlvA0Ga3eR2nPalXsDPXCeMNychR77u2Y8KqPmGbVAPx2Utegbep1dcLkKrPqQPdtn0JXrDQIxUZtVYOs1PR1ivp0rgfrIRrpnlK3DM19b7VVliGhjqS5OS4VGIJrmdtRDZhnMn/MkxxVleBbkc1h3bKWK8A9r9fRSQtMBbMTMKNsL6S20Qf7Ft3Qy3VMFvYnlsxyZQ4aK+nkJarwUxy36VarPsByFj6fdhhNeew4/s8Vfv4Vw3KVeMx9gTPJX6JNb4jE+2TsG+5XHww98yEBmjjQmyWeJ/jTQNhufa1tibL8SqYvcyWyNT3GRJMdu4Dypg== 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=Ufgh35zpABvHcri2rikCwKTSNZOwz1LsK/G4HcgWwbc=; b=JPaYvEby9U+bK9AZoHeCHANxVwRBPuuHh7+VpO1UlGSHcdm1y8PKhiev0EqLmAW1Mi62VP4vH5Rc7JXczKnksA/gGdTb6JEO+QmTx2zhTUZYaHfL/kJrOIFCDfw2d02iobDLa69gf8axQu2/PbAoSLCQIldhpKgZtWr96LuftRTEZSL+shQH1FpHIGnhinPBGZ90TwYzD1Cly5tjSE8tZndrKtinS3SPc+2BKJukJQ6N/qLPndyIKVMQiej5a3e5EaGmnd/k1uAbbsVKAY9NlFZmy3Q4l5XEBofhvt/KB0nj3qU89MvX7L5J5Wf2bJSDdL84UzWwN8ACtUVt6nZZDg== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by PH7PR12MB6634.namprd12.prod.outlook.com (2603:10b6:510:211::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8314.17; Mon, 6 Jan 2025 16:55:33 +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.8314.018; Mon, 6 Jan 2025 16:55:33 +0000 From: Zi Yan To: linux-mm@kvack.org, "Kirill A . Shutemov" , "Matthew Wilcox (Oracle)" Cc: Ryan Roberts , Hugh Dickins , David Hildenbrand , Yang Shi , Miaohe Lin , Kefeng Wang , Yu Zhao , John Hubbard , linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v4 09/10] mm/truncate: use folio_split() for truncate operation. Date: Mon, 6 Jan 2025 11:55:12 -0500 Message-ID: <20250106165513.104899-10-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250106165513.104899-1-ziy@nvidia.com> References: <20250106165513.104899-1-ziy@nvidia.com> X-ClientProxiedBy: BL1PR13CA0119.namprd13.prod.outlook.com (2603:10b6:208:2b9::34) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|PH7PR12MB6634:EE_ X-MS-Office365-Filtering-Correlation-Id: 50d48514-a24c-4957-ec86-08dd2e72ef40 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: bhSkYxeLT/WR8cQJQ7r0GGBBztMzfEtniFO+G7TC/3TqqfVzIFlBkUN+eHBDKDivOOkws9y/21OJe08jRpl5KZTLrfMTyA7TlAF/MuGkluy1thUY5AXUQD1ot15NKzTjh1etm7QTZPmP7ufT/SXAl+kMAeynT83Q4ZDfYNBTAsBdGmbJkB5yCy2QkfHg+lu2a1YBk5MUlUsJ5xlMJYnW40MKE+FsNmycCSqgwrz9Jdwph0UGPeCfRe8BXUczy4AkRvMM9TuO4ScHIqcVJhUzMcOUC94xCuWhzkNkocsPXuHTrXBtAa+32AXqnw545c9YxScxEb/k1/7yUk5YbEXTzLFHObkm3ESU+sKy+XdbdGOKKRXHiX3FgOVirVPu1CMoJYW0y81IjXt6EuhNK+H3vcuvfheWwJ3Nr9sK9wmrVmV+IWd0Ny5rJWC6wDLhyIDkwj1xrYpsDQJJUMGF3mTxBx9yStS0/2B3ImUqb3PR5fheh7F/6G/oJ7ys02crCbuhVajmDxdd3UzxLjiq2RZ5ikNyWWWJdo5/+u4TeTeuLTuxESP324HAIxqdK6146xPtdz3FrMuMOym6jCvI38hkY3ZMZwHOlFLY7h/u+fyzhmCI4aG7NkaLaFqPxTjzDPWpgSkr254HHhJ/BafgDyQx+EnMgYLyARWB/fqMPO0o03V28rksPiX7exph8VvOlaNIU9YbrPujRJIrHFDVjl4yLjcIX1BbtCGXDo4O8s+ItXWpEl+D++SCgJq8BPYZxkNJtVwxx+Z5gBJU98xFzhVXgp7l202h5cx5pifaaFcyP1ksN53U+VnqP8zS7pTmaq571SZwnagt/NGGufMnnVKPgptlGhFWLZ2NP9JAz2upet4lKefgGTpOWYAyCH9jrxhVg4dGvhOCo0yfWuJZmAaekAyjDJTP55EmpGeTcDAyoD7L1rhWLF+uRvEOShHtdXQP0nFtPsoEC9/IIAlv9v2ke3halQwcniozsZUYxMEZS401i/2WtIA3rG4UKEmWzcZ6OZIxFnBO11lL51EkwMIuyiEdQzfDTC1uKkF0XchB3FqFRrsFvIX4WaOUnYoICy/oJoJbGXpAY7yQSdnDKohT5c/ec3OcBZ96xVSaVV/Z+h4TPtg4MJ6PQMZHHo2DZr0p2cD6tmXbPmLIeLjKqTlN96kCkPGCZmSR8YPC+DIuasaedeL74ASSP46jnTy4Wb9mwTaA+aB/2QZ/XhB9dXH+06i/D1V/kKIqAfSdDYUAegvPKI9mQgWYYQncYymYEEy7CdWIDN8ySCdWTlCwfnRWjHZt7Z1HPTaPj7xwRhizAwFRwEW9QhjWWaWDbJEHBDBeVd/rl3OYvmc4BNXBm0gTC8ZSkoMkEFOpPgmanR6F74lOIDR76yirUfs96PGGvKlA 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)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RaRhGp/RMA1k7ZlpJ6PrM1n9CD9J1tRy4a8tM3Nk2Co7roPgzKx6tfTqXVXQWdsRwpWx89jRgTkemHKpAU8ys6j4woE4UfF0qCST+A/GFvzWrouXMIxdfDW1xvgaq0xWmhRfrlDLKZ8nHvh91ixEQmlQx8C9uzzNNBG1QJd4ur4FPXj2HjeB3/4SUYB7oDyqOejiSmrzRgGNJB2ZI/bkW8vLGGoqQD5fql6w7WLdtux70CCxwn4iaZs1siHC7BiYurq9OoiLvc7uKRxJ+xupVAypbseuf96Q4fBedWrX3wRjK5HHvygadv8UKfHpi2+U+jzwYhZMnmFqaRFOCIGGN+PJEq48mBfymGNJ1StKhjq+7zyYmG//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YUDMrHm4KAAPt/hlfgU03soiEs39xq0vDqgeOWZN5RGRHofARujd7Eqg73E3kmzSZCHiZes80OuwSg0HFt8qRikOklfIWlkfQ1M5v7/rU/46QDg2lYUN/fVS9JFdC/oYWxVg30DmcC0FMPg/kBCB7uKot X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 50d48514-a24c-4957-ec86-08dd2e72ef40 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jan 2025 16:55:33.2831 (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: U+Vx6PtZ+JcDPsFGsC8cVLFM93QE5xIv8ml+/o97Z4LorHoVJUnBlFjT6jVig9wI X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6634 X-Stat-Signature: 7c91e3d6fj9stwp37bwtu1zf4898r3yu X-Rspamd-Queue-Id: 0A1D540007 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736182560-853956 X-HE-Meta: U2FsdGVkX1/5/8o4a0xRCy98BAhShAoweDwvEK6QQN28JoYJq4/PM4mBa/MYzvmXQe/RsZMd02wqkp9DOeqrwwZZqPkY3B+WmqQhdFb4xIhlZGtnFgvMf08E8aRf5J1p501fM/izCYTztW1gaAJu+L8Ij1kti//pGh2oo/+YnfBvIAWvJc/iI7bxXEOswiHTjnwvZC1Kv0S74OAq8Y6/ehsnRRQxtK59bsZztSkbujtzAyLeNFWWiwF7ym9PGo7SIJMNI/474Nwuu9AewePEJL60AqS5lMurscq63gzY5yAYmDornFt++O+u5wA6Kb+Iu+qLOE0gWb7xUBGvGQqh5BHGxHIUylufIIovhbeeaufALhTG7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Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of splitting the large folio uniformly during truncation, use buddy allocator like split at the start of truncation range to minimize the number of resulting folios. 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 | 17 +++++++++++++++++ mm/truncate.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 93e509b6c00e..e5693820cb0d 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -341,6 +341,17 @@ 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); +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; + return folio_split(folio, ret, page, list); +} static inline int split_huge_page(struct page *page) { struct folio *folio = page_folio(page); @@ -533,6 +544,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/truncate.c b/mm/truncate.c index 7c304d2f0052..a8bb0c6e685d 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);