From nobody Mon Feb 9 05:59:28 2026 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2076.outbound.protection.outlook.com [40.107.96.76]) (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 831FE14037F for ; Mon, 6 Jan 2025 16:55:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.96.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736182539; cv=fail; b=kPaiVC/yaxK8539pxkPLrgrecy/K0wJqAd/VkQ63ggdGDHzvZ3PyXK7ov7voJjK2tFXYwkrWcQCLSt4qife0KU2qVSatqvGBISJTH7Ti4aOMA8Qw8vmxjJZygv5+CkBgUV5VnSg2H30jw09nOgW7xF/LBmamA+uOaW1/knihGVw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736182539; c=relaxed/simple; bh=ii0LiKg1wq4UN+C3gZ3yK0DZW35UK0UlsjWrl+u0ls0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=u0iz5Qrt50SqY1JWBJ7p2X2+S9dSThlcM+C3eljx/9zNDGosbfftG1I0PkZrLTPY4TI8eZWhMnr3NwshaAFZ4b72ChQZafAP4D2QSjQ+eMhcd1mBTQTGoIUnI3ynKHISiDwhtJAiqVRmsiLRORjzXnMCCntABlm0vWEU/9pPwMg= 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=sl6MG5mZ; arc=fail smtp.client-ip=40.107.96.76 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="sl6MG5mZ" 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== 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 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> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BL1PR13CA0241.namprd13.prod.outlook.com (2603:10b6:208:2ba::6) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: =?us-ascii?Q?00z+cm404Pj2Lp9kd7t5NJ8bQLyDcJIoCO+GoCEKWL+mSyRT3LRUW26bdUYm?= =?us-ascii?Q?PQOU7K8c//XurTLK98SohzMAFWV4hkZRVbVwoNpbZYJD8x0CQoTXrLRe3IDg?= =?us-ascii?Q?MRz5tKYBUmft9xvgyIMXIEiDn6+sUX22wguIfmeuZ8ebZ7WsvRzrIKkcVuVy?= =?us-ascii?Q?B7y7xyBXX/ZU4KjU9+KJNFPZNDk0D7WeFoFsDc5hll8/BlsMqIVRTfQTSWFh?= =?us-ascii?Q?eHCPyLElMt7p5Vp1+y8+BFkw3+Y6mQeA6bKeg9B2f4UcEzVVNA2X5AxwD4v3?= =?us-ascii?Q?nn+VMRKcc4SanRm6GdSCn5YWmOygpC2+Xn+MOECKAGldOP4vbXMWWRWWw+nx?= =?us-ascii?Q?M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p0hT9f870RkMIl8E?= =?us-ascii?Q?IGu3+BDkbwl6qAjyBgtlvyqFfecqWwI9ODtWnhromaOYeEToDRj71YI3Rj6k?= =?us-ascii?Q?kKit1CbwHGAj8enkEEhtgBu/pZRcKYDorUDfpNCWpyErPPDFHGXoSIH1j2JR?= =?us-ascii?Q?NfO+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: =?us-ascii?Q?7FWQgeGE4eXS9x8WzNyrrIQENHE60wAA4CTxdUNqtWKrHstfQMD7Wyz7hKIS?= =?us-ascii?Q?MrdCKZaiUPNyxj2OCCxjSEbTHSj+NnDCHg1R6yeebS26xpxt9eqwwEtfW93S?= =?us-ascii?Q?UmmtozvA2FEcARBinD+JbRuBNTAKke+LPFRhPJF0oSAM1bNE3EVrduGCLhak?= =?us-ascii?Q?Cd/f4xnCqipe3uFBWV5KFVY0HGxuVKbizMkhOSPkTKWQrDFW0Bhar74XIrpP?= =?us-ascii?Q?v7sET4m3/fNs/27r2xJjd/gKtUX9gx90/SjAUU0fLikXf7G4QRe0z3rQeMhG?= =?us-ascii?Q?AKexbSMUBaXIuR4/Dye5+KHPBpMg5Za7WpQU7YeniRyGI8gNp1TB3ZrN2gLf?= =?us-ascii?Q?rjorGQItyl/Xc+lVYHHEA0hbpN/yywQo6yvyQg8rgRt2j60OuYorZe6wAVVa?= =?us-ascii?Q?fqhhMPJ6ANfdpeW70Hm/6fgClgwUJDsJ2oJlvSoGF2V8pEKU/hCngKIMm8OH?= =?us-ascii?Q?+sfw63XOPrQcBmn2nQpVh00xRpRCRyXWHpKJBJinUefvD0vwD/wsaD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QXCk90zdh3kV30Ol2rJHQv6URvupaxOUIHVz+kKtM8EvM11Ms?= =?us-ascii?Q?0girduE0+Yrpz3Rimcy9UcybzFCCi2X5ZDFaHJz1B0eE66eGQ4kiPFaEMQPH?= =?us-ascii?Q?SnMKx5sNspr61vv7qYSVLxSIgR+mGko/iPcAISHHBoembXPlNWAq9UhF5zie?= =?us-ascii?Q?o2lfKvM+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 Content-Type: text/plain; charset="utf-8" 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); =20 @@ -3378,6 +3377,353 @@ bool can_split_folio(struct folio *folio, int calle= r_pins, int *pextra_pins) caller_pins; } =20 +/* + * 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 =3D folio_order(folio); + long nr_pages =3D folio_nr_pages(folio); + long new_nr_pages =3D 1 << new_order; + long index; + + if (curr_order <=3D 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 =3D new_nr_pages; index < nr_pages; index +=3D new_nr_pages) { + struct page *head =3D &folio->page; + struct page *new_head =3D 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 =3D (struct folio *)new_head; + + VM_BUG_ON_PAGE(atomic_read(&new_head->_mapcount) !=3D -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 &=3D ~PAGE_FLAGS_CHECK_AT_PREP; + new_head->flags |=3D (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 !=3D TAIL_MAPPING, + new_head); + new_head->mapping =3D head->mapping; + new_head->index =3D 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 =3D 0; + } + + if (folio_test_swapcache(folio)) + new_folio->swap.val =3D 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 @fol= io'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 mem= ory. + * @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 sp= lit) + * + * + * 1. uniform split: the given @folio into multiple @new_order small folio= s, + * 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_sp= lit 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 on= e by + * one in PFN ascending order and perform stats, unfreeze, adding to li= st, + * and file mapping index operations. + * 2. buddy allocator like split: in general, folio_order - @new_order cal= ls to + * __split_folio_to_order() are called in the for loop to split the @fo= lio + * to one lower order at a time. The resulting small folios are process= ed + * like what is done during the traversal in 1, except the one containi= ng + * @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 m= apped. + * + * 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 =3D NULL; + struct folio *origin_folio =3D folio; + struct folio *next_folio =3D folio_next(folio); + struct folio *new_folio; + struct folio *next; + int order =3D folio_order(folio); + int split_order; + int start_order =3D uniform_split ? new_order : order - 1; + int nr_dropped =3D 0; + int ret =3D 0; + bool stop_split =3D 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 !=3D 0) + return -EINVAL; + + swap_cache =3D 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 =3D 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 =3D start_order; + split_order >=3D new_order && !stop_split; + split_order--) { + int old_order =3D folio_order(folio); + struct folio *release; + struct folio *end_folio =3D folio_next(folio); + int status; + + /* order-1 anonymous folio is not supported */ + if (folio_test_anon(folio) && split_order =3D=3D 1) + continue; + if (uniform_split && split_order !=3D 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 =3D xas_error(xas); + stop_split =3D 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 =3D __split_folio_to_order(folio, split_order); + + if (status < 0) { + stop_split =3D true; + ret =3D -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 =3D folio, next =3D folio_next(folio); + release !=3D end_folio; + release =3D next, next =3D 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 =3D=3D page_folio(page)) { + folio =3D release; + if (split_order !=3D 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 !=3D 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 >=3D end) { + if (shmem_mapping(origin_folio->mapping)) + nr_dropped +=3D 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 =3D origin_folio, next =3D folio_next(origin_folio); + new_folio !=3D next_folio; + new_folio =3D next, next =3D folio_next(next)) { + if (uniform_split && new_folio =3D=3D folio) + continue; + if (!uniform_split && new_folio =3D=3D 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_or= der. * @page can point to any page of the large folio to split. The split oper= ation --=20 2.45.2