From patchwork Thu Jan 16 21:10:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13942309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07B9AC02183 for ; Thu, 16 Jan 2025 21:11:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94B98280005; Thu, 16 Jan 2025 16:11:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FBE6280001; Thu, 16 Jan 2025 16:11:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74E40280005; Thu, 16 Jan 2025 16:11:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 57DB8280001 for ; Thu, 16 Jan 2025 16:11:37 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F0B4D418D0 for ; Thu, 16 Jan 2025 21:11:36 +0000 (UTC) X-FDA: 83014561392.29.2DECB3F Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2086.outbound.protection.outlook.com [40.107.237.86]) by imf30.hostedemail.com (Postfix) with ESMTP id 3448180012 for ; Thu, 16 Jan 2025 21:11:34 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=GxpHvIUu; spf=pass (imf30.hostedemail.com: domain of ziy@nvidia.com designates 40.107.237.86 as permitted sender) smtp.mailfrom=ziy@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737061894; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7pPFUg6rNWV6UdEno3cFBDu9+zQhqBh8bUtDTehyLM8=; b=YU7FNTfCugCWzfZH/rGacnsSdqvuV/Jf4znAzfyYNRKXCO/yWLfAKI7pygGgJyFjV6v0dA C1eFWJb1Tk0px3WVS072F7x4+hR0aGoYOXqgbtmpNEXzwTdwBQi+hjAEdrUGEolMT76Cio 3PC2iChB2zuswWyjYanFnyFVycSLFnI= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=GxpHvIUu; spf=pass (imf30.hostedemail.com: domain of ziy@nvidia.com designates 40.107.237.86 as permitted sender) smtp.mailfrom=ziy@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1737061894; a=rsa-sha256; cv=pass; b=wgvKI9OtS7ml4t5203MI+6BUs4Lyi89vW8/Dd0BiM03xRR2mbiAbulHvmc0c5TPLME/eC7 BCJ4EuCz6cu5myRJjttOgUgun+OCg/jg69P2nt/o59DJxyJopF4ZcGBqeuE6yRP6LL4G65 HpVWMItEPp0AL6IWsYWAGjLdgqZycxc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FD8kVHZ4htzMejmWUL9H9ybI3yveJSBtYqx+Q03CKv2TLr83nqGds89exMQ4pTN37K3OjRQqXe6sNpaKABfMBAq43QskKCa8daFMeiJfcTslTp4TzHKQrkSO5GbP7qTa7SXdjHGy2cmQd1hXcFHvEWemnzT1gtV4XcobUHROx8M082HnvelPf1gUOyiGlLvLX/+zfWRJER3gzkTuIRxFTfpf27v6tQwKRKgzVnntYL6lNHrIxN+T1En9Ovpb3NZq3NQC8BtcE5bmoabteb/xMO2yIDT3TTX7LrdbIo22/dxvewyKGFzALq5UalVw6TjNYa2RtSYqUBHnztqjpbOkMw== 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=7pPFUg6rNWV6UdEno3cFBDu9+zQhqBh8bUtDTehyLM8=; b=TZZTQ/4ROV6giNL2SpIlZFOpocWJEogIMiLZ1TDLjJZSL7FR52ebWxZWXTYZTjSZ0OTnTLuMxKb1NobXhEORpw3CjdCpoqx6FQOh/3I3ZdFHohTZf3Si+FLY0BdyQI0a1sk0JwMwkUbnuEFh5L5994Qzv3qR5Q7JtcEiuSiG+QZHYZZsJwF5RaJERXH9vQWsKy3HSo+WmLK1VddxHHwepNsP0LtKvNhSf0lPgjM5eU33AGS92xtXNMvm+kPj9NbqPEz/hdmoYq3ybt7+mRgysWQJHjRvpjrSyxW3sKHQRI/JnDXW+HOf5RINkbXsQehffYD7o++eGTuD8+xPzH+3Cw== 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=7pPFUg6rNWV6UdEno3cFBDu9+zQhqBh8bUtDTehyLM8=; b=GxpHvIUukfz31nxX7Q8XuBC1yvAqDueKZOqbZqKoVCJ5vREWclxiwmV47Rg609q/i5Lqn4TFJPo6aEOJSChQJ832MKAbX9qEtFWcbabaKJ5msnOSwPBcP2+0fBVEsNxyuLMeVKVIOp2Rik339rKksg5CH+ebVUmaL35QiFr+7oFy1vrupjCMY7YZaD3Y2umZ7GHBeCUlLO/XqhQuj6z/Cdpr5Pk8OYzWBHotQhyRk05Ahr+1/gOcXHV3v77Ef/NeGuxgego6iWvMbWO6kj9hfViQbHgzCT7JZnR4N9TsFLwcw2ocFRLidj1fbuwtJQ3QaNHRAJ57axowGe383er2YQ== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by LV8PR12MB9232.namprd12.prod.outlook.com (2603:10b6:408:182::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8356.15; Thu, 16 Jan 2025 21:10:58 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%3]) with mapi id 15.20.8356.014; Thu, 16 Jan 2025 21:10:58 +0000 From: Zi Yan To: linux-mm@kvack.org, Andrew Morton , "Kirill A . Shutemov" , "Matthew Wilcox (Oracle)" Cc: Ryan Roberts , Hugh Dickins , David Hildenbrand , Yang Shi , Miaohe Lin , Kefeng Wang , Yu Zhao , John Hubbard , Baolin Wang , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v5 05/10] mm/huge_memory: move folio split common code to __folio_split() Date: Thu, 16 Jan 2025 16:10:37 -0500 Message-ID: <20250116211042.741543-6-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250116211042.741543-1-ziy@nvidia.com> References: <20250116211042.741543-1-ziy@nvidia.com> X-ClientProxiedBy: BN0PR04CA0007.namprd04.prod.outlook.com (2603:10b6:408:ee::12) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|LV8PR12MB9232:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a53e528-d0e3-4056-e7a5-08dd36724607 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|1800799024; X-Microsoft-Antispam-Message-Info: 9z4k0xAT+YKR52S7SYg4w5vW0C53hnj5KNtpitxNIF5wfqQvOhqbNtahpmlTxo9PWyvNuXWN/bl28OEmy23Ej0uKjM08j0d8ZEwgixEc8O8dMVOg8qTYBlPTnV9CUltc7xDD0ZxCLjGEhP3vZMN5iPljBMZ/vf6FbQPWm/wcUQKx0PqMsaAPM/NQVdYDyP+8sd7zTVOel0L1NoW7P5LNQZIpvgpn6lkMVk/HR5Pwov8hXT/v1eanq2xHF8/7sZaKfLSSWO4LMYnPolz+SDbR+nLZgyP1vXr+uEJUgEFbvr49kFvJyupvM5tIhKejZehDtQ55BolI/QLnwmALnD4VjyqG1syJ66EmES++nAE5XmTxuQr7tZ0Fli+YxMLNNhU7tCPI8jekOTh3clmeqXY9dWTrrZA9tkCr3IgjEXRZtcwfPZXdRmcQ48ljewDbwJlRfjmXKjr08LZnf1fLKL+VaIY6Gg/dAZ5B9zw3hYh5I2bHQ6PsyvHTJgxNGAIERVbkPYLFJmeWoFJWal7vrHMJVT2n3t4uoYVaJmOPWwpCUGTNI41AFemQ88HLDv8WcBjqe5On9eUfUeiZQXuu9tyWZ5OuzJ0XFufMag9JPGXXDWkQdvprJ/9Ct6rO11FT4iD40RBhqLtxQ6fzyoa0fHNniRVPmDIPEUy709ZpG/dOaBBd9t4uKOjJklIEmwEhdAg/B68duW+kO2bMuVfiLWUJEGh4zlCRhVrpzLaWhYp4Z89Pxu2qPPQsjsUd9HURVirbEKxh23BmgjtuD8Q8OTMJZpuwqJrAg3evFR8f1gO7iBFVzxX/dueY4sg1gg2wMY3/2r07Sm8HR3B/UqFLbqUqLFnICxJElKfT9kPUS0nVwXtOHdFWD2/VngtjCiYJzi7c+4QHMqzrBOXuVhHCo5ndyDmPG3YI7U0tanv6cjdXAGMdgVyDfrW2nZzQ/cTw7Y+E88bzOroluKz9ELw60M+ueqIeBVfSkFwDNKiGd3T8dyU365deAYp+cVYaUbz/rrB2pIoktmFXDAwxP+UtJu/kQFJmJIPaq8M166LnyUPkUQKz7al+ZJxSXYiErBqHUEE9SiRx6EKl10ML7j4BD6nwH0s453u+9AlhzVEUIWf6WylASKVy5hHMkzvc10SUyNhRxYvQA2PYIqYLCxkBSuqNVdELJMvfc4YAQmrxNWv9dhSJVxuGgqDj3DvjH9OTHR0E2of9+RecmnDUmE0IdjeqapIEi9zUktLsDnku+pu5UeZmAX06Zhb7VyGH8M0JdjN8o1CGbrby/XJFTklN2YFVKkz0jvHXRq0AItR5A8yReQp/uFbbLZ/BQvze+YfC2DVtm/4rDbexpSWy6Qkp77kpoOaEBtKl2dB8M68iLSfUqgUo9bRLOHKGeWL6fFd3J3wT X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR12MB9473.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ME+dIDvH7UfmRFe0GjEVjq7FHSTha1DiKrqjV8Eu5ATZbrveLtHbETV1waaMEE8OrJ/NxzQYvzCH3CYmfvIxrx1j2bwvjpoBZ/8zGqLa2K/W7KjGZz+OxJCU1CaQKzYkS6afpTK176Q7Hq5PpSq4ZLc0deSbCD675Fv75aheBdIjhv8I21fR4QcfvviWh50FMrKz3V8X7EjWvDLrSjYzu8YHBuaTyYz5HdMH1HzFRL+92+aYVIGBLxqiiF54ZfG5u2qcdyC0U2VxnBSZX1YU5b9kar4rBqgj18f3VBhwBmzGDseHUG9uwdkJRRToMuuVbI/cineUu6joS+LkvUnM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iMU87nDjCXgNvVMRnmocd0bF1WfyLRJBI+qkcACqfHaOHZQS2hIcCFM7JdnJS7cvLBkq6ddOn372XebXkrmIAp3J/v+WB0ROeZ2ErjoEl9BghHsTT1X5aJcKX5Xc2Mfzb8f4c5kFsRJpVVnCMqO2roPV0FHAE32kdy078HG8nQ8hwhLjmmxURD0xN5qhCoWHyig X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1a53e528-d0e3-4056-e7a5-08dd36724607 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jan 2025 21:10:58.6680 (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: /A8AGZIE9xTmQyq7q14YpO3i9WPQ8B5Oe5Fvy04vKOixePE+FrkgDRvF2WTE08hn X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9232 X-Rspamd-Queue-Id: 3448180012 X-Stat-Signature: 4o6wswq9z3g3wgszcnha3umjte68k6rz X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737061894-733391 X-HE-Meta: U2FsdGVkX19HHmuY/W0eUBIhnIBesOAydAgzZlFhGRk6ydSmvlPtUrJ4ip0FfryikX2lqKjy7KO4p6RtrCS9HgGzLu657kj20FXDCHAvQSB8Mh+vXls4N3XrtJMSgrNpuIWSZ9uFZrzBSgFG+34aDnM95NKJdAzVSmwkbPlCuH5l12AdibJUEu3plYzMIQu/na/6OIg9jMuI/LX+CP6HfS5U7sIkkrqI6rPlYMc6ae7HqE/hUrEhzyerPvcynKiiBeMjL4kXk0q+p8P4dwpbLy0GY6t37DJJ09yrq+j4s7dF/KUgFxOVFcz12EGfT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, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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 c98a373babbb..6c0089a0bcdb 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3738,57 +3738,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); @@ -3998,6 +3950,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))