From nobody Sun Nov 24 20:53:14 2024 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2069.outbound.protection.outlook.com [40.107.223.69]) (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 885B21A4F2B for ; Fri, 1 Nov 2024 15:04:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473462; cv=fail; b=aGsBK03g+ZZZB1CQ5GYKZzgQZoFNezYlkSiMdWUZnmvYR2BV6/k4jGUDaWf5iMN2TdN/D8WrprwGISHsDagHXMfbOmNqwDieZ0ZWPXve9njQZElpLoqPE3CN7KfpNOKIiJUfHSShAvtFYFNHxlOwUPAvsn3hMyKfq3SampTf4vQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473462; c=relaxed/simple; bh=Jamppz00floVL1Vvvg54h/DIra0JoskiFGBq2mcKi64=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Ki9ZBpGxNYuf2ypLkyPQCW7pEV89zzPWpwwDJ7jkk6doR1g/n14jdCcLk6GdAPUx4FX0j4jJizDwjkrALP6EvFj77rAJf2X/c1bIc60AnF3pifPBdy7zbjGifAQfgyzcn1xoi+f0+cjck7Q1EojwE2UBBXTf+QUASSbUg8dwMKY= 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=LSccG+AY; arc=fail smtp.client-ip=40.107.223.69 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="LSccG+AY" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oHVz29DmmK6auSY3sMz9Qh+l8gmdgEDZKlgfVhikODDLaoNPWE3k7WHJekV4hSYTZuE864ZhEDc6mqlaCwFPRh/x9oZvDKLPKmQFzCc6ufydaQW4XTT5mlQ8Kz8k/sg/jlHKHoJIhCM8sxbFsVmGWj8LPmU01zLg75GmMdybJs+lBnuAzPRjvwmV6GMizC0QhxxOz6c9Gk3my97guqN0ki7FF4ugojSqwHAGOS6KxAhByZU7QVFy7O3KjPQtCLRM0y2jaTKD7YITPZtXBX7IKxSN+hC3pwMHAU0bNsXUtJRv+DOgMzbiIFY9OkQDTNjbfZ+9fxdmI/yndZTinyTtVA== 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=eJ7mtvl5+aRqSZmDpJAAE0enIUlEdEtuMZ5VoenL7co=; b=fWzUWqZRQnqTQHDe85hGeqtyp1TRjT4G69iPXVRQOkn3ahq7AopW7j5lHoQlF63/TJs/YdzK2SyIuRYdj7Y4k/QzaowQBEqbe/davsjIirZcwQIVA34xqE2zIw+Q+9YCA4yBHOT+Pq5q94kTGFmSU7N6I+JwJdUgaH0JaZ2ezHvM7I+WwrpxO4c8uGMabztfVaHUfyNyMfwoREuzjuONKKOj6q60zBGoZR7Z2mtIlGv8/QZzPPDvJMYGfgK0FlrNK4XkP9x4epIgQUiJPCAtqX+ylJ58NwZSMDufIxnexBSN8glWs+LxR39SeFu1Uu0MYRyqo/X37dT7On7eaSiI8g== 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=eJ7mtvl5+aRqSZmDpJAAE0enIUlEdEtuMZ5VoenL7co=; b=LSccG+AYEttToFeO0f+mF13iWqqGGEQj0x2F8D1elSMexwrfO9RuzKzPCNKKVlRgoPZGcdj6yd9GqeryQQCNIdddKdQT3arH7Kab0NgEXUE8EF6IO3BcdmHKOeULrPBStpbp/f/xeCinytKYUXZABj5WsQmZ5Yk5Q+4xstI+MJK91NQgNmtpeWkRPCE5UmbZUfB7qpGruLTn2JzgIX9QTck7byYQ/kkUlLxFZVvzqGO/bYgFFJYJCI7tN4hMTkgcFkpEYbIEGrgDXULeQhya7XEU8O6GINNwTOj2bkqaS0S85ogzzBaHL5hbW8qOucgwNX9DnJ7n73TRGv0GJMqIWQ== 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 CH3PR12MB9453.namprd12.prod.outlook.com (2603:10b6:610:1c9::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.18; Fri, 1 Nov 2024 15:04:08 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:08 +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 v2 1/6] mm/huge_memory: add two new (yet used) functions for folio_split() Date: Fri, 1 Nov 2024 11:03:52 -0400 Message-ID: <20241101150357.1752726-2-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9PR03CA0109.namprd03.prod.outlook.com (2603:10b6:408:fd::24) 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_|CH3PR12MB9453:EE_ X-MS-Office365-Filtering-Correlation-Id: 681b0ca5-bfe9-42c1-9813-08dcfa866f9f 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?SkIMXVg5P3jbKaLprVOdyJ+Y2Z0AMp+iAnXsp83b2L5381qlPrcm3TXbxBfR?= =?us-ascii?Q?fR66ZgC+Jjpt6YQ5qZq7r4Sq2Lyvw/hWW5axeC8KmTgIfVySd7WjeMSY215e?= =?us-ascii?Q?LPi7fJR1V2StsYgnlLKNRP9wLmCOogcQ7hYI90p+pKk7FQxzFmUK/0/h0TYn?= =?us-ascii?Q?3rsiG9rHjUlCGwaOs7RZQip7POlP8C4AFIm1/0qS9UalLadnCsR6KyJKuUqm?= =?us-ascii?Q?pYlYQgugOmkFFUVgvcmYHNWEB769cosimwdPgelNlWAKFhLH2YwD6vZF/3/r?= =?us-ascii?Q?POTzzlHBnsKYTFI4zTQQFGhgIo6ehQ+dvkykbw5k+GUWEdVL6Cbtirx3bDbc?= =?us-ascii?Q?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LdTgqD?= =?us-ascii?Q?nA+BZPMFoiegnTzPduCTzFhz3boHXpNRjBvteWOGX1LracXKGCyn++oe1gxy?= =?us-ascii?Q?ezsKyEJHBXnCw2wACiY/7WsM?= 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?Qnp1EXSvt4oAOH81UHBiKlnSm8eZJSN29g6q73v6oUv8jtHHKVfmJGYR/Zna?= =?us-ascii?Q?OWh43nLtoO1zdExsd7JfAMYeS3ZTPonJoDXIdcX1/WtaE7YbCw+w8CAUFVvR?= =?us-ascii?Q?f6S5viXxuKveOrbWaDLiZP+XBp/QvQ+X2YSMs79NxOBT3b+S0vgBxda2RFAZ?= =?us-ascii?Q?M83kthAzMTFx4F79IBQ4OpQZnr9zctfOJ3guDDMq1YlvGLjAxOrKtQWh+CeP?= =?us-ascii?Q?BvhSqMmWH3cdispeP6DFcPsbgcFIV1e7j6fYLfhfv005JpLt5f3d7BMe6EGU?= =?us-ascii?Q?mrhC/RceFQq/Dc/tYVjNdlCTXVUlzQBvMikv0ApP9QX7LRlRqvtnaJT0HWBf?= =?us-ascii?Q?Rk6XPKr/5ATAgcijQO7q3O+2WfFC8oYcS+tcBUXkIBq9r8/ynXGsl/qDLIyZ?= =?us-ascii?Q?fKXSpPTAoyuWP5vkpCRldht0vpqDbw+cf+SPwYFuJU5UbHiqlEZKmkqdnguS?= =?us-ascii?Q?dMsNrh3l3KpMHvU7i3nMyaEm6eBGoOsI4S6W6bCPqtMBHmm9JraAX0IIyUQr?= =?us-ascii?Q?ABIwi2ip0l5FY3TfCkAnqx2Exldqwsq8rRV9b6ewOwRr7adsKuQ8ERmgrH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xdfAqqNpi5UMmD78x70iVTyyBHJQzOneHgdY8c?= =?us-ascii?Q?F3f63j5zoiDIwUqC6LhZmPS7supr8LPynq3Ayh++cOyAgpRUBrTMg7GUoWsN?= =?us-ascii?Q?O79Om6usN/m2LzHrrKv9Klv8kC/1aLaah2BewqZR?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 681b0ca5-bfe9-42c1-9813-08dcfa866f9f X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:08.6370 (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: PD/jTcoBynkQMgMF/tkvN8EHQdzLdfxV5C/XloNNzL+OaNZJKniqep0GnGEleZcu X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB9453 Content-Type: text/plain; charset="utf-8" This is a preparation patch, both added functions are not used yet. The added __folio_split_without_mapping() 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, __folio_split_without_mapping() calls it once to split the given folio to the new order. For buddy allocator split, __folio_split_without_mapping() 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 | 328 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 327 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f92068864469..f7649043ddb7 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3135,7 +3135,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 @@ -3379,6 +3378,333 @@ bool can_split_folio(struct folio *folio, int calle= r_pins, int *pextra_pins) caller_pins; } =20 +static long page_in_folio_offset(struct page *page, struct folio *folio) +{ + long nr_pages =3D folio_nr_pages(folio); + unsigned long pages_pfn =3D page_to_pfn(page); + unsigned long folios_pfn =3D folio_pfn(folio); + + if (pages_pfn >=3D folios_pfn && pages_pfn < (folios_pfn + nr_pages)) + return pages_pfn - folios_pfn; + + return -EINVAL; +} + +/* + * 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; + + for (index =3D new_nr_pages; index < nr_pages; index +=3D new_nr_pages) { + struct page *head =3D &folio->page; + struct page *second_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 *)second_head; + + VM_BUG_ON_PAGE(atomic_read(&second_head->_mapcount) !=3D -1, second_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. + */ + second_head->flags &=3D ~PAGE_FLAGS_CHECK_AT_PREP; + second_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 && second_head->mapping !=3D TAIL_MAPPIN= G, + second_head); + second_head->mapping =3D head->mapping; + second_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(second_head->private)) { + VM_WARN_ON_ONCE_PAGE(true, second_head); + second_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(second_head); + if (new_order) { + prep_compound_page(second_head, new_order); + folio_set_large_rmappable(new_folio); + + folio_set_order(folio, new_order); + } else { + if (PageHead(head)) + ClearPageCompound(head); + } + + 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)); + } + + return 0; +} + +#define for_each_folio_until_end_safe(iter, iter2, start, end) \ + for (iter =3D start, iter2 =3D folio_next(start); \ + iter !=3D end; \ + iter =3D iter2, iter2 =3D folio_next(iter2)) + +/* + * It splits a @folio (without mapping) to lower order smaller folios in t= wo + * ways. + * 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. + * + * If @list is null, tail pages will be added to LRU list, otherwise, to @= list. + * + * For !uniform_split, when -ENOMEM is returned, the original folio might = be + * split. The caller needs to check the input folio. + */ +static int __folio_split_without_mapping(struct folio *folio, int new_orde= r, + 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 =3D order - 1; + int nr_dropped =3D 0; + int ret =3D 0; + + if (folio_test_anon(folio) && folio_test_swapcache(folio)) { + if (!uniform_split) + 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, + * intermediate orders are skipped + */ + for (split_order =3D order - 1; split_order >=3D new_order; split_order--= ) { + int old_order =3D folio_order(folio); + struct folio *release; + struct folio *end_folio =3D folio_next(folio); + int status; + bool stop_split =3D false; + + 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_set_err(xas, -ENOMEM); + if (xas_nomem(xas, 0)) + xas_split(xas, folio, old_order); + else { + stop_split =3D true; + ret =3D -ENOMEM; + goto after_split; + } + } + } + + 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) + return status; + +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_each_folio_until_end_safe(release, next, folio, end_folio) { + if (page_in_folio_offset(page, release) >=3D 0) { + 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++; + 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); + } + } + xas_destroy(xas); + } + + 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, so that it + * will be left to the caller to unlock it. + */ + for_each_folio_until_end_safe(new_folio, next, origin_folio, next_folio) { + 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 From nobody Sun Nov 24 20:53:14 2024 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) (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 8105F1A4E9E for ; Fri, 1 Nov 2024 15:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473462; cv=fail; b=W/LzFkwWWSdDzAOi7AP1nGDiS4/4q8Kh4mSJhU5Q6LUTb7w9OeKHz6dZTMoUAa8XqzD1aX4ZfwtMZ1U2SrMw7ScGt78NR8ZMNRmr+4Eiy4UXMed1Dz6Y7BiFmdtebt7BhTqaVQmnJmdSudGIgtLQnNs0/yzo+AbjQmL3VPRwhcg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473462; c=relaxed/simple; bh=LKCSIu5pehvAt8c894JqZpQNSfF6MISdOps70TIpgms=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Ma2s/qFI6SHkd6RoLOw+8XTMkT/fkh9s5HLBV+jSylwgTPIslFvULuvjxIhlS7uiF8TJKMS/TuKZOXSeCiTzi4bK3BwOzzsaTV89whupMdf95LBxeJ6vkY5NkjDO6mlZ9WpaJT0WlwkZHXbLxuyZkByM/i3bg/AAowQBiHW3U2o= 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=ljC0i1Hy; arc=fail smtp.client-ip=40.107.92.79 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="ljC0i1Hy" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k62DSH0vuRFqfyFHGdTrWLaZSUXJ1466kLuBPxbT88gv2Z33SqBmKew1KRAkykXF4hquWGpbP6hSoBiP1cCwUIGr7YIjIIrlWJiESkfp9AHYQfkkO3dK/mNw0auXsG39QuuDJU7XZzfhfmgtg2HTlyc40Ad0c8gUsfCK5ERbUQBqVeiMh8IusVWDLcY+ZfE0EbJOaEH1jYU314DUxe7CzsFUDOlBddfFUP33q/TMfHuIKciS+cgcDnC8jnZNrqxy3RB4MTMa0NKq2TKg/VieX8Y6SPiYfalOnlcqNwgz6Qc1U9ThLrymxNs19VjQZDHNheJ7xJgLQ3rQQdSl9DkB/g== 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=Ls+U6Awbsximkn/QkNJbncmd3R1uDypxcexS3nCzLLo=; b=Sk/Hq+TdD3UjwlQMsfgZQ8PKa6L4PH0yBKT3aIOuiz0ykG15hHmuvFqEoX21e1z57NN7bwC7lB/javorK1sVtFFGMeOkDx7D/jrZkB8oGe09usbXwdiR0ImrkuFODmTbpSqILFzQmVC8mmJdjqQznuIPY3+v4xVTMGXMQ2fF6kaUhpmAwv2k+HeGtgQyXLMV0RZaSPjcOJdReSPrU8oaSbID85PtWzxcEwmsqTN6heAPxqJ9zb3fkrs8q1j43U+X8HjWfy4bWiuP/6wvBO7M7GavjD0h8tUwDgqDjkNik0eMOrWRri6Ohh+KMgOPGTrggffcZfDBnpwjdIgoV5q/ZQ== 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=Ls+U6Awbsximkn/QkNJbncmd3R1uDypxcexS3nCzLLo=; b=ljC0i1Hy/4ewI3gN5e1vPV3z7v643B/I2beBTgWDvjeiLL+kesfIl0Vf9CxO6iszqvyCFFeEoh8bgBwlhmBwzHqcYH73CT9NK5EvYqSVTnIRbYX4R/dXM32rqYmvaU0HJvEBjaUWxgJ31tXG44sHDUj5aXN7nGR39tWSoESbBeUhJELW4kLQmvpk2DeNUn6TOdmRNuWOdOFqzRce9LadUtXCDFW3/qajX7NUk0jxRZtfldhNYdFXNVLZUXOvpKnvIs8T5h1HAEgKaMNqxJzCOHwfAKIigZifOBifcTVdyk3FoUXEUnG2eDez9HA86YSq5UG+s0hxEvcM9PfPL/SIYQ== 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 CY5PR12MB6621.namprd12.prod.outlook.com (2603:10b6:930:43::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20; Fri, 1 Nov 2024 15:04:14 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:14 +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 v2 2/6] mm/huge_memory: move folio split common code to __folio_split() Date: Fri, 1 Nov 2024 11:03:53 -0400 Message-ID: <20241101150357.1752726-3-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9PR03CA0107.namprd03.prod.outlook.com (2603:10b6:408:fd::22) 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_|CY5PR12MB6621:EE_ X-MS-Office365-Filtering-Correlation-Id: cffa0d44-24cb-4c23-5a11-08dcfa8670a4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?tgLLugnwwLJWq5ZOuj+yJsrQskDyptgQNrsb7qZu1kIw6jkOYfKID955bXyN?= =?us-ascii?Q?XdUUFDGmxpUe/j0wUi+V8N8WM3iKjH/1TwJ9OQRGUgzDbplovu+MiM62q9TP?= =?us-ascii?Q?HUFiGxfWfwngyQlLDAkFm/9hZ3jnfUTguzjujNfaHkCvLFuDe35V2XKBzQqr?= =?us-ascii?Q?n1ja3v5Q42EInXoEgnbUXll6vi3xkVUNPs8pRX+9uN+vG5/QFrSQZUzjFQue?= =?us-ascii?Q?Z76eg/bhSVsD+kTOXPN9mgXJl06KDNLM9KH/z/Knaxgy6yJxUUGLWpN2ioK7?= =?us-ascii?Q?VO1qP7fDXyF6unp2mzbqarJj/nObh4hFpZfHHlN8SAqMiEiH853a6j6Mp4RP?= =?us-ascii?Q?/fQsC2oNzKVFv5nTvGamhpmL848fCwHfaUhwGuUCC6eJD0Gi7WnXDZKcDqRS?= =?us-ascii?Q?Wnmqv3GFLrZwwv8jMNIseddSPV1pAOZKMfjdeAmPxM1ECRb5fLYh2m6HEZbX?= =?us-ascii?Q?XLDfdrTcTis/E4uMMFW9B0FXw6ShFWJuDdHb/vWuAQ31+Ny9RHQEs/ai7Bfm?= =?us-ascii?Q?CnkHhjWUsR+M7S20kmsdVMmiqWmoPOqwl6A3+o7HsQDvEodNTKNz+vjK1Zr0?= =?us-ascii?Q?9hexyRngoOk0wWgS1MUrvoN8UoWDhPX2iTFv1HaArml3PjhxoIQc8RKGGHOr?= =?us-ascii?Q?ai43ojeHqsIZNUFluW2u657lD9esDpVVUy0FQJtZdUDwPiWLreiXRlTKoeN/?= =?us-ascii?Q?jblQmTvPoncYyHGm5AKKuH0AHtPpub2qJ5arQHLpFqwz9k6XNb9sicaRKtYD?= =?us-ascii?Q?DRUU73jaLz6/PBWebJhiW2B59g1kUKD/Ztqssczh/rEKoLzsoZD9n2XrAHnN?= =?us-ascii?Q?lAOkUlrXUtKwQ5lRIdxsOA2R9GXaXvXkszpwTUVw6MuxJufj+YQu1p3zJ9lV?= =?us-ascii?Q?9wOQBFDsOvo4oSjOpnxs7YtbAb0pE8PiUALpgJNbgygJuKNnfvLi5hvh/iqM?= =?us-ascii?Q?59eKKYtCXa60VPy5hnsMO/6sHRFI0grrXWsj7zT1a+0IBPlzY805aeHWmQdH?= =?us-ascii?Q?0zUyb1srRjfObtg3ltVA8zUZZlNaHgQolHZ035mCx6Op9XiSuOKMjyvf03sG?= =?us-ascii?Q?Z/zCtY4qreEWVBL2QCT+MECNrUXt+nCmwCo34lFy367ndB6XS8L4HF9kaKpL?= =?us-ascii?Q?xadQvTualBuYTpbs0B//+B3/074aL0zLt+g16b+uvKNIfPdPKf9CutwB6+Hw?= =?us-ascii?Q?xZsYEskLKpMTT91H7oKHssqwf7WWK4B1R8zGQdb8DXuxBDIG7cRaLPQNywJO?= =?us-ascii?Q?j2+9NIfgTd9urEBp0EpcW7FJkIwPsBvATVkEgXkdsNPdXnXx7/HekPPmru4F?= =?us-ascii?Q?oUwGHfj2dp6MZiKx0RK20yJI?= 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)(7416014)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?mVY18KV+DRA891tghXeycdKct+BoHR8C60T+yFDI0rGQtkvH8qlZILGphxao?= =?us-ascii?Q?PvMnUAb4b6IVKs0UE+imN5YkKG9w9yp3aG0zjS8/PtTCNZrri/V4J8+efwRI?= =?us-ascii?Q?QoWM6hbW1wi9mqK8dIIALutY8w+ziTwZ97YYoI6tT0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nZLVlQpLa7GTHJ3W6QMprXnE4PoKPkcSaQnbASYCbUHiO4fq?= =?us-ascii?Q?4/EJ12vBmSydvyi32SQ9LK1tfPWf/gxZp3j0tKABYg6ThRuom0tQRoUNv7MJ?= =?us-ascii?Q?JvJiF6ebpfSUzbt8l65fF/EU8ZdmKoVyQDkGH531fYQUbK6ZkWTwdXG+7pEH?= =?us-ascii?Q?BYVoEzxYtwIQQ60fMCM2ljb9FA6TzGlmai7wPZ+aZE0vQrbeb1L8LmvnWoWT?= =?us-ascii?Q?cMeeQdITsNjMY0pj4n8BATShzTuvgttcZ9iZoUS2M9rAHnY14333HOuFl27Z?= =?us-ascii?Q?sbD8p7Wiqxd9HBy6dW4WMjnjst3ej8FFDWY4Id9hJu65dpLsD6oiZqqwrdVY?= =?us-ascii?Q?zgTUpJQAabpialqADbDbEtvj/zQhiJht7OQtw0l0LqifYDI4Noq+TAV75WSX?= =?us-ascii?Q?+R2g9S7YXuhkAFF+ZDwzUlNHlmCHwnWJooXCw6UsQ7M0Eovr7O/gRtdZ+iHA?= =?us-ascii?Q?I3gcx8UPnoncY9pugYfANAUAUMGySdWNUUmDXCDrQiDgoPnDu3l9bc5nc/vI?= =?us-ascii?Q?wIwCGbozd3Vf/d8AiFLXCWY+YNxmQDpGu6uG786U?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: cffa0d44-24cb-4c23-5a11-08dcfa8670a4 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:10.3552 (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: D96vuK6PyP89Br657QAtqqCpL5tDdEtTOGLmkS0IRjWi3L0E1gs8Kppa4MBA4Vs9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6621 Content-Type: text/plain; charset="utf-8" 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 f7649043ddb7..63ca870ca3fb 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3705,57 +3705,9 @@ static int __folio_split_without_mapping(struct foli= o *folio, int new_order, return ret; } =20 -/* - * 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 - * does not change the position of @page. - * - * Prerequisites: - * - * 1) The caller must hold a reference on the @page's owning folio, also k= nown - * as the large folio. - * - * 2) The large folio must be locked. - * - * 3) The folio must not be pinned. Any unexpected folio references, inclu= ding - * GUP pins, will result in the folio not getting split; instead, the c= aller - * 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 o= rder-1 - * folio only has subpages 0 and 1. File-backed order-1 folios are supp= orted, - * since they do not use _deferred_list. - * - * After splitting, the caller's folio reference will be transferred to @p= age, - * resulting in a raised refcount of @page after this call. The other page= s 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 disappear= ed, - * 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 =3D page_folio(page); struct deferred_split *ds_queue =3D 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); @@ -3971,6 +3923,61 @@ int split_huge_page_to_list_to_order(struct page *pa= ge, struct list_head *list, return ret; } =20 +/* + * 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 + * does not change the position of @page. + * + * Prerequisites: + * + * 1) The caller must hold a reference on the @page's owning folio, also k= nown + * as the large folio. + * + * 2) The large folio must be locked. + * + * 3) The folio must not be pinned. Any unexpected folio references, inclu= ding + * GUP pins, will result in the folio not getting split; instead, the c= aller + * 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 o= rder-1 + * folio only has subpages 0 and 1. File-backed order-1 folios are supp= orted, + * since they do not use _deferred_list. + * + * After splitting, the caller's folio reference will be transferred to @p= age, + * resulting in a raised refcount of @page after this call. The other page= s 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 disappear= ed, + * 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 =3D page_folio(page); + + return __folio_split(folio, new_order, page, list); +} + int min_order_for_split(struct folio *folio) { if (folio_test_anon(folio)) --=20 2.45.2 From nobody Sun Nov 24 20:53:14 2024 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) (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 31B5D1AA792 for ; Fri, 1 Nov 2024 15:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473465; cv=fail; b=i3Od3pOnzFaSt/U02nQ7rK9CQbzK0JcFlC5QSN0d8QWF+EalTZJH+1SglSdAsJeqLn8jnJfu6YpzSMYALLuVlt2qpfBFVZFG1lS+9ZpUF29CEZ8xZb3Lhg+YeY1LBSmJ0vT/UoJL/9ga7E5XkH7q7VSnwTAa3S6LjYReKoVdTOc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473465; c=relaxed/simple; bh=MbFvptQaohBLwgHytlTvxV6fSSSBEH2B6x9YFoX+AJY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PPAofJfQ2eEZ5VHYKJ5YE0Y4as+S38M3Rk2rvAzPIsifM76TexUYUHeupGFfEnkY+6zyW9JscrZ18DJE/c+6Z3pcaXRaLrNgg1jl+86zufxWa4GH4bqxsCSNh91SU4rOlQqXaPn+zNrUJJYKpDqbGjdam1P/YoK9tmZeYEGhlVI= 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=Eo2gG95e; arc=fail smtp.client-ip=40.107.92.79 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="Eo2gG95e" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=i+3dZDJYJTsFXi9IjFNOva9oVaYsqWUklTS8atRzfxKEtDPWplFaK1j11CjkgkkYitLDkj4rU1w6O10iIKxuPlQvJVWMP3O704zJbv6QsHkxR1yzcsJZbwCQNR+xves89uMMA64LbyGxjraPKWS1PQm9f6FQ77NkgRY0FQQu9y6rjUHx6Vmw/Bm4tLcuuR1RrMCmwDMhVig2c4qPUcTLFrApw79unqTXdApYhrD+lTkkIH5CKt4ZmykjcF3TpPEzURleodkBFs/OFCyS6fnrq3ytNiYpkEJUaBNaJOrJk5/oXGYyy3re/L4FvrTMyuHKEzWNARKFRJavT4KiwioHIw== 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=wKioLr1HycFarfrZhAg1ByCjDsmUJhCDoEV444XxKIE=; b=MZRlOAbGcbwGp9du39Pm12CdH2fmLgkQs97foLeWcHrTiCj8rXj72exvHlwTb7UkgZ9fm/BzsKkJs53+rTzeeVUmnW6Yf1V2hjr30HImlkiLJ6SlA/mc2JkkcGY73Ga0YOAWr8pguOBzVJWJr3TPzpHo/58UWH4qBivwDpuPZwvaMkfC2mU56tb0iI2B8ox5PzITx8NcWD43+S0hCQkkJjRUIi3o8G58GWiv4q6dx20x2VZLhdzNbtg9E1YZnCrQE6wWUzP3hoioNO/sZIY4BgGi/YZx7fD5voqiBHHSMOpFN21BAI/nhbxEO9RPbMjQFUrxU0YGka75Spx215a4kA== 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=wKioLr1HycFarfrZhAg1ByCjDsmUJhCDoEV444XxKIE=; b=Eo2gG95e23JftwobdxP9NxR0XvbDR7F191JuyHUjKUdZIDNjf3MZFK0fZYLKjFL3X04/Qz7gJBUmcTJ0Png1kR2mYwNfC1Mi153c0KgmiOXYNm/FkhrXDkFYm+fIzbrCz5SpsnoYSA3dx38aqHNatgI+qzao5DerHfi0pyIpLKuyDkIbBcsyCTktjll2Z3PsVI4BMUTXWpr3oqBfK+1PRqs3NNEPP99B8naB1HTxkBXYjnDyGgyXbvwydf15Vq5pZzWZvM2V1k1QeBopfLI6DQHCzfLQf8buuhDdcgr2UyG95/WxXMF8iTTMz2SR84nh7XHWkDH8hpJWGsBh6Nl5bA== 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 CY5PR12MB6621.namprd12.prod.outlook.com (2603:10b6:930:43::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20; Fri, 1 Nov 2024 15:04:14 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:14 +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 v2 3/6] mm/huge_memory: add buddy allocator like folio_split() Date: Fri, 1 Nov 2024 11:03:54 -0400 Message-ID: <20241101150357.1752726-4-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN9PR03CA0104.namprd03.prod.outlook.com (2603:10b6:408:fd::19) 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_|CY5PR12MB6621:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f806450-3353-41e3-a9b0-08dcfa867195 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?2xLtUKd59diCwbYnvUE1rCj9K5pkd6OHA+BwvnMsSwf0rPYwzuVQT584elsL?= =?us-ascii?Q?/WnyE4Wqtj1A/Wvs9Lgb/UPD0p+1eQ7G1ZCPuc/Gelgrm/l2lGhzH55sQOrf?= =?us-ascii?Q?EU5A8kBQOTpSymrx+Dxh6RW7OpQk5bithpI66SZ7XO0+k/srY+T2Pzibgbi7?= =?us-ascii?Q?QVUHfSEy+Xpe9XPHKVOBNfsEhbmAOz3FE1DYgQcJHO9snU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/qB5At46AJRHDViTZvXr2HAmOdnp0RiRBM76?= =?us-ascii?Q?U8GZJRUzlhU+NxOkFZfmTBdbs/XJVvnla9YZwiDSCyMD3pz4i56mNccISeky?= =?us-ascii?Q?RsRBp5OZXTiTHZaHjQaAFyKFZXyg6zh1Hb/6b/5Bv9EQGwGNPvazFI8M+JMq?= =?us-ascii?Q?LSRDr5mvB3H6MQSaUOnK5hD0Q9ocQiipx4A4cd24fswboepYIo3FVcjllelG?= =?us-ascii?Q?svXjvXcAp0hHAPYN5cgF045h?= 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)(7416014)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?+uYK1geha0o2VRJocO2907wiRjG6hNi1tdzSGb4a+k7SWA8qRq0BNj0+IJ0x?= =?us-ascii?Q?G48smVVbH7rlo1ZS0N48jqD7GbHPz5xOebEr8A0KQTolUET6eRMNO34ooVB+?= =?us-ascii?Q?2/0yrx3/s+Q4fze+Blsu9KmHVuysLa8q/I3aZDrSox+0pF0glfc8G9qlEjjX?= =?us-ascii?Q?v+ZzHK72kTr+602CrAV5LStC20lcq3n67l4xffjcBH2xyEQjaCbNS3JzthbV?= =?us-ascii?Q?qIWI6fWGGOSHgp7ciE/kMBN+vQpHCqdOR1PSM4dD64Stlc4r6hee0ZnxDKcq?= =?us-ascii?Q?HTuC540+ZrOfaEQHPRwcbHgds+w7W0Xsp1+wwpSHjPeeAtFLOPTOgsd76axw?= =?us-ascii?Q?p3HEfTQyTNHSEu1/Gdy+rn6D2q3J1Qxq87149PASUOoUUJsxOR7UETNjXfjF?= =?us-ascii?Q?AGnN+20W+xOPiQnOBIEQADoj8a/AJDpwT/uiQRw2k20VF46Zn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ZuRCn33IXqI7d7B1CE7lK7H911GWPO6LHZ1bdzCddu4dc3ax?= =?us-ascii?Q?wP/1kwYR8zJSLQpEYSpGdVEFdlunFAj8lIB8koesZDmBAdK9PzW5P4fvk34F?= =?us-ascii?Q?Rm0Za77qVBquJ3cpHBIKgxmddn5csQgZTef9eaFoFyiXuRYCK1x51OvzNCts?= =?us-ascii?Q?JkvCRJSeuIlVIQCn6TcCAAkhQz+oHq6Lod/akUN0SFpLB7T+TJzLqiUSFDw8?= =?us-ascii?Q?0pXH1grj3VHxLIx8kkl/Yt6UBhxTNT5vepWOb0Iv?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f806450-3353-41e3-a9b0-08dcfa867195 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:11.9369 (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: gWhB/mhHh1S3cKk54Ffm4G7e0O59KhunT/Sq8f1zmTpfByb/sskKmI2gVP/xkEal X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6621 Content-Type: text/plain; charset="utf-8" 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 O-1, O-0, O-0, O-2, O-3, O-4, O-5, O-6, O-7, O-9 if it is pagecache Since anon folio does not support order-1 yet. It generates fewer folios than existing page split approach, which splits the order-9 to 512 order-0 folios. 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 __folio_split_without_mapping() using uniform_split variable to distinguish their operations. Signed-off-by: Zi Yan --- mm/huge_memory.c | 56 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 63ca870ca3fb..4f227d246ac5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3706,11 +3706,10 @@ static int __folio_split_without_mapping(struct fol= io *folio, int new_order, } =20 static int __folio_split(struct folio *folio, unsigned int new_order, - struct page *page, struct list_head *list) + struct page *page, struct list_head *list, bool uniform_split) { struct deferred_split *ds_queue =3D 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 =3D folio_test_anon(folio); struct address_space *mapping =3D NULL; struct anon_vma *anon_vma =3D NULL; @@ -3731,9 +3730,10 @@ static int __folio_split(struct folio *folio, unsign= ed int new_order, VM_WARN_ONCE(1, "Cannot split to order-1 folio"); return -EINVAL; } - } else if (new_order) { + } else { /* Split shmem folio to non-zero order not supported */ - if (shmem_mapping(folio->mapping)) { + if ((!uniform_split || new_order) && + shmem_mapping(folio->mapping)) { VM_WARN_ONCE(1, "Cannot split shmem folio to non-0 order"); return -EINVAL; @@ -3744,7 +3744,7 @@ static int __folio_split(struct folio *folio, unsigne= d int new_order, * 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) && + if (new_order && 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"); @@ -3753,7 +3753,7 @@ static int __folio_split(struct folio *folio, unsigne= d int new_order, } =20 /* Only swapping a whole PMD-mapped folio is supported */ - if (folio_test_swapcache(folio) && new_order) + if (folio_test_swapcache(folio) && (!uniform_split || new_order)) return -EINVAL; =20 is_hzp =3D is_huge_zero_folio(folio); @@ -3810,10 +3810,13 @@ static int __folio_split(struct folio *folio, unsig= ned int new_order, goto out; } =20 - xas_split_alloc(&xas, folio, folio_order(folio), gfp); - if (xas_error(&xas)) { - ret =3D 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 =3D xas_error(&xas); + goto out; + } } =20 anon_vma =3D NULL; @@ -3878,7 +3881,6 @@ static int __folio_split(struct folio *folio, unsigne= d int new_order, if (mapping) { int nr =3D folio_nr_pages(folio); =20 - xas_split(&xas, folio, folio_order(folio)); if (folio_test_pmd_mappable(folio) && new_order < HPAGE_PMD_ORDER) { if (folio_test_swapbacked(folio)) { @@ -3896,8 +3898,8 @@ static int __folio_split(struct folio *folio, unsigne= d int new_order, 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 =3D 0; + ret =3D __folio_split_without_mapping(page_folio(page), new_order, + page, list, end, &xas, mapping, uniform_split); } else { spin_unlock(&ds_queue->split_queue_lock); fail: @@ -3975,7 +3977,31 @@ int split_huge_page_to_list_to_order(struct page *pa= ge, struct list_head *list, { struct folio *folio =3D page_folio(page); =20 - return __folio_split(folio, new_order, page, list); + return __folio_split(folio, new_order, page, list, true); +} + +/* + * folio_split: split a folio at offset_in_new_order to a new_order folio + * @folio: folio to split + * @new_order: the order of the new folio + * @page: 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) + * + * Split a folio at offset_in_new_order to a new_order folio, leave the + * remaining subpages of the original folio as large as possible. For exam= ple, + * split an order-9 folio at its third order-3 subpages to an order-3 foli= o. + * There are 2^6=3D64 order-3 subpages in an order-9 folio and the result = will be + * a set of folios with different order and the new folio is in bracket: + * [order-4, {order-3}, order-3, order-5, order-6, order-7, order-8]. + * + * After split, folio is left locked for caller. + */ +int folio_split(struct folio *folio, unsigned int new_order, + struct page *page, struct list_head *list) +{ + return __folio_split(folio, new_order, page, list, false); } =20 int min_order_for_split(struct folio *folio) --=20 2.45.2 From nobody Sun Nov 24 20:53:14 2024 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) (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 D2E5C1AB50C for ; Fri, 1 Nov 2024 15:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473468; cv=fail; b=dILrORR5nymt3THwVRsOHkR1gBlHkymTC3HpPwKsnhUZtUxXhRmMtW697XQLOIbMA28jKHldb3oYjFxxarLvl6jsv1hbEWzpCxHukY3jxQPDv7ZXJmqNlQ5Kz6DmN2mFlR3iU3NhvcOkq6De6jwZHT3z2hm9HzffqiujWRh4V9Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473468; c=relaxed/simple; bh=OkBxp+xM9B0gZQ2+9JngD8H/sxIDxX+RqKQR1gWv0nc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=G0LjgCXtvLIvrdiI8UQhlF6ixuxE5ox0YeIHRVON2Hqubff2iCxcvt0++GHd3vTUONILAczi7EW9ObtKtP5Ch6dI6T3byBl3ct9aYbty9ERsx5yn4JNCJiDpYUzKrbDvK3hfWyoV+sixL3lOUDXrJYNYrv6QOMszbH44QO+bsWY= 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=l4OYe1+0; arc=fail smtp.client-ip=40.107.92.79 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="l4OYe1+0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AZlRRYN70X2vT2olV8qJKpPEvrbACTgC7ac1YX3T2T7PjOK4Qj/19trLjsc9ngQwOdv188OIHSaKtwG2PFh4WVl2myOwANQCHFmmqu8sMJ5k7Spge7CxRGP1ygOM7aR/4sEPbOn51J6k+XJeOeYQQgawsmgySXk5oPpv+bVBhNsbqub5ABExyPEDvGakWNd1yQm3fORDNfD6HvyB2QB0PuVO0l3gXi1dy2FjN68yQ9SI83PHP2EFcC0tiN2QLVp/w6DRVMJdv+osgfpWqHwkbuLoHpKCJ/FzBbgszj8TBgfrZBmrbMCpXWP/B4m4f9Ec/7pjKwm0Sx/6wgZjLiey/Q== 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=suGnhTcndxZ9XndQoK2ebKftv0rgruxD+X4FouvzF9c=; b=CdM1W1g8jXCKqKU841PkUewl6/czv1xW8JnEz5IXgqog5nVl9tpmwuL3gIZdmYapwlXlt6Q7aCLrll1CEhK9UdDR7JIcIRIFeLoG9/dOprAx25hENOEFW+zPC+rswFPSAO3J5MV43x1+3WHIz1ghdEX0PzP9dCFTvtmRp/GR1d0oiJSOw3ExczvrlBqlQPqaDmApkfvrUGFhwCtNBUkJ43ZmD4Z6qx/H4waOis1hAfCfOCTI3NR/PtPeqXn2g/PZXXJ1ZZV1N5mUx+Z2WSeU73lWfzYYjbAdNMEVPfsj6SjP8LKIl6PNX9kMvTPnoQq8ZmX64LNXPUDEMH8/UU8JMQ== 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=suGnhTcndxZ9XndQoK2ebKftv0rgruxD+X4FouvzF9c=; b=l4OYe1+0RpmXoQxhZ4LoAV/5/23E1WGMrIhO8IDZpVdgIDcy4s40ON4XZ5oojkEascDWqhMPPakLgBTRUCNhbGInHczUG0/V/Cr/N81SJ2uKwOD81dtt4YYLlXsr7aGB/ORuX4lLJ8oU3lFjNpyL6q1cqKCHjEgIsG0IWv4aniWoeRQaHEGaC1pQRsjDn7uxbn3iT0tcQsp7u2yOhRWn10GGd3WS4yEkY8NEusfKrEAN3BloIHyNErL1suI2KGx317Ls3bxyGBnPZuaAxYCpzMLu6nd9rASK0WQx0VqF0o0y7PMKp6txmb3m4ssPPmcPKF0lwsSNbQ9ztHSX1H/zFA== 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 CY5PR12MB6621.namprd12.prod.outlook.com (2603:10b6:930:43::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20; Fri, 1 Nov 2024 15:04:15 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:15 +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 v2 4/6] mm/huge_memory: remove the old, unused __split_huge_page() Date: Fri, 1 Nov 2024 11:03:55 -0400 Message-ID: <20241101150357.1752726-5-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN0PR04CA0134.namprd04.prod.outlook.com (2603:10b6:408:ed::19) 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_|CY5PR12MB6621:EE_ X-MS-Office365-Filtering-Correlation-Id: f2b5bfad-d154-4ba6-0c20-08dcfa867283 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?KODxz14H2Z+04WtnCRS86SP8e1cgftFdKIpf4i7flPSneg38dPYqkHJp1kGk?= =?us-ascii?Q?nd91pt+kJRKjr+XSIz0dMUwUE1+0kHFfJaYnaMqxs7fn+1jidOQjPPitpyak?= =?us-ascii?Q?C8taqBtRKtmxB8z2iRQ9GEWPRnlNjZ1d9lkkGKFr/PP9dv34ILYWWXkEPpAb?= =?us-ascii?Q?krtLWfMW8CENd3vgsREFb+8nK0+JoSyHm0DEzni2YO4I72g1zdfn/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BIXYDuXJbPmmrNBdm//YcXIOnUn2ed+5?= =?us-ascii?Q?DYN/MdOdVfvhYrJLWB5gBMLgqHEg4Q2CatQHEv3yTTYe7qaqx3IBp9XQ7sdA?= =?us-ascii?Q?q/rBu/yd0Gbj8KzXH/FqwyiE9+XL+BVtEcY3Jp0tsycagEzhIOLVlJeGGuni?= =?us-ascii?Q?M0ykuX8VumndIBR8T+lFSkbaSVsX6Qu4Q8ZuMHmwzthCIat5P0VP80ZGt08t?= =?us-ascii?Q?YBN3+yTDr1AKBuHTa1xMKdI2?= 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)(7416014)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Yec7W7Z+gHe7zdlQHiexxmyyfpYRIIHe+i1r0Gf0xYf7hkwtTsGCI01vpTT2?= =?us-ascii?Q?j9txV0KD38Y9DdH2N9sGF5RXBXFKlZ6xIXNjOYVDUThl55FWuGusYwwbI6eg?= =?us-ascii?Q?4WPHMQJUMmF4KeVzp8Gym9Gnryf/K3rY05kDzJMG9xqbyXvu6B4JHYST8usP?= =?us-ascii?Q?/j1/TlS1CrgTn9b0l5eMTiNYfJNZ39jpZBYk6GcERaqsb2ml9Orsd3ok3yMp?= =?us-ascii?Q?vdZb6XaUYYot+lYPj79Dc06yjtMBCQEVauz4Ydmo1XB1G5HLMuk1wNJqP/CH?= =?us-ascii?Q?g1ohPsu5qcjx3+cbw0v+vxyDuJeqvLsm0z/yKaOP+X7gC0CiUQCW6oWUJFfS?= =?us-ascii?Q?nmnK7CsPClJ+upvLx1FTlcl6cX94iTt0DJ6Rp7R54GXYPHGDH6P8kdbudIcd?= =?us-ascii?Q?tdRWFZDcFHICwRKPUDNNkl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-ascii?Q?4Rzj7ar42m5VftERUAo4iHqsiyIMTAZUN1pJxjgRN6WCmtpueryXfPTbcsfD?= =?us-ascii?Q?oksl3OLh4bKLFG4zlUGb53i1SMzC74XlPfSBPLfY5pOz48Tedoo5iCql4rav?= =?us-ascii?Q?WakKzuQjjCGQ30ARbLGe60PWC+jomkm24T76NQlF6KyexWmYdr4s+Etkp6eB?= =?us-ascii?Q?mjv2BqVKB5e7KdrSs9GbyltEDaaNdm9VIYbp0k9DUvNB3Njh4DqIACqaHG6C?= =?us-ascii?Q?G+jdg9XUqZwjl+hZvFi3zpFA/ZVVfNY77K1zLsNW?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f2b5bfad-d154-4ba6-0c20-08dcfa867283 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:13.4858 (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: lqibmb7cZzV3h34dzGD9OwOJR0D6vQs1p1EIJjCro09XO1IaUwX8UsAQLY0MlVA8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6621 Content-Type: text/plain; charset="utf-8" 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 --- mm/huge_memory.c | 207 ----------------------------------------------- 1 file changed, 207 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4f227d246ac5..f5094b677bb8 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3154,213 +3154,6 @@ static void lru_add_page_tail(struct folio *folio, = struct page *tail, } } =20 -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 =3D &folio->page; - struct page *page_tail =3D 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 =3D (struct folio *)page_tail; - - VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) !=3D -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 &=3D ~PAGE_FLAGS_CHECK_AT_PREP; - page_tail->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(tail > 2 && page_tail->mapping !=3D TAIL_MAPPING, - page_tail); - new_folio->mapping =3D folio->mapping; - new_folio->index =3D 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 =3D 0; - } - if (folio_test_swapcache(folio)) - new_folio->swap.val =3D 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 =3D page_folio(page); - struct page *head =3D &folio->page; - struct lruvec *lruvec; - struct address_space *swap_cache =3D NULL; - unsigned long offset =3D 0; - int i, nr_dropped =3D 0; - unsigned int new_nr =3D 1 << new_order; - int order =3D folio_order(folio); - unsigned int nr =3D 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 =3D swap_cache_index(folio->swap); - swap_cache =3D swap_address_space(folio->swap); - xa_lock(&swap_cache->i_pages); - } - - /* lock lru list/PageCompound, ref frozen by page_ref_freeze */ - lruvec =3D folio_lruvec_lock(folio); - - ClearPageHasHWPoisoned(head); - - for (i =3D nr - new_nr; i >=3D new_nr; i -=3D new_nr) { - struct folio *tail; - __split_huge_page_tail(folio, i, lruvec, list, new_order); - tail =3D page_folio(head + i); - /* Some pages can be beyond EOF: drop them from page cache */ - if (tail->index >=3D end) { - if (shmem_mapping(folio->mapping)) - nr_dropped++; - 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 =3D (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 =3D compound_head(page); - - for (i =3D 0; i < nr; i +=3D new_nr) { - struct page *subpage =3D head + i; - struct folio *new_folio =3D page_folio(subpage); - if (subpage =3D=3D 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_pin= s) { --=20 2.45.2 From nobody Sun Nov 24 20:53:14 2024 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) (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 C8CA81ABEAD for ; Fri, 1 Nov 2024 15:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473471; cv=fail; b=LCrm78G4Nrj5fGStk7T2YdD5+FO8IHIUppj3N3WwGlibuME+Jmh5pAR37MRY39OB9fWb2B+2yxrFIZGXr4g0kxqV73z3NYQgVY5wH7jEuVgKbuWNtLtPSa0xRy/ISV7ut+8aCFbbDlU2fCkGBXhPDloC/FX3Cs8qmeRpsmsI2TM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473471; c=relaxed/simple; bh=1OJszN63K+B8qcqQWeVe+rWUqKFZZV+YE+ClpSrDnew=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=L1w5l1AFrU+xrrIcBVP4lv4vUfpSOsex4i5ipRpkDuWi3qqm3CtwLBpYNX6wLuiPz5uaBqXJJplst2tsyZZkPD8aua6eIBGuwh2OReg+LQGronNkKbTg/7cluipXzo/GDWVQnT4/6mIeOqserPNIz9vjYVytZ1z0wNeUFkKc5D8= 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=Dz1dhLtT; arc=fail smtp.client-ip=40.107.92.79 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="Dz1dhLtT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=tQnj7ZDygmp8h/mj30v6Qy/0m6tlq8r6v/4BZ8XjbbtAYKiAfGpKajX+wXRRqvnnKse4xdlZGnmyoYgHC9Wb8m2zsKKEqlU5HXq9Uy2E7uyntXpZwq0WLluuW22X55xImD5K6kb8Pr7RcQwq0cyFS8uxTCeaYB4CB5KcjGSwxWoOPpMCIg6cAMBAcbcyfsBGyGrUm2hhkDMQfa691KAUfO1FeuRhxU2526OD1HOmBk6H2oTM6QAXCO4exS6x3EhqunlsBDD2aJ9lo/0/UB4sMFr1/tPA7ab/h8nBA1bBYHwmuuX2Q/KKxtwVSS919Qcz18E93Fel5RlIKY+CJLiwdw== 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=gBVeuolEa+O1KRTMeV6OyPyYPynOukZYFHHM5jLMtFU=; b=StTe88+Mr2y/W0vPEVCmqgleHduGRs66ry10bcatjFC4leLamxrSforYxZlJ97Z7OS/G0picClmlhNHfHimibNI0nEJvJQyPTM0tv9S3GkJPI63Yl3ilpLIebsvbtDjxe9JVS6T2v7OFWyq96bvi+ycz+lkiLqV0CMX+oNFYqadT8zxk54pqjrF1PI8031eMnQKgJnq5BapdT8JgXgvRhPXNl7FkGxgqZOz1tTxsQEePrrtLuboC6t5OhJYoimlMP3KVmrsS82W8m2XUvCGujwRyt1FfH5obw2P+N/Kd04s4W9hhakDiv0Y9zq8dxu7qZTL/iJ+y4qhhx2SQ/Mg/nA== 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=gBVeuolEa+O1KRTMeV6OyPyYPynOukZYFHHM5jLMtFU=; b=Dz1dhLtTXAQTk/dmWI7mA5nFyXAxjI4Q1aouSFwJH4mjE8RjGdzyAPbYEbm91+6yPy7ag3rWpG+U1xegZY8VsKEB8PxXEUixV9IhtGVtDfPsbu/UAfLiIoFI17aMGawHORCURscXNlbW0gIM+j+EIr/Nh5P28f8W39a4827bcdmXJjS/FgjIfkaCghjecGnfs5fTtpa1S7ZNu3dXyemDxz7gadSQOVSUWQ3VSIx/qDz56btcO8VXrJckk6evXaKcPv8yAHBfsqXa42SG9H+eQLIDN6kN3i81Pbe3YlelIGRE/X9Q7xXpAGElMvAoDtXBN+xLYy4d7XH91cOYCeAvGQ== 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 CY5PR12MB6621.namprd12.prod.outlook.com (2603:10b6:930:43::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20; Fri, 1 Nov 2024 15:04:15 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:15 +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 v2 5/6] mm/huge_memory: add folio_split() to debugfs testing interface. Date: Fri, 1 Nov 2024 11:03:56 -0400 Message-ID: <20241101150357.1752726-6-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN0PR04CA0135.namprd04.prod.outlook.com (2603:10b6:408:ed::20) 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_|CY5PR12MB6621:EE_ X-MS-Office365-Filtering-Correlation-Id: ce708f5e-7615-4d8c-a958-08dcfa86734f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?FoyIBZDO7BdEpcDp0twe0uAbBbGRkp874e0tWn9d93XX+qmURgiB9DNT6W60?= =?us-ascii?Q?qduPSGb3lrhR+e8U9Cndr3tvpXwee1csazRWSYLhBQy8IUlAXCDYhbK9ohYO?= =?us-ascii?Q?HeLim/m772OQ1GSQ3atN3gAQ/FvfC4SkL/FALoSMJ35fNH1OCgQXafpiPgL/?= =?us-ascii?Q?0ZRcWLKrNwx9emfW2NWZ+L0I0cme/sf2mRlyOclHgXLfV2ZYWQ+B/dD8aARN?= =?us-ascii?Q?DfL11ckK9WRoEH37UVvZAaQ0YJotK+d1O0jVPblYoXxHaPMcM/RGVv2N+pu7?= =?us-ascii?Q?uiZ/0gO8PwzYqOC7PhZg2CNzZ1csfsVHDAH91BUxxizymnJ4f7ththtWEd3c?= =?us-ascii?Q?Hbs2u0rQ2b5lNI0qjXWCre9a4VoIbuxKtUGnBH/pwh8ksmNCmfITGHz+Y3Mk?= =?us-ascii?Q?EMFz7s3ALL1oMj5rL2nRnWayqJIwqFiDOzNX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jpZCMMZjywj9Oe6Dgc9?= 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)(7416014)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?e+vAXa4ZygGn6rI7M5GWRwVTi+CpxKcUInyzqpuWNy84isFGxmK1W7JYW3u5?= =?us-ascii?Q?mEU46+Nd+JXRglpSyQzQz6ZN9Kgb/pkkwB7EZdRSrBJ2jiDsaWueSwAOuEJF?= =?us-ascii?Q?LjLyYNGg/Kb7qMs/6AWxpGMRyIq7tMYxXMLLzPuCwA67zVnLbjfSEVnJtFDr?= =?us-ascii?Q?lGj5Q1Oo0NmHeBXgZQOVOtUN/MJ86ww+Ue7VjYXFJEbSsnGHTFgvBw5VOVQf?= =?us-ascii?Q?KkLdSMCd0ze2BfDGGQPU2vqKlCg3cR7dDSLa+sU6djvG9lxYVyeRlqw3NP6U?= =?us-ascii?Q?OseXCndiv5Q37enkLL1wtMAG4x1Me30kqfq49xONjpOgKZj6LkCHY+alEkA6?= =?us-ascii?Q?Yy8cbq3rGQb2sP6TpHHZdmChoF1mr2+UQHxMh5owEHMaxOj3k666BS0AAf3V?= =?us-ascii?Q?NkI+0n4Pz8EFM0LRypnOSDY+0w3RIqJojwN34ySww4cl7ZCghK+pXlrvPHAR?= =?us-ascii?Q?LcfOdwa//5MiCG05+C8dKk39QHFu5grgT+pxE10BZq3ugVbr8v44Qv/wYO6i?= =?us-ascii?Q?CuEnD0bEY9WJfvdEqf/gRQExxs/wcNlbFKetFpkx/+FiUpJzsn+zinh7wItJ?= =?us-ascii?Q?6P9mYHIwLzHd/Dp8LBo2NU/yyW3OSkr4dD2e+JQE36jemQfpFV4etWrLbkEB?= =?us-ascii?Q?/wjlJ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FPB7909nw?= =?us-ascii?Q?sjK7SqvUQQQc6wtvqKyXq85lZC/rMwXnyYCTwS3s?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce708f5e-7615-4d8c-a958-08dcfa86734f X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:14.8298 (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: ugqISsVnHsr9ItUxg7u1BX/sPEzvHbo3DjH/+B5fhpjUvLTCTY+C8XkOioM3pCQe X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6621 Content-Type: text/plain; charset="utf-8" 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 --- mm/huge_memory.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f5094b677bb8..1a2619324736 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4114,7 +4114,8 @@ static inline bool vma_not_suitable_for_thp_split(str= uct vm_area_struct *vma) } =20 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 =3D 0; struct task_struct *task; @@ -4198,8 +4199,16 @@ static int split_huge_pages_pid(int pid, unsigned lo= ng vaddr_start, if (!folio_test_anon(folio) && folio->mapping !=3D mapping) goto unlock; =20 - if (!split_folio_to_order(folio, target_order)) - split++; + if (in_folio_offset < 0 || + in_folio_offset >=3D folio_nr_pages(folio)) { + if (!split_folio_to_order(folio, target_order)) + split++; + } else { + struct page *split_at =3D folio_page(folio, + in_folio_offset); + if (!folio_split(folio, target_order, split_at, NULL)) + split++; + } =20 unlock: =20 @@ -4222,7 +4231,8 @@ static int split_huge_pages_pid(int pid, unsigned lon= g vaddr_start, } =20 static int split_huge_pages_in_file(const char *file_path, pgoff_t off_sta= rt, - 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; @@ -4271,8 +4281,15 @@ static int split_huge_pages_in_file(const char *file= _path, pgoff_t off_start, if (folio->mapping !=3D mapping) goto unlock; =20 - if (!split_folio_to_order(folio, target_order)) - split++; + if (in_folio_offset < 0 || in_folio_offset >=3D nr_pages) { + if (!split_folio_to_order(folio, target_order)) + split++; + } else { + struct page *split_at =3D folio_page(folio, + in_folio_offset); + if (!folio_split(folio, target_order, split_at, NULL)) + split++; + } =20 unlock: folio_unlock(folio); @@ -4305,6 +4322,7 @@ static ssize_t split_huge_pages_write(struct file *fi= le, const char __user *buf, int pid; unsigned long vaddr_start, vaddr_end; unsigned int new_order =3D 0; + long in_folio_offset =3D -1; =20 ret =3D mutex_lock_interruptible(&split_debug_mutex); if (ret) @@ -4333,29 +4351,33 @@ static ssize_t split_huge_pages_write(struct file *= file, const char __user *buf, goto out; } =20 - ret =3D sscanf(buf, "0x%lx,0x%lx,%d", &off_start, &off_end, &new_order); - if (ret !=3D 2 && ret !=3D 3) { + ret =3D sscanf(buf, "0x%lx,0x%lx,%d,%ld", &off_start, &off_end, + &new_order, &in_folio_offset); + if (ret !=3D 2 && ret !=3D 3 && ret !=3D 4) { ret =3D -EINVAL; goto out; } - ret =3D split_huge_pages_in_file(file_path, off_start, off_end, new_orde= r); + ret =3D split_huge_pages_in_file(file_path, off_start, off_end, + new_order, in_folio_offset); if (!ret) ret =3D input_len; =20 goto out; } =20 - ret =3D sscanf(input_buf, "%d,0x%lx,0x%lx,%d", &pid, &vaddr_start, &vaddr= _end, &new_order); + ret =3D sscanf(input_buf, "%d,0x%lx,0x%lx,%d,%ld", &pid, &vaddr_start, + &vaddr_end, &new_order, &in_folio_offset); if (ret =3D=3D 1 && pid =3D=3D 1) { split_huge_pages_all(); ret =3D strlen(input_buf); goto out; - } else if (ret !=3D 3 && ret !=3D 4) { + } else if (ret !=3D 3 && ret !=3D 4 && ret !=3D 5) { ret =3D -EINVAL; goto out; } =20 - ret =3D split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order); + ret =3D split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order, + in_folio_offset); if (!ret) ret =3D strlen(input_buf); out: --=20 2.45.2 From nobody Sun Nov 24 20:53:14 2024 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2069.outbound.protection.outlook.com [40.107.223.69]) (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 A7F8B19F131 for ; Fri, 1 Nov 2024 15:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473464; cv=fail; b=Shf6OuteucGNU4IHvrStSlNjqwBb06EO2v8I+W36QpTX/4H8a86cKvmTxGUevBeGH7JKdHe7LoV+QO6revqpJKdWBBhH4GZ6NRmYUrLKlc9BblD4S3hHv3ANykoEW1//IgdZ9fh9fzrys0VvUIweyyXYXQ0mCayIf+y+gerFGoU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730473464; c=relaxed/simple; bh=HL3X04qkydD4bB+r3Hnt729ZOIX6O8hAQQwzaJYfVsA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Nhfy5VOLIwF9kyrMqD8EcnwhJbCAq8sLgDJkJBJdklVSePmUn9FlyAnP0FTszrJ82AtSRNPH5uLvbEsB8/fmrl8t3LHuxec8y04OS0lGByjz5Z5AI5LBE5+2iwfw0SdONZZwpneF48zMbWO4ca2uVoIEwTMzPQ99kaSXut9RGUw= 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=oFJuAa+K; arc=fail smtp.client-ip=40.107.223.69 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="oFJuAa+K" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jjUBhVB4kJdF27GMJG0+tDiN1sMLXJraRLU5N5J/8NBuchCYwlMv27ctu1YhQ3udxO220zOkHdGQ5bhPRdqlnrceZqR3eo6JRCz5VppNI3c6RAnL2GfXVTMZakr7RDaY3NXb8X84IoAcV0FpVnhpMAgrxhaEGD8USDm8yNEFRWTSpz8jx1PzUV/rD6H9z8OZz6XOz7NiHQuR/9+y2ePgp9wxPmjoJaomL9+IIi6gY6ZzzvigoP0yntOq49MfeC501LoJmSct34z4P0hNaNwOkelaVudj+ITQgetZjyBOHQL65qn9x5bX/Mv2LYmWJYvqy6jVZdud4gEGPB0oZDc5tQ== 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=lI+AH28x7kVbKn6FgZA82KmuNv3qy1tf+VlP9HU+4jY=; b=rQA7kRukg0BB7HhdXxeT1jJSvloOCvW2MIEii1pw+Rh1wNPbLuQw5FhMW2inop2m3Fpw55IV2s1UAIYwtGlKCToK6tYBMDynN8FcWXThLvBy78Th1ux6WrKsHfbH3uQfH/I/T/4QjMhvYic3tKGfVMySqZPD0J/FWYwSU1ngbyOB4OiadS1J+oZDs3iw3TRzHI2+eQbDENsMeeDV5TXj/V0Li1J6V7dhTA9Hwgx3yuJno/bh/vsUTz+Js71NdsMaKBXT+2ykjRt55gGl6WTJKXT4M1zwvJ8XimWmdteK+KL8kVf0gNVDgRCrL9iTxyjnoIDUWRNFkcIzvG8yIsysFw== 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=lI+AH28x7kVbKn6FgZA82KmuNv3qy1tf+VlP9HU+4jY=; b=oFJuAa+KNelwsqXgZ2POTq0xuAs+q6SOqKPX8ZX63jBts5BOq1hl8gxl+w5v7nn5a9hs1o3EPtvs5TEeCsOMe4HeSB8oFNKC9zdNf+X6brZM48B1j+3FIGyRcXBbwm959vTREOnc6CurtrvVo8kZkhkHtrw4sz8fTMFg+la8qvkep6IcXazlZztiY/RkOksq7qvqmKho3B3kWJiZ9bygoputHr32jzTSGcQbE/Xz76gSx6dt43pnURAMom+peaZObk861ta+p40Ke/Ts2D+xhV2YknB878B8Qky9OGovY83bbVzUBm7bLQ7tgOwbDesJkx6vruB/CgfGJld+S6HvrQ== 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 CH3PR12MB9453.namprd12.prod.outlook.com (2603:10b6:610:1c9::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.18; Fri, 1 Nov 2024 15:04:16 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%7]) with mapi id 15.20.8114.015; Fri, 1 Nov 2024 15:04:16 +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 v2 6/6] mm/truncate: use folio_split() for truncate operation. Date: Fri, 1 Nov 2024 11:03:57 -0400 Message-ID: <20241101150357.1752726-7-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241101150357.1752726-1-ziy@nvidia.com> References: <20241101150357.1752726-1-ziy@nvidia.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BN0PR04CA0130.namprd04.prod.outlook.com (2603:10b6:408:ed::15) 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_|CH3PR12MB9453:EE_ X-MS-Office365-Filtering-Correlation-Id: 7cd5493d-1c37-43c5-c054-08dcfa867438 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?E7+tN4QhvbR50Mn6ojl/YYLw4YEa1wxSEem39qSRpT2cOjQKYhtCM1wqbSCI?= =?us-ascii?Q?6+shx1MyNs/SfKRX+gdnu2ZxQpItHOLPtsDCxkLO1SFPDEAH7zJIRqOND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PEaQZK0bMJiQ/l/BYe5b8w2CRfjqZHeMY?= =?us-ascii?Q?kxTsIi5tyfuczIGcI+OXqktjQdDUnpz5/ngfYygzvr7N3Kv0pklJMl6Hys6O?= =?us-ascii?Q?ajm81feF323sbTDBhk27puKrCpPC9a4N3ajnHtIdGOyiHi9qBzDjhpflCBYH?= =?us-ascii?Q?Zw/ZuPgMFlpK3oBGzE2WVrkkZfzTwcuiJHaE023lYcgFwzInjBE9jzfXzof9?= =?us-ascii?Q?OOrpPyupI7ErUayD+Nh66UXOWRB78KpeOOD8mXM/DvkKN+8SRLZxmdd+Yhme?= =?us-ascii?Q?xRwQWfoQWj8LOHANlohXI9ePArTQXcap1e/RZGU8vOk5+zp9USlfIktEl8d3?= =?us-ascii?Q?/mjnnB1n3ZftkhyLiE+D9xnV?= 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?bm70WWlL0Zgu9Y8GJvMkTLecZhz8T+Cnc/o7YJuonvewZGHmSN4QmKrO/0KG?= =?us-ascii?Q?OFWcmQZCPSIP9K/19xPrQVh2HZt5mSrIeokIL8YAuenM1K9hDBLgPl4E0m54?= =?us-ascii?Q?kAHHtirTRWltECQ+tE5rLuWhYPMu1EmaDfKSnHcOkg23JYSDj/zOTGEqs4oi?= =?us-ascii?Q?fK/Ejx2Gntbt7kuJ/DImCSMmVz4SOM+bFyIOvgTfgDMCf4nS0tg7AXmltC7g?= =?us-ascii?Q?C4WqzusQF4txrj+adTgA9qQ3gsmGbib+NBi5SEqU5qTHZBIiZWui04sG70uR?= =?us-ascii?Q?Banjpsg4W89ldl5/Gp0E7VCGzCfbJjTj6jfvNGJI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SpfcYD2QSHLnTZJ12j/oI3/rRNALwt7h+p7Qm7mPNGUqkgi?= =?us-ascii?Q?eyzqiv0iaAaaRPKxr85B/KmEBMxu0x4+p2pGIGz3YIi+HlDAi2SRqrN9u5RI?= =?us-ascii?Q?sUTCC8IXHYiN5OKW6OJFdR5vL/AermKI8LX4p3CEd/hg4RuBv6JDoz4nbrhY?= =?us-ascii?Q?zon68+FLxXZebjsINOYkwmNHj0I/czd4/jrUG7g0GCCkdU0Ikkub7WS9ihaq?= =?us-ascii?Q?rlLcdkbVlCbHU8t6hJVgjHKC6xN2PhBBnqt7nARHlnfyFqSh5AiQPWzTcw6R?= =?us-ascii?Q?WI/tIWYJ6002GAWPHsAoXthCob+MXm+qIJC1+WlZhxEpPFDIwIP1sN1XDYb8?= =?us-ascii?Q?Qe8dHIGt+x9vfppp+k+t0bym8cbp4xCuwh7kpnHw?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7cd5493d-1c37-43c5-c054-08dcfa867438 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 15:04:16.3650 (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: OljR/ToYuKDg40rgbQSGjP8Y2qY204OfODaQ+qkAJkU27A9HCVIn0V8hrVHqWyAK X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB9453 Content-Type: text/plain; charset="utf-8" Instead of splitting the large folio uniformly during truncation, use buddy allocator like split at the start of truncation range to minimize the number of resulting folios. For example, to truncate a order-4 folio [0, 1, 2, 3, 4, 5, ..., 15] between [3, 10] (inclusive), folio_split() splits the folio to [0,1], [2], [3], [4..7], [8..15] and [3], [4..7] can be dropped and [8..15] is kept with zeros in [8..10]. It is possible to further do a folio_split() at 10, so more resulting folios can be dropped. But it is left as future possible optimization if needed. Another 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 investigated when necessary. Signed-off-by: Zi Yan --- include/linux/huge_mm.h | 12 ++++++++++++ mm/truncate.c | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index b94c2e8ee918..8048500e7bc2 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -339,6 +339,18 @@ int split_huge_page_to_list_to_order(struct page *page= , struct list_head *list, unsigned int new_order); int min_order_for_split(struct folio *folio); int split_folio_to_list(struct folio *folio, struct list_head *list); +int folio_split(struct folio *folio, unsigned int new_order, struct page *= page, + struct list_head *list); +static inline int split_folio_at(struct folio *folio, struct page *page, + struct list_head *list) +{ + int ret =3D min_order_for_split(folio); + + if (ret < 0) + return ret; + + return folio_split(folio, ret, page, list); +} static inline int split_huge_page(struct page *page) { struct folio *folio =3D page_folio(page); diff --git a/mm/truncate.c b/mm/truncate.c index e5151703ba04..dbd81c21b460 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -179,6 +179,7 @@ bool truncate_inode_partial_folio(struct folio *folio, = loff_t start, loff_t end) { loff_t pos =3D folio_pos(folio); unsigned int offset, length; + long in_folio_offset; =20 if (pos < start) offset =3D start - pos; @@ -208,7 +209,9 @@ 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) =3D=3D 0) + + in_folio_offset =3D PAGE_ALIGN_DOWN(offset) / PAGE_SIZE; + if (split_folio_at(folio, folio_page(folio, in_folio_offset), NULL) =3D= =3D 0) return true; if (folio_test_dirty(folio)) return false; --=20 2.45.2