From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012060.outbound.protection.outlook.com [52.101.48.60]) (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 EC6792F3C18 for ; Thu, 13 Aug 2026 04:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.60 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786595106; cv=fail; b=O7avBb9k0+5/zg6wjaz+IfILkkMTMqnUcjHuhlIXkHp5KcilfCBihm1zXNPDb6jiedsYBUW+ujjFFRb6v7o3stasUgLbePBnYXGr2vIBseEvXlTu+2kMj4UC9CiZhHhFVOraydgiyh0+qfVh574WYaYotHNROXCxXRQzB/pxEko= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786595106; c=relaxed/simple; bh=N04qQs6ab3QOp2plxNgK76AdZ7rwOslb9VXB2PbRwNk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=ESbMkiRVcq1NsZ/U/1RF0qSidIo8M1CByrrFowyse1D4ykazLh8FCkT4KdT7DjkEtpk0A7WDL96/hnBLp70rSBmqu0GD+ptNwtXcgN4FBjBK1pjgL5BvfKZR+9rhDDU5r0J6wFZ3HpALEwDgtgHwxQn2ZIGHoLvLMGEHgR5+M2g= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=x3fd42jB; arc=fail smtp.client-ip=52.101.48.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="x3fd42jB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ns+WIQmp0dLl/WQQgCyX8jeGpRqzoiI4rWTRBxLCG6vD4MxExKKukgp7LsY0v6UPZpA0M6mg2zBpjKA6g6NfjLvBOOKXY94J5i00rmtk9JtnSVfNm1bv3iwMELg4uumof11louGVtZi8LLLqBzWo0/h2UD17Ss5wTmTqQtqP9DFNUKsqhrTGogVYRYgmiSnc7Q1KVtJc4kd14HxctCIBEtRKLmELPfufFasm0FkOucFTBdzLYc2/g+nprnYgMnZBfQmjFb3o8/HBfoHAAgp8lXVkeLez41k3rGsibLmfybO9xC3+xY+uyLvszLLl2uGe2YzTfQsMdOnRw18RROzJOA== 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=xJmcLgRoaeaouSkf3yJv6TF6RMZzz0FrmGos9xFDxFc=; b=G1z5TVFWiSdqVPC2L+8xgbJlHtnUAXYacTleZQuoHfRsFoI3s12UY4VFoZr3bfg6UyYj/8ww2px0CcPXANSdKUtKuiRoK5GVlHnyFtMyOHuRCDWHCwLS665NrMNE0L8xeVFg7levicNaneXLRwtYkL9QonNgnMK73rzfMbhkOHxeYEyxST0M7Bsuw9EocyeUT/sfW4I6Djt1kQQiHtUxJMJf/x0ZnHXXbC+85tgOJbPCCPDRirkFVu7hX7V5M/rZnICe1h9SkVQDXGY5ovd8RcpSSrhcMo1tU2EBdY1n4nQ2z1XMUirdEcqsXZA4bXlGYCQV+v1g2Jc5njJWEdC3mg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=surriel.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xJmcLgRoaeaouSkf3yJv6TF6RMZzz0FrmGos9xFDxFc=; b=x3fd42jBBoFApQ9qSgNFVts1TnhSLPEclP2/38dK7NQs25ObQ++rcPwWGU34hW4xJsNCJrEYAftEvl14c/NiQMZA4UZiTGzyyypRk5FTw3A0V2u0pgqdIayMPeL5UziHaop4eYeKwBDkWprunjyd02hDRt3ACn8TMga/PnogaVo= Received: from BLAPR05CA0005.namprd05.prod.outlook.com (2603:10b6:208:36e::14) by CY1PR12MB9601.namprd12.prod.outlook.com (2603:10b6:930:107::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.315.14; Thu, 13 Aug 2026 04:24:55 +0000 Received: from BN3PEPF0000B371.namprd21.prod.outlook.com (2603:10b6:208:36e:cafe::91) by BLAPR05CA0005.outlook.office365.com (2603:10b6:208:36e::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.339.3 via Frontend Transport; Thu, 13 Aug 2026 04:24:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BN3PEPF0000B371.mail.protection.outlook.com (10.167.243.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.339.0 via Frontend Transport; Thu, 13 Aug 2026 04:24:54 +0000 Received: from [127.0.1.1] (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Wed, 12 Aug 2026 23:24:46 -0500 From: Shivank Garg Date: Thu, 13 Aug 2026 04:23:18 +0000 Subject: [PATCH v2 7/7] mm/rmap: batch the unmap of large folios in try_to_migrate_one() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <20260813-migrate-rmap-batch-v2-7-3c5424c555c7@amd.com> References: <20260813-migrate-rmap-batch-v2-0-3c5424c555c7@amd.com> In-Reply-To: <20260813-migrate-rmap-batch-v2-0-3c5424c555c7@amd.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Rik van Riel , "Liam R. Howlett" , Vlastimil Babka , Harry Yoo , Jann Horn , Lance Yang , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Zi Yan , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , "Alistair Popple" CC: Karim Manaouil , Frank van der Linden , Kinsey Ho , Wei Xu , Bharata B Rao , David Rientjes , Dev Jain , , , Shivank Garg X-Mailer: b4 0.15-dev-47d62 X-Developer-Signature: v=1; a=ed25519-sha256; t=1786595034; l=7623; i=shivankg@amd.com; s=20260518; h=from:subject:message-id; bh=N04qQs6ab3QOp2plxNgK76AdZ7rwOslb9VXB2PbRwNk=; b=LZGQQRKQxxrd5UfHQJW67iLHdSc9AzHrsMjwBxSj4DBrm7sUVRZomdXWG4/fFq8Ad7t5c3mfj QO6Jw4nyR60B9R/9He0xlAFU9Xhjd2luoGU9D8S/uaTvVXUJTmcUDUM X-Developer-Key: i=shivankg@amd.com; a=ed25519; pk=2l2QGTeXuGkZTtfmx0nPQU8iFZfjYmX/ymMojitevx4= X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN3PEPF0000B371:EE_|CY1PR12MB9601:EE_ X-MS-Office365-Filtering-Correlation-Id: d9cb8934-ae4a-4355-c3a4-08def8f2d336 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|376014|7416014|36860700016|82310400026|1800799024|921020|56012099006|10067099003|11063799006|5023799004|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: slDoFa0XVeCXamLiOKfcrYlDMUSkk+6/44H2Hljoj0sEsNrdyG8GC9Q40zhGT5pVSij1vGxGom9suOY3tT3lLLfDbPiCoNSWVKlWTOly08wRjIyJlpMHsiEt/L1C0ediR9z66IghQCLzMl6GAMp6PpBfe9anwXd77Ogi2tj/lOVG+SoJJ8jxy3pH++cM5sAHjg0pKVjo+up5MTiPJT5xFx23Is27xpE2FbhopqrRF+Eep3R2HMilYAD3CdPIHhUcnY7lN+tIyo/nt92DELZq8uCz1ME2I1kkx6bEb8mR10hwcEIiry6aEHuWoiTTPNi7laTfal8rk7HCRM73JiMaLr7tKAiwRxsTgWxnUhCHeckhgV5CJlXi08oZZFNH14N7LDFJMcKyINBwWPcDhqE0VaztIcfkaicDMpdsgnGlwcms1OBe4SsgFhxHmkotOgr/A59OnitmNvjEINK0T5a9HL4HdIEgaLDXDtnJn6GbJ+c56ySi23/+uU/+uXwchKICqV2SXKp8NFeEWQ2Zr2bZ9MpoAIK+dvUrfAptyTerAv+HQUWirTKA6GdzE1UkTbenW+VbafEOpEyuwBxpOBWQQVFf/FHWMJMf0gQZ00wlRcoS+N1E7fdGQb6Tp/KEd9vVK8NN3L+5zpoB15wKX4l2UlflNFcQib8O/Qmx3/Rm+m+gCEkwmM4bpfvv+WW1IVy2a2HqOF92Kumi+Tvx/6P5dr5BBwBsljpCip2/8J97W21dTNWGT52jThalMRRul5ln X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(23010399003)(376014)(7416014)(36860700016)(82310400026)(1800799024)(921020)(56012099006)(10067099003)(11063799006)(5023799004)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8Hmd0TyVHivaYeSWowyOE5RXs+Vt/crDQk32hQG97Mlws36m/5I7CipAGBWrJyl2aa7gyGCBrM6ztDTFmpWZRJtlqKld0e4qs855DRybmNt7rUlIojUKPgx9gU7lHR/AgVCsvFdiFUxQu66lvdyHdjow4hG7fT/csGPuWpWCYS0Lzi2ongPw3e+5lNojTfAPuyOulkVwEoDIvMm8beQYALNV4UO323UEe0xtps70X2cNh5eyK5JnRhcCrJCC69msgRY0kKcN6sIUHZwqBbcTN00aCzSQM3QWVJAsq9SiojamBpuHK9KOiQoaaoihyaPF8rQlwX8buVL8plavSub/6AQUZOF85L6+oj07OBWFuZ+dUf3anzyhHKb3C/Xbh4Wim0RGWOx75TVbBtL+mDJSb2aDadWd5MReLHx9A5eozxhXqTRNHHyaG4XFAefiUQZg X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Aug 2026 04:24:54.2139 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d9cb8934-ae4a-4355-c3a4-08def8f2d336 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN3PEPF0000B371.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB9601 try_to_migrate_one() converts present PTEs to migration entries one at a time. For a PTE-mapped large folio, this repeat calls to ptep clear+flush, the migration entry build and set, folio_remove_rmap_pte() and folio_put(), each re-entering page_vma_mapped_walk() once per base page (256 times for 1M folio). Mirror try_to_unmap_one() to introduce folio_migrate_pte_batch() to detect eligible batch for PTEs mapping conseuctive subpages of a large folios, and convert the whole batch in one shot using the batched helpers. A side-effect of this change is trace_set_migration_pte() will record one event per batched run instead of earlier behavior of one per base page. Signed-off-by: Shivank Garg --- mm/rmap.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 29 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 35752a70f3a0..63b885c0b7ef 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2675,6 +2675,44 @@ static bool try_to_migrate_hugetlb_one(struct folio *folio, return ret; } +static inline unsigned int folio_migrate_pte_batch(struct folio *folio, + struct page_vma_mapped_walk *pvmw, pte_t pte, + struct page *subpage, bool anon_exclusive) +{ + unsigned long end_addr, addr = pvmw->address; + struct vm_area_struct *vma = pvmw->vma; + unsigned int max_nr, nr; + +#ifdef __HAVE_ARCH_UNMAP_ONE + /* Cannot batch unmap if arch_unmap_one() is defined. */ + return 1; +#endif + + if (!folio_test_large(folio)) + return 1; + if (folio_is_zone_device(folio) || folio_test_has_hwpoisoned(folio)) + return 1; + if (pte_unused(pte)) + return 1; + + /* We may only batch within a single VMA and a single page table. */ + end_addr = pmd_addr_end(addr, vma->vm_end); + max_nr = (end_addr - addr) >> PAGE_SHIFT; + /* + * If unmap fails, we need to restore the ptes. To avoid accidentally + * upgrading write permissions for ptes that were not originally writable, + * and to avoid losing the soft-dirty bit, use the appropriate FPB flags. + */ + nr = folio_pte_batch_flags(folio, vma, pvmw->pte, &pte, max_nr, + FPB_RESPECT_WRITE | FPB_RESPECT_SOFT_DIRTY); + + /* Limit possible batch count to a uniform PageAnonExclusive value */ + if (folio_test_anon(folio)) + nr = page_anon_exclusive_batch(0, nr, subpage, anon_exclusive); + + return nr; +} + /* * @arg: enum ttu_flags will be passed to this argument. * @@ -2686,12 +2724,12 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, { struct mm_struct *mm = vma->vm_mm; DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); - bool anon_exclusive, writable, ret = true; + bool anon_exclusive, hwpoison, writable, ret = true; pte_t pteval; struct page *subpage; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; - unsigned long pfn; + unsigned long pfn, end_addr, nr_pages; /* * When racing against e.g. zap_pte_range() on another cpu, @@ -2744,11 +2782,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) || !folio_test_pmd_mappable(folio), folio); - if (set_pmd_migration_entry(&pvmw, subpage)) { - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; - } + if (set_pmd_migration_entry(&pvmw, subpage)) + goto walk_abort; continue; #endif } @@ -2773,10 +2808,25 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, subpage = folio_page(folio, pfn - folio_pfn(folio)); anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(subpage); + /* + * memory_failure() can set PageHWPoison concurrently without holding + * the folio lock. Snapshot the flag here to decide whether to batch + * PTEs or install hwpoison entry. + */ + hwpoison = PageHWPoison(subpage); + nr_pages = 1; if (likely(pte_present(pteval))) { - flush_cache_page(vma, address, pfn); - /* Nuke the page table entry. */ + if (!hwpoison) + nr_pages = folio_migrate_pte_batch(folio, &pvmw, + pteval, subpage, + anon_exclusive); + + end_addr = address + nr_pages * PAGE_SIZE; + flush_cache_range(vma, address, end_addr); + + /* Nuke the page table entries. */ + pteval = get_and_clear_ptes(mm, address, pvmw.pte, nr_pages); if (should_defer_flush(mm, flags)) { /* * We clear the PTE but do not flush so potentially @@ -2786,11 +2836,9 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, * transition on a cached TLB entry is written through * and traps if the PTE is unmapped. */ - pteval = ptep_get_and_clear(mm, address, pvmw.pte); - - set_tlb_ubc_flush_pending(mm, pteval, address, address + PAGE_SIZE); + set_tlb_ubc_flush_pending(mm, pteval, address, end_addr); } else { - pteval = ptep_clear_flush(vma, address, pvmw.pte); + flush_tlb_range(vma, address, end_addr); } if (pte_dirty(pteval)) folio_mark_dirty(folio); @@ -2809,7 +2857,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, /* Update high watermark before we lower rss */ update_hiwater_rss(mm); - if (PageHWPoison(subpage)) { + if (hwpoison) { + VM_WARN_ON_ONCE(nr_pages != 1); VM_WARN_ON_FOLIO(folio_is_device_private(folio), folio); pteval = swp_entry_to_pte(make_hwpoison_entry(subpage)); @@ -2837,19 +2886,15 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, * so we'll not check/care. */ if (arch_unmap_one(mm, vma, address, pteval) < 0) { - set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; } - /* See folio_try_share_anon_rmap_pte(): clear PTE first. */ + /* See folio_try_share_anon_rmap_ptes(): clear PTE first. */ if (anon_exclusive && - folio_try_share_anon_rmap_pte(folio, subpage)) { - set_pte_at(mm, address, pvmw.pte, pteval); - ret = false; - page_vma_mapped_walk_done(&pvmw); - break; + folio_try_share_anon_rmap_ptes(folio, subpage, nr_pages)) { + set_ptes(mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; } /* @@ -2859,19 +2904,31 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, */ swp_pte = make_migration_pte(subpage, pteval, writable, anon_exclusive); - set_pte_at(mm, address, pvmw.pte, swp_pte); trace_set_migration_pte(address, pte_val(swp_pte), folio_order(folio)); + + /* Set nr_pages migration entries, advancing the PFN. */ + set_softleaf_ptes(mm, address, pvmw.pte, swp_pte, nr_pages); /* * No need to invalidate here it will synchronize on * against the special swap migration pte. */ } - folio_remove_rmap_pte(folio, subpage, vma); - if (vma->vm_flags & VM_LOCKED) - mlock_drain_local(); - folio_put(folio); + finish_folio_unmap(vma, folio, subpage, nr_pages); + + /* + * If we batched the entire folio, there is nothing left to + * walk; stop right here. + */ + if (nr_pages == folio_nr_pages(folio)) + goto walk_done; + continue; +walk_abort: + ret = false; +walk_done: + page_vma_mapped_walk_done(&pvmw); + break; } mmu_notifier_invalidate_range_end(&range); -- 2.43.0