From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011045.outbound.protection.outlook.com [52.101.62.45]) (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 09D082F1FDF for ; Tue, 29 Sep 2026 01:53:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.45 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790646793; cv=fail; b=DzyvHY8zJ1Jz0aoM4EeEXI3S9AKrIPsGwUL5LF3crzT4+ktGGGk62LKsw7vVSclkDtvzh3mP0tkHaEVtvT1ep5dJkxa0xuTgGDh3EoBFGHlciC6bi9LBnwp14ckAV0hLlOIAuN409hf7Od8AdL6uy1BKOukRSnqyaEvHUfR82Rw= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790646793; c=relaxed/simple; bh=AZF1QvMtb98579z8AWFdcAhNsAFRtfuomV5TiC+6vNE=; h=Content-Type:Date:Message-Id:Cc:To:From:Subject:References: In-Reply-To:MIME-Version; b=VkB8mnHPurnWzXg5x8jFHN8J17BsawpvmpQDjvGepaeCPKL+w9rGzCGj7I8fbQ5t+VKmlgIiixahq+EqXdKB8/+t4/ZyoMSjnwgeykMnpFAS4Xmvez582eCAsQpaFG4dq1Qn+ncg6sm+nPv2r59DnrJpwCICjSrGYetYFanEHAY= 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=S7Ue9AhX; arc=fail smtp.client-ip=52.101.62.45 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="S7Ue9AhX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xsmCrY9MTv1nz8eby5VWpPSobjvbSpQKcZbbwhFbZbiyt9xaB4fEgYEaTo5qPAQQhgQ0Ge52gDSL3O478xV5uDT8iEbPx6WWPeoI6FMrDxg21DEg/m4+X6nbr5tUFncqMQxXgnzQdgiZdP8mT4KOQSweAOFXFeaZQKwmgT4S9T1tb18yoIi1vlm290H32GxO5G7vXu+h52UqWJIBfwB+jivgGaLMsFVRstv+PPKkbhabKvJCVBUm+re0i5eOQpuLhxbgyFEeJtEfUrSANJgM52YKidvDyQnSeyE+GHm4DMb46GwMNeXljp1pU4JP0r+Mlhr0zl89LaJikRjimIFC8Q== 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=gXYxAZ9m7UJndTCehaAcsYFtF+EyXG0B7m+hXglqxnc=; b=LIt+K/jY5eIBviH9FmffSMTeZBfEq6CCu/DavXk2yBQey0b1oyvH2Wb9UkoEzRUyg/Q/xXdl88TTDLktchX2ryJTRTe85NzFWwujWj2HIo94LEGWTgbHrK5ZDhZ9sC1bxAqtb4N+WBK6/BzJia6KsWUuvuO4uCc7o5aM4nLRjpJg+oU590/xRrTnMGCF9+i+J0xgCF1O9Ci31KxURfPyKIPqOtaCuozrMTB9HBLev4E09B6fWSSNiHnNQOMPfb22OeF+hf1vQtBGSftSOmnGNSx1dqae1LKL6Fjt38najuSxIYCwmI8/mB5Htv2Ybl6L/d0bWAd5/8oRsXFor7f/ag== 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=gXYxAZ9m7UJndTCehaAcsYFtF+EyXG0B7m+hXglqxnc=; b=S7Ue9AhXDNyzd+oiDfGLPnMx7hCIyjbupDkOVj3V4k5zkGiwbZCCAw6W9+rVMJasmAcaTcbfvDovpItwCNz3QaRCo3maXCyFPQALHF05vD/zrPwJZdyaxKhRBp2pBuBcsonHz891GZRgRHcDE6nop5FHwgMO42XihY5llyTXwlHXAGaLxYdGGClkOrDYOIq6wxmjXdioZKCuPF6+V4BEaZ7LthkhBckTpsk9eRA/h1zLDRPvi+WjIXPf6ct7h9EKklwxCoG4vBywjNEk9KYwxuPMiRVFFu2KzqPTbEs1HpQxpLL59psYR/ar3i+PYAbOQX8jRxZMi5eljOnUiJggsQ== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from IA0PR12MB8374.namprd12.prod.outlook.com (2603:10b6:208:40e::7) by MW4PR12MB5626.namprd12.prod.outlook.com (2603:10b6:303:169::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.24; Tue, 29 Sep 2026 01:52:48 +0000 Received: from IA0PR12MB8374.namprd12.prod.outlook.com ([fe80::d85f:4c87:ae84:3f16]) by IA0PR12MB8374.namprd12.prod.outlook.com ([fe80::d85f:4c87:ae84:3f16%6]) with mapi id 15.21.0451.022; Tue, 29 Sep 2026 01:52:47 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 28 Sep 2026 21:52:45 -0400 Message-Id: Cc: "Kiryl Shutsemau (Meta)" , , , , "Liam R. Howlett" , "Nico Pache" , "Ryan Roberts" , "Dev Jain" , "Barry Song" , "Lance Yang" , "Usama Arif" , "Vlastimil Babka" , "Jann Horn" To: "Kiryl Shutsemau" , "Andrew Morton" , "David Hildenbrand" , "Lorenzo Stoakes" , "Baolin Wang" From: "Zi Yan" Subject: Re: [PATCH v4 09/13] mm/collapse: separate scanning a PTE table from collapsing it X-Mailer: aerc 0.22.0 References: <20260928100630.21870-1-kirill@shutemov.name> <20260928100630.21870-10-kirill@shutemov.name> In-Reply-To: <20260928100630.21870-10-kirill@shutemov.name> X-ClientProxiedBy: YQBPR0101CA0093.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:c01:4::26) To IA0PR12MB8374.namprd12.prod.outlook.com (2603:10b6:208:40e::7) 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: IA0PR12MB8374:EE_|MW4PR12MB5626:EE_ X-MS-Office365-Filtering-Correlation-Id: 3c83a928-2ea7-45c9-bc40-08df1dcc5c4b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024|23010399003|4143699003|22082099003|18002099003|6133799003|56012099006|10067099003|11063799006|5023799004; X-Microsoft-Antispam-Message-Info: aCl4mvEoAhS2GMxnGXPdF1nVOZDm6vyjevzMIdJBALGeFb8EC0ZewfgVp9etJu/vQDTn9h8yBhorSENYr/wdawkQkZXrO7J1CJptVFFHaeH+klTZ8CtQ/RE/ZB8FKllrdINyNrYRrN1yFDbQsEVL6HuR0z7zFIGr6lvTJGqzKJ90Lsx4stCMVmMEANMpB0txQW6Elih+NSsNLJILys/8nL5mFPiC7FVZ/5ZQaJ6maxhUGGstmxyrvN+awYzNFNIh0Nvdo1x+vRvhAZbG/qoysHe+f5thrmBtlv6EIBUJ3B8vhSRs+53z+9XFPoMymOy4c1vQa6qtkHTGb6WneyJeqbahjlbXtfnkqKd6kvmKu51ku8epXQA126bF3DG/roDXVdwhGrhjf/vgFZNEZ99DCBoRNaJbCOYyHWvE/6EB7dfdC4bPLtWUo4qDfL4r23OSB4H51ZYvR7sIfKABMVfqRRHsoBRgKMLI28XcXI82hMa5EECDBScPq8ZnkVPMduHX4U3VSDzQQvX1C8zdlml920IkG0fQLTrJPKmgWkrSYG9O0/M1mANZ8vSb9WyAyZi/gW3GM66PSCAyGGMvp331c4xrc1eh7Z4IFK8FMxMLPSc8FK/60lzGxCAiZWBFcGSTOvu/u25+6Tn7Xl+cVPgCY06gjPJ2Kzip1WjODGVRwwI= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:IA0PR12MB8374.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(7416014)(376014)(366016)(1800799024)(23010399003)(4143699003)(22082099003)(18002099003)(6133799003)(56012099006)(10067099003)(11063799006)(5023799004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?d3FQR0ZVUkFJRXNRME1Hb0F2Ujlsb2VzV0t6dmpKZy95dXh3SXBnSnU4Q0Ew?= =?utf-8?B?SlFJcVQ5WlE2NFROYTBwUG9YdjlUeXFCQXorTXdkWDU2QVYrRk9QQjhSeXlj?= =?utf-8?B?NWpjMXI2Rk5BZ2VzWGdoY2I2U3NRMFI5SmhNRVEvNElOaW90M0N6S25DYU44?= =?utf-8?B?V0xLL1FCeUxtVGhQbnIyR2xFVGtRU3VkTFAwMVlKcDFSc0NuQjlLS2pMSjUz?= =?utf-8?B?T1llc0ZiMlNUdzVneEtnV2NHNmFBOTNka0FDcUdGQW5ITEl5LzJDZndFakRB?= =?utf-8?B?R0lwNG95L1YrRitJVkdSUFUrTEpEcWRZRmIvZ3RXWkJRNUx0eU95Wml1Y2NY?= =?utf-8?B?Q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bkVPanpzVmU2Ymc0SGJG?= =?utf-8?B?YUxTeGlJVUZKWmVxYlNTWU43L2xqL3N4RmF2aGJTUVM0NWlkMVE4bFhTRlJr?= =?utf-8?B?c2J1aS9YL05YUHE1Zm1pQkozQnFaQ29QSjlRWEQxdkRWZ1hjT0hUTnBPVk5P?= =?utf-8?Q?r8JI6pnWJTgcDB4hlL?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3c83a928-2ea7-45c9-bc40-08df1dcc5c4b X-MS-Exchange-CrossTenant-AuthSource: IA0PR12MB8374.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Sep 2026 01:52:47.0102 (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: 6wXBzJlbuzHweME8QqbXORYxSO8I0QaEq26qQWsDLsUBikmMIdeso/UgZqNNHYnp X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5626 On Mon Sep 28, 2026 at 6:06 AM EDT, Kiryl Shutsemau wrote: > From: "Kiryl Shutsemau (Meta)" > > A collapse is two jobs. One reads a PTE table under mmap_lock and decide= s > whether the range is worth collapsing. The other allocates, isolates, > copies and flushes, and wants the lock given up first. > > collapse_single_pmd() did both, so the boundary between them was somewher= e > in the middle of a function. > > Give each half its own function: > > - collapse_scan_pmd() scans one table. The anonymous scan that used to > carry that name keeps its body as collapse_scan_anon_pmd(), and > collapse_scan_pmd() is now the entry that picks the anonymous or the > file side. > > - collapse_run_pmd() does the collapse the scan asked for, and is > handed what the scan returned. SCAN_SUCCEED means there is > something to collapse. SCAN_PTE_MAPPED_HUGEPAGE means the page > cache already holds the PMD folio and only the PTE table is left to > retract. Both are work for the run; anything else is why there is > nothing to do. > > collapse_single_pmd() is now the two of them with the mmap_lock drop in > between, so its callers see what they saw before. collapse_control_init(= ) > sets a control up before its first scan. > > What the scan found and the run needs travels in collapse_control. For > an anonymous table that is the orders and the referenced and swapped-out > counts, which mthp_collapse() and collapse_huge_page() now read from > there instead of taking as arguments. For a file it is the file itself > and the offset in it: a file collapse works on the page cache and never > sees a VMA, so the scan takes the reference while it still has one and > the run gives it back. > > The file scan moves under mmap_lock with the anonymous one, where before > the lock was given up first. The lock is now held over the page cache > walk, an RCU walk over one table's worth of slots with no PTL, and taken > fewer times. collapse_scan_mm_slot() ends its walk whenever the lock was > dropped, so a refused file table used to cost khugepaged an unlock, a > trip back through khugepaged_do_scan(), a relock and a VMA lookup. Now > only a table that goes on to be collapsed does. > > Assisted-by: LLM > Signed-off-by: Kiryl Shutsemau (Meta) > --- > mm/collapse.h | 14 +++++ > mm/khugepaged.c | 141 ++++++++++++++++++++++++++++++++---------------- > 2 files changed, 108 insertions(+), 47 deletions(-) > > diff --git a/mm/collapse.h b/mm/collapse.h > index dcd117071955..ca7b367c89cb 100644 > --- a/mm/collapse.h > +++ b/mm/collapse.h > @@ -98,6 +98,20 @@ struct collapse_control { > =20 > /* Each bit marks a PTE the scan accepted as a collapse source */ > DECLARE_BITMAP(eligible_ptes, MAX_PTRS_PER_PTE); > + > + /* > + * What a scan found and the run after it needs. Live only between the > + * two, and read by nobody else. > + * > + * The file side takes a reference while it still has the VMA, since a > + * file collapse works on the page cache and never sees one; the run is > + * what gives it back. > + */ > + unsigned long scan_orders; > + int scan_referenced; > + int scan_unmapped; They are used by anon only. > + struct file *scan_file; > + pgoff_t scan_pgoff; They are used by file only. I think the comments need to address them separately. Otherwise, LGTM. Acked-by: Zi Yan --=20 Best Regards, Yan, Zi