From patchwork Tue Feb 18 23:50:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866595 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 BDA461D8A0D; Tue, 18 Feb 2025 23:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922625; cv=fail; b=DN8yYxerKJngyzihjK2opeSj7EtcGrLxr9mwJZQuRLJsK8XpfEDUZk+8refXS26K/dorUm+QzEm99S9rq838EtIaybT4X4qAIRGd1c0pT2OaKsl+5cvX3nxeqCBzMFprG/QgR7y+sgSAVHKiCw83B9E5JUnTzf5wp2tzt/0Xuw8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922625; c=relaxed/simple; bh=KdkmW7tDzDjo1erXBiRihk/Gb0C2PVtzW0RA6w2ZXAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YzFylFD5cfJk1w5rnCQRPzeeHHopfqcxBeYZXaKtYqc+acRWPmQ8MBhvHEUHpZWzU90ucrHJOCOnTqUXhffQP2czp3c+Dxeak56av1BNlMemwsSJAg9E1s7bU1UD5ROYC3QVDtyzXwCEcEPFVDQVbA5PIl1WGKhohpEbqDh3Ia8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=kZQmPmJ1; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="kZQmPmJ1" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cHestNL2GmPxGn2+8euWhFd7xmT9UknVJTeAvMNNfyp1e0RD+egnflA+hUHuPR4ol/dA1rAtLgwkQic/IFZ5RUInMcgm5Kn0+PfQ21d7pEVFKHXLUP2j+XGbiv6y+uEvcZFdTVO7jhlTjNgtFUv/sIqJkd4S1sZZKg4mlg2mXY1n9yRyjjzSSmkf8G8K/B2lRlaGMLKJJe9V4zbf3duFSq/+gFUIKXfNxRhsMQavnUR8i7N3gQBfBGbcizFMfvC6gQaOYV/dSQgm7EQmV/3XQNBIMJdFvg3kB1HKcKe+735xni2ERaJ8bBNSan62JT6veBDZ5AlFQalF2nsjmqlp3g== 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=EgICjOcm38ocqZikxGIl6O6eXZ912RghvM9zgt1IXPs=; b=WBfEdwKGhq/VavOSV+tLROmUqtnj7jPZsKeBuFiJBnTrjC25CQELQuml7b9VJCN7OdMHr86de+F6/QMAeAgD7HWDiMCvWKw9yZBB4dlSXl6itAymaXyiT21Y20/agxu9M6Blnvb/m4A6bnwSshdvVOs57iV/ln3FF7nu3uq8rjLVVDM+7YS/FGwOOViusEUO9vozAWZg0+xmkDDrv3p8QIlrvzfB3oLdZlXza3j+SkxGemyvn/KM/Ebylj3BayvhJfJ+REJ/bQrtxvHgIPVHxW6fFpBkfenJ0Wdo0IBiFtul47OiYuawUh4ZKKVauGhaaJ9xzZaUmZovk64hPXVGNg== 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=EgICjOcm38ocqZikxGIl6O6eXZ912RghvM9zgt1IXPs=; b=kZQmPmJ1/pxpSHyYrqZkOP53CZuhxatgk9phuru2DOiU9QeECM8xufcvsYg9MSSrz4pnjQ3lcY4Yzhv26i1es/nIjkWig67GPjtvsaNVEYOIVn4PCxU14ki8L1B+VE3aCN8W3gaRXWjtXyZHSSaz75/uwAUOy2rLZFP3VHRWoHrXp/esyNlfiiR3yIlOFCT3xQMUq1EWaUclP4EkgiowtmCH0YdSIrovaV0RVENqVG17IKFUVX61K4x4kOje7yWbb4utdlnrv6qYnE/aKvIBaHFUPot4gCljSm9MA0O2l0VT1h5EpKhwooduSnK4a2hJFL8tuLlwP+ZoCOXwXQEHbQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:18 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:18 +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 v8 1/8] xarray: add xas_try_split() to split a multi-index entry Date: Tue, 18 Feb 2025 18:50:05 -0500 Message-ID: <20250218235012.1542225-2-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: BLAPR03CA0090.namprd03.prod.outlook.com (2603:10b6:208:329::35) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: 86587819-ab44-4579-0fe8-08dd5076ffe5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: 03cCbxOFHLCLHRlOlr9q4y37+bmMdZ42ZLBnc8+JtZLPV2mw19aMYTLj6EQojrrjQJ89I8FsJaxks5OQlqFmOiHuXF3VQiGjoTR3wOO7H/DbOvHB7R5KuyhKWHMgDVjHnBKdxc4SlHgqQ9ILe3rXm7aa/JHTrc2R5JtNxXOtYbnM/1ikYoLQ7MliREnUFN3Xy/65oRuzTw2XwyF7j1RPKympJOw52WXEOCw1gDYfoDjD+k7VoRhJLuBa9RjFfyjVT1wnJKQip6IylhIbEDT5aDOnVEkzrtVIrSkMCGWPPIDsu3NsrLYUMK//U5nKLsALEGUhxZ8dAWlOhTZcmZnHH5af4cKITBH+3GqkrLd7ZWNqokrb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G:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DS7PR12MB9473.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(376014)(7416014)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: IalGbAuNCZtsE6g8SPJqktoN74YSV+vRrw31Qbk2Yfg2/QbYA2UU3Z6uxQet49mowjWfoyIEwFZshWv915VXHI1jd5qXpKHJ7Deb8GPYSp+LzUww7XYngLM9K6H4ld2124SlBj/cqaI/ueSROzh4BWb/bHE2uSOZccjrXPJVoPvH7kVriTni2/sDsMiCZ+r416wVVsFkuRoj9pVdMlmXjh1s3J5VcCGlnCmYbdCCYM4TR+Q9QN9M1jLRVoce9GD19X/NVIzCn0tjqgMmuPBZ7XWNXntsO2epLOfwKocWBkUzbfWOca2wzrsCn7YGILm/d+7LW/yTPBQKGR9SLq2h+8TFtEgaPaW+sU6/f3MEUxj4fYi8lwn8HRrW4T2RrIN5XYkyBcrKsllCE1+TFasX09m1/328Yj+zncrTXFQ/2s/GI9pq74gvAispht/Q+r3WcM9kZnq6M7V07g5HIb4nPY7jd1E4XEuvGKFEYY/+TjMmooffv+DWcHOEVruQ3A6jgUzoXPeWh13f5UqR6eBOHqlEbxtmU5r5NPqpfDngM2XK75L254T+9PONZLxXBoku+3CYMx11TrF8S4xNpVuHVC5YlIEYwal+/efFJ934iLsh58mkZTfy576vLNW03v7ULq2BwzcUaHZuT2xsCGg5YgLmBH95/3E7rYE1q3JhhEGgtG0zmWcFs3rd0iKv/BIvXu1oX5TAFkWRO2hZizg+leQ9/Qfe4YnoGFYNLr4Kckz/4eRonMhlxUhvwDMZmZwoROXIBo4IBWx0LEwTAgySsikx4GqY8TtJKchmeDDPa0jHNHFjEjC7xCDLst/vjreGQkmWC9DCguriarNzkkUUCT5XSpQbAJFUSs+mJnUsWwTkO3d5cXIlg2lYN3pZDM7DcZHfkKfjw3WhXkHbOdME7mBqd11ulqjMgZ3s39Dq3slSjuTceKETZCMRZu9PAx4W/ikJXBYkDsILsOnZcQm20Lr3Z5ZQ/YKTYu1nVorNHQetlnlDSLVoFjPae9NSXyWkk6W6BvjOs34xofYJ68sa3LY0vB9OIUXaX3DcWk3BnVzguaw7t2mfJ8ufESGkXQiYBMauliMLrg2RaHcNuQHzI0q8BFKmkARP7y+1MeTHBrvI3Gu3wtOWFbp8ctNXyeC/o/Mwn4tDUbnnT+HJA0cWbKJQEmPuB3bSYrsCsbyIaMYLfaXZAAsKbHVeL2+xFXsoMpl+9OWrgSPrIBKKNw239Z2lDFx2gexZMP2cjHKp0O/i5VtpPykCsZhM6wd1x9s2Vo+UXv6VoRiUOVdXIXuc6OkdRbMsXhVFnkvocL/XsX9nFuL28WrSipkpVW3Xu8LE/V/C5il599FEgOrdvwzrRDKCgQWXk1WZpmfW/lMAEIyMiBMp6xIiMWb4+1rNnwf+ZQsnvbAOZUtltCrtL/pC40Ssem4iALrDIhiraq9Zal0mhAoclMo8Atzg1yCFGknLLW4fEnWmlXm1oGAgOnAW5hQTT+FM/majNpFYCzA5RNeJtnh7KQhLOD1wUpG607UnbViL7L+DinywLp9Q8Szk/1SO/PC27O7J99RWm87BIrm1aJoQBgQGV+H+W6+mhqIC X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 86587819-ab44-4579-0fe8-08dd5076ffe5 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:18.7192 (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: 6Ptw7INMBVHc/oZUmG5EtYvUzIrgKvYOqQ7xmaWSt2+BfzBC5ieYicryTyrjEtQB X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 A preparation patch for non-uniform folio split, which always split a folio into half iteratively, and minimal xarray entry split. Currently, xas_split_alloc() and xas_split() always split all slots from a multi-index entry. They cost the same number of xa_node as the to-be-split slots. For example, to split an order-9 entry, which takes 2^(9-6)=8 slots, assuming XA_CHUNK_SHIFT is 6 (!CONFIG_BASE_SMALL), 8 xa_node are needed. Instead xas_try_split() is intended to be used iteratively to split the order-9 entry into 2 order-8 entries, then split one order-8 entry, based on the given index, to 2 order-7 entries, ..., and split one order-1 entry to 2 order-0 entries. When splitting the order-6 entry and a new xa_node is needed, xas_try_split() will try to allocate one if possible. As a result, xas_try_split() would only need one xa_node instead of 8. When a new xa_node is needed during the split, xas_try_split() can try to allocate one but no more. -ENOMEM will be return if a node cannot be allocated. -EINVAL will be return if a sibling node is split or cascade split happens, where two or more new nodes are needed, and these are not supported by xas_try_split(). xas_split_alloc() and xas_split() split an order-9 to order-0: --------------------------------- | | | | | | | | | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | | | | | | | | | --------------------------------- | | | | ------- --- --- ------- | | ... | | V V V V ----------- ----------- ----------- ----------- | xa_node | | xa_node | ... | xa_node | | xa_node | ----------- ----------- ----------- ----------- xas_try_split() splits an order-9 to order-0: --------------------------------- | | | | | | | | | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | | | | | | | | | --------------------------------- | | V ----------- | xa_node | ----------- Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Miaohe Lin Cc: Matthew Wilcox Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao Cc: Zi Yan --- Documentation/core-api/xarray.rst | 14 ++- include/linux/xarray.h | 7 ++ lib/test_xarray.c | 47 ++++++++++ lib/xarray.c | 138 ++++++++++++++++++++++++++---- tools/testing/radix-tree/Makefile | 1 + 5 files changed, 190 insertions(+), 17 deletions(-) diff --git a/Documentation/core-api/xarray.rst b/Documentation/core-api/xarray.rst index f6a3eef4fe7f..c6c91cbd0c3c 100644 --- a/Documentation/core-api/xarray.rst +++ b/Documentation/core-api/xarray.rst @@ -489,7 +489,19 @@ Storing ``NULL`` into any index of a multi-index entry will set the entry at every index to ``NULL`` and dissolve the tie. A multi-index entry can be split into entries occupying smaller ranges by calling xas_split_alloc() without the xa_lock held, followed by taking the lock -and calling xas_split(). +and calling xas_split() or calling xas_try_split() with xa_lock. The +difference between xas_split_alloc()+xas_split() and xas_try_alloc() is +that xas_split_alloc() + xas_split() split the entry from the original +order to the new order in one shot uniformly, whereas xas_try_split() +iteratively splits the entry containing the index non-uniformly. +For example, to split an order-9 entry, which takes 2^(9-6)=8 slots, +assuming ``XA_CHUNK_SHIFT`` is 6, xas_split_alloc() + xas_split() need +8 xa_node. xas_try_split() splits the order-9 entry into +2 order-8 entries, then split one order-8 entry, based on the given index, +to 2 order-7 entries, ..., and split one order-1 entry to 2 order-0 entries. +When splitting the order-6 entry and a new xa_node is needed, xas_try_split() +will try to allocate one if possible. As a result, xas_try_split() would only +need 1 xa_node instead of 8. Functions and structures ======================== diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 0b618ec04115..9eb8c7425090 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -1555,6 +1555,8 @@ int xa_get_order(struct xarray *, unsigned long index); int xas_get_order(struct xa_state *xas); void xas_split(struct xa_state *, void *entry, unsigned int order); void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t); +void xas_try_split(struct xa_state *xas, void *entry, unsigned int order, + gfp_t gfp); #else static inline int xa_get_order(struct xarray *xa, unsigned long index) { @@ -1576,6 +1578,11 @@ static inline void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, gfp_t gfp) { } + +static inline void xas_try_split(struct xa_state *xas, void *entry, + unsigned int order, gfp_t gfp) +{ +} #endif /** diff --git a/lib/test_xarray.c b/lib/test_xarray.c index 0e865bab4a10..b76d9809f5c1 100644 --- a/lib/test_xarray.c +++ b/lib/test_xarray.c @@ -1858,6 +1858,49 @@ static void check_split_1(struct xarray *xa, unsigned long index, xa_destroy(xa); } +static void check_split_2(struct xarray *xa, unsigned long index, + unsigned int order, unsigned int new_order) +{ + XA_STATE_ORDER(xas, xa, index, new_order); + unsigned int i, found; + void *entry; + + xa_store_order(xa, index, order, xa, GFP_KERNEL); + xa_set_mark(xa, index, XA_MARK_1); + + xas_lock(&xas); + xas_try_split(&xas, xa, order, GFP_KERNEL); + if (((new_order / XA_CHUNK_SHIFT) < (order / XA_CHUNK_SHIFT)) && + new_order < order - 1) { + XA_BUG_ON(xa, !xas_error(&xas) || xas_error(&xas) != -EINVAL); + xas_unlock(&xas); + goto out; + } + for (i = 0; i < (1 << order); i += (1 << new_order)) + __xa_store(xa, index + i, xa_mk_index(index + i), 0); + xas_unlock(&xas); + + for (i = 0; i < (1 << order); i++) { + unsigned int val = index + (i & ~((1 << new_order) - 1)); + XA_BUG_ON(xa, xa_load(xa, index + i) != xa_mk_index(val)); + } + + xa_set_mark(xa, index, XA_MARK_0); + XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); + + xas_set_order(&xas, index, 0); + found = 0; + rcu_read_lock(); + xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_1) { + found++; + XA_BUG_ON(xa, xa_is_internal(entry)); + } + rcu_read_unlock(); + XA_BUG_ON(xa, found != 1 << (order - new_order)); +out: + xa_destroy(xa); +} + static noinline void check_split(struct xarray *xa) { unsigned int order, new_order; @@ -1869,6 +1912,10 @@ static noinline void check_split(struct xarray *xa) check_split_1(xa, 0, order, new_order); check_split_1(xa, 1UL << order, order, new_order); check_split_1(xa, 3UL << order, order, new_order); + + check_split_2(xa, 0, order, new_order); + check_split_2(xa, 1UL << order, order, new_order); + check_split_2(xa, 3UL << order, order, new_order); } } } diff --git a/lib/xarray.c b/lib/xarray.c index 116e9286c64e..b9a63d7fbd58 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1007,6 +1007,31 @@ static void node_set_marks(struct xa_node *node, unsigned int offset, } } +static struct xa_node *__xas_alloc_node_for_split(struct xa_state *xas, + void *entry, gfp_t gfp) +{ + unsigned int i; + void *sibling = NULL; + struct xa_node *node; + unsigned int mask = xas->xa_sibs; + + node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); + if (!node) + return NULL; + node->array = xas->xa; + for (i = 0; i < XA_CHUNK_SIZE; i++) { + if ((i & mask) == 0) { + RCU_INIT_POINTER(node->slots[i], entry); + sibling = xa_mk_sibling(i); + } else { + RCU_INIT_POINTER(node->slots[i], sibling); + } + } + RCU_INIT_POINTER(node->parent, xas->xa_alloc); + + return node; +} + /** * xas_split_alloc() - Allocate memory for splitting an entry. * @xas: XArray operation state. @@ -1025,7 +1050,6 @@ void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, gfp_t gfp) { unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; - unsigned int mask = xas->xa_sibs; /* XXX: no support for splitting really large entries yet */ if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT <= order)) @@ -1034,23 +1058,9 @@ void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order, return; do { - unsigned int i; - void *sibling = NULL; - struct xa_node *node; - - node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp); + struct xa_node *node = __xas_alloc_node_for_split(xas, entry, gfp); if (!node) goto nomem; - node->array = xas->xa; - for (i = 0; i < XA_CHUNK_SIZE; i++) { - if ((i & mask) == 0) { - RCU_INIT_POINTER(node->slots[i], entry); - sibling = xa_mk_sibling(i); - } else { - RCU_INIT_POINTER(node->slots[i], sibling); - } - } - RCU_INIT_POINTER(node->parent, xas->xa_alloc); xas->xa_alloc = node; } while (sibs-- > 0); @@ -1122,6 +1132,102 @@ void xas_split(struct xa_state *xas, void *entry, unsigned int order) xas_update(xas, node); } EXPORT_SYMBOL_GPL(xas_split); + +/** + * xas_try_split() - Try to split a multi-index entry. + * @xas: XArray operation state. + * @entry: New entry to store in the array. + * @order: Current entry order. + * @gfp: Memory allocation flags. + * + * The size of the new entries is set in @xas. The value in @entry is + * copied to all the replacement entries. If and only if one xa_node needs to + * be allocated, the function will use @gfp to get one. If more xa_node are + * needed, the function gives EINVAL error. + * + * Context: Any context. The caller should hold the xa_lock. + */ +void xas_try_split(struct xa_state *xas, void *entry, unsigned int order, + gfp_t gfp) +{ + unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; + unsigned int offset, marks; + struct xa_node *node; + void *curr = xas_load(xas); + int values = 0; + + node = xas->xa_node; + if (xas_top(node)) + return; + + if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT) + gfp |= __GFP_ACCOUNT; + + marks = node_get_marks(node, xas->xa_offset); + + offset = xas->xa_offset + sibs; + + if (xas->xa_shift < node->shift) { + struct xa_node *child = xas->xa_alloc; + unsigned int expected_sibs = + (1 << ((order - 1) % XA_CHUNK_SHIFT)) - 1; + + /* + * No support for splitting sibling entries + * (horizontally) or cascade split (vertically), which + * requires two or more new xa_nodes. + * Since if one xa_node allocation fails, + * it is hard to free the prior allocations. + */ + if (sibs || xas->xa_sibs != expected_sibs) { + xas_destroy(xas); + xas_set_err(xas, -EINVAL); + return; + } + + if (!child) { + child = __xas_alloc_node_for_split(xas, entry, + gfp); + if (!child) { + xas_destroy(xas); + xas_set_err(xas, -ENOMEM); + return; + } + } + + xas->xa_alloc = rcu_dereference_raw(child->parent); + child->shift = node->shift - XA_CHUNK_SHIFT; + child->offset = offset; + child->count = XA_CHUNK_SIZE; + child->nr_values = xa_is_value(entry) ? + XA_CHUNK_SIZE : 0; + RCU_INIT_POINTER(child->parent, node); + node_set_marks(node, offset, child, xas->xa_sibs, + marks); + rcu_assign_pointer(node->slots[offset], + xa_mk_node(child)); + if (xa_is_value(curr)) + values--; + xas_update(xas, child); + + } else { + do { + unsigned int canon = offset - xas->xa_sibs; + + node_set_marks(node, canon, NULL, 0, marks); + rcu_assign_pointer(node->slots[canon], entry); + while (offset > canon) + rcu_assign_pointer(node->slots[offset--], + xa_mk_sibling(canon)); + values += (xa_is_value(entry) - xa_is_value(curr)) * + (xas->xa_sibs + 1); + } while (offset-- > xas->xa_offset); + } + + node->nr_values += values; + xas_update(xas, node); +} +EXPORT_SYMBOL_GPL(xas_try_split); #endif /** diff --git a/tools/testing/radix-tree/Makefile b/tools/testing/radix-tree/Makefile index 8b3591a51e1f..b2a6660bbd92 100644 --- a/tools/testing/radix-tree/Makefile +++ b/tools/testing/radix-tree/Makefile @@ -14,6 +14,7 @@ include ../shared/shared.mk main: $(OFILES) +xarray.o: ../../../lib/test_xarray.c idr-test.o: ../../../lib/test_ida.c idr-test: idr-test.o $(CORE_OFILES) From patchwork Tue Feb 18 23:50:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866292 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 E27881DE3C7; Tue, 18 Feb 2025 23:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922627; cv=fail; b=ch/QQ9r7v1kkAJdH2EpIuGKsbYvwyowDAJ7MrVHDIhd7qsMNgMStHe9ilD2ddtgOevT04d7bfN2lVZRRh5OnSm3nsvJ9Fyp2YiyWJGRkw3ysKCWb5r2aKveSJcm3A2NWk1EDQYmBzV5PuEmT/gpxwbBoHMH5s+3DdyW3NpVndk0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922627; c=relaxed/simple; bh=jk28+P+MVPh3m/G97a5eKe+qgl25/Tn/xvwpkGT2W0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=WcNSMyDt3btNFLyjLWpNY5EmxV9oe31Ft2pted8yfFFk7yZrzDnQZDnYaiJKnxzmageGl7yi/Sc1ubpB9wyfXDjgNeAk8Jk7vJ13k/HtBYdqfRAz5v1K6TWNanmH2nAwZNXAdIeOo2BNwOc57wCSPl2Xw+D/++RByIkqUC6PYRM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=U/eXySGm; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="U/eXySGm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pBSJRrgvMOCoWCyli5ZslgXBreyT6DLOd4kBXyAYs8rhD10J3BIvR4moIfzYYwdyJZEpwcmr/sQKOsY0n5f2P8uiqbCYl9xdFLfNZWnlKFB2qnTwbSvUN3hDGBjdo04A4AcdRtYV3FnQtAiT7gOsEsOtPPJDl8EVbRlG2l9E7BHxqJLxo6kymJPTpEquzrawyso4v/JIlu1nzIykodNRNqxnBcUi+xon7JHcfkFkzj485f3E4YWX8mTXOw/H1pdCx20QABl1zxEfnKO6YiRLTeZcPclJlA9yTSRAVzhEJ9FDi/Lhj02dCbMrW0n3c0w1Dn+NVFa9STbOf7fApD9Wxw== 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=gcmqRGnFLKvtlY1e1CY4oxtUbAdoYFhIt8CwrLHI+zw=; b=nFDtuRxuNpqB6RVGV4uNlwUpmWdylwAQT8usxW+i33FzHbjiQJ+cQzwt7I2Q/Rtd50vbZ1v62dnMFrS2Bw/YRH4AEtoJ5QQgN1bUc4TpDrkDZur/H4B0DFQFeimHRue9BeNbQ0gscnB4fhc2zpJBULCRRFL/dL976jAKwXzAque+vyaSBSrgbSKXhSMF9tIdq/RZxAAuhnGhndO5GKLerq44yizlZXjOidgDo8icoJHjO8D/kyeRwCmQF8cYaZU/egCMkzQu/jmKCE55NiPdZpZnvjwAMUkSlPx2kXMpk7qv+o75BjOOf6Xz/ZgFQ/yhfJocyLC2eiXvHWPap2Eg1Q== 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=gcmqRGnFLKvtlY1e1CY4oxtUbAdoYFhIt8CwrLHI+zw=; b=U/eXySGmhUV/CWxWLtqUheRomdAT4bYB5s38yGQyjT7WoIJtGtEp//7ygXyRSgy0g+D7ydggW+Ox5eUaWP5VTYyYvfo3u+l2NmAJelMbVDZvn5SFVThBOxMsRj7OjXqVJC7OdJiUWZderKsiWwF5IqzL24tq2j5hK43/L3URO9Hl4OOZEmP5/NNs2TKUmsg7dgL3K1Fy2Avk0esJrvQBC5JQdOwlCjXFv9qlxbv9/LYAchTkYzhzzyEdfvmbfcgHoYohpG3ih5Jiqip1xUSWjjwsm45ASRGfQ/qAMWN+gdfqbhbSvKK9TqDS+Vkans474WJxy5YHen6f8TbIu3mIdg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:20 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:20 +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 v8 2/8] mm/huge_memory: add two new (not yet used) functions for folio_split() Date: Tue, 18 Feb 2025 18:50:06 -0500 Message-ID: <20250218235012.1542225-3-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: BLAPR03CA0066.namprd03.prod.outlook.com (2603:10b6:208:329::11) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: 60ae6314-1dbd-4120-4546-08dd507700af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: ydqjzstnGqnN7/f4IrP85Jie/RjKdkZ2WWEp50D4NwHmvHQhyktlWmE+8pFx//AtdBcfCp+ARuOD+VzBysBjLbc4I0ppAD80wJiLZdjIcEillTWsFTTTiRlh20OUx+WG5xGFj130/1yJeWaPpuoSZ/m4V7qeE+u+68QsNZ90jnCx4qRwwUYKd/USPvpfHbCYn2LHnMXYVxa7AVfN66X2h96kR7OwHp1mW+G5qRigf9VRNtt/sUyNBUx3FpOkmEN0wENlTE6+cGyMLwarDVKn9VE58Bjyr/ATjN7yN3LEfy9CIZNfCJTxqzGjvgkjLGZfxfGzD5Ro5lKPV9zN9iyM6IM7yApPjBU7AzuQVKGpWiBuHehIEZ28o2N8WucH3J4JQymok8wFq8L/j/FMDqWFC9cil4tD5ds4ZVxthNOlrhozTENnRviVuGi4ayRECrcECAM9nehRIAGp9+FAsrtAa7ug46qBASsN5Cw+1hWIlPENeqJrJ8blr0PRthK2xehB8gkWuscnC8BOiJdW7VbT3Qf11m2Ci7E+QdVekRnZW3scVUL5O/ABu7KRr8aLoqq5bm3Nc7exkctVj1EnQxJNzeqmCc+WouW1PbcTE6bu5PUj1M+n5LJ4Vmh9kbjxaC0eFNuv1i293h8MNpeQYXgRluTlg/uXTVcU4lxgIoZuqB2yV2xOq7oarhAP7JpEtjUxiwEP0izgRXRj3TC1XR8LmDCWc+29R6ld+6bmih9mlifi+/vuONV26Nw240NOSmkN9A5tf8PPzDFk2Ccx4FKNXHFC4+i1sG+0g3wGIPUC5Re3V6DVVxdvgcnbZ9ys9nOM6gEsFbhB750UffBLwzEyoTmR48QsyRkK3/w1jl1WFbG74kB68D7W3GorwsC9r+lG0OdeXxlrxK5C+1ja/BA64NqIsniaw08nvrRRzlFWDwHApZuMkYx6gdTV9dxGuG6OQM9/z8JhX+dXcT1bdYOfH7Z5y2cXD7/PjU36frVU6+7VHX1qly+Z6ECDLWvqesfQH0E405NqY7TbEGsdEsm8mPImjvlUz+EIc+rZyNRJl1RjxCKuziEFLghrm8skmWvYtTQI4lUEQZePoR/8i9ntQ+j6UBg6IosDWFm7nZEzqSywvClKlOErmfM1ZAE8NKlw7cBfvj+JN94JbirLUGO3GGpxG7jAdk9cX9F1ryxdMbcyrjj87Ft98f70adfEZYIt3VAHj56bS5fCtY2aESJ7g7BoWZp3Q4c6TeeKu+B4I+dXtwTo4FNudP3hhW/h+QYHks2EnxsHNNb3oT90JsigMnRczwgibhq7qEH6BFNzZFMdQIZMRF2Fl4P2MOE85ljVne0pNB3v9s6iJz/didZ1Q7AbP1lFw3jrWgq2tYjjh1GEI6d3lrIUGO1oMuhnR0FV 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: FiF9Bd8kCTd5jlX8ZHqe5EWsosgQAHopV5OX3tVDz0OkxhB4CJsP9cRn1zhkl2T19kEiiIN99J/f7290oAdpIqtSkEqN3O7c+XgTbyeDKWYxDh5Xc5npCEGqIkkeESI4lZo7Yl5wyHEcvLRaejk/tZ+7qTRsAydSaOvFy/WiWis/mEVaW3eD91zQ99Zcujd46VMcsbsWo1okJZDziejXqZROzmcwFR3B6HONCWb2dTVCmP4XfIYr7p5F9bRDnq+wlySf8/SRLh7dyPUfOdb9urE+4fvEUR+bMO4F1vGnkSwa4NzpXQ62t5ySHCvA8XCHlw/KEAHNSzS910r3gAggZJ7Vt3yHx+SqavVNQvu21g9Ult0R0GvHFj1LV+1v4VG0KWy8+8oS2oRgfwiU2w38tRXEAJy8eZ6tqk8wx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bnGPsP+uwgsbuWw3F3PPN49nS9846g6QtSIMe98RiOZxAJ4uM8gTNkLEt+6dnb4lCavp1/jeL+1cUQr8vxbpuI9sbtvCDAMeD9jH/Q7 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 60ae6314-1dbd-4120-4546-08dd507700af X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:20.0296 (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: 9WTd6jZsS8uFTvEFRmgTZL2sctXqOhxGO3HrtmPQ7PXY4x36PX41IX5BdJzCV7LO X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 This is a preparation patch, both added functions are not used yet. The added __split_unmapped_folio() is able to split a folio with its mapping removed in two manners: 1) uniform split (the existing way), and 2) buddy allocator like split. The added __split_folio_to_order() can split a folio into any lower order. For uniform split, __split_unmapped_folio() calls it once to split the given folio to the new order. For buddy allocator split, __split_unmapped_folio() calls it (folio_order - new_order) times and each time splits the folio containing the given page to one lower order. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- mm/huge_memory.c | 339 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 338 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index de72713b1c45..385e424252d4 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3149,7 +3149,6 @@ static void remap_page(struct folio *folio, unsigned long nr, int flags) static void lru_add_page_tail(struct folio *folio, struct page *tail, struct lruvec *lruvec, struct list_head *list) { - VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); VM_BUG_ON_FOLIO(PageLRU(tail), folio); lockdep_assert_held(&lruvec->lru_lock); @@ -3393,6 +3392,344 @@ bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins) caller_pins; } +/* + * It splits @folio into @new_order folios and copies the @folio metadata to + * all the resulting folios. + */ +static void __split_folio_to_order(struct folio *folio, int new_order) +{ + long nr_pages = folio_nr_pages(folio); + long new_nr_pages = 1 << new_order; + long index; + + /* + * Skip the first new_nr_pages, since the new folio from them have all + * the flags from the original folio. + */ + for (index = new_nr_pages; index < nr_pages; index += new_nr_pages) { + struct page *head = &folio->page; + struct page *new_head = head + index; + + /* + * Careful: new_folio is not a "real" folio before we cleared PageTail. + * Don't pass it around before clear_compound_head(). + */ + struct folio *new_folio = (struct folio *)new_head; + + VM_BUG_ON_PAGE(atomic_read(&new_head->_mapcount) != -1, new_head); + + /* + * Clone page flags before unfreezing refcount. + * + * After successful get_page_unless_zero() might follow flags change, + * for example lock_page() which set PG_waiters. + * + * Note that for mapped sub-pages of an anonymous THP, + * PG_anon_exclusive has been cleared in unmap_folio() and is stored in + * the migration entry instead from where remap_page() will restore it. + * We can still have PG_anon_exclusive set on effectively unmapped and + * unreferenced sub-pages of an anonymous THP: we can simply drop + * PG_anon_exclusive (-> PG_mappedtodisk) for these here. + */ + new_head->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; + new_head->flags |= (head->flags & + ((1L << PG_referenced) | + (1L << PG_swapbacked) | + (1L << PG_swapcache) | + (1L << PG_mlocked) | + (1L << PG_uptodate) | + (1L << PG_active) | + (1L << PG_workingset) | + (1L << PG_locked) | + (1L << PG_unevictable) | +#ifdef CONFIG_ARCH_USES_PG_ARCH_2 + (1L << PG_arch_2) | +#endif +#ifdef CONFIG_ARCH_USES_PG_ARCH_3 + (1L << PG_arch_3) | +#endif + (1L << PG_dirty) | + LRU_GEN_MASK | LRU_REFS_MASK)); + + /* ->mapping in first and second tail page is replaced by other uses */ + VM_BUG_ON_PAGE(new_nr_pages > 2 && new_head->mapping != TAIL_MAPPING, + new_head); + new_head->mapping = head->mapping; + new_head->index = head->index + index; + + /* + * page->private should not be set in tail pages. Fix up and warn once + * if private is unexpectedly set. + */ + if (unlikely(new_head->private)) { + VM_WARN_ON_ONCE_PAGE(true, new_head); + new_head->private = 0; + } + + if (folio_test_swapcache(folio)) + new_folio->swap.val = folio->swap.val + index; + + /* Page flags must be visible before we make the page non-compound. */ + smp_wmb(); + + /* + * Clear PageTail before unfreezing page refcount. + * + * After successful get_page_unless_zero() might follow put_page() + * which needs correct compound_head(). + */ + clear_compound_head(new_head); + if (new_order) { + prep_compound_page(new_head, new_order); + folio_set_large_rmappable(new_folio); + + folio_set_order(folio, new_order); + } + + if (folio_test_young(folio)) + folio_set_young(new_folio); + if (folio_test_idle(folio)) + folio_set_idle(new_folio); + + folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio)); + } + + if (!new_order) + ClearPageCompound(&folio->page); +} + +/* + * It splits an unmapped @folio to lower order smaller folios in two ways. + * @folio: the to-be-split folio + * @new_order: the smallest order of the after split folios (since buddy + * allocator like split generates folios with orders from @folio's + * order - 1 to new_order). + * @split_at: in buddy allocator like split, the folio containing @split_at + * will be split until its order becomes @new_order. + * @lock_at: the folio containing @lock_at is left locked for caller. + * @list: the after split folios will be added to @list if it is not NULL, + * otherwise to LRU lists. + * @end: the end of the file @folio maps to. -1 if @folio is anonymous memory. + * @xas: xa_state pointing to folio->mapping->i_pages and locked by caller + * @mapping: @folio->mapping + * @uniform_split: if the split is uniform or not (buddy allocator like split) + * + * + * 1. uniform split: the given @folio into multiple @new_order small folios, + * where all small folios have the same order. This is done when + * uniform_split is true. + * 2. buddy allocator like (non-uniform) split: the given @folio is split into + * half and one of the half (containing the given page) is split into half + * until the given @page's order becomes @new_order. This is done when + * uniform_split is false. + * + * The high level flow for these two methods are: + * 1. uniform split: a single __split_folio_to_order() is called to split the + * @folio into @new_order, then we traverse all the resulting folios one by + * one in PFN ascending order and perform stats, unfreeze, adding to list, + * and file mapping index operations. + * 2. non-uniform split: in general, folio_order - @new_order calls to + * __split_folio_to_order() are made in a for loop to split the @folio + * to one lower order at a time. The resulting small folios are processed + * like what is done during the traversal in 1, except the one containing + * @page, which is split in next for loop. + * + * After splitting, the caller's folio reference will be transferred to the + * folio containing @page. The other folios may be freed if they are not mapped. + * + * In terms of locking, after splitting, + * 1. uniform split leaves @page (or the folio contains it) locked; + * 2. buddy allocator like (non-uniform) split leaves @folio locked. + * + * + * For !uniform_split, when -ENOMEM is returned, the original folio might be + * split. The caller needs to check the input folio. + */ +static int __split_unmapped_folio(struct folio *folio, int new_order, + struct page *split_at, struct page *lock_at, + struct list_head *list, pgoff_t end, + struct xa_state *xas, struct address_space *mapping, + bool uniform_split) +{ + struct lruvec *lruvec; + struct address_space *swap_cache = NULL; + struct folio *origin_folio = folio; + struct folio *next_folio = folio_next(folio); + struct folio *new_folio; + struct folio *next; + int order = folio_order(folio); + int split_order; + int start_order = uniform_split ? new_order : order - 1; + int nr_dropped = 0; + int ret = 0; + bool stop_split = false; + + if (folio_test_anon(folio) && folio_test_swapcache(folio)) { + /* a swapcache folio can only be uniformly split to order-0 */ + if (!uniform_split || new_order != 0) + return -EINVAL; + + swap_cache = swap_address_space(folio->swap); + xa_lock(&swap_cache->i_pages); + } + + if (folio_test_anon(folio)) + mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1); + + /* lock lru list/PageCompound, ref frozen by page_ref_freeze */ + lruvec = folio_lruvec_lock(folio); + + folio_clear_has_hwpoisoned(folio); + + /* + * split to new_order one order at a time. For uniform split, + * folio is split to new_order directly. + */ + for (split_order = start_order; + split_order >= new_order && !stop_split; + split_order--) { + int old_order = folio_order(folio); + struct folio *release; + struct folio *end_folio = folio_next(folio); + + /* order-1 anonymous folio is not supported */ + if (folio_test_anon(folio) && split_order == 1) + continue; + if (uniform_split && split_order != new_order) + continue; + + if (mapping) { + /* + * uniform split has xas_split_alloc() called before + * irq is disabled to allocate enough memory, whereas + * non-uniform split can handle ENOMEM. + */ + if (uniform_split) + xas_split(xas, folio, old_order); + else { + xas_set_order(xas, folio->index, split_order); + xas_try_split(xas, folio, old_order, + GFP_NOWAIT); + if (xas_error(xas)) { + ret = xas_error(xas); + stop_split = true; + goto after_split; + } + } + } + + /* complete memcg works before add pages to LRU */ + split_page_memcg(&folio->page, old_order, split_order); + split_page_owner(&folio->page, old_order, split_order); + pgalloc_tag_split(folio, old_order, split_order); + + __split_folio_to_order(folio, split_order); + +after_split: + /* + * Iterate through after-split folios and perform related + * operations. But in buddy allocator like split, the folio + * containing the specified page is skipped until its order + * is new_order, since the folio will be worked on in next + * iteration. + */ + for (release = folio, next = folio_next(folio); + release != end_folio; + release = next, next = folio_next(next)) { + /* + * for buddy allocator like split, the folio containing + * page will be split next and should not be released, + * until the folio's order is new_order or stop_split + * is set to true by the above xas_split() failure. + */ + if (release == page_folio(split_at)) { + folio = release; + if (split_order != new_order && !stop_split) + continue; + } + if (folio_test_anon(release)) { + mod_mthp_stat(folio_order(release), + MTHP_STAT_NR_ANON, 1); + } + + /* + * Unfreeze refcount first. Additional reference from + * page cache. + */ + folio_ref_unfreeze(release, + 1 + ((!folio_test_anon(origin_folio) || + folio_test_swapcache(origin_folio)) ? + folio_nr_pages(release) : 0)); + + if (release == origin_folio) + continue; + + lru_add_page_tail(origin_folio, &release->page, + lruvec, list); + + /* Some pages can be beyond EOF: drop them from page cache */ + if (release->index >= end) { + if (shmem_mapping(origin_folio->mapping)) + nr_dropped += folio_nr_pages(release); + else if (folio_test_clear_dirty(release)) + folio_account_cleaned(release, + inode_to_wb(origin_folio->mapping->host)); + __filemap_remove_folio(release, NULL); + folio_put(release); + } else if (!folio_test_anon(release)) { + __xa_store(&origin_folio->mapping->i_pages, + release->index, &release->page, 0); + } else if (swap_cache) { + __xa_store(&swap_cache->i_pages, + swap_cache_index(release->swap), + &release->page, 0); + } + } + } + + unlock_page_lruvec(lruvec); + + if (folio_test_anon(origin_folio)) { + if (folio_test_swapcache(origin_folio)) + xa_unlock(&swap_cache->i_pages); + } else + xa_unlock(&mapping->i_pages); + + /* Caller disabled irqs, so they are still disabled here */ + local_irq_enable(); + + if (nr_dropped) + shmem_uncharge(mapping->host, nr_dropped); + + remap_page(origin_folio, 1 << order, + folio_test_anon(origin_folio) ? + RMP_USE_SHARED_ZEROPAGE : 0); + + /* + * At this point, folio should contain the specified page. + * For uniform split, it is left for caller to unlock. + * For buddy allocator like split, the first after-split folio is left + * for caller to unlock. + */ + for (new_folio = origin_folio, next = folio_next(origin_folio); + new_folio != next_folio; + new_folio = next, next = folio_next(next)) { + if (new_folio == page_folio(lock_at)) + continue; + + folio_unlock(new_folio); + /* + * Subpages may be freed if there wasn't any mapping + * like if add_to_swap() is running on a lru page that + * had its mapping zapped. And freeing these pages + * requires taking the lru_lock so we do the put_page + * of the tail pages after the split is complete. + */ + free_page_and_swap_cache(&new_folio->page); + } + return ret; +} + /* * This function splits a large folio into smaller folios of order @new_order. * @page can point to any page of the large folio to split. The split operation From patchwork Tue Feb 18 23:50:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866594 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 21F261DE4D8; Tue, 18 Feb 2025 23:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922630; cv=fail; b=cC8MJk/q+c6cG4xwsk02S25OVr+fXTA64XefLmV+jKZ62BpEiRdN1OPl8axciX8UnSb2ovDBjLEBGiiMxmIGl553rCWp7jx1a4TdyaqN+5feTmmmRPyUbgHKpXMs25POoGrbhRFFBweQXTtj4SSfLqp+XLHrwLz6dTrd6Om9urw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922630; c=relaxed/simple; bh=Nzg4xUyhgcUI185/PYI4Mu47OoFDj4ftTMwvA7XDet8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=uYrb9S35TNKcGHVE8J96deLloiOIaNIxXr3WoPVdv2jG60l/TDDcaB1ufRpR9m3vG/nw/PZ6NKIIceA+6IRkau9anVjH6V0FGLGjvQWBoTG4EVenqoIHEc5j/oGHybBKzQyvg0p0caMvL7SFIrnXsD7W62pOhm3EH6rQYvIBRX0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=BC4pwUcj; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="BC4pwUcj" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=XnJ/jB9QoK/Z5XQm+6+ytH/COghtAMJzO5HAvnvMdRGMYoKKRDiuE0mNd4Fj8elNjp61GNzj6LSNoNPS1D19B1TIEjJ+4eSCfTu5r0sCdUBt+U5ZbbCBO+ggahuVUnSrZvFMZCRUt5Oq4dOIAhFnKR+AmULpZVZIocor+WwjLcND8fz4DbRF+xZfBgwUrbp6cNvasM1IarpfyIdLfuJM6fiUNaiANj34WK4sWEYkwf7NmAEdO+hTHGQWFkE6/ah7J6kqgAkFoM+QwxuZn9U/lvlF53a4Z+DmE5rJrIxEUrMfWs+M4NqNzCTjFl8vS/WMYxvZJSSn62e6aGlVbI5DAQ== 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=qjJYG5I9oY97IgtR6hgx74fjlKX8/ku946e9EFhfCAQ=; b=CXk68jYu5o/gSjlZ32nWk5PykX8NHRAudl/CB4H93jgC2pBGgRHRYLC//yMr7JhjApi+FZyV7jatMkuEI0pCLLX4unxy4XmOF+LYpyaLa5PUonj6AXllFtG+9hSxC+Os6Ks3pohRn6Z6OBE1u79MDYx2jqvsd317THs7vo4vlDemhOtp5wcS3eOgadX5Z15d2Q6NPmXBo6HDQGSyIi5BtL1Cr2Z7TZPZqgtUTYgCjSubx3KK7EFVMJ3r4MnVrrfXyRyqzS0RDHIDkDEcA00GgKg73zBu5rHONSpcpp9uYqTKzykPHiR3JX88hOFMilqWvF6ZIsNo4vEVTzVHzu6+uw== 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=qjJYG5I9oY97IgtR6hgx74fjlKX8/ku946e9EFhfCAQ=; b=BC4pwUcjg4U+SR89Rsz5HTktHxDkt/BLZPvR5dOwtNZDr43OjWvyeMQ5OxqRpDefhfogScp91vD6edM4lf2SIluDERB18G05xVMwbfU4g7tKsyC4um5BpPc1e0YLYgnvOIIJnO3U7OaOj0HRCLVG1Zj6NPNXc1IS3kT/+jUP71IN3/x/dxGK5EQ0p3jqWKgBV0JuOEVlidHxUhyG3QKkPxWDvZjslDJgjvztKBB8bNS6+mwzMTE8Ui1IUEHJqJ0Tm3sIswN1shgNVRrb6fh57UQC/K1L7nLx9t/EXGxeN2u8byJ58LhqXtyYLlaGMOAC2bwwYng82DnvEQZO/TsVMA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:21 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:21 +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 v8 3/8] mm/huge_memory: move folio split common code to __folio_split() Date: Tue, 18 Feb 2025 18:50:07 -0500 Message-ID: <20250218235012.1542225-4-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: BLAPR03CA0069.namprd03.prod.outlook.com (2603:10b6:208:329::14) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: c45fe844-6d8f-41c3-a9d0-08dd5077016a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: ol85hoJoYZFD8U4PJv1qMrxxXqCSPon1/adRoPI/a95yior1EVi/HKHW+snjz+TC7uE98CbikzEAeSW4OqDu37uZX9JRfUYVNSaLERuHL//J/KusOqTyUscQUvY9foHdrTiH9QveOBTEmpnfOJLlo5koMMPa+W0qjyp9Nxfu8TtIAdGJo1LS5m+uLpg516waMfaCw/T/SHToU9kF3u6kvVUroanxvJWJ82Tc/RQqjNurjEBtnRAsu5VyeIsYPO0uSHiwIvEf9nfPqFN/8I4kJ2iF1FCEwoZYkYCM/oCZAbrQs8ez0jxgl4iJqTcY9rWdJZSUMoMPV7jIgwOD/ObzhqDhXFD1jMeEv2taKHLTMLXea68S8PpDwxoOaR7SMvzdS8w8BDzJ/yi1M5L/X6pI2SezwukubrU7MohRtqkvmza/GoUquDGJMKN/8KHC+61gv0chIWoyN5vFnhIoDvoMsY7ignWdGTTxfoVqmfNTOF8i6jsZafDWyBdxtmsTo6M2Utx84wch6KgbVTmnuo74tDr1Uh3DQGo19NqOgfaOjc7TwS4tei3a2guq0c/dnO9pnhTt7y13IEZwdp7v9qeOyLIm0fPoLM1Z/tgRrKj45G0b9Su2d8Nns7KPjIJRurPhdeI9sBZkhjoSzKLk42CDKgFxCKWgB3c/0wgIwujoyGOxB7Y6YKdEgsr8nsphqVt1RSKA/L6xCiWyCIjEd9P/joOOBe1oGyNC2JFpcohb1+LZQ00mZRs6/+r6X+hqeyEEDd/5YsHen6kSt1eD43r5xCG5drDJQNPkdL6nN5jDcM5xTfkxmv64aOL+guwDX703JxD6I2+fE6hc0+J+CP86BVS6eYCToL/qpVqkwu9liwmF35Zg0Q4fXmXMyu1jS+6w7f3+I5o8fOD5+eeFKqI4wCKz1x7lLZmse7ErVbrsSmxVfnvP720qI+2wcXHIyiEvkWvMLnXeDwmsivFcBY6/4xdX8qo/Nc/cULQ5X+S7JeamDsWqA2ahzbC3T+dbfOxoL8sXNY3VNUJ2PmCeheicNyOJDm4eEhiklWaoIuT96sS9kmiK9eSF8iuLqXL9RVF9G/DAw1dAkdqq1pZJAyh1wmlq2JNkjHrMbpYooN3eTDRAMpGxqlbw1tENrMOSj0EOhe/p4XGn6nEzQoH0V7Z68u6BFYQodivkMPkd7O1S0cnkv6u4TX1rRbULDUyP+9ZP4H7VHHP1RxHPB6R9x8SSy9lxFi5UYSqTQW4ioebuCH2NwqXWbGnCl1plSgnCW95W74W+r6QlZ1mqdjiu/bSoisJ/ESRF2kUSynocFdKpi/yoR+5HeblRYU/VKcXC9Tc1iwDS84Zkn8l9OCrGwl8CSaqNuPi6y2VFjt2FonhFK5UZX+VCwzO6W5jas3YHeoIe 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9d5iyc+1zTRhGh+Cy5PRNaEwETiaoZQosgTV3HCzjA9m2XER7Y/NTbEhBC5tGhYVFEw+f34kQ2VGBjgc4NKS6dHH8/BA7H1qz0MYVjNwRrfR7mk5cixwrdDzJSvZbYJYJCRHZSZm8aKx8Co8QahZEHDyEPURBUQgcQB53lQ9UWd6x5kAVDYYERiF/Aca/CNxNQLzr+NF+RGBDIqlbSfzmiHPnsBPWUD2a8JvIQGPV40ox713o/ToBIa7Xldg+L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jEgzz9H7Ev8BV+POnbT4JCojYFte0pkgl/B4Drpn6x91J58tZUS6zKqNkE39rziAJ3jjm2fWm5ADta/yaJ4nWfDWZVq3wXVBmg8a6PahkTpVYlqgej86KrUYvx3yb+1v+F8IwUl7kuFYONMTrNhM3Gx1bEvyHf4v6f2McbZsTcEIah2I8eCY0Zq5ix/liHjR/jCeUv6CSREVW1MV8PfJKNwjmGR3BvP+AG9eimwCOhDoZ21NaBSkPEQHl4tlwFHNwHt0XOIC90cQd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c45fe844-6d8f-41c3-a9d0-08dd5077016a X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:21.2726 (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: EFVXM6/CAUhsBczZq9bdKZbddyYPtgYwjztvSqyIEjvld00ac0kfFSj3MtyVBt28 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 This is a preparation patch for folio_split(). In the upcoming patch folio_split() will share folio unmapping and remapping code with split_huge_page_to_list_to_order(), so move the code to a common function __folio_split() first. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- mm/huge_memory.c | 107 +++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 385e424252d4..94ed85aadecd 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3730,57 +3730,9 @@ static int __split_unmapped_folio(struct folio *folio, int new_order, return ret; } -/* - * This function splits a large folio into smaller folios of order @new_order. - * @page can point to any page of the large folio to split. The split operation - * does not change the position of @page. - * - * Prerequisites: - * - * 1) The caller must hold a reference on the @page's owning folio, also known - * as the large folio. - * - * 2) The large folio must be locked. - * - * 3) The folio must not be pinned. Any unexpected folio references, including - * GUP pins, will result in the folio not getting split; instead, the caller - * will receive an -EAGAIN. - * - * 4) @new_order > 1, usually. Splitting to order-1 anonymous folios is not - * supported for non-file-backed folios, because folio->_deferred_list, which - * is used by partially mapped folios, is stored in subpage 2, but an order-1 - * folio only has subpages 0 and 1. File-backed order-1 folios are supported, - * since they do not use _deferred_list. - * - * After splitting, the caller's folio reference will be transferred to @page, - * resulting in a raised refcount of @page after this call. The other pages may - * be freed if they are not mapped. - * - * If @list is null, tail pages will be added to LRU list, otherwise, to @list. - * - * Pages in @new_order will inherit the mapping, flags, and so on from the - * huge page. - * - * Returns 0 if the huge page was split successfully. - * - * Returns -EAGAIN if the folio has unexpected reference (e.g., GUP) or if - * the folio was concurrently removed from the page cache. - * - * Returns -EBUSY when trying to split the huge zeropage, if the folio is - * under writeback, if fs-specific folio metadata cannot currently be - * released, or if some unexpected race happened (e.g., anon VMA disappeared, - * truncation). - * - * Callers should ensure that the order respects the address space mapping - * min-order if one is set for non-anonymous folios. - * - * Returns -EINVAL when trying to split to an order that is incompatible - * with the folio. Splitting to order 0 is compatible with all folios. - */ -int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, - unsigned int new_order) +static int __folio_split(struct folio *folio, unsigned int new_order, + struct page *page, struct list_head *list) { - struct folio *folio = page_folio(page); struct deferred_split *ds_queue = get_deferred_split_queue(folio); /* reset xarray order to new order after split */ XA_STATE_ORDER(xas, &folio->mapping->i_pages, folio->index, new_order); @@ -3990,6 +3942,61 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, return ret; } +/* + * This function splits a large folio into smaller folios of order @new_order. + * @page can point to any page of the large folio to split. The split operation + * does not change the position of @page. + * + * Prerequisites: + * + * 1) The caller must hold a reference on the @page's owning folio, also known + * as the large folio. + * + * 2) The large folio must be locked. + * + * 3) The folio must not be pinned. Any unexpected folio references, including + * GUP pins, will result in the folio not getting split; instead, the caller + * will receive an -EAGAIN. + * + * 4) @new_order > 1, usually. Splitting to order-1 anonymous folios is not + * supported for non-file-backed folios, because folio->_deferred_list, which + * is used by partially mapped folios, is stored in subpage 2, but an order-1 + * folio only has subpages 0 and 1. File-backed order-1 folios are supported, + * since they do not use _deferred_list. + * + * After splitting, the caller's folio reference will be transferred to @page, + * resulting in a raised refcount of @page after this call. The other pages may + * be freed if they are not mapped. + * + * If @list is null, tail pages will be added to LRU list, otherwise, to @list. + * + * Pages in @new_order will inherit the mapping, flags, and so on from the + * huge page. + * + * Returns 0 if the huge page was split successfully. + * + * Returns -EAGAIN if the folio has unexpected reference (e.g., GUP) or if + * the folio was concurrently removed from the page cache. + * + * Returns -EBUSY when trying to split the huge zeropage, if the folio is + * under writeback, if fs-specific folio metadata cannot currently be + * released, or if some unexpected race happened (e.g., anon VMA disappeared, + * truncation). + * + * Callers should ensure that the order respects the address space mapping + * min-order if one is set for non-anonymous folios. + * + * Returns -EINVAL when trying to split to an order that is incompatible + * with the folio. Splitting to order 0 is compatible with all folios. + */ +int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, + unsigned int new_order) +{ + struct folio *folio = page_folio(page); + + return __folio_split(folio, new_order, page, list); +} + int min_order_for_split(struct folio *folio) { if (folio_test_anon(folio)) From patchwork Tue Feb 18 23:50:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866291 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 306611DE4DA; Tue, 18 Feb 2025 23:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922632; cv=fail; b=K6TnEjWhZINIVlKWz+OVTM5fdUuAKcToXOPXIu7ywnrvVFw0OkvXB5HXlJ3WNNg10Sokyvjgz1UmYAZOETVR+2psPMHA5hR+NPhkuVfyZZMsLy2VvPi0I4X97JHis35Q6wo54wROjjdz4Ukig/Rra1lr4vQPxku+Lg1hT/KFqhY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922632; c=relaxed/simple; bh=jtqwzLqkAkgMcKGpYB1a4b6/aEikdZRRDuSFALpnzVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=W+fIdnfMapkNH5O11+uQ0XITad4Xr3BSTXee6PSkohgaj+1YXy/G7ePsOuLBWy11diN4xR0A8CvZTL446EPBtyZK/eENdb2Jkz0jBMsjDqJ2tLpXLMf4KS3jO4SuCTRw0BfjV0dXa3TV50ePRNf+vXawu1Fsj2zw8AYSX+OjzR4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=eqRF+P2B; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="eqRF+P2B" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IlWN3m4ZTFRkRMQ36PyOheGdwfE0IEzlXIudPHtqbHIuO1MCXtdW+GZIrGiJL4622wrQhrRCR1ZNB7DWrMIwySGFyHukbGvWzFswzSjPt2bx12fiTJVTETOskHNvq2r7gfBNEg9EXGsz3r0wepCcKLkuhBE45I5SxwgRGZ3JZWkcPnhI+Zd9fwBwbbbYoyaXmNaiP5NaOGnoMduI7EBfh2SiZ8McGyKkHp7WWm5HpvZ8TGLnA7kJN8u2sPUtSh5XvS0yqq07aCIcFSjGo41lVIt9fHk0/AVbE0HHxAq4vtNYeTPi76ZJkyHUrY+C2UbyII2D3yVQyL83Np1ldgp0pw== 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=6WCU82Jd1uNgVmSGztRSAEgr67cG0iKflrPCprWFeRc=; b=icoc8b+zhxE/HiQd15Pw6j9EhjzLlbiEbzXV/bOpYITtzdJg5AMZHaswtJR37JW/Pa0cCWqooiby4gLp3ixMCn/f6ySa1hQ+DM+bkTg0jn3sy3HQTGYQ/qk1C+6yJUmEFUz1W3mIA6sZeUcE9FVdnBYI9Z4gkkKLH/eq78fsrFa9rNB4oGRb3c0l/99AtHESwOZ6P87Jnxo6Gp3Pnb2SvPwxKvyPzsvAqDXb7YzK8BzCHTO++g+eK7WOKLjWC6Cg3Lk6wzyMRhOkuLJWWmXTzOV6M/tf47CcFF6Telj3tCMu0kHCRs83xVTkspIRhAzpI0OhaQ++ifee2msM9H8jUw== 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=6WCU82Jd1uNgVmSGztRSAEgr67cG0iKflrPCprWFeRc=; b=eqRF+P2BqlPJWN0Koi8YXZaJkOG/+wrMSUw/QTXI9/ubt/TRBqpZVMRcehzI3Hslby9IJYXReDUY/3Jo8jyqRGh8CcdIjEr2eHl4rfwF7doGWLwtTqYnufDm1SGy2Aznu6Wzt3Hnupgoxpjuh5gnjkEqHy4/vCxb59bBnyObNEt4ZVXrOGxD2ABSuy42xJ2ngsFTSdo34d9p2RnTHkQkdFwRM4YxN+4lqPvb02R9CQroreW+hjhdXXH4/McmiikC+T03Sw+hsM9sgQYBQc/NxS8Q8moqqZT0lj1YDQIup0aXulW7FTQZNaLx/zqB7lWklsI4/XNfm2zeYyAkayTO6Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:22 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:22 +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 v8 4/8] mm/huge_memory: add buddy allocator like (non-uniform) folio_split() Date: Tue, 18 Feb 2025 18:50:08 -0500 Message-ID: <20250218235012.1542225-5-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: BLAPR03CA0080.namprd03.prod.outlook.com (2603:10b6:208:329::25) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: 072bb02a-3076-44c4-7e08-08dd5077022e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: 11Sh9pasSGqhsjPWwC22lqg6FW+mQH2Iei/jT/0v4QS4s+dpkrM5MTe33Vw8pI9NFKXuKvp1WpW3Vmreagj+P+rCXNIIjhTOe3gpLfvfWLgiV+s2k3NOWPUsVBDKDIecN0p00NnJlBSA+XLfwYaYImndIiDvZqC6e7yKC3Pmz4exxQxH+i/lWNxXX6S/Vwy9MhRbUZyRwEeqpd3+Wb0jgGjndqsBvih1cpgp7OTqj/KX+RSgFGVsVlzdnYMc9Ccis+KT3QGHoECJ2EycIy44eJGVIRGQsLo6DAPm6A/jXDjqCktec75/xHDlbOXDNV7J9TrjriDrCVAprRHzRX8uMFe+5xzBbW1tSd9Ag0beyauY22pAUmpMyuMjTLo+6MyTWnJgsGakVK0fk2KMTBhBe3WEQ5rN3EJbmYiuw/x8D9Bb7jPV+8ZWpV8bvPK3EqGeDpFWD0vM64x12SPSkZvIDdMQktU+yPMH1Wd34K8qIfmovd65WDaeYNCc3wOHBbDs7phJv3EVK15U9xPFyOcen8VZfa1Juzj/Ojm7TpwOEBxl6y/MuaBjwePwrXmeziVb/67b8jqgryntPLfbkINmmkCjUpT3yjEE/SW0zVKQ3EsJxYQzj4C3Zzn6JEOv6afYleP6G+Y03YQfxiZrq0tLqjMuWoiJWhXdCyB/DgRYV9BFhSyt/RewUvl3/JXPuHE8tTffGJcKjFAWfikWYVNyWlRei+7JmQ45Bngtne7kIcca3xerv3GeSp+KlLaPyrY+9p44uk4DoRYeDkzRYucirXcrn9SekvNhnrmEnp7n0Mh+WIAcWBljPmgXqBZT2QwOffZshoRfVpdWUX/MkVGF4OVQkEcN+zUB5rEU18t3AK3FQ/PxUfICEcKUAFkPJiJ9Zra4g7zyzi3iA2/SjeWmCa2Od3afrKe3Z/KM/Lf+J9HrK0E35sYj8lo980PRVK5Gfj/LsEL3rKSHV5xFJrsID2vjmcnks8JqWULKxXDzqcFjO0khnwydERNZr24Bp7iu3FOvsugeLIFWKnwD63FFfr4DUCwrGO90l1x12tgtB4H0cazn9oCEhLygXaO3NilBj60DTywuLEUldsxGKYpZQcA6W3hHUl42ueK+KacSFlVWA57ibeB5W3ab+mlZJgeXJE9ijJyu/2qp78ggfK0+hRyauKRd0Wj5MOu+lcbwh+pZNgKQEpDtIVzD91PLKndFeRJZJApDO4iueAcKlsmozIbjUVLkPMQ+wLof3RfpZwpK/25SIkDSOMPb4LSuv0Ej5Gk84LEgAFZXPhHd/eLKVXO46eg8WR8jKDFe2KBYJNY3Ru0M1k0VFCxu3+MEuV9qTEmynvv43lcrxv+Z6O19Ikac2xwxQtWV3uhQmP4Sskyz81YfESvIie4F93zBpITj 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iqZtUwHiAEP6JKB9RAfb9/p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W+fAFN7oNwMIi7NVVhO0m41mtlEZnBduh6Z5nBJajwKm00HcqnbI3heGUm9cdQ6H2+CAcswGaO9Fmh93nKugFq6wnMsRDujIqV6+08AkLhohzcQ+30ywDiWXCw2vd1zwZUpgsfaslvBxp5awYmpB2vjOtUu4Tm5Y/LJnALzz+9m2QEM8y8RvYB8iQptEp56H6yaIW2H5NUmysUSYpJCIlqXmdax0zHPtyneW3TfIYwzdRvZN2jO166IwH54gb8LTZIB7SrTIVMtskOyIh1o+aLJs4KSpBRIqJkTDqqDRILc/r7IISQF9flxHvWhjWNEej6akLP52WhHTPn3/Fgnz/KiqLRvrB2H7HyElwRvDDQjvXWXM/JazCszAVPZ3G/qpioaauNj2aqIuda79zClTr4oBKVN3EIEHGXU/pYgdiv46LmA12oGTRu7gZG2mzHpHIC1XWhO/MZhpTNPuI2l46Zudy2pAApkB4nPoKE9DfHIL3eDlgaGv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 072bb02a-3076-44c4-7e08-08dd5077022e X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:22.5773 (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: k1Hrepb7kn+S1SEmgERTlDw9ZIXeHwTv6VQroPpQS4eHNeabsOYqyBVUWn9dEgJv X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 folio_split() splits a large folio in the same way as buddy allocator splits a large free page for allocation. The purpose is to minimize the number of folios after the split. For example, if user wants to free the 3rd subpage in a order-9 folio, folio_split() will split the order-9 folio as: O-0, O-0, O-0, O-0, O-2, O-3, O-4, O-5, O-6, O-7, O-8 if it is anon, since anon folio does not support order-1 yet. ----------------------------------------------------------------- | | | | | | | | | |O-0|O-0|O-0|O-0| O-2 |...| O-7 | O-8 | | | | | | | | | | ----------------------------------------------------------------- O-1, O-0, O-0, O-2, O-3, O-4, O-5, O-6, O-7, O-9 if it is pagecache --------------------------------------------------------------- | | | | | | | | | O-1 |O-0|O-0| O-2 |...| O-7 | O-8 | | | | | | | | | --------------------------------------------------------------- It generates fewer folios (i.e., 11 or 10) than existing page split approach, which splits the order-9 to 512 order-0 folios. It also reduces the number of new xa_node needed during a pagecache folio split from 8 to 1, potentially decreasing the folio split failure rate due to memory constraints. folio_split() and existing split_huge_page_to_list_to_order() share the folio unmapping and remapping code in __folio_split() and the common backend split code in __split_unmapped_folio() using uniform_split variable to distinguish their operations. uniform_split_supported() and non_uniform_split_supported() are added to factor out check code and will be used outside __folio_split() in the following commit. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- mm/huge_memory.c | 160 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 123 insertions(+), 37 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 94ed85aadecd..2880dd010bff 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3730,12 +3730,85 @@ 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 warns) +{ + if (folio_test_anon(folio)) { + /* order-1 is not supported for anonymous THP. */ + VM_WARN_ONCE(warns && new_order == 1, + "Cannot split to order-1 folio"); + return new_order != 1; + } else if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && + !mapping_large_folio_support(folio->mapping)) { + /* + * No split if the file system does not support large folio. + * Note that we might still have THPs in such mappings due to + * CONFIG_READ_ONLY_THP_FOR_FS. But in that case, the mapping + * does not actually support large folios properly. + */ + VM_WARN_ONCE(warns, + "Cannot split file folio to non-0 order"); + return false; + } + + /* Only swapping a whole PMD-mapped folio is supported */ + if (folio_test_swapcache(folio)) { + VM_WARN_ONCE(warns, + "Cannot split swapcache folio to non-0 order"); + return false; + } + + return true; +} + +/* See comments in non_uniform_split_supported() */ +static bool uniform_split_supported(struct folio *folio, unsigned int new_order, + bool warns) +{ + if (folio_test_anon(folio)) { + VM_WARN_ONCE(warns && new_order == 1, + "Cannot split to order-1 folio"); + return new_order != 1; + } else if (new_order) { + if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && + !mapping_large_folio_support(folio->mapping)) { + VM_WARN_ONCE(warns, + "Cannot split file folio to non-0 order"); + return false; + } + } + + if (new_order && folio_test_swapcache(folio)) { + VM_WARN_ONCE(warns, + "Cannot split swapcache folio to non-0 order"); + return false; + } + + return true; +} + +/* + * __folio_split: split a folio at @split_at to a @new_order folio + * @folio: folio to split + * @new_order: the order of the new folio + * @split_at: a page within the new folio + * @lock_at: a page within @folio to be left locked to caller + * @list: after-split folios will be put on it if non NULL + * @uniform_split: perform uniform split or not (non-uniform split) + * + * It calls __split_unmapped_folio() to perform uniform and non-uniform split. + * It is in charge of checking whether the split is supported or not and + * preparing @folio for __split_unmapped_folio(). + * + * return: 0: successful, <0 failed (if -ENOMEM is returned, @folio might be + * split but not to @new_order, the caller needs to check) + */ static int __folio_split(struct folio *folio, unsigned int new_order, - struct page *page, struct list_head *list) + struct page *split_at, struct page *lock_at, + struct list_head *list, bool uniform_split) { struct deferred_split *ds_queue = get_deferred_split_queue(folio); - /* reset xarray order to new order after split */ - XA_STATE_ORDER(xas, &folio->mapping->i_pages, folio->index, new_order); + XA_STATE(xas, &folio->mapping->i_pages, folio->index); bool is_anon = folio_test_anon(folio); struct address_space *mapping = NULL; struct anon_vma *anon_vma = NULL; @@ -3747,32 +3820,17 @@ static int __folio_split(struct folio *folio, unsigned int new_order, VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); + if (folio != page_folio(split_at) || folio != page_folio(lock_at)) + return -EINVAL; + if (new_order >= folio_order(folio)) return -EINVAL; - if (is_anon) { - /* order-1 is not supported for anonymous THP. */ - if (new_order == 1) { - VM_WARN_ONCE(1, "Cannot split to order-1 folio"); - return -EINVAL; - } - } else if (new_order) { - /* - * No split if the file system does not support large folio. - * Note that we might still have THPs in such mappings due to - * CONFIG_READ_ONLY_THP_FOR_FS. But in that case, the mapping - * does not actually support large folios properly. - */ - if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && - !mapping_large_folio_support(folio->mapping)) { - VM_WARN_ONCE(1, - "Cannot split file folio to non-0 order"); - return -EINVAL; - } - } + if (uniform_split && !uniform_split_supported(folio, new_order, true)) + return -EINVAL; - /* Only swapping a whole PMD-mapped folio is supported */ - if (folio_test_swapcache(folio) && new_order) + if (!uniform_split && + !non_uniform_split_supported(folio, new_order, true)) return -EINVAL; is_hzp = is_huge_zero_folio(folio); @@ -3829,10 +3887,13 @@ static int __folio_split(struct folio *folio, unsigned int new_order, goto out; } - xas_split_alloc(&xas, folio, folio_order(folio), gfp); - if (xas_error(&xas)) { - ret = xas_error(&xas); - goto out; + if (uniform_split) { + xas_set_order(&xas, folio->index, new_order); + xas_split_alloc(&xas, folio, folio_order(folio), gfp); + if (xas_error(&xas)) { + ret = xas_error(&xas); + goto out; + } } anon_vma = NULL; @@ -3897,7 +3958,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order, if (mapping) { int nr = folio_nr_pages(folio); - xas_split(&xas, folio, folio_order(folio)); if (folio_test_pmd_mappable(folio) && new_order < HPAGE_PMD_ORDER) { if (folio_test_swapbacked(folio)) { @@ -3911,12 +3971,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order, } } - if (is_anon) { - mod_mthp_stat(order, MTHP_STAT_NR_ANON, -1); - mod_mthp_stat(new_order, MTHP_STAT_NR_ANON, 1 << (order - new_order)); - } - __split_huge_page(page, list, end, new_order); - ret = 0; + ret = __split_unmapped_folio(folio, new_order, + split_at, lock_at, list, end, &xas, mapping, + uniform_split); } else { spin_unlock(&ds_queue->split_queue_lock); fail: @@ -3994,7 +4051,36 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, { struct folio *folio = page_folio(page); - return __folio_split(folio, new_order, page, list); + return __folio_split(folio, new_order, &folio->page, page, list, true); +} + +/* + * folio_split: split a folio at @split_at to a @new_order folio + * @folio: folio to split + * @new_order: the order of the new folio + * @split_at: a page within the new folio + * + * return: 0: successful, <0 failed (if -ENOMEM is returned, @folio might be + * split but not to @new_order, the caller needs to check) + * + * It has the same prerequisites and returns as + * split_huge_page_to_list_to_order(). + * + * Split a folio at @split_at to a new_order folio, leave the + * remaining subpages of the original folio as large as possible. For example, + * in the case of splitting an order-9 folio at its third order-3 subpages to + * an order-3 folio, there are 2^(9-3)=64 order-3 subpages in the order-9 folio. + * After the split, there will be a group of folios with different orders and + * the new folio containing @split_at is marked in bracket: + * [order-4, {order-3}, order-3, order-5, order-6, order-7, order-8]. + * + * After split, folio is left locked for caller. + */ +static int folio_split(struct folio *folio, unsigned int new_order, + struct page *split_at, struct list_head *list) +{ + return __folio_split(folio, new_order, split_at, &folio->page, list, + false); } int min_order_for_split(struct folio *folio) From patchwork Tue Feb 18 23:50:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866593 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 55CED1DE8A2; Tue, 18 Feb 2025 23:50:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922634; cv=fail; b=OLPVHkRg5NyNwcMnXnuTGs5T3gKuTI/avcrlLeYtZ04txAIRyreIpkGYijydiqUpqARdxVRf3uqk0suJ103Cu4ML5zkOgDo0UxtfWXT/fLGaRZf46Aph+u+NxM7b537AZ0875SBw+7FlZoi/VjhKFU4eisd39G1gQ5aa69y/foU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922634; c=relaxed/simple; bh=aXQg98Yg7uCl7MwqDC6yhbMrj0DaXag6QSmnECCARB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=XNcn7BtQwaJIAMAZUop7Xg571H+DSpLn8KxkjaPcr5XwZq1NKQYW7pcBxWy4VAsV4o+cMQZXSKFT72wRRKLqBMflHw1T3OcZLr0vSysO4Bi7fSoBF8dGrMA5iAgXOfjoLP/VK/quY0VmVgcHcRO8GBEOPXFiyQC5WDGc1u1LiB4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=kXd13dd8; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="kXd13dd8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Zmln+RSMiJ97Qzi0JP+VlFeBLdhivcRK1LbhtCdOSkyp4qmH0YfK0kqdMshQLpXB81Mqo4SbsL4jQ7jI/+/kRt/INsiGVPbECmEYKc76N+tU+ZIQU/HnnYS50XlDqFfHlMYN15TP3bosD4eJQLV2wHZRjZceA9FIwQm2lT66NS3/KpjRpoz2mRKRTAOykdq/Gs/5eBtPBP5lq4SIg1HSBIZhc9CTZbhtQsCh+kbE9N1vBl/ASHLO5FKypHuKyTMHhr6Fy2kOh9+4mKLesSvwjboXDKnB/Wk7h6wl+8hKPWuL1zL6njLYbJMnEyTppmT/rI/rVGcXitTs6GqDM00wTw== 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=LodQMgwtSalEnTvSN1UxhYrt3Qo5c8mY43vKRMlOiT8=; b=LxDtnwzpqd+0WgWR50DKzujRAIa7JFUndhTd0z1Aaq36ZBFSmDeuWjCzWkDYS6XJtcXe2pnj1+8IV/bBqc2c85YNEB4SnRdm1HNvlNEiqOsIuxo7Px55GnFFCWMLRGcV47Is9f2zhoPu67qowUHNvNw/7qu8eXo4PYNZ4jsvpnxS/9OSvZxttFy47IjPvsliL3SB5APKaKPMm17qfPobwtSV24Qporq4CrUplYynRU/B1kxh1bIgsxGhNlpd+4i3485kAAvhKRA4ywgKwPTtc41vojbvONIgDxO1db1mvAXSJCyOC8fXaE0XMrGeTCKivkN+Tfq3MjXUHoJNXhSvVQ== 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=LodQMgwtSalEnTvSN1UxhYrt3Qo5c8mY43vKRMlOiT8=; b=kXd13dd8PASUxxS861j2Znsg0WDfyX78547DAKythzLbrDg7SfzKI+eFkxMCbWigeej3oGX6X/A3up4iBHXjO8FTnYD71Tk/OSxhkyCrrZHS+a+Ap/mWxE035e670iwusnKuaZp525U545kHaQKyTVRwPXfOTrlWKjgMrrMIkW+MyWWqRCw+9kQVdxKoZGVC27AazdT+PyKDZRmt3t1cte2qbqnjeYpgPxc117gSWVIaYZ0b1yzrkVH6xpRqmAoWkZ4dNQ9XFJRT5Ie/NftcB01wpGXj/t2NORf6NSEF14erQqZQKYrpjkUWRRNeDnit7eRwZiFl+HJ0Plnged9OCg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:23 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:23 +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 v8 5/8] mm/huge_memory: remove the old, unused __split_huge_page() Date: Tue, 18 Feb 2025 18:50:09 -0500 Message-ID: <20250218235012.1542225-6-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: MN2PR22CA0005.namprd22.prod.outlook.com (2603:10b6:208:238::10) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: ac130c2c-49b5-421f-c33c-08dd507702da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: T9AtyS0jtKXF+hivYgE2uAIrY6XGSIdBDhrJoG9mvQg0XOxp1jFkkpCoOqYY5Stu+EdVPesvGy8FR+I0H+AppwBWdZlAG5WP2fr5j52+7B0PBMNiI/4cSh9Jys5SwezGPQkJ06fPrMT4kSJVnLOY15+Cbf0uNQVzXY0Nyg+fEK0ASx8MvzUsRMZdTAB2dm7up53ETcSrdQOP5+eSLkvTliT2yBBtX13ANt1B2MXeebNyIjl4MuoeCr8M8U6ZHwwmG9sVUemtHNYSgsCfwptEc378A9BNatbE0LbR4/5YPrjg1Nl82Z0BwED2vubMUAQrcyZQIwbAnXtDYtfSpFzHYFnbQZdNZC1iJHxkAlQEilzWbwD59KVGwTaSjCkgMBcOeSdA7OdtCPVlNZgpd+Os+ALqx9Tu3j22rbwfpSNjrY673cfWIIfkAAZa1nGW8KU8vurySFkwTiAbyc4U5/Jn0Fv2E4nNSDjvS88NP3dzCTzEHbBnKBZdOMRqY7TuXZH5BSR6GNSea+5KjHXqbudPL5s/Ldt8+ghZVYL9pqvl6xI+yBEOsXskeHslzac7bKBhAZ9wuiAjBzsuKt/DsxAGakfdDyvL/UvRbo6ZO4HFFRU59DlsZygWmGmcTKdVsRy81v+PR4zMixPbppC1PelSRCI8m9oyF80MqExv1EZ7EzcHob6GxUx2VxkwTvi5CuB7PonW7wJFFYBJjeUlr4onADL8LKz0b0y1XQVyYY65wXp19gmfIpX3UJihUsvCUdzSsVXXKCVYpLvwZUSp1EasAIO6EnO2BF847/Z6X9Ms0+ix44SrvH50vh5gg3IHBNqWLM46zjBu901/WqNhMcdWanY3QM3MQdbs4g6lv0HxbMompHhjPt2ddSw/6SNBE41ksf5KhpM3BDWeecYnnTg0RLNKTexd/NOFwXehMSh9UHhCVyP0lbDPZ13L33qFwNrO7bYV0ud7lIF6HpFKiMQoHOFZUTLNzITb9mvizCrV512TsOsUpiL8NvmaSSZA3NGccyrK8eVed5ujZiUrRZVwtNryW7rVeGF9RThFgzNw9/6KTDe/gVaf31d1NFiojwNm3cK5iarj39EDkbt7gL8TJWJ284+Lzi9x9/2evJtxT9vbFaZznv8sWf5aKnEDb65go+aoV9PuDVZAqYn6kNdUZ/ewGr3QfijzZxShFC8oYt41myYnXdQDrou8brg0uSX9yYX9Pd2QMFEyOU7CTGGnzTuXT0uPnls/k2CLLrNteMoKiofozCRZxl6M0+eBGdWVDo2sGhl4SAGc77mSgpgeSKJgB/o52b+flCQ8Q2MaqKE8Q4f4ZfR8aZaOeG/T+IfvHeIExwe/xLp5Rt/ACLFHKdmtnPsksOciju/kCK4ddmT2K1f/DaCtTbKAKOZ7tnRA 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HEquv/rUQDLpwbGocHAmkrpHMbcc11qeWvaGP/tH2tL3CX/PekKBA1APEKQ77qARLcZafPXzKKjZcLh9dVvvyw+STqCpbroL1Ks7JSIDm+UEFvguvD4TBS+Cnr6lrNV9EsUobBWLYX9p6Y/bbnla82Ext7Q2r3CgV+49QW9cXUiWlixwmGSHUZaN9WkCx9cP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ANF/HTeqalDVXasDzLAzUGKDbmqivvJ15B0iKrrXN454I8WW6TcCXbqQ1k9xEm9rV66FE7KeHSM0n2LBikeMH5NpKr+Jq74oRraxUpuYQs5p86stsyKsB+QYuR+ofd+DjXRouISKtsiCqhVKsKOseS9SNb2g0GvKMstLJcAdAKRYk4+kVvC6Caf10nHbF22tZnzN1i5beLAI/FBAMHv200QrJ1/esfVdIKNyzrMbPT21xZdusfN2iTv7xg3ia7v50kJUnyWiAxzEeAffKtiIHTF1r+CRQmJHpaa2KoS2RcvlwvRfO79FEa/oUWz2vh0VwIablfVFjtVMb2c1B3XaXEZWi8OXx X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac130c2c-49b5-421f-c33c-08dd507702da X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:23.6892 (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: HNrnACCPBjFZJXYSKfBQiITUWmNAtRt4RZwPRdJDkSZfDMPnhyr+YIh8H5PUhR5d X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 Now split_huge_page_to_list_to_order() uses the new backend split code in __folio_split_without_mapping(), the old __split_huge_page() and __split_huge_page_tail() can be removed. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- mm/huge_memory.c | 207 ----------------------------------------------- 1 file changed, 207 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 2880dd010bff..ab9fef680a9d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3168,213 +3168,6 @@ static void lru_add_page_tail(struct folio *folio, struct page *tail, } } -static void __split_huge_page_tail(struct folio *folio, int tail, - struct lruvec *lruvec, struct list_head *list, - unsigned int new_order) -{ - struct page *head = &folio->page; - struct page *page_tail = head + tail; - /* - * Careful: new_folio is not a "real" folio before we cleared PageTail. - * Don't pass it around before clear_compound_head(). - */ - struct folio *new_folio = (struct folio *)page_tail; - - VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); - - /* - * Clone page flags before unfreezing refcount. - * - * After successful get_page_unless_zero() might follow flags change, - * for example lock_page() which set PG_waiters. - * - * Note that for mapped sub-pages of an anonymous THP, - * PG_anon_exclusive has been cleared in unmap_folio() and is stored in - * the migration entry instead from where remap_page() will restore it. - * We can still have PG_anon_exclusive set on effectively unmapped and - * unreferenced sub-pages of an anonymous THP: we can simply drop - * PG_anon_exclusive (-> PG_mappedtodisk) for these here. - */ - page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; - page_tail->flags |= (head->flags & - ((1L << PG_referenced) | - (1L << PG_swapbacked) | - (1L << PG_swapcache) | - (1L << PG_mlocked) | - (1L << PG_uptodate) | - (1L << PG_active) | - (1L << PG_workingset) | - (1L << PG_locked) | - (1L << PG_unevictable) | -#ifdef CONFIG_ARCH_USES_PG_ARCH_2 - (1L << PG_arch_2) | -#endif -#ifdef CONFIG_ARCH_USES_PG_ARCH_3 - (1L << PG_arch_3) | -#endif - (1L << PG_dirty) | - LRU_GEN_MASK | LRU_REFS_MASK)); - - /* ->mapping in first and second tail page is replaced by other uses */ - VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING, - page_tail); - new_folio->mapping = folio->mapping; - new_folio->index = folio->index + tail; - - /* - * page->private should not be set in tail pages. Fix up and warn once - * if private is unexpectedly set. - */ - if (unlikely(page_tail->private)) { - VM_WARN_ON_ONCE_PAGE(true, page_tail); - page_tail->private = 0; - } - if (folio_test_swapcache(folio)) - new_folio->swap.val = folio->swap.val + tail; - - /* Page flags must be visible before we make the page non-compound. */ - smp_wmb(); - - /* - * Clear PageTail before unfreezing page refcount. - * - * After successful get_page_unless_zero() might follow put_page() - * which needs correct compound_head(). - */ - clear_compound_head(page_tail); - if (new_order) { - prep_compound_page(page_tail, new_order); - folio_set_large_rmappable(new_folio); - } - - /* Finally unfreeze refcount. Additional reference from page cache. */ - page_ref_unfreeze(page_tail, - 1 + ((!folio_test_anon(folio) || folio_test_swapcache(folio)) ? - folio_nr_pages(new_folio) : 0)); - - if (folio_test_young(folio)) - folio_set_young(new_folio); - if (folio_test_idle(folio)) - folio_set_idle(new_folio); - - folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio)); - - /* - * always add to the tail because some iterators expect new - * pages to show after the currently processed elements - e.g. - * migrate_pages - */ - lru_add_page_tail(folio, page_tail, lruvec, list); -} - -static void __split_huge_page(struct page *page, struct list_head *list, - pgoff_t end, unsigned int new_order) -{ - struct folio *folio = page_folio(page); - struct page *head = &folio->page; - struct lruvec *lruvec; - struct address_space *swap_cache = NULL; - unsigned long offset = 0; - int i, nr_dropped = 0; - unsigned int new_nr = 1 << new_order; - int order = folio_order(folio); - unsigned int nr = 1 << order; - - /* complete memcg works before add pages to LRU */ - split_page_memcg(head, order, new_order); - - if (folio_test_anon(folio) && folio_test_swapcache(folio)) { - offset = swap_cache_index(folio->swap); - swap_cache = swap_address_space(folio->swap); - xa_lock(&swap_cache->i_pages); - } - - /* lock lru list/PageCompound, ref frozen by page_ref_freeze */ - lruvec = folio_lruvec_lock(folio); - - folio_clear_has_hwpoisoned(folio); - - for (i = nr - new_nr; i >= new_nr; i -= new_nr) { - struct folio *tail; - __split_huge_page_tail(folio, i, lruvec, list, new_order); - tail = page_folio(head + i); - /* Some pages can be beyond EOF: drop them from page cache */ - if (tail->index >= end) { - if (shmem_mapping(folio->mapping)) - nr_dropped += new_nr; - else if (folio_test_clear_dirty(tail)) - folio_account_cleaned(tail, - inode_to_wb(folio->mapping->host)); - __filemap_remove_folio(tail, NULL); - folio_put(tail); - } else if (!folio_test_anon(folio)) { - __xa_store(&folio->mapping->i_pages, tail->index, - tail, 0); - } else if (swap_cache) { - __xa_store(&swap_cache->i_pages, offset + i, - tail, 0); - } - } - - if (!new_order) - ClearPageCompound(head); - else { - struct folio *new_folio = (struct folio *)head; - - folio_set_order(new_folio, new_order); - } - unlock_page_lruvec(lruvec); - /* Caller disabled irqs, so they are still disabled here */ - - split_page_owner(head, order, new_order); - pgalloc_tag_split(folio, order, new_order); - - /* See comment in __split_huge_page_tail() */ - if (folio_test_anon(folio)) { - /* Additional pin to swap cache */ - if (folio_test_swapcache(folio)) { - folio_ref_add(folio, 1 + new_nr); - xa_unlock(&swap_cache->i_pages); - } else { - folio_ref_inc(folio); - } - } else { - /* Additional pin to page cache */ - folio_ref_add(folio, 1 + new_nr); - xa_unlock(&folio->mapping->i_pages); - } - local_irq_enable(); - - if (nr_dropped) - shmem_uncharge(folio->mapping->host, nr_dropped); - remap_page(folio, nr, PageAnon(head) ? RMP_USE_SHARED_ZEROPAGE : 0); - - /* - * set page to its compound_head when split to non order-0 pages, so - * we can skip unlocking it below, since PG_locked is transferred to - * the compound_head of the page and the caller will unlock it. - */ - if (new_order) - page = compound_head(page); - - for (i = 0; i < nr; i += new_nr) { - struct page *subpage = head + i; - struct folio *new_folio = page_folio(subpage); - if (subpage == page) - continue; - folio_unlock(new_folio); - - /* - * Subpages may be freed if there wasn't any mapping - * like if add_to_swap() is running on a lru page that - * had its mapping zapped. And freeing these pages - * requires taking the lru_lock so we do the put_page - * of the tail pages after the split is complete. - */ - free_page_and_swap_cache(subpage); - } -} - /* Racy check whether the huge page can be split */ bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins) { From patchwork Tue Feb 18 23:50:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866290 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 7D7D21DED55; Tue, 18 Feb 2025 23:50:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922636; cv=fail; b=PdGkE/HZwJvmBtSHHKk8mCheXiuRckgzUya7gXZlqbhnB2LYVvDK8uvtqpS2hIA1PvPu3JNwIUZJAaajgNxRTXVlOCI1plEFPcSsPdanoj7oS3yVQUjxBKi9TotgGAg3efMsOiIBr8fIBLLqGZKD240Yw1fwGA+h2E8mjiVpZvQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922636; c=relaxed/simple; bh=tCCUvjFOphCgbkzlWhbRZMBbY88DhPVHyfA76difAO4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DlcWsWLeshygp6gBF8KfWbivKudsbkF6SRX9ZeCZB9snpN6CuY75BW5dj+bHaWv78bch0OTNvcr0d7lwbgbUolHpfvgGPuqPMqq/A1mErxVuSwRYEE1ri0qDajShsHVHcwbp520kqEsjjz0Qzb5LdMNzPwVWmTa9yuJc4bLWVEY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=sAfMxFOv; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="sAfMxFOv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JemjTtK8+ecaEKhHtg5Ga9rD+lJxRkZaODmxq91+6LNnDSVb66SPF9Po//tfy8HUJBjlWYLuwJpvP+HQPvsGAHXi8r+wWlHgX2oPitpRl9MZwjdb5/HCChmjH7nCVyGQ6SEoGK5ya344D6iQ89gGnTm7GTzcH0Sgig5UZdGZ5KDKTmMfcj8R+H6YUc5tMOtLKsT4Ukvhj1icshzygCQ2whDA2FAFSeQ6SuH48Tv6Pqefj8zEc/nI0hiOQyXEiNzHJ3/CgEwnqNEEaUjSVxPsTZ2BWh9tkPoDrgEwnc6BOMMgbL/563wIV6wkCjKHpz+lubYfmJiTOoZsr6LMCFTWcg== 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=YxOY1Bq1sDkRJtGmo7nmA6wB/fQXbKL1h6SHGfjliHA=; b=Gwe2k5sf4hzn2vCbLrci5wZlXNuFge7F5Vati68f3+7F+n6l6YEX6ZkYWkiSMR72aMO2I54QqDUeceK4/E/Jf1iAfcNZY70cPjUtmV7s+6OLy0lqLmeekel3BDxlZ1fw70a8ERin3xjL8FPMyLcrw9NyIF2WKMhEIkp9kSC2LdOqYU77CSnkE8VDcC9uCDzIj2UQcLPk32TqTGtykaE4FxtnWWJIXjrE4jbSMVJBMpdMRKaJr+ZeYq6onyreAm1bpMyS1I+8iESGxcIWckumoN6GvJlbknzNIgdiSddi27I5ON4Ul6KoDdeJj7ZWrZ3aOGVcgWgTwtgR/3+zOKIHAw== 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=YxOY1Bq1sDkRJtGmo7nmA6wB/fQXbKL1h6SHGfjliHA=; b=sAfMxFOvdO18u2B6ivg5oyASb0NaeBsn3Q2jC+UyBE40mOXghHuquRpD38ZwT8Cz1wiIheWRl8xL+4VFnq+jLlUzOIppBElO6Fl4I79xX5oPvoxhFPCw3c3ROg9LhctsnH924adWUL9rvw5KJeOHF14tdT8XUhyB4hqnJ8FYZ35sIYudUHWmyUBWuhe3KAojrTLvOA2TRSx/lGxOJqMlpUKJ3+Q8FpwVQjgykjGfPa7uO/YPZBAk9kCol2xRw7pTzYftLPc6mfiENR8nUAESL3mCZQyi22DoRFE2UUKU6dTTW15SJVKE3sNc+GY25Lz4ZOFmbiUgdhrxa5OJItwcrw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:25 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:25 +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 v8 6/8] mm/huge_memory: add folio_split() to debugfs testing interface Date: Tue, 18 Feb 2025 18:50:10 -0500 Message-ID: <20250218235012.1542225-7-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: MN2PR22CA0017.namprd22.prod.outlook.com (2603:10b6:208:238::22) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: efbc0d20-75b3-4ad8-fa69-08dd507703d4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: EbxpDgnqm4p46lF4c57E5vvG+LRogsg+0Tcl4REJ6VvhR08/cF82S3cVpYf+gZfBbZ9RPdbw/flYVQlqow2TgHEJOOjJiLJci/RrAYozQy5FkpY6uRm2b8QETm+MZjV79SvpR2p0D/tQ+sOeuFtOVBAZUPJMZhug5G/pNSyXs6bkLbBggbMkgh6kMRvc8tHyIo2nMTsvpERRnUmicMqqDpA7i9GZs5zt93RAj7VwzMzf16ywqOgNj4f4nh9dRTX8WQ+QsVj2b34R1GZYadQWfBnqCXOsD1UyeDPs/kA9Xv0M5g/RvHis8RQ6BDtHJK4OzgWgRJgE04+t1N8M5DmIfv/lRo0KVEbi1R5Z8g433mMzJa1DMK3rp7Q/Gc0+pChJbVAfUizvah1gV4HbgR0WLBveGeBXXo32emJJx+hwGeYDleuP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com; PTR:; CAT:NONE; SFS:(13230040)(376014)(7416014)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jWCDhCAlKk0UhhQqUXFkAHF75S/MCtA/xcJGRJqvmX0dWClSJD4r1REdoUNXw3jweYmMRn813dKdO71NFZRo9Rn+IAXvQMqXcz/7kIALFIzvBb8JngXRua4wS9c2L+JZnWa8udL19np4FQRno988T3FseDihlZ3d0guKekFd2wTK4l5dV7uPVwTmCswdJYUlxhmkQfsuDDRFs62n6AcMUVqUZx9zvq4tyydCeFcc8xRRZgEVrhZId10/nyqpgo7eRU9TObw78PgzQ1syEm5aHia8GKPG9MBlZ/yheSWO5v6aT/Xx2Ta5b8+P3dy9/3sX6EraPh3/nE2VwnSaSfUOW53fJs6uooDOTKolMM4+U6A8pVagTHdtPsqFjhrh3sR2cMBAHqZH2XENTGTAc9CTHaoxMSqVUEH1DbadqDpCPEP8Mz8d4Q9sU5cU9aACTqg1ClatjmxCHHFdsNEvrB/1BrOw4nfwQqkMaxSCq7Jy7OuzMnCXuLujOfCOoyk4QBndaOV0XnoE5HvDncF3qE1+D1m7B84Sq8aTpP1gVmfOzf8NG2k+WpYbyWZjICRF1++2VCQ0Yo7WIP9Rn/zCiLN0pDU4FmDmb4S1VovFud5nF/zZeKVsZiSaAV8Urz4CVDvI9UzPTaKvdf6ShI19ufmzWI2UJ3PJ22PLXfw9knfNqsY0zS6HdXuuhU5mZmMeIOM7JxePnK8Ad0AMQ5oelXqgWEx43fFDVDmp4XjFTAF/sgFSYwvPHN190uFtoX22KDIa+lW9hRnuwj0nCIeWKOC4lrLJx4CbsG2UzKaHZCOHOlv5IGtgyIx33SdSw/w+HQz/eh4Awjf/zVXakqBvftON+dsCTlMdG++wJXiZq4UFiCIqUAFFIhjKmTAswEDYw+NdMHRJ/24dFQCxYnHy0H13i+FN4/szlHQhf5z41F/9kUn7bgnSmSpynmBwvH2pfmT8gQHKnxtMNDky9+1wJE1QxkyiuZDgH0EBgfGtKk6cqZb3akZpzv7+ey0y2XZHjdE035QXkUg5dL1qh+XOB3Gpoq0UEhpBBf+uIi5t4QRtuKEwIqHXcw6KJkHyrsNTkdzRKSQQQ6dBWmAhJYFs4bWKmuLvgQA1AL513LkqJx6BGXdE1aM7ju0Usxdxa2zuH+meJ/dwi7ZGrxpAYEvNvTr3XSq/G3lIl9aDzSTd+pNLS4vDR++AAsqBsGb/idNgdz/WL3S0fhBzm0J5oDDJ2t/akFgyCv5bkoim0Q5td2gZS5/ZkLXzC9EAI9LyxuCj4saiLw7/BgeGwcVbkWMusCA7684O49qOiuUwYvZTCb0acK+1UpJanTc4vNWLFH5NDFuujbbY8yjnbgTESvux/JbnbQX6VOLH/s3ZqPZOhkrXtO2aV7LyzbfHMZ1c2hsjDST4dpu9/tEetZlHMBx7RjuQR4AAnL2Rp2I+ccKeR5o8wChpsMf3z/dzsHBY5MtDU3oG6+o09EZrAWdt+n3xYsNoIFC2X0feb6RK2v+PmyiHje2HJNPNaAzg+ePIXkCV00uhG/Im64PPJkbbeakbTqKqtyFKGe7r4neLv+c+xUOU1elnQsExa3CdwJCfT3YWLH5d X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: efbc0d20-75b3-4ad8-fa69-08dd507703d4 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:25.3347 (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: 0XkjC/4VOgbo+C2qsXpDDzbEATgVjoG0RteJxzQn5yuWlE2J5h2sVT20zQXTFvq5 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 This allows to test folio_split() by specifying an additional in folio page offset parameter to split_huge_page debugfs interface. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- mm/huge_memory.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ab9fef680a9d..9c257c4006f5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4195,7 +4195,8 @@ static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma) } static int split_huge_pages_pid(int pid, unsigned long vaddr_start, - unsigned long vaddr_end, unsigned int new_order) + unsigned long vaddr_end, unsigned int new_order, + long in_folio_offset) { int ret = 0; struct task_struct *task; @@ -4279,8 +4280,16 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, if (!folio_test_anon(folio) && folio->mapping != mapping) goto unlock; - if (!split_folio_to_order(folio, target_order)) - split++; + if (in_folio_offset < 0 || + in_folio_offset >= folio_nr_pages(folio)) { + if (!split_folio_to_order(folio, target_order)) + split++; + } else { + struct page *split_at = folio_page(folio, + in_folio_offset); + if (!folio_split(folio, target_order, split_at, NULL)) + split++; + } unlock: @@ -4303,7 +4312,8 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, } static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, - pgoff_t off_end, unsigned int new_order) + pgoff_t off_end, unsigned int new_order, + long in_folio_offset) { struct filename *file; struct file *candidate; @@ -4352,8 +4362,15 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, if (folio->mapping != mapping) goto unlock; - if (!split_folio_to_order(folio, target_order)) - split++; + if (in_folio_offset < 0 || in_folio_offset >= nr_pages) { + if (!split_folio_to_order(folio, target_order)) + split++; + } else { + struct page *split_at = folio_page(folio, + in_folio_offset); + if (!folio_split(folio, target_order, split_at, NULL)) + split++; + } unlock: folio_unlock(folio); @@ -4386,6 +4403,7 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, int pid; unsigned long vaddr_start, vaddr_end; unsigned int new_order = 0; + long in_folio_offset = -1; ret = mutex_lock_interruptible(&split_debug_mutex); if (ret) @@ -4414,30 +4432,33 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, goto out; } - ret = sscanf(tok_buf, "0x%lx,0x%lx,%d", &off_start, - &off_end, &new_order); - if (ret != 2 && ret != 3) { + ret = sscanf(tok_buf, "0x%lx,0x%lx,%d,%ld", &off_start, &off_end, + &new_order, &in_folio_offset); + if (ret != 2 && ret != 3 && ret != 4) { ret = -EINVAL; goto out; } - ret = split_huge_pages_in_file(file_path, off_start, off_end, new_order); + ret = split_huge_pages_in_file(file_path, off_start, off_end, + new_order, in_folio_offset); if (!ret) ret = input_len; goto out; } - ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d", &pid, &vaddr_start, &vaddr_end, &new_order); + ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d,%ld", &pid, &vaddr_start, + &vaddr_end, &new_order, &in_folio_offset); if (ret == 1 && pid == 1) { split_huge_pages_all(); ret = strlen(input_buf); goto out; - } else if (ret != 3 && ret != 4) { + } else if (ret != 3 && ret != 4 && ret != 5) { ret = -EINVAL; goto out; } - ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order); + ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order, + in_folio_offset); if (!ret) ret = strlen(input_buf); out: From patchwork Tue Feb 18 23:50:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 866592 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 86A591DED59; Tue, 18 Feb 2025 23:50:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922638; cv=fail; b=TqMXqExglr5MASb14/FQ9cF0GLLc37foD/p4ViGf0ZqeigvafzB44vKDbckPay040mQ7BLz95S0eo4WIsFgL0j546GG0tVQ+joZw3sfol9tQiM+ruuC8tRfpqSSCMbliMZiutVIQLb1v81LQ+64ts3Lx0eJLGtyRF/ZryC+mJPM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922638; c=relaxed/simple; bh=vD7QPAsdzZg918nfIVKVdnKgkFzi1UFKGV76SQQBZMw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=I26BIhSqM1fICjSNtj14Gm3ekT6SCIRwX6zkzpKfr5eTkIX86kEIsLPi3NsGrPhNJaHz44FYydhyLZuSw1vHzPm+WXKOWeVyeJ7UJlQAzoJiP2sv5y8NsDWC9WGwbjs2XWhgxZQ8hbd0Zj6FYBajoieCEEC91FRpOTNrUCXBgcM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Ha0PQVyf; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Ha0PQVyf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DgoxzcAhdHtOzXrGDSO0wG7zcc+jtytcdx8ACvfBAG3REmczfwnq1IxHRt6sb238Tg9l/1xoHfIXW2BsxEW+psDZHZQpaV0735XHiGAdA96IVgd548vW5/8RGc1imhXXTchraGpHXaePSrtE7WTCJnULi1lzk5b+Vh5w/ZCqKzH6BjSKD9tMpRzB24+nPFJ7a5YhHV/XECpiIMRRtpyP2RuIQbR2ptVJ9E2o13Fib6sgvq6K4s1vqHV9K1XZ8DQhDJaguimWx8loc8CeP6VQuo0dACW9P65clE8tO1QooqTlWNyYZQSO1nsZphVSbWYetK+ZS0I799DObnOZG9SFvg== 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=9PJ7zBIViLKphcXY2b8VdDiqcqFp+9EBhh42TsYKGoA=; b=C2Oihr+M2LqagNjXqrgjOiq+4V3jYYtF2bbtaK+Pw9fJ5LMkP9SBs4qC+EbxH+v/Uw+lpKGfsMLXVcVgeqUwZ4ppcrhgRZqcxxltRU1R00Z7e7NcSER78WPi9pgaHSZtfb9yXo+gFyXcDbkpBGftjenFORycqbCCXg8+7FRpUA//BE+go6qbJKSgJhY1X7aiikPQ5vK0H6L57Z1cD4xwmCEJcwtYH+gYfqaofYILy3tYJwzJNWOsdkgWBYVcg+2UMmt9EhJmUF92mWsq8LIW7nRVx9liZYYGW2R2SyxIjnIjyWpW4fv4y7/ojwFCTlRZmYduVN0DmJMEQUit+vsrvg== 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=9PJ7zBIViLKphcXY2b8VdDiqcqFp+9EBhh42TsYKGoA=; b=Ha0PQVyfv69SJcF0U4RIvgdLtJOpcCEpIFtMmTwTNcpTPcrnV1PEzCpjiiwumvxSpX1P3ptYGXm5D0FXv3TNoz6zHO3w1VxWrw3cCWhUg3Ac2UrDFOo+B9pMAo6UA5Ty0W4R/HftVwEAhNuhMRqhOiilqh/9YXQjALIqU0e8gra1qragmbLpWmAbu0XoI5xc4jRTD5jOCWkFLHinzQfcDZN8JA9CHWvzbihpbujnOyu5cOdJi858nxUdjm2V73lRN0+kIXANDpT6gHHypPg0YDvluKacHptDCxJodtCIbLgVZS0yynJ5K7BmG7m7J5RgoDfcB6fW7NWhCWiNK84DeA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:27 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:26 +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 v8 7/8] mm/truncate: use buddy allocator like folio split for truncate operation Date: Tue, 18 Feb 2025 18:50:11 -0500 Message-ID: <20250218235012.1542225-8-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: MN2PR22CA0023.namprd22.prod.outlook.com (2603:10b6:208:238::28) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: 465ef961-511d-49e5-15ee-08dd507704a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: XZGqm1WUYQ02z3zTjBS7lTgg04EZYXo3E9eMgjvyn4VpaEuLWS3yYl2zYpUifmHrLMuaOxxlXuj2ePxziAUssKEROcMx4NDEC8/GT4C2X5ScQZtNYxrjYQeqGk3r6Ocm9E4buOo9zXcB/qCfAJ3pR5FNo3/L4GQ2cSRSRoDsig01AlXZCWSK2XP1GJy5v2TWjbv/Nt7WUh1ker5SXawqTKHbUQAx+z2YVZw7skFgE9Bm1ryG1SCpRu9rs/SxszOX2T6aI+KZM/rwU3S43boRIxFR6zyjY4VVHiW07sZMMzIgQFKriozJM6a3/JuJJ7UB5crQQA3n4aEWcdD4nAmJFJqdj19bjnoBsLrojGf6FA2MA+Er8A/0zCi+5NMMGGJyx+OPVWNKQB3wnGgwb+7er5fKk7WsH+JIPSwudbc0E6ZCPiK/pADRNvBBItl/JBaX81IwFmF9APbWRJHQfboYavx0WM0yr0ICd+BxZt09VehaOhX4ynLHnnn4KZfBuXUbi6SskK9F3cTZ8T9mzOy3gript2B5qpWpjuFtwpHfmg+ltLxUe+aAwrW7Yf6XxhiohYLadAaYEYzRblcKoEojS5fBHzsnmahTG1uoHBBAPlTtJv04MImLoZOB5GAjWbG6dKRB/C6z8+G+ak0CnDHXgyWAmaHta2vt9nWS5FFUF5uDwK75KU7qyoiVDlsUnDEVx3jzgl6Xp3qB8W7RCI0iH39bG2eEcUTUMT3fIJWj4SmdwOBLGb3qncGLeaqwcSgoXZAQ4dG5xBt8M3CJuYiKArNbNsms77CiPRH6JMEfTB8crNOJvxXrW3FzQyHU7Tmo+i0LlF5Zpe7sA0T6GkevU9irKI0XxzDs2WYS26q8FZtY71BGZeOiiBZsaGm4WN+CB/Z+UwdZhZ/nBzmw+6oxAj8yIVAnjPUZNYfHWdcr6XyaKejD6EFiVC9J5fhnGqcNWIE4rAbgrCRUTMyxuJzqfz0JcuzLSBfuoMePOgmaFBrYpZzQUF9Ltsk/B1QxZ25oDhllBaen1DXIJkKE8QVCn4/6inIk37Gr+KwFIr8deKbx0p/YHhiX8BN3OQVOuOcIfxEAqpeCX8Dx8Wo7c4MwWcoiLHE24tMn2v7rHwSNoj6FuvxteE7QG3VtMVYKipRT/IorXwXKDGCdw5kOCkkRcQoDRYR88aNDlYiQOT6WgoZHqW9Fghb+mWAgaIaKxFTgRGiHe5BvZqrDxS835tFeBzhuMa01UN+P4S35e84UtGO0LUAojuA+1pauC3PH5343lOJ9f5SzhneoW4pIjM58geAbHOS4AYlkopHHohMyhk288lMaeyLJIsbCyszIZMeI63GjE0KjCmv7nHy42IJc1PqbntiW9lZzo4Gtf/xr0hPBZTvnRFM3oRwQP/6H2QuK 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nZ/X/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+WhdA7c7FWlRiY8lzo0RTawYHCtXDjPkvvkZwn0+5ZN41R8ZqThKpnjmSHPQl0V3bHP5gguo0/kdLHEmqHZg5mdEmZbQayAe1D0ZrMvwTHAUFwCUneus4SXVlRmDkAW11F4S/Yi9TCIipZtQ6hTGXZOg4YowlwA/xhT3MjEvscy8wgVCHyerOU7aMwGHyzgCZLskzxd4KipYevDJklEnnnaa9NVhgsm6bJ64ZbYZhpzSCW6F6lYcDYOw98885Oa6HnhXk7n6cRWW9zqeVy/ShmXlysqkHvXkQkzJOwxq+phmNGE/a/Da+b1Gdc7OkeUTP3KkE8S7Kpc6aNmsjWN2BzS9IgKgWaERxJ6zWXFQvDbAfjuWv07Pg/+vLrr4G3D0cjCd7fs7R5l4MohPwjgXjF+I/at6AVAneVGx0TTVh8vTfHHI3Nyecvc062VhB+GeM2vY/gdi5UfLC/ukO3LumWI5kGMRlsKXADgkfM7OoWdwyIXH8KiOqqt+BsYMQldqxzGpenYZjGgiAOTgTOh X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 465ef961-511d-49e5-15ee-08dd507704a8 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:26.7105 (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: suRP11NNToaNr1A0gWcA0uRZy5sC4hGgMV4qozEZ74qnglBe5VqjMfHH+Xg8vqTC X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 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. try_folio_split() is introduced to use folio_split() if supported and fall back to uniform split otherwise. 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 Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- include/linux/huge_mm.h | 36 ++++++++++++++++++++++++++++++++++++ mm/huge_memory.c | 6 +++--- mm/truncate.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index e1bea54820ff..2bd181142b96 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -341,6 +341,36 @@ 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); +/* + * try_folio_split - try to split a @folio at @page using non uniform split. + * @folio: folio to be split + * @page: split to order-0 at the given page + * @list: store the after-split folios + * + * Try to split a @folio at @page using non uniform split to order-0, if + * non uniform split is not supported, fall back to uniform split. + * + * Return: 0: split is successful, otherwise split failed. + */ +static inline int try_folio_split(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_huge_page_to_list_to_order(&folio->page, list, + 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 +563,12 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lis return 0; } +static inline int try_folio_split(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 9c257c4006f5..e98d322c3ac9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3523,7 +3523,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) { if (folio_test_anon(folio)) { @@ -3555,7 +3555,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)) { @@ -3869,7 +3869,7 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, * * After split, folio is left locked for caller. */ -static int folio_split(struct folio *folio, unsigned int new_order, +int folio_split(struct folio *folio, unsigned int new_order, struct page *split_at, struct list_head *list) { return __folio_split(folio, new_order, split_at, &folio->page, list, diff --git a/mm/truncate.c b/mm/truncate.c index e2e115adfbc5..09948178f2f4 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 (!try_folio_split(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 */ + try_folio_split(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); From patchwork Tue Feb 18 23:50: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: 866289 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2064.outbound.protection.outlook.com [40.107.237.64]) (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 9ED241DE4C4; Tue, 18 Feb 2025 23:50:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922640; cv=fail; b=CrKnwaoc8KX4J6GoLzmSqZN+y6IV8AMQCMy1jBymLEtMzp8jf1wCL34pRqnoVM891Lq6R7928E+0/9H280Pio27Z0WLgkscrYVIwtNtiIPUwq1kxqMOI7Ct71+bitUYQYL+L538km4vgn/27Nv4GVVTfO8qLO51gPE6M+xFVtlw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739922640; c=relaxed/simple; bh=ufWe9ZEWcKo16BeR3J089CNd6WeiCcjcex1zA+tFkno=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PqxOoJYJIB63XlL1u5YzOLmAUYXtCoHJjfX5IpkLBNd2eP7tYRF+W+iUs6Gkc+aK5l5uScnEOR9QbnHwEyNk/whlJF47VLIclfXaQCNQTtEoLgM4qEkjBZQQblkpw/rb/FdQpdJ04qTNryEhT4LjPF+hGUDnvcj06akL0YH8HfM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=XSLbfr+2; arc=fail smtp.client-ip=40.107.237.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="XSLbfr+2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AftDVJMWB4+8YgUHzs6wm8VtRoR3gX+/bcKBFOvYhYucSDjH5H76lQj3aDJ2GbyKVCrzNBZIHelUkgjW1fMS7diD0RvK3fT5XXABQQiKI6qhnPtuEM0OOBGLrJU/LlH/HtyNWJGZHtu6q8nEzEnrX7w4AUp6RK+HGP+2aGh8PR5CyAGdy06USTgmDY3Js7gkBYgQ99l1hFaHZMl2jg/QpA5P0CeAAKqtZd+fOfsYDwxW4QXBRtfMzm+s20YIbnZzFrx5CNDQiNLt76I/WOlf2s5b9m0kRhMYqQxJlLSBE894cWOA2IXVGhIyHW21eE0ECJuZroUIAYD0HqrtIA7CgA== 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=7Do02JI1EZFcQWrSMpFxurM4sFuf/GMUE6etfB8ub98=; b=Y628s7JQzSl1FpUlY5+DDyt1on0/QbmcAhSGKC7BTn5O3UBn3iwxywMZkiA/HFMuWAHAbqmBc/hvJ/PZnocpISmftftwQIXCSsipXN/4ZAEs2yu75Zb5uhQDSgBHLB+k/Aa1Lh0r42QrXoWbR06PcLq2zGSGgcQ2AH+xGsx5YsXcq7AIylbd1VRWoqY7zxPJLnlE+8eLpMPK9em7dOEnKJCDz0VWR54wOIc6yqXmUwPb7WpVFHozlyyIJJqPLnrUHcTQUrUlDjF/A2jhBMZ5N3QPSNv/YCgqkIS985mqg7OdZPhaXEpwy1cXbLdG4bNVv+A+EoJ25PSvq1CsXWo/eQ== 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=7Do02JI1EZFcQWrSMpFxurM4sFuf/GMUE6etfB8ub98=; b=XSLbfr+2nfaY3leSEyYW28DPxWLWL5tFW0Hri5VH/tXYUcTP/pWblhrHg1Mrm9eqcB5ZnagUMaUzLf2MNtF98RQWtFGTEVNaZvDwaFdnpzT2Kho25E5m7WnaaRg8f2XRykCuXuYP7VNfartC+XXmdQHQKsO6Pl5bGxZVF6qTLA5uWvtiACu1411Un60mgfBvJlUB9rH9XZIe8+XrFacjo5oieYipArN5QyaFahuUuSESWXcLFc/K4THVLJMoqba1Vdgiyo0EJDu66RbvdS79GlLZL+JMVhQim3evvIhkpBQUdzOI5jbBQrKS8ZvikGYw3MY9mEoWgPxWk/xLH6Dz2Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by CH2PR12MB4326.namprd12.prod.outlook.com (2603:10b6:610:af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8466.14; Tue, 18 Feb 2025 23:50:28 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%5]) with mapi id 15.20.8445.017; Tue, 18 Feb 2025 23:50:28 +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 v8 8/8] selftests/mm: add tests for folio_split(), buddy allocator like split Date: Tue, 18 Feb 2025 18:50:12 -0500 Message-ID: <20250218235012.1542225-9-ziy@nvidia.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250218235012.1542225-1-ziy@nvidia.com> References: <20250218235012.1542225-1-ziy@nvidia.com> X-ClientProxiedBy: MN0PR02CA0021.namprd02.prod.outlook.com (2603:10b6:208:530::23) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|CH2PR12MB4326:EE_ X-MS-Office365-Filtering-Correlation-Id: 11e44858-f593-46d1-d281-08dd507705ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: TeOfJHbOI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xqyNyZOu3HqwGh5AID0gP1Ljnfw1AYmrfNP43BfC7WpfLVJnhFxicO/hhhS3LrFbW0hU2oShZZTjcKM9micRk812abn7C1LCuAqnSNBFOgxVWCdVIHiJOGD2SGAH+guSF7LeMb2Ha0pD+LjilsDTPDEF744wb6tu2bai2FzlYpn8NOTpc/UyMi45Nysw+xhSEUyW/A6PfZ7Gj+9Q/x7cr5C9WtvYB3S43k6XcnjodERA6obRUHSrWVIDK4oXH7q5915XCUacmvFxtqdMXdDJXBF07ffSj1twRuiANuftRF 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)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qKWB4Mz4V2dh1l4/8NIN5u8nYYy7r46eN6LHe+fzgjNpsc1RGdC6F3CWi25IUikdj1uDanI3ARWOIB1OBnYBVDia3DQQ5Z84lf0UpbLa0y4tPvsNFPF4Qp8bgc/G4vTeAfoGa3R1LFS8m8HV700iBnbQ7geSfdLLNcSh7AQSvZPFdRjDMH/LjbPe2X/75+IOY48I4djSvBGluaNYSZ7Tpi+xSAB38pAaHysCMDkL6QIzjhiUKym7fe49sovkFC4X4QsFJJlPiT7x7y3doalFrrOOOpPW/1lQtERCTBYCaF7NGESww1dH72VeemWqEeoApIqJwlzRlVDD/K1xA3nm7TC7ZwgM95lCQ0KBVz9dq/5heFFeCiB/ZJTiMTMno12PoqsptyFxmN1I44UeM6ZI3kJAiZWlIwrJ0u5/LDyussnyyONCCFv71nfNX5d1B3LWQkF4mKg+jGTCZcXjMjd6exmfH0dZWR1uBQNdv8/yg6Pck0Z41+lj7RuW+G7ozRgw77yOjKQWrKQjxcRs6vdtvjxCtGAcQXIzsWAtoLmFzwVuZaJhsyV9txo/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46d1-d281-08dd507705ad X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Feb 2025 23:50:28.4291 (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: tKgXbSMw8weK4xSGe2xYWZ4D+e+krfEJhpRucMMdS5PDCm03W9MVNvSrft5EsTvb X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4326 It splits page cache folios to orders from 0 to 8 at different in-folio offset. Signed-off-by: Zi Yan Cc: Baolin Wang Cc: David Hildenbrand Cc: Hugh Dickins Cc: John Hubbard Cc: Kefeng Wang Cc: Kirill A. Shuemov Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Ryan Roberts Cc: Yang Shi Cc: Yu Zhao --- .../selftests/mm/split_huge_page_test.c | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c index e0304046b1a0..719c5e2a6624 100644 --- a/tools/testing/selftests/mm/split_huge_page_test.c +++ b/tools/testing/selftests/mm/split_huge_page_test.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -456,7 +457,8 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd, return -1; } -void split_thp_in_pagecache_to_order(size_t fd_size, int order, const char *fs_loc) +void split_thp_in_pagecache_to_order_at(size_t fd_size, const char *fs_loc, + int order, int offset) { int fd; char *addr; @@ -474,7 +476,12 @@ void split_thp_in_pagecache_to_order(size_t fd_size, int order, const char *fs_l return; err = 0; - write_debugfs(PID_FMT, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order); + if (offset == -1) + write_debugfs(PID_FMT, getpid(), (uint64_t)addr, + (uint64_t)addr + fd_size, order); + else + write_debugfs(PID_FMT, getpid(), (uint64_t)addr, + (uint64_t)addr + fd_size, order, offset); for (i = 0; i < fd_size; i++) if (*(addr + i) != (char)i) { @@ -493,9 +500,15 @@ void split_thp_in_pagecache_to_order(size_t fd_size, int order, const char *fs_l munmap(addr, fd_size); close(fd); unlink(testfile); - if (err) - ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d failed\n", order); - ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d passed\n", order); + if (offset == -1) { + if (err) + ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d failed\n", order); + ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d passed\n", order); + } else { + if (err) + ksft_exit_fail_msg("Split PMD-mapped pagecache folio to order %d at in-folio offset %d failed\n", order, offset); + ksft_test_result_pass("Split PMD-mapped pagecache folio to order %d at in-folio offset %d passed\n", order, offset); + } } int main(int argc, char **argv) @@ -506,6 +519,7 @@ int main(int argc, char **argv) char fs_loc_template[] = "/tmp/thp_fs_XXXXXX"; const char *fs_loc; bool created_tmp; + int offset; ksft_print_header(); @@ -517,7 +531,7 @@ int main(int argc, char **argv) if (argc > 1) optional_xfs_path = argv[1]; - ksft_set_plan(1+8+1+9+9); + ksft_set_plan(1+8+1+9+9+8*4+2); pagesize = getpagesize(); pageshift = ffs(pagesize) - 1; @@ -540,7 +554,13 @@ int main(int argc, char **argv) created_tmp = prepare_thp_fs(optional_xfs_path, fs_loc_template, &fs_loc); for (i = 8; i >= 0; i--) - split_thp_in_pagecache_to_order(fd_size, i, fs_loc); + split_thp_in_pagecache_to_order_at(fd_size, fs_loc, i, -1); + + for (i = 0; i < 9; i++) + for (offset = 0; + offset < pmd_pagesize / pagesize; + offset += MAX(pmd_pagesize / pagesize / 4, 1 << i)) + split_thp_in_pagecache_to_order_at(fd_size, fs_loc, i, offset); cleanup_thp_fs(fs_loc, created_tmp); ksft_finished();