From patchwork Mon Jan 6 16:55: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: 13927683 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 B674EE77188 for ; Mon, 6 Jan 2025 16:56:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47AF66B009B; Mon, 6 Jan 2025 11:56:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42ABD6B009C; Mon, 6 Jan 2025 11:56:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A5D26B009D; Mon, 6 Jan 2025 11:56:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 073B76B009B for ; Mon, 6 Jan 2025 11:56:14 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A6AF01C507E for ; Mon, 6 Jan 2025 16:56:13 +0000 (UTC) X-FDA: 82977629826.08.4A03506 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2067.outbound.protection.outlook.com [40.107.243.67]) by imf22.hostedemail.com (Postfix) with ESMTP id D36F9C000A for ; Mon, 6 Jan 2025 16:56:10 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=sl6MG5mZ; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf22.hostedemail.com: domain of ziy@nvidia.com designates 40.107.243.67 as permitted sender) smtp.mailfrom=ziy@nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1736182571; a=rsa-sha256; cv=pass; b=wo+Jo8GN4LdzBCGZN8WEYMapsqzzLgdi0eMEqULjqkLfGz5sxRrDSkxZyDX4jrXug90mfN hf/aiyUbTLuxbzg08TiNX35uTrWnMxn+5L0enKDGr35tVM0ppqAZnFe4frt64zYNzYWu/H xqGh+1fc6FBDInH0FosUagEKPeumKL4= ARC-Authentication-Results: i=2; imf22.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=sl6MG5mZ; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf22.hostedemail.com: domain of ziy@nvidia.com designates 40.107.243.67 as permitted sender) smtp.mailfrom=ziy@nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736182571; 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=w5KWdyZeoa2VeruT4r2+LenfbzuPXZNx6nW2k88D9S8=; b=ghrX6CrIuyAJB1K5Km0MEVH/x7Q2Trd6Ngm18eWsUuj8KAICetJm+OO8Ipq8t3N0jpWX9H wmHpKIfta9Rafw5d96YTb4cFPUIkqyv5DL1RNdCxCff8IBpUSUnjsyZG3YpcikU1nMY1dQ S/5id6yF4okSmPyJ1FUQv5+CS1mu140= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WIXuc5IRD+fOflraJ9nGO94mSohAyuFxF17aLvOVEAYQ+4ANV97fLLK9SSsQUlf8yxFsYUIAz5XMFfl8sDAKG52UwDIBdL4ZLLu1bQuUes125uVmXPzd7YG7auNoPzPXYEHvCpuQDAW1FXjoqoU/sikRYuS0h97tItdHvKETNPnsoPVeejlpGngM7VycXkYy3ZNa1nJP08hMXl2DxbP1UC/IgkUJf59uPZCoV3kY9eTZhw5g+DVBgjhi95xSlaz8WZSFB74+9gVmfQz83a8I3LLlBI3At72sxvc//McoCzYOqH415bDlYx/HSPs0qy5nJw/0kcyH9nhgYyDQNhfWfA== 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=w5KWdyZeoa2VeruT4r2+LenfbzuPXZNx6nW2k88D9S8=; b=Wy9oepApLGQfWWgRgH/Wxhx6TjMCYZIPEVQPzkc9fZHfHstyckKbf1d+EDpztERYGGcce9rwDSoLhps2Ye0R7Dhnw9XasFlnckc1mF+T3gItJaIemjZzEO7KEs96sZRgX7jXFnfXa+k163QnotoVKEt3CzYycs55Z542AJpGzdy1YvvIAoIQUw8Ko9WURexCRx46WMsLZgJ5KVG5nYgHqVw3/B89RtKH8JNJ9CnZfj7lHN8zaIq8Cb9GB3Ed1yDDBpOIoTYnH/XvUmXroOmS12b/eLr5ZNJK2vgAOTt0v1sjb9No4UPXPQbeakbkMj8C/1PdDQyG1P9NTl2i3OkfTw== 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=w5KWdyZeoa2VeruT4r2+LenfbzuPXZNx6nW2k88D9S8=; b=sl6MG5mZ9qV7d0i5xFWYSqLFdzNfVgJ0cl2JrmREilX6wfZJ+KcDfbxSg6EfmtuHAa8TEjqwYz14m8FWy6HFLsL4XG84NWRVYGeSv6lOH2AZEs1VzWPEkso504h0p25hdZz5wB+uHq2YQI6s/+3YY6MUEE3qTFyd4ypu2mvdLy0sfjRAV8zBWxNYAV/pARE/XOjAJwSb3bdoR07uxbNJc7aXwDHgSAxFONvdoHMGJ801SH//ksB4UxzdkSiKZ7NHMTGHIkCPvtMmNEOScpF4uKttJgudfwINtO28C4DG4F/Aic5JrmFQg4rzSKa69ZKOX3s2ahICXkd0Dqb6QBtleQ== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by PH7PR12MB6634.namprd12.prod.outlook.com (2603:10b6:510:211::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8314.17; Mon, 6 Jan 2025 16:55:26 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%3]) with mapi id 15.20.8314.018; Mon, 6 Jan 2025 16:55:26 +0000 From: Zi Yan To: linux-mm@kvack.org, "Kirill A . Shutemov" , "Matthew Wilcox (Oracle)" Cc: Ryan Roberts , Hugh Dickins , David Hildenbrand , Yang Shi , Miaohe Lin , Kefeng Wang , Yu Zhao , John Hubbard , linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v4 04/10] mm/huge_memory: add two new (not yet used) functions for folio_split() Date: Mon, 6 Jan 2025 11:55:07 -0500 Message-ID: <20250106165513.104899-5-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250106165513.104899-1-ziy@nvidia.com> References: <20250106165513.104899-1-ziy@nvidia.com> X-ClientProxiedBy: BL1PR13CA0241.namprd13.prod.outlook.com (2603:10b6:208:2ba::6) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|PH7PR12MB6634:EE_ X-MS-Office365-Filtering-Correlation-Id: 8322bfb5-c7cb-4d98-1a95-08dd2e72eb15 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: 00z+cm404Pj2Lp9kd7t5NJ8bQLyDcJIoCO+GoCEKWL+mSyRT3LRUW26bdUYmPQOU7K8c//XurTLK98SohzMAFWV4hkZRVbVwoNpbZYJD8x0CQoTXrLRe3IDgMRz5tKYBUmft9xvgyIMXIEiDn6+sUX22wguIfmeuZ8ebZ7WsvRzrIKkcVuVyB7y7xyBXX/ZU4KjU9+KJNFPZNDk0D7WeFoFsDc5hll8/BlsMqIVRTfQTSWFheHCPyLElMt7p5Vp1+y8+BFkw3+Y6mQeA6bKeg9B2f4UcEzVVNA2X5AxwD4v3nn+VMRKcc4SanRm6GdSCn5YWmOygpC2+Xn+MOECKAGldOP4vbXMWWRWWw+nxMuuxaGpQQGzmXwN7SP81FkSyhZQiaiGkLi1A0E1zGsuyHHcuC/4KPFDEYSg4n7JO9ktfCLzNRGehYIzVSye0LXCZ5uHv3WnxLsSuYloUr5qTYrtKApp1lHjvAtjcrt/FYe6vvceLX8QlB0Cg/IShjWKSNxl0hRSH2NwE3RPXYst36qO1WwiGvmw1yo7/H4qCulh8ZtJqMHFp3HIpnpS+FUHVUokypXuWcuEzv2bjMUGIt2Nmc/RQ69Z6LMJkbFYYhctsWGBYTTCNnIdgQ7S5o+DDo7Tj+G0pRm3H7IK+sjjz15YS0FJGvgC87uZX0zuAaAAB9njR75fWyLWqg4C+oRm/gfwvCjdDcCmHBptQDm77oxFqlNrmFTJ7BVbPiXTRejRB0xF5wSMtBovCRjcOm0sk9IOV23teSsdo+VR+gLPS8pMoM7C35oYJD+entw6c8f7y+KYRQZEwpYnopj3zSVsXLb/g0PFBU4WwKlHaofa2frHgwkxPZPy+SJHgRNRssXXZ7h0whGC+ZruV/xAQRY99EuwjbRFrLHCnWjLCVQHOX0x6acq7nICZCIYs9bkLsYIOhB2ebVD8xDz5AQiqH4jvatG4yquxLCW2z35DZOjbQP/ofpsyW0nRWsbuUHbri/Ss048AkakvU6+QgmCRc6N68FDDrRrLiEQbkHBoTmpvGkiG0sv3u9/cNLro84AhguSbZOH7XkO4eWwIgKh9dzqPQJoAOJfoS7gAaoqHDdA3e20nXyE/jWVDbys2LhT3TrAioGep2UecdR50eGraiIP63p0jVJiLx1/ErccJaw6UxgmOPD2oDgH5zg4ytPH1YzeJwfFdzuCGIS1iLjCnLcNa5gyR1cwpOmAuzu/H0XNJOk1qW/uIp0hT9f870RkMIl8EIGu3+BDkbwl6qAjyBgtlvyqFfecqWwI9ODtWnhromaOYeEToDRj71YI3Rj6kkKit1CbwHGAj8enkEEhtgBu/pZRcKYDorUDfpNCWpyErPPDFHGXoSIH1j2JRNfO+fmhZi4l1WUVe6an3lbSS+Pm9 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR12MB9473.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7FWQgeGE4eXS9x8WzNyrrIQENHE60wAA4CTxdUNqtWKrHstfQMD7Wyz7hKISMrdCKZaiUPNyxj2OCCxjSEbTHSj+NnDCHg1R6yeebS26xpxt9eqwwEtfW93SUmmtozvA2FEcARBinD+JbRuBNTAKke+LPFRhPJF0oSAM1bNE3EVrduGCLhakCd/f4xnCqipe3uFBWV5KFVY0HGxuVKbizMkhOSPkTKWQrDFW0Bhar74XIrpPv7sET4m3/fNs/27r2xJjd/gKtUX9gx90/SjAUU0fLikXf7G4QRe0z3rQeMhGAKexbSMUBaXIuR4/Dye5+KHPBpMg5Za7WpQU7YeniRyGI8gNp1TB3ZrN2gLfrjorGQItyl/Xc+lVYHHEA0hbpN/yywQo6yvyQg8rgRt2j60OuYorZe6wAVVafqhhMPJ6ANfdpeW70Hm/6fgClgwUJDsJ2oJlvSoGF2V8pEKU/hCngKIMm8OH+sfw63XOPrQcBmn2nQpVh00xRpRCRyXWHpKJBJinUefvD0vwD/wsaDbPTHnZlQ1ptoaLUS7J6Zlrfw1Q2TsokUjLgHzNNxXI6lnCm5Bkn8oAdXHrvGESN9zoLndx1kXhUykno5HujUMFJ3BLB5B5eq9o5VsCn0niwLgKNF55W7Swku7BISoTtSYvSCEhoM9ZC0/B/Lcs4fLTjeoknOKnSry6yOvrozwnzEfOPMsrhjU7IWAai2wNPt+PS4o8iPoxmXkvqBRlluYvvYdEJRENu2bUKV9x5Mw5vCHtfZwxzzPQLxqDAjNGXkl6eopYQGFbwQmoVqMs7PW0GiVY3MnFemr9UZUZNR5fRQNtQR/SaHXmWBLK6/xuHlcBZn0su1snwgOk2VSQty7HzWJFewT/6xPki4tako7qjOQtfaH8yPMBrOKt8g6gfN6SliqAZKXq1zsjxwMnMpl9LuhCKf8x7CGx2BdsMSggyI5+v4NwXQFNnROuckz8P0UwDM+Xf9dEmb9lwd/BKhEN5PCtfd4zZcDTKUVxuRoyFShEPoln6QwMkgmuFPs3qZEc0UyhU3GWcQsZ2pJLYcE0ZGbwVK9m+y8v/mgTRQNC6SKmH2pGuoEPxS/fhjqvIxUYXRjozyHArtnnBWE6fKDmTLgzSBxxv1LzICUVH54Y1vlqcaH/f2RfkNsY8pynNE1Ovyehrfy0H9sCkLaqXX7NQNuO0rX0NxazVuruoWzLwdrwIVao7Xfr7YaLxTxnEqUqLEXgBqpaig4dAK46VWqQkpcA9y46KtNwhu+qRy9cYa9Uj0JWTS+B1dFnRAJWiyBUbiwhKaO9sv9S96slUfUTNUCiuR5Y1h2twtgZGtZH1PeWPjgREylF/QNrXO5uZJGLwDWhxTWAUS6I73tqeIqQanrQXCk90zdh3kV30Ol2rJHQv6URvupaxOUIHVz+kKtM8EvM11Ms0girduE0+Yrpz3Rimcy9UcybzFCCi2X5ZDFaHJz1B0eE66eGQ4kiPFaEMQPHSnMKx5sNspr61vv7qYSVLxSIgR+mGko/iPcAISHHBoembXPlNWAq9UhF5zieo2lfKvM+W1GNZT22hWGyAGa3Xs8RVCjNwse6rlH5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8322bfb5-c7cb-4d98-1a95-08dd2e72eb15 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jan 2025 16:55:26.3198 (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: uhUYT2mD/xlIZiCnA5/NVczW197dnj3OqAgYJm8qZj2NMGQChYhe3vDpRL/X1R4q X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6634 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D36F9C000A X-Stat-Signature: 65u4fon9fk6xydeixf4syxgyhhgfqbge X-Rspam-User: X-HE-Tag: 1736182570-732938 X-HE-Meta: U2FsdGVkX18AvynQvTl8bLUSAhV+WnDP7Jh4/aygmJjs+EhYsR6T6ZrYOHyjc9GM9E+XzBhMNwRt7IgmG0PoZw+svJUG/FeNj8CFQrR/GTr/pefzK+YGv2UR3lhCccshkSBQ64ZIkVXlVPRVzcpd+jRE8iJVl39dDVTY8am7RDgj5r4ykE49CF/ReTlDW2CUQMXBJZKJHCNR26bODpGBqUIvQUa4+vG9KYiIsnk6r8vjIL9HM9U/KyrG4v955oyQX4sRb+syYoucp7wRmiW7+hwcS52i4Q9iN3irKhjN4Qtk8R7D8BTXFtAXwx1+4DSE0WXMppoBZNqWYH2skWTpNdxMxSgTarRwKDaltTlHewYyF1XyypIgUofOD0sKz+dEag0iciwmJv71mnvwB+7fGaUoZopP7K6kqIOMYWT8WcBGlWluet0YPz7cilFCA05oG4ZBD+VswD+GvatMXmTZLRMgunZcDesUecE830cfA+eXA/X8mnmkMtcyST+BoZaM0vrL0dDb58DGk5OwMf1NuCuQsyKkn87k19fRfrq1SCE8xs1d+qmYlQofVWhLfPRAsOeyycExd6OuOj0FCFO2H5N5CD2U4yla5kaPecz0XXPYRPBvUvnFylXbXq/j4zyhgogOOdVx9X4VCtZdEqxhEWi9CFb1wgFHTviHfyaV+FsAmKZzCV18fwL830SQvcYUMhQPrP7Q3KugxiW3sdQqddt7dJd7qqlC3ekCajV+HEm7VwjJfvIyMueAWFrN6fKhs4qPHpQ8mKQVsvtL1aQy3mBm8WiIHczUzZShx0eyG5jTsb8i0CV6ZSeyHxLlEbam2o/9WFkW7kXGeX2O6/WXRUKO0rIXQVgeKzEIyiqLHrg5yDvOioq0WEda3CoD3C+GG6u66cThGD9LfoYSNTKug9bzmy2Gu61BJ6c65r8s+C6es9veGTemuuXF1Srmv4lydGk+PIRN5S/FZMBzp2f Iw/7EuXs AH+mMlnYm5PpJT+VwCIkjaowqWu9xoycL2M03adttfu/6VAsDo52mmpYTaG/ygar+IKwCoIF7wKPnijqBvHFx6x5Voq6jzurOmQzEFSPcPMNVUZNeSZZ4B0hmSTWBEi373yl0j9sWw4W8nKBKrukEbp1QxwD5d/1wI5OQQix0NRfDqsiKtTUL0j3kG3ZPQRRKk7w5iUXRSp30JKirXy5+FCqzHkocaVqk+34Bxf9nNsuxX/aSmA9vQl3ry5aT6RPuozcgtZGKVgPvPsWUSjtpRfoyIq4tMlPumFlK66CJQTqBCbY9beR7Im2gNh3qi79AiedGHnodLNXZ5nwT27ni97j7VS4EirQ8pDxroItP0xs5orR3zVx3FhU9dJLIhZKlfjlxWoZ5OnKP+RE= X-Bogosity: Ham, 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, 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 --- mm/huge_memory.c | 348 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 347 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 511b5b23894b..d8e743f81e76 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3134,7 +3134,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); @@ -3378,6 +3377,353 @@ 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 int __split_folio_to_order(struct folio *folio, int new_order) +{ + int curr_order = folio_order(folio); + long nr_pages = folio_nr_pages(folio); + long new_nr_pages = 1 << new_order; + long index; + + if (curr_order <= new_order) + return -EINVAL; + + /* + * 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); + + return 0; +} + +/* + * 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). + * @page: in buddy allocator like split, the folio containing @page will be + * split until its order becomes @new_order. + * @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 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. buddy allocator like split: in general, folio_order - @new_order calls to + * __split_folio_to_order() are called in the 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 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 *page, 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); + + /* + * 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); + int status; + + /* 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, since xas_nomem() might not be + * able to allocate enough memory. + */ + if (uniform_split) + xas_split(xas, folio, old_order); + else { + xas_set_order(xas, folio->index, split_order); + xas_split_alloc(xas, folio, folio_order(folio), + GFP_NOWAIT); + if (xas_error(xas)) { + ret = xas_error(xas); + stop_split = true; + goto after_split; + } + xas_split(xas, folio, old_order); + } + } + + /* 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); + + status = __split_folio_to_order(folio, split_order); + + if (status < 0) { + stop_split = true; + ret = -EINVAL; + } + +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(page)) { + 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) + 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 (uniform_split && new_folio == folio) + continue; + if (!uniform_split && new_folio == origin_folio) + 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