From nobody Sat Feb 7 08:12:15 2026 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2040.outbound.protection.outlook.com [40.107.223.40]) (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 5796527453 for ; Wed, 30 Apr 2025 10:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.40 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746007403; cv=fail; b=CTBw3CC+iAsXtSZd5swK4PabPJ/fi3d+yHxcx3KyxyK9ka+AqFqUpa0oUpAtxbH2cMLKKk+SyKVNSJ9YOvVPFp5OPe5XutioNoDTd9IjCdTWXZ/tY87vWkH1VPoenoyMzB5B3rJd0Hn55VBFT7sHEXtBNAx8++YXTyj1ylFEbZA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746007403; c=relaxed/simple; bh=TMAAr98XsyCx/tElSQ+O6JdqeLFq3w+DvT/SyiFsVwQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qPJrl7jU7jfkJ9PbAg4RPQ7YEkw2Q9idOC+JN/TMMfk2URQZhFfo9OSqdCx3bOav85VdtPfqGW+yB9z+F5q4taP9ke8lii3BIBNia8Ci/EiFZToInTPiEPh5TZjRJCju+ZKDOTkyV7zwZz7SpkWIc7GsUsXpeit2p5Xjxh3IvbE= 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=Ec7n2s3K; arc=fail smtp.client-ip=40.107.223.40 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="Ec7n2s3K" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FpuVYCeu8S/FN5/rcV+AryomSqCjg0lMx6ddFRB+I7RCd4Av9+C9Dln7aqDD71T7nnz4InHsK2wC2dEwFpAR0xqcEgxVsXargJH/UezoN0dDNQ1SFIEvgn0tL+wMw/f4dQu2BYvu9un8tAp6juwSXD4B/eGq3ox+4JJIPmVvDcccTLipEO/90SX/W5lTOX5cqg/Ism9gnAH2UjfoXKgJ5CJnwcZ5zo1Sr2KsKfkvbPWLM19xqIy1C8pa311UCD4aK7MGn3gxqpAsiQ5kKtHYgCdMN17P3bkky9D8BjRKx2HLO7WcfW+i0t4enL5EA6suDDPW8Sqp9mVY0xZigisAYw== 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=ezd84mvxlJVn7FW9Vvr/ZbGl+vZ3qJOeGDykPBAqeP8=; b=h7mNQx/YFFPJspYXVOLpgCARydEY0yCreLalb535YeloeC7ybhpG88jLJnxUirBGGj2qbnzrzST+7TskQVuDDgjn3ikRWagxYwWiQy+SwZXHxGysonKhS/bxs5rFw2hnwi/+u3+qwk1meFaBm0Q9iqxI6cRp++WCrkLQ5i2ctUooppn7/ucMb+ER6nWDYxKQ6A4i8BqfBWKZX+R1oQswWNJJ9jB1yF5r/cbf3yCQjQQ7xaLjAN0tKrtBBPQLXli2PktV/P46jEJ6v8L9L7qp2Q+XtcBvTvU5LogWfsDDpOqePYyS9KjWxgjY9di+xSHcV3DXvJm+oo3LwmLsiL3LtA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org 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=ezd84mvxlJVn7FW9Vvr/ZbGl+vZ3qJOeGDykPBAqeP8=; b=Ec7n2s3KR0bT44R4Fb2VSu2y7XSgdSU0Gg9GaJKORmwvb2y7GoC7tcFgc7x/V/Zsl132M58pKrJdrKiGo4OSlUZpwW3hWNDt/Cw9x8qX5cdyfUUkT043feyOPquXWQ2CyP7hfTHE4oajfbzuyCGgm2gko7VWHFpmO/DR35C3Zdk= Received: from BYAPR02CA0001.namprd02.prod.outlook.com (2603:10b6:a02:ee::14) by BY5PR12MB4211.namprd12.prod.outlook.com (2603:10b6:a03:20f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8699.20; Wed, 30 Apr 2025 10:03:13 +0000 Received: from SJ1PEPF00002320.namprd03.prod.outlook.com (2603:10b6:a02:ee:cafe::cc) by BYAPR02CA0001.outlook.office365.com (2603:10b6:a02:ee::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8655.41 via Frontend Transport; Wed, 30 Apr 2025 10:03:13 +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=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00002320.mail.protection.outlook.com (10.167.242.86) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8699.20 via Frontend Transport; Wed, 30 Apr 2025 10:03:13 +0000 Received: from kaveri.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 30 Apr 2025 05:03:08 -0500 From: Shivank Garg To: , CC: , , , , , , , , , , , Subject: [PATCH V5 1/2] mm: Add folio_expected_ref_count() for reference count calculation Date: Wed, 30 Apr 2025 10:01:51 +0000 Message-ID: <20250430100150.279751-2-shivankg@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250430100150.279751-1-shivankg@amd.com> References: <20250430100150.279751-1-shivankg@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00002320:EE_|BY5PR12MB4211:EE_ X-MS-Office365-Filtering-Correlation-Id: d1f214a8-a16e-4b94-6e32-08dd87ce386a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?AfRaPPBhbkboz2Nf5Hp/4Nj83ADT5RadOGxvnMzOicZormnjvrd4wl4VgFKf?= =?us-ascii?Q?zaK0rUv22Xj/5RWHjbmyrSbbVD2r9JtIK5nShoS7oQL8j+yweacrUYv5keeH?= =?us-ascii?Q?90bzcTiSUio0njggMZ0wkdolTw6vxaBW8JnzaT/whWbtxFy9+W3Wyh2snyOJ?= =?us-ascii?Q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6q3mxU4ss9Hu?= =?us-ascii?Q?AcEAdgcVN/fweoAeQKELR9FJGHLsTxOpHpUUl2uIlnJsFW1zzsqIw6tilFXt?= =?us-ascii?Q?OpJCdNg6cBRPhUZ/FNwKFUvfvShd5Q1C6PX3xZ79cp5sPHnC+ppD9bS0/2rv?= =?us-ascii?Q?wmYkxgarC1g6yc97OuMJV9Wkr+NZVkj4Hb/ZcZm2VbEsBjPlzs8GefsHCLxy?= =?us-ascii?Q?T5HY95ab7I6ICkildM717ujmq+rTSLpH0o4ZagXgR/DJBFqjQMihAWRv/diI?= =?us-ascii?Q?KpY9hSN+GZXzfuDgSRQKDlFkJ3RfgpSnd50O1jQ7tciVwgJTVpRQ4KVjxplA?= =?us-ascii?Q?c6j6RRrto3WJlTd+leh+n++ijCYr9mqhu5aL4FaeeUVUFIoCSElJbDhSaHna?= =?us-ascii?Q?rks5adz9gVab0LCBA4nOaoWhqQQVgTUSLuv142D2CZRaUMXfoeCnm83SZ7iD?= =?us-ascii?Q?AlQboqAQN16liT+dLdwLhgNQmyN3MOFQyWYD?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(376014)(7416014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2025 10:03:13.4567 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d1f214a8-a16e-4b94-6e32-08dd87ce386a 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002320.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4211 Content-Type: text/plain; charset="utf-8" Implement folio_expected_ref_count() to calculate expected folio reference counts from: - Page/swap cache (1 per page) - Private data (1) - Page table mappings (1 per map) While originally needed for page migration operations, this improved implementation standardizes reference counting by consolidating all refcount contributors into a single, reusable function that can benefit any subsystem needing to detect unexpected references to folios. The folio_expected_ref_count() returns the sum of these external references without including any reference the caller itself might hold. Callers comparing against the actual folio_ref_count() must account for their own references separately. Suggested-by: Matthew Wilcox Co-developed-by: David Hildenbrand Signed-off-by: David Hildenbrand Signed-off-by: Shivank Garg Reported-by: syzbot+8bb6fd945af4e0ad9299@syzkaller.appspotmail.com Tested-by: syzbot+8bb6fd945af4e0ad9299@syzkaller.appspotmail.com --- include/linux/mm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++ mm/migrate.c | 22 ++++--------------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 68d3dd14b323..1db540bb9381 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2115,6 +2115,61 @@ static inline bool folio_maybe_mapped_shared(struct = folio *folio) return folio_test_large_maybe_mapped_shared(folio); } =20 +/** + * folio_expected_ref_count - calculate the expected folio refcount + * @folio: the folio + * + * Calculate the expected folio refcount, taking references from the pagec= ache, + * swapcache, PG_private and page table mappings into account. Useful in + * combination with folio_ref_count() to detect unexpected references (e.g= ., + * GUP or other temporary references). + * + * Does currently not consider references from the LRU cache. If the folio + * was isolated from the LRU (which is the case during migration or split), + * the LRU cache does not apply. + * + * Calling this function on an unmapped folio -- !folio_mapped() -- that is + * locked will return a stable result. + * + * Calling this function on a mapped folio will not result in a stable res= ult, + * because nothing stops additional page table mappings from coming (e.g., + * fork()) or going (e.g., munmap()). + * + * Calling this function without the folio lock will also not result in a + * stable result: for example, the folio might get dropped from the swapca= che + * concurrently. + * + * However, even when called without the folio lock or on a mapped folio, + * this function can be used to detect unexpected references early (for ex= ample, + * if it makes sense to even lock the folio and unmap it). + * + * The caller must add any reference (e.g., from folio_try_get()) it might= be + * holding itself to the result. + * + * Returns the expected folio refcount. + */ +static inline int folio_expected_ref_count(const struct folio *folio) +{ + const int order =3D folio_order(folio); + int ref_count =3D 0; + + if (WARN_ON_ONCE(folio_test_slab(folio))) + return 0; + + if (folio_test_anon(folio)) { + /* One reference per page from the swapcache. */ + ref_count +=3D folio_test_swapcache(folio) << order; + } else if (!((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS)) { + /* One reference per page from the pagecache. */ + ref_count +=3D !!folio->mapping << order; + /* One reference from PG_private. */ + ref_count +=3D folio_test_private(folio); + } + + /* One reference per page table mapping. */ + return ref_count + folio_mapcount(folio); +} + #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE static inline int arch_make_folio_accessible(struct folio *folio) { diff --git a/mm/migrate.c b/mm/migrate.c index c80591514e66..67f2bf1eb51e 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -445,20 +445,6 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pm= d_t *pmd) } #endif =20 -static int folio_expected_refs(struct address_space *mapping, - struct folio *folio) -{ - int refs =3D 1; - if (!mapping) - return refs; - - refs +=3D folio_nr_pages(folio); - if (folio_test_private(folio)) - refs++; - - return refs; -} - /* * Replace the folio in the mapping. * @@ -601,7 +587,7 @@ static int __folio_migrate_mapping(struct address_space= *mapping, int folio_migrate_mapping(struct address_space *mapping, struct folio *newfolio, struct folio *folio, int extra_count) { - int expected_count =3D folio_expected_refs(mapping, folio) + extra_count; + int expected_count =3D folio_expected_ref_count(folio) + extra_count + 1; =20 if (folio_ref_count(folio) !=3D expected_count) return -EAGAIN; @@ -618,7 +604,7 @@ int migrate_huge_page_move_mapping(struct address_space= *mapping, struct folio *dst, struct folio *src) { XA_STATE(xas, &mapping->i_pages, folio_index(src)); - int rc, expected_count =3D folio_expected_refs(mapping, src); + int rc, expected_count =3D folio_expected_ref_count(src) + 1; =20 if (folio_ref_count(src) !=3D expected_count) return -EAGAIN; @@ -749,7 +735,7 @@ static int __migrate_folio(struct address_space *mappin= g, struct folio *dst, struct folio *src, void *src_private, enum migrate_mode mode) { - int rc, expected_count =3D folio_expected_refs(mapping, src); + int rc, expected_count =3D folio_expected_ref_count(src) + 1; =20 /* Check whether src does not have extra refs before we do more work */ if (folio_ref_count(src) !=3D expected_count) @@ -837,7 +823,7 @@ static int __buffer_migrate_folio(struct address_space = *mapping, return migrate_folio(mapping, dst, src, mode); =20 /* Check whether page does not have extra refs before we do more work */ - expected_count =3D folio_expected_refs(mapping, src); + expected_count =3D folio_expected_ref_count(src) + 1; if (folio_ref_count(src) !=3D expected_count) return -EAGAIN; =20 --=20 2.34.1 From nobody Sat Feb 7 08:12:15 2026 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2082.outbound.protection.outlook.com [40.107.236.82]) (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 9D817238142 for ; Wed, 30 Apr 2025 10:03:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746007406; cv=fail; b=Yz/HtBVzKpTSJVmF/B6vi9Q2BDew1T2J2lC3GuRPSQCkglKjcwv/f947E8DS0ARr3VMhvuAcadK6snKTWNvlRN2XjxOcJiRC1cUleeZ7HGh9q3rFYPI5Waua8JSo8voZLV6z5ACwVsTqUrx0LginLR2AgRFlF9uY5vJkpgcx8CE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746007406; c=relaxed/simple; bh=ayRAXiLcOTqB+39WtIgsCnYFzDvDeCXYculxOocilmw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RwBiNh1i9o3tDTPuLIaZ80GhmuFoFeCC1mea7vAoVhFsIgKvXNQlWr+aRad6mWAqsQmzqWE1rio3ywDuQ3Yw0V7gI4Z+7H0nkZoKVYdai/inW/Tx2HYgsz30yJXM1amu8ejE89lcrxuC5J1NlsgRDDknL7/xqHaZcseobX8a4i8= 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=VcwAqeOQ; arc=fail smtp.client-ip=40.107.236.82 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="VcwAqeOQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=w0PneVeOiop5gVWiOH+e/SfoRvA3sk6m0JLw62QxeQLVJKYyPgQGUh0He+IAmoo50GoaYrG/C0own15grXmOxDqTrKRSrvjkUc4Xk//NsT9aZ9D1TOPYdtM5S8drERuW+wnXgpd7vDR1ODfYfRhsOHcjcjix8CJpO2X0KlVsIrjFe8DcBy+UOtyNd4DHhqU/DAtGovMTCBD35QgPJdCKbNRP6WVm0XVXT1fEdtFUn6EhFGKfvhlXnX+GO5d0GrHSllq1IWcHWEMdrcLGmkZ0UWeiVk0OVKyIcXRA+BPjBlBx3vfLC5bRC7qH/gzbTsP1MrjQWNoVaOZU4BMCCpH6vw== 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=Jmx4GcCdkuO7r1OgEBOfsuZM1OJ2exaOGX/Ud8dm26o=; b=IXkVOEnKhgTaF6tnrVrv2G7ov2umploAOU5kWcXeAGYeRxLO10c5CijwytptXTBbheLUizyv5+ZQbNpXln9y1sy2hxJ8dm7MNakYyYojeuj+diLiK+cIaremticfYQi4loIDap7Mtv2WVPjPfombjofbQFXP6X7QOi9KHJrNttQwa3SCqDNTudSH+swLH0NgO0lYOtNxSCrYdyugGjclfXay+u7vJF58bnu2K+uLrX2qxfGfS2XB6WPGpYx/mtbXEUrFWnUNQAOSr9+bcMNKIIqdSCWqxkzZxjgFayLyVdvkyrN1ZjdhXVEmCvIiGHV8O30nu9tO53qGbfFjnpSmyA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org 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=Jmx4GcCdkuO7r1OgEBOfsuZM1OJ2exaOGX/Ud8dm26o=; b=VcwAqeOQ5CbZ+z0DuI8Qg6VKHqxApCLF8NzSmvoeJL3Kim0hExEyEqSt4iXyJte/IqWX7+CYDXzOdJDm7L/HfrYLvRVvicXTGe5vcRLdTN8LJ+JJUYx/5PGNai0q2TFpMSjayDt+WPvsHTxbZM6bhHN6mxAL4a8AvKYfaXb+0c4= Received: from SJ0PR13CA0191.namprd13.prod.outlook.com (2603:10b6:a03:2c3::16) by BY5PR12MB4148.namprd12.prod.outlook.com (2603:10b6:a03:208::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8699.19; Wed, 30 Apr 2025 10:03:19 +0000 Received: from SJ1PEPF00002323.namprd03.prod.outlook.com (2603:10b6:a03:2c3:cafe::4d) by SJ0PR13CA0191.outlook.office365.com (2603:10b6:a03:2c3::16) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8655.36 via Frontend Transport; Wed, 30 Apr 2025 10:03:19 +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=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00002323.mail.protection.outlook.com (10.167.242.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8699.20 via Frontend Transport; Wed, 30 Apr 2025 10:03:19 +0000 Received: from kaveri.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 30 Apr 2025 05:03:14 -0500 From: Shivank Garg To: , CC: , , , , , , , , , , , Subject: [PATCH V5 2/2] jfs: implement migrate_folio for jfs_metapage_aops Date: Wed, 30 Apr 2025 10:01:52 +0000 Message-ID: <20250430100150.279751-3-shivankg@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250430100150.279751-1-shivankg@amd.com> References: <20250430100150.279751-1-shivankg@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00002323:EE_|BY5PR12MB4148:EE_ X-MS-Office365-Filtering-Correlation-Id: 27f8d04f-a025-445a-8cf9-08dd87ce3bb8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|36860700013|82310400026|13003099007; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?VjhAO7SFmoF7HrAGaKMGp24vvXicsK4HkeFRBQ7abmtcBEX3Wdi+qVxLJpDH?= =?us-ascii?Q?BRrTBhKfr4nEvSWJwCf5nG/6djvc2ly4Sf0Buqwq3oh/EQq9QGbIBiZCukKK?= =?us-ascii?Q?8jS2cfIqx2p6Dvrnx7bvSmGn1abW2QDkTnACLkQh/cUgCcX59uM7D/xzlUYI?= =?us-ascii?Q?k+GZCcIRcg67UtmvaIqgHKAqPZ5iMzwL3yf6nuYoFew39XnPnZWr1Fb66J7M?= =?us-ascii?Q?0hj9TX263pFVUSPcO1fwAyenqgOfsOFjehZODityDC7XyaaV2jwM6hPGmGdX?= =?us-ascii?Q?r83vZC94DGIdu1BitbhZlKhhorGEVqBFgKVGj8NRZ3Z4iVMCJsdftrRxBQt2?= =?us-ascii?Q?0szW6cjn1B1h+s53Fzeuy1/W1/QYUtwQlTqYcRTOEG0IMLngNnxbVTBY9z48?= =?us-ascii?Q?OsWkPVFR+kFWQDUs3hEGraQK91yRbJboIIv8iqTsBxYee1mwR3PCUoXrMiWX?= =?us-ascii?Q?Yf/ISleWRMoYAYq5gsCZZ+hQb0NEn9FcGty9j2EDR0f2SFecUH5G/RcRnOZV?= =?us-ascii?Q?cuZR5/GbbOWtZI2qA9JfndgpTcxjmJfnyrr/TmmcsY7fEVDhbnGW4ETyQ+5X?= =?us-ascii?Q?WDO3qS6KF0D1vxk/lq1AIQq8WR+X/F/qS3ggpC0et0/hbTgX7JX0RP7IuWZc?= =?us-ascii?Q?rrij9Nj+UJoHxd+sMMLaI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D=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(7416014)(36860700013)(82310400026)(13003099007);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2025 10:03:19.0033 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 27f8d04f-a025-445a-8cf9-08dd87ce3bb8 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002323.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4148 Content-Type: text/plain; charset="utf-8" Add the missing migrate_folio operation to jfs_metapage_aops to fix warnings during memory compaction. These warnings were introduced by commit 7ee3647243e5 ("migrate: Remove call to ->writepage") which added explicit warnings when filesystems don't implement migrate_folio. System reports following warnings: jfs_metapage_aops does not implement migrate_folio WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 fallback_migrate_folio mm/m= igrate.c:953 [inline] WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 move_to_new_folio+0x70e/0x8= 40 mm/migrate.c:1007 Implement metapage_migrate_folio() which handles both single and multiple metapages per page configurations. Fixes: 35474d52c605 ("jfs: Convert metapage_writepage to metapage_write_fol= io") Reported-by: syzbot+8bb6fd945af4e0ad9299@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67faff52.050a0220.379d84.001b.GAE@googl= e.com Tested-by: syzbot+8bb6fd945af4e0ad9299@syzkaller.appspotmail.com Signed-off-by: Shivank Garg --- The commit e7021e2fe0b4 ("x86/efi: Make efi_enter/leave_mm() use the use_/unuse_tempor= ary_mm() machinery") is introducing some regression on linux-next, blocking me from testing JFS = issue. https://lore.kernel.org/all/SJ1PR11MB6129E62E3B372932C6B7477FB9BD2@SJ1PR11M= B6129.namprd11.prod.outlook.com/ After reverting the above series, the syzcaller did not report any issue wi= th my patch. Syzbot Test link: https://lore.kernel.org/all/6811f0a8.050a0220.39e3a1.0d08= .GAE@google.com/ --- fs/jfs/jfs_metapage.c | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/fs/jfs/jfs_metapage.c b/fs/jfs/jfs_metapage.c index df575a873ec6..f863ba521533 100644 --- a/fs/jfs/jfs_metapage.c +++ b/fs/jfs/jfs_metapage.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "jfs_incore.h" #include "jfs_superblock.h" #include "jfs_filsys.h" @@ -151,6 +152,54 @@ static inline void dec_io(struct folio *folio, blk_sta= tus_t status, handler(folio, anchor->status); } =20 +static int __metapage_migrate_folio(struct address_space *mapping, struct = folio *dst, + struct folio *src, enum migrate_mode mode) +{ + struct meta_anchor *src_anchor =3D src->private; + struct metapage *mps[MPS_PER_PAGE] =3D {0}; + struct metapage *mp; + int i, rc; + + for (i =3D 0; i < MPS_PER_PAGE; i++) { + mp =3D src_anchor->mp[i]; + if (mp && metapage_locked(mp)) + return -EAGAIN; + } + + rc =3D filemap_migrate_folio(mapping, dst, src, mode); + if (rc !=3D MIGRATEPAGE_SUCCESS) + return rc; + + for (i =3D 0; i < MPS_PER_PAGE; i++) { + mp =3D src_anchor->mp[i]; + if (!mp) + continue; + if (unlikely(insert_metapage(dst, mp))) { + /* If error, roll-back previosly inserted pages */ + for (int j =3D 0 ; j < i; j++) { + if (mps[j]) + remove_metapage(dst, mps[j]); + } + return -EAGAIN; + } + mps[i] =3D mp; + } + + /* Update the metapage and remove it from src */ + for (i =3D 0; i < MPS_PER_PAGE; i++) { + mp =3D mps[i]; + if (mp) { + int page_offset =3D mp->data - folio_address(src); + + mp->data =3D folio_address(dst) + page_offset; + mp->folio =3D dst; + remove_metapage(src, mp); + } + } + + return MIGRATEPAGE_SUCCESS; +} + #else static inline struct metapage *folio_to_mp(struct folio *folio, int offset) { @@ -175,6 +224,32 @@ static inline void remove_metapage(struct folio *folio= , struct metapage *mp) #define inc_io(folio) do {} while(0) #define dec_io(folio, status, handler) handler(folio, status) =20 +static int __metapage_migrate_folio(struct address_space *mapping, struct = folio *dst, + struct folio *src, enum migrate_mode mode) +{ + struct metapage *mp; + int page_offset; + int rc; + + mp =3D folio_to_mp(src, 0); + if (mp && metapage_locked(mp)) + return -EAGAIN; + + rc =3D filemap_migrate_folio(mapping, dst, src, mode); + if (rc !=3D MIGRATEPAGE_SUCCESS) + return rc; + + if (unlikely(insert_metapage(dst, mp))) + return -EAGAIN; + + page_offset =3D mp->data - folio_address(src); + mp->data =3D folio_address(dst) + page_offset; + mp->folio =3D dst; + remove_metapage(src, mp); + + return MIGRATEPAGE_SUCCESS; +} + #endif =20 static inline struct metapage *alloc_metapage(gfp_t gfp_mask) @@ -554,6 +629,24 @@ static bool metapage_release_folio(struct folio *folio= , gfp_t gfp_mask) return ret; } =20 +/** + * metapage_migrate_folio - Migration function for JFS metapages + */ +static int metapage_migrate_folio(struct address_space *mapping, struct fo= lio *dst, + struct folio *src, enum migrate_mode mode) +{ + int expected_count; + + if (!src->private) + return filemap_migrate_folio(mapping, dst, src, mode); + + /* Check whether page does not have extra refs before we do more work */ + expected_count =3D folio_expected_ref_count(src) + 1; + if (folio_ref_count(src) !=3D expected_count) + return -EAGAIN; + return __metapage_migrate_folio(mapping, dst, src, mode); +} + static void metapage_invalidate_folio(struct folio *folio, size_t offset, size_t length) { @@ -570,6 +663,7 @@ const struct address_space_operations jfs_metapage_aops= =3D { .release_folio =3D metapage_release_folio, .invalidate_folio =3D metapage_invalidate_folio, .dirty_folio =3D filemap_dirty_folio, + .migrate_folio =3D metapage_migrate_folio, }; =20 struct metapage *__get_metapage(struct inode *inode, unsigned long lblock, --=20 2.34.1