From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11010070.outbound.protection.outlook.com [52.101.56.70]) (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 04F242561A7 for ; Sun, 12 Jul 2026 07:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.56.70 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783842071; cv=fail; b=Ddo4Qrf0Bomgc4gps/Hx35Uu+IwYO/OcQhnMk6nBtsVJCHrhv05xOWYz+efLonavN914K0dryaaKWjlvKzAkUFj+g9vrdcOhTv1qU7Sch6Nn6UhrYopJSr6rvYGGCGrhTmwJ/ptl7gcVXJO85xw0P+PywFZYm/OtJbw0mKgynMk= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783842071; c=relaxed/simple; bh=srTISDEaD/Ktq0rhvvy6JqlSSASppyptfsmb53TFtgU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=PWfaIbKeeRsGB9Y0M4/+OAYujWesaMjs5P5cCKnUjT/Z6dsDVYtTSN1BPtKdst9dC7tgH8O9xxNvYkzMkrKdylyMo15ROb/mW91qcH6NUYMqvzZzRWQgP4LMRAskDyaBaiJddT92IwjHi5RsHkoZeAqWUVeK5AgCzWPHlgcMwf4= 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=yXilX6+s; arc=fail smtp.client-ip=52.101.56.70 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="yXilX6+s" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RKhNOd20jRrZjwqUQcabPs4Jp0c4YV7QJkesXM7tf6FKEo4GTutdrQMh21D5vPYYGjDo6YzitTcmx2azB3tQJt8E7j4WoYi7tEHiy7pdZ0sMCq/KjUuuwufnHvO1zAZzkfDpxpQ3RGwb+ZHttyWvJy+ntMUkE3SIY337yHKWghQ904FP2hIMj0Z3BTnLbkyGDI/WXcAqYVU1ZDhnpBQzMNca2fvMhrcOOVzt1jn9OvlREiIlapcpBwgr/U+K6xP/ux72sJJcIjaJpydsXKkL4dd6ZeumarsTxaRONTwig0CvcPjBlJMBnGrr63Jr+rpzBNSAGwZfyZWxcxhj/9oFTw== 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=+pnx6PsVHcrvSxWAaNCSG/M3DsulMI24w6J6EwBrV9o=; b=MgeFI/DR8l0v245pUQFiHDxf2SHnWDwblR9om8qQGi7U5IBrUAXvmlkYMZk3kEnHC6UopB3lG7OPrV2/QjiVVXitPQVmFkutF1IQUuhRISDH2VZ8xMjdPR489oH6hOnt38DBgcnrcVKugYKH1xNFUXMbheck9+UnTyeJwDNeHISONQm00REpC17XGzThhRPF/CNkpTT19eoo/cX5ak5oQwySiCZFJxYFVI3AZS/m4IfH4+olti8oXrONm+WdiK5lZPWhz/c81c9s49Uv32NfY97OR3nmVZ9wTh2241niQFkaCkaW2P9b90z+YJiWsVAj2/zmt/5FG2iwiIqzN5pgsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=google.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=+pnx6PsVHcrvSxWAaNCSG/M3DsulMI24w6J6EwBrV9o=; b=yXilX6+sCKtSqFgF5iwqJIiozRhLtV2nLwFHNolNY3L7IPia5Ua6Unng90Og40Bihwp1f4rFLfbfvGIsXSO05uRV3SGA4OXRsjP0mvrfYyS4P4yjbkg+wUDxlPBaNVBsmL9ep7VUKrBoFGiPKWE2AKsibNoJ45kQIBHT+Yvt96g= Received: from MN0PR03CA0015.namprd03.prod.outlook.com (2603:10b6:208:52f::17) by SJ2PR12MB8717.namprd12.prod.outlook.com (2603:10b6:a03:53d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.19; Sun, 12 Jul 2026 07:41:00 +0000 Received: from BL02EPF0001A102.namprd05.prod.outlook.com (2603:10b6:208:52f:cafe::65) by MN0PR03CA0015.outlook.office365.com (2603:10b6:208:52f::17) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.202.19 via Frontend Transport; Sun, 12 Jul 2026 07:40:59 +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 BL02EPF0001A102.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.202.18 via Frontend Transport; Sun, 12 Jul 2026 07:40:59 +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.41; Sun, 12 Jul 2026 02:40:51 -0500 From: Shivank Garg Date: Sun, 12 Jul 2026 07:38:47 +0000 Subject: [PATCH 1/6] mm/rmap: Add batched version of folio_try_share_anon_rmap_pte 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: <20260712-migrate-rmap-batch-v1-1-872a734431d1@amd.com> References: <20260712-migrate-rmap-batch-v1-0-872a734431d1@amd.com> In-Reply-To: <20260712-migrate-rmap-batch-v1-0-872a734431d1@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=1783842044; l=4856; i=shivankg@amd.com; s=20260518; h=from:subject:message-id; bh=g1KssW8+1v2HJaJEhqcOFZIerfOk9z+DUKil2dMLEUk=; b=PdS3GgyyjGtODvWWy5S3bZHiQpJzmSt5Z3pvDHLGpkbfJO6fF0ARPuaiguu4Zb7pKB5EqhIwt f3UF5gyB2T8CUazjdOfaVglvSEKjSmLV2XpYPA7F05LpojSFUyR56V0 X-Developer-Key: i=shivankg@amd.com; a=ed25519; pk=2l2QGTeXuGkZTtfmx0nPQU8iFZfjYmX/ymMojitevx4= X-ClientProxiedBy: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0001A102:EE_|SJ2PR12MB8717:EE_ X-MS-Office365-Filtering-Correlation-Id: 4cb754a3-09c2-4d2c-914c-08dedfe8eabf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|23010399003|376014|36860700016|7416014|22082099003|18002099003|11063799006|13003099007|56012099006|921020; X-Microsoft-Antispam-Message-Info: MYBliWtnoQEevF9RUtEK+QXzWrlA5bUhC6zjO3xbX0GFR+qqhY9BMs6AglSy/0F0BvpGSAW8GoEGrTwFT+zskJvrlp2iL/MQwVoL6fj8A5d4MG4GIGNTuIeH6yhjCwTxKwwDlGfHwxIOlz+lh2FkoYOSHvHcSxG9mwJsDPYvw6IMieDzIWz1wkbTeosh7C9wGJYsrINtpoek5JAyLMS1Hny5VMiW4XMWsrNtsotGQTWfQQBW9FsaY/N/4iIpL4xXzRfla7WttmJhHod9KcrStypjxdnsRbWs80NSCsj4VZWfIwrHMFXE4KYFE6RobwceAYvZ4N2q3UnCx2zZr0M3vhlV6VlMPDrDrwqOPxDbaJLGnGXl4KB8jFWHOmsSJeJxRljybzO+zU01yGQtTmDapybe91pjLJEP1KgwCdJJ9SDH4px9pmxjfOJX00+Kn1GXmkKmqHvllDW17a7TwxaBWPcTq1P/D2pBiq+AAg5+I3jwEszlgoI252QAFW1kzzyOjYHdNPHhB4KYUZA/1jy6wzIOjTn4sXIP6punfVbXwH7f/LDtfIwwCrbT/VFxRVoyUG07zk9rhVbMhXFInYTb3/sqA+A6aQjOwxo3qoFi+horAFNM6/ELkIB64ckyNN1gOC5W141cSEGyTekuzVXHeNeRScRFz1IX3C2zXnXm32oeZIdyAr5pPXH7UpfRoU5CRrikYtej/8yKIqErIVti49fw41oty1YPkYLSl6v2irflNoyWoSyR3xSOGGfO0YDe 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)(82310400026)(1800799024)(23010399003)(376014)(36860700016)(7416014)(22082099003)(18002099003)(11063799006)(13003099007)(56012099006)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hjVD6tketPAgS+PzPhktg9HulDDypzqqvuZr1cPIhgYBiM9lDDf1C9n906WiDmI3JJatgtVJ+/OchlpdbXmFtscKTTrR3h4A4t4yM/mQ/zOCa1/X1GZ2+M4ZbJc+9+H4wuuSuqCouZPaDEg2T+FpZcDsfRGAmSPtNmyb59txnmfQFbDwmoYbgP8G9oZlfpA1JnxxBhgKvJq1DrdnnNbwpo6R8lGypdqEpX/yj/n6rIPm0kIoUsky6P+E5Mba9XdZf+TnyX9WoBkb/c4ND+BoBGOOXOX7tG8se449xZT9bRz56H2r8iHL+SOQqUzWvI8l6gygJA6TDFqo9eCyXhACx+UqJR8p1FZfmf1UlAQukZPnuQYdtIcnXD3jHlLBy4DnqW0e7FBVWW4gVSX4PCiaih2tR+9ha7fBl/4UGCi6Q7QLFmQ5levJKKggfXJsGfsY X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2026 07:40:59.6571 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4cb754a3-09c2-4d2c-914c-08dedfe8eabf 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: BL02EPF0001A102.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8717 From: Dev Jain To enable batched unmapping of anonymous folios, we need to handle the sharing of exclusive pages. Hence, a batched version of folio_try_share_anon_rmap_pte is required. Currently, the sole purpose of nr_pages in __folio_try_share_anon_rmap is to do some rmap sanity checks. Now, clear the PageAnonExclusive bit on a batch of nr_pages. Refactor the function such that the clearing of the bit can be done at one place without duplication. Note that __folio_try_share_anon_rmap can receive nr_pages == HPAGE_PMD_NR from the PMD path, but currently we only clear the bit on the head page. Retain this behaviour by setting nr_pages = 1 in case the caller is folio_try_share_anon_rmap_pmd. While at it, convert nr_pages to unsigned long to future-proof from overflow in case P4D-huge mappings etc get supported down the road. I haven't made such a change in each function receiving nr_pages in try_to_unmap_one - perhaps this can be done incrementally. Borrrowed from [PATCH v4 00/12] Optimize anonymous large folio unmapping [1], will drop and rebase once [1] is merged. [1] https://lore.kernel.org/linux-mm/20260526063635.61721-10-dev.jain@arm.com/ Signed-off-by: Dev Jain Signed-off-by: Shivank Garg --- include/linux/rmap.h | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 8dc0871e5f00..64929490a7cf 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -706,17 +706,18 @@ static inline int folio_try_dup_anon_rmap_pmd(struct folio *folio, } static __always_inline int __folio_try_share_anon_rmap(struct folio *folio, - struct page *page, int nr_pages, enum pgtable_level level) + struct page *page, unsigned long nr_pages, enum pgtable_level level) { + /* device private folios cannot get pinned via GUP. */ + const bool pinnable = likely(!folio_is_device_private(folio)); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); VM_WARN_ON_FOLIO(!PageAnonExclusive(page), folio); __folio_rmap_sanity_checks(folio, page, nr_pages, level); - /* device private folios cannot get pinned via GUP. */ - if (unlikely(folio_is_device_private(folio))) { - ClearPageAnonExclusive(page); - return 0; - } + /* We only clear anon-exclusive from head page of PMD folio */ + if (level == PGTABLE_LEVEL_PMD) + nr_pages = 1; /* * We have to make sure that when we clear PageAnonExclusive, that @@ -760,29 +761,38 @@ static __always_inline int __folio_try_share_anon_rmap(struct folio *folio, * so we use explicit ones here. */ - /* Paired with the memory barrier in try_grab_folio(). */ - if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) - smp_mb(); + if (pinnable) { + /* Paired with the memory barrier in try_grab_folio(). */ + if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) + smp_mb(); - if (unlikely(folio_maybe_dma_pinned(folio))) - return -EBUSY; - ClearPageAnonExclusive(page); + if (unlikely(folio_maybe_dma_pinned(folio))) + return -EBUSY; + } + + for (;;) { + ClearPageAnonExclusive(page); + if (--nr_pages == 0) + break; + page++; + } /* * This is conceptually a smp_wmb() paired with the smp_rmb() in * gup_must_unshare(). */ - if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) + if (pinnable && IS_ENABLED(CONFIG_HAVE_GUP_FAST)) smp_mb__after_atomic(); return 0; } /** - * folio_try_share_anon_rmap_pte - try marking an exclusive anonymous page - * mapped by a PTE possibly shared to prepare + * folio_try_share_anon_rmap_ptes - try marking exclusive anonymous pages + * mapped by PTEs possibly shared to prepare * for KSM or temporary unmapping * @folio: The folio to share a mapping of - * @page: The mapped exclusive page + * @page: The first mapped exclusive page of the batch in the folio + * @nr_pages: The number of pages to share in the folio (batch size) * * The caller needs to hold the page table lock and has to have the page table * entries cleared/invalidated. @@ -797,11 +807,19 @@ static __always_inline int __folio_try_share_anon_rmap(struct folio *folio, * * Returns 0 if marking the mapped page possibly shared succeeded. Returns * -EBUSY otherwise. + * + * The caller needs to hold the page table lock. */ +static inline int folio_try_share_anon_rmap_ptes(struct folio *folio, + struct page *page, unsigned long nr_pages) +{ + return __folio_try_share_anon_rmap(folio, page, nr_pages, PGTABLE_LEVEL_PTE); +} + static inline int folio_try_share_anon_rmap_pte(struct folio *folio, struct page *page) { - return __folio_try_share_anon_rmap(folio, page, 1, PGTABLE_LEVEL_PTE); + return folio_try_share_anon_rmap_ptes(folio, page, 1); } /** -- 2.43.0